@medusajs/draft-order 2.10.2-preview-20250909090202 → 2.10.2-preview-20250909120201

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9757,27 +9757,6 @@ const BillingAddressForm = ({ order }) => {
9757
9757
  ) });
9758
9758
  };
9759
9759
  const schema$5 = addressSchema;
9760
- const CustomItems = () => {
9761
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9762
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9763
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9764
- ] });
9765
- };
9766
- const CustomItemsForm = () => {
9767
- const form = useForm({
9768
- resolver: zodResolver(schema$4)
9769
- });
9770
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9771
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9772
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9773
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9774
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9775
- ] }) })
9776
- ] }) });
9777
- };
9778
- const schema$4 = objectType({
9779
- email: stringType().email()
9780
- });
9781
9760
  const Email = () => {
9782
9761
  const { id } = useParams();
9783
9762
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9800,7 +9779,7 @@ const EmailForm = ({ order }) => {
9800
9779
  defaultValues: {
9801
9780
  email: order.email ?? ""
9802
9781
  },
9803
- resolver: zodResolver(schema$3)
9782
+ resolver: zodResolver(schema$4)
9804
9783
  });
9805
9784
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9806
9785
  const { handleSuccess } = useRouteModal();
@@ -9843,7 +9822,7 @@ const EmailForm = ({ order }) => {
9843
9822
  }
9844
9823
  ) });
9845
9824
  };
9846
- const schema$3 = objectType({
9825
+ const schema$4 = objectType({
9847
9826
  email: stringType().email()
9848
9827
  });
9849
9828
  const NumberInput = forwardRef(
@@ -10820,283 +10799,6 @@ const customItemSchema = objectType({
10820
10799
  quantity: numberType(),
10821
10800
  unit_price: unionType([numberType(), stringType()])
10822
10801
  });
10823
- const PROMOTION_QUERY_KEY = "promotions";
10824
- const promotionsQueryKeys = {
10825
- list: (query2) => [
10826
- PROMOTION_QUERY_KEY,
10827
- query2 ? query2 : void 0
10828
- ],
10829
- detail: (id, query2) => [
10830
- PROMOTION_QUERY_KEY,
10831
- id,
10832
- query2 ? query2 : void 0
10833
- ]
10834
- };
10835
- const usePromotions = (query2, options) => {
10836
- const { data, ...rest } = useQuery({
10837
- queryKey: promotionsQueryKeys.list(query2),
10838
- queryFn: async () => sdk.admin.promotion.list(query2),
10839
- ...options
10840
- });
10841
- return { ...data, ...rest };
10842
- };
10843
- const Promotions = () => {
10844
- const { id } = useParams();
10845
- const {
10846
- order: preview,
10847
- isError: isPreviewError,
10848
- error: previewError
10849
- } = useOrderPreview(id, void 0);
10850
- useInitiateOrderEdit({ preview });
10851
- const { onCancel } = useCancelOrderEdit({ preview });
10852
- if (isPreviewError) {
10853
- throw previewError;
10854
- }
10855
- const isReady = !!preview;
10856
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10857
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10858
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10859
- ] });
10860
- };
10861
- const PromotionForm = ({ preview }) => {
10862
- const { items, shipping_methods } = preview;
10863
- const [isSubmitting, setIsSubmitting] = useState(false);
10864
- const [comboboxValue, setComboboxValue] = useState("");
10865
- const { handleSuccess } = useRouteModal();
10866
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10867
- const promoIds = getPromotionIds(items, shipping_methods);
10868
- const { promotions, isPending, isError, error } = usePromotions(
10869
- {
10870
- id: promoIds
10871
- },
10872
- {
10873
- enabled: !!promoIds.length
10874
- }
10875
- );
10876
- const comboboxData = useComboboxData({
10877
- queryKey: ["promotions", "combobox", promoIds],
10878
- queryFn: async (params) => {
10879
- return await sdk.admin.promotion.list({
10880
- ...params,
10881
- id: {
10882
- $nin: promoIds
10883
- }
10884
- });
10885
- },
10886
- getOptions: (data) => {
10887
- return data.promotions.map((promotion) => ({
10888
- label: promotion.code,
10889
- value: promotion.code
10890
- }));
10891
- }
10892
- });
10893
- const add = async (value) => {
10894
- if (!value) {
10895
- return;
10896
- }
10897
- addPromotions(
10898
- {
10899
- promo_codes: [value]
10900
- },
10901
- {
10902
- onError: (e) => {
10903
- toast.error(e.message);
10904
- comboboxData.onSearchValueChange("");
10905
- setComboboxValue("");
10906
- },
10907
- onSuccess: () => {
10908
- comboboxData.onSearchValueChange("");
10909
- setComboboxValue("");
10910
- }
10911
- }
10912
- );
10913
- };
10914
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10915
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10916
- const onSubmit = async () => {
10917
- setIsSubmitting(true);
10918
- let requestSucceeded = false;
10919
- await requestOrderEdit(void 0, {
10920
- onError: (e) => {
10921
- toast.error(e.message);
10922
- },
10923
- onSuccess: () => {
10924
- requestSucceeded = true;
10925
- }
10926
- });
10927
- if (!requestSucceeded) {
10928
- setIsSubmitting(false);
10929
- return;
10930
- }
10931
- await confirmOrderEdit(void 0, {
10932
- onError: (e) => {
10933
- toast.error(e.message);
10934
- },
10935
- onSuccess: () => {
10936
- handleSuccess();
10937
- },
10938
- onSettled: () => {
10939
- setIsSubmitting(false);
10940
- }
10941
- });
10942
- };
10943
- if (isError) {
10944
- throw error;
10945
- }
10946
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10947
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
10948
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
10949
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10950
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10951
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10952
- ] }),
10953
- /* @__PURE__ */ jsx(
10954
- Combobox,
10955
- {
10956
- id: "promotion-combobox",
10957
- "aria-describedby": "promotion-combobox-hint",
10958
- isFetchingNextPage: comboboxData.isFetchingNextPage,
10959
- fetchNextPage: comboboxData.fetchNextPage,
10960
- options: comboboxData.options,
10961
- onSearchValueChange: comboboxData.onSearchValueChange,
10962
- searchValue: comboboxData.searchValue,
10963
- disabled: comboboxData.disabled || isAddingPromotions,
10964
- onChange: add,
10965
- value: comboboxValue
10966
- }
10967
- )
10968
- ] }),
10969
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10970
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
10971
- PromotionItem,
10972
- {
10973
- promotion,
10974
- orderId: preview.id,
10975
- isLoading: isPending
10976
- },
10977
- promotion.id
10978
- )) })
10979
- ] }) }),
10980
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10981
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10982
- /* @__PURE__ */ jsx(
10983
- Button,
10984
- {
10985
- size: "small",
10986
- type: "submit",
10987
- isLoading: isSubmitting || isAddingPromotions,
10988
- children: "Save"
10989
- }
10990
- )
10991
- ] }) })
10992
- ] });
10993
- };
10994
- const PromotionItem = ({
10995
- promotion,
10996
- orderId,
10997
- isLoading
10998
- }) => {
10999
- var _a;
11000
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11001
- const onRemove = async () => {
11002
- removePromotions(
11003
- {
11004
- promo_codes: [promotion.code]
11005
- },
11006
- {
11007
- onError: (e) => {
11008
- toast.error(e.message);
11009
- }
11010
- }
11011
- );
11012
- };
11013
- const displayValue = getDisplayValue(promotion);
11014
- return /* @__PURE__ */ jsxs(
11015
- "div",
11016
- {
11017
- className: clx(
11018
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11019
- {
11020
- "animate-pulse": isLoading
11021
- }
11022
- ),
11023
- children: [
11024
- /* @__PURE__ */ jsxs("div", { children: [
11025
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11026
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11027
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11028
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11029
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11030
- ] }),
11031
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11032
- ] })
11033
- ] }),
11034
- /* @__PURE__ */ jsx(
11035
- IconButton,
11036
- {
11037
- size: "small",
11038
- type: "button",
11039
- variant: "transparent",
11040
- onClick: onRemove,
11041
- isLoading: isPending || isLoading,
11042
- children: /* @__PURE__ */ jsx(XMark, {})
11043
- }
11044
- )
11045
- ]
11046
- },
11047
- promotion.id
11048
- );
11049
- };
11050
- function getDisplayValue(promotion) {
11051
- var _a, _b, _c, _d;
11052
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11053
- if (!value) {
11054
- return null;
11055
- }
11056
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11057
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11058
- if (!currency) {
11059
- return null;
11060
- }
11061
- return getLocaleAmount(value, currency);
11062
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11063
- return formatPercentage(value);
11064
- }
11065
- return null;
11066
- }
11067
- const formatter = new Intl.NumberFormat([], {
11068
- style: "percent",
11069
- minimumFractionDigits: 2
11070
- });
11071
- const formatPercentage = (value, isPercentageValue = false) => {
11072
- let val = value || 0;
11073
- if (!isPercentageValue) {
11074
- val = val / 100;
11075
- }
11076
- return formatter.format(val);
11077
- };
11078
- function getPromotionIds(items, shippingMethods) {
11079
- const promotionIds = /* @__PURE__ */ new Set();
11080
- for (const item of items) {
11081
- if (item.adjustments) {
11082
- for (const adjustment of item.adjustments) {
11083
- if (adjustment.promotion_id) {
11084
- promotionIds.add(adjustment.promotion_id);
11085
- }
11086
- }
11087
- }
11088
- }
11089
- for (const shippingMethod of shippingMethods) {
11090
- if (shippingMethod.adjustments) {
11091
- for (const adjustment of shippingMethod.adjustments) {
11092
- if (adjustment.promotion_id) {
11093
- promotionIds.add(adjustment.promotion_id);
11094
- }
11095
- }
11096
- }
11097
- }
11098
- return Array.from(promotionIds);
11099
- }
11100
10802
  const InlineTip = forwardRef(
11101
10803
  ({ variant = "tip", label, className, children, ...props }, ref) => {
11102
10804
  const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
@@ -11361,91 +11063,368 @@ const GridInput = forwardRef(({ className, ...props }, ref) => {
11361
11063
  "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
11362
11064
  className
11363
11065
  )
11364
- }
11365
- );
11366
- });
11367
- GridInput.displayName = "MetadataForm.GridInput";
11368
- const PlaceholderInner = () => {
11369
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11370
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11371
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11372
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11373
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11066
+ }
11067
+ );
11068
+ });
11069
+ GridInput.displayName = "MetadataForm.GridInput";
11070
+ const PlaceholderInner = () => {
11071
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11072
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11073
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11074
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11075
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11076
+ ] }) })
11077
+ ] });
11078
+ };
11079
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
11080
+ function getDefaultValues(metadata) {
11081
+ if (!metadata || !Object.keys(metadata).length) {
11082
+ return [
11083
+ {
11084
+ key: "",
11085
+ value: "",
11086
+ disabled: false
11087
+ }
11088
+ ];
11089
+ }
11090
+ return Object.entries(metadata).map(([key, value]) => {
11091
+ if (!EDITABLE_TYPES.includes(typeof value)) {
11092
+ return {
11093
+ key,
11094
+ value,
11095
+ disabled: true
11096
+ };
11097
+ }
11098
+ let stringValue = value;
11099
+ if (typeof value !== "string") {
11100
+ stringValue = JSON.stringify(value);
11101
+ }
11102
+ return {
11103
+ key,
11104
+ value: stringValue,
11105
+ original_key: key
11106
+ };
11107
+ });
11108
+ }
11109
+ function parseValues(values) {
11110
+ const metadata = values.metadata;
11111
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11112
+ if (isEmpty) {
11113
+ return null;
11114
+ }
11115
+ const update = {};
11116
+ metadata.forEach((field) => {
11117
+ let key = field.key;
11118
+ let value = field.value;
11119
+ const disabled = field.disabled;
11120
+ if (!key || !value) {
11121
+ return;
11122
+ }
11123
+ if (disabled) {
11124
+ update[key] = value;
11125
+ return;
11126
+ }
11127
+ key = key.trim();
11128
+ value = value.trim();
11129
+ if (value === "true") {
11130
+ update[key] = true;
11131
+ } else if (value === "false") {
11132
+ update[key] = false;
11133
+ } else {
11134
+ const parsedNumber = parseFloat(value);
11135
+ if (!isNaN(parsedNumber)) {
11136
+ update[key] = parsedNumber;
11137
+ } else {
11138
+ update[key] = value;
11139
+ }
11140
+ }
11141
+ });
11142
+ return update;
11143
+ }
11144
+ function getHasUneditableRows(metadata) {
11145
+ if (!metadata) {
11146
+ return false;
11147
+ }
11148
+ return Object.values(metadata).some(
11149
+ (value) => !EDITABLE_TYPES.includes(typeof value)
11150
+ );
11151
+ }
11152
+ const PROMOTION_QUERY_KEY = "promotions";
11153
+ const promotionsQueryKeys = {
11154
+ list: (query2) => [
11155
+ PROMOTION_QUERY_KEY,
11156
+ query2 ? query2 : void 0
11157
+ ],
11158
+ detail: (id, query2) => [
11159
+ PROMOTION_QUERY_KEY,
11160
+ id,
11161
+ query2 ? query2 : void 0
11162
+ ]
11163
+ };
11164
+ const usePromotions = (query2, options) => {
11165
+ const { data, ...rest } = useQuery({
11166
+ queryKey: promotionsQueryKeys.list(query2),
11167
+ queryFn: async () => sdk.admin.promotion.list(query2),
11168
+ ...options
11169
+ });
11170
+ return { ...data, ...rest };
11171
+ };
11172
+ const Promotions = () => {
11173
+ const { id } = useParams();
11174
+ const {
11175
+ order: preview,
11176
+ isError: isPreviewError,
11177
+ error: previewError
11178
+ } = useOrderPreview(id, void 0);
11179
+ useInitiateOrderEdit({ preview });
11180
+ const { onCancel } = useCancelOrderEdit({ preview });
11181
+ if (isPreviewError) {
11182
+ throw previewError;
11183
+ }
11184
+ const isReady = !!preview;
11185
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11186
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11187
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11188
+ ] });
11189
+ };
11190
+ const PromotionForm = ({ preview }) => {
11191
+ const { items, shipping_methods } = preview;
11192
+ const [isSubmitting, setIsSubmitting] = useState(false);
11193
+ const [comboboxValue, setComboboxValue] = useState("");
11194
+ const { handleSuccess } = useRouteModal();
11195
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11196
+ const promoIds = getPromotionIds(items, shipping_methods);
11197
+ const { promotions, isPending, isError, error } = usePromotions(
11198
+ {
11199
+ id: promoIds
11200
+ },
11201
+ {
11202
+ enabled: !!promoIds.length
11203
+ }
11204
+ );
11205
+ const comboboxData = useComboboxData({
11206
+ queryKey: ["promotions", "combobox", promoIds],
11207
+ queryFn: async (params) => {
11208
+ return await sdk.admin.promotion.list({
11209
+ ...params,
11210
+ id: {
11211
+ $nin: promoIds
11212
+ }
11213
+ });
11214
+ },
11215
+ getOptions: (data) => {
11216
+ return data.promotions.map((promotion) => ({
11217
+ label: promotion.code,
11218
+ value: promotion.code
11219
+ }));
11220
+ }
11221
+ });
11222
+ const add = async (value) => {
11223
+ if (!value) {
11224
+ return;
11225
+ }
11226
+ addPromotions(
11227
+ {
11228
+ promo_codes: [value]
11229
+ },
11230
+ {
11231
+ onError: (e) => {
11232
+ toast.error(e.message);
11233
+ comboboxData.onSearchValueChange("");
11234
+ setComboboxValue("");
11235
+ },
11236
+ onSuccess: () => {
11237
+ comboboxData.onSearchValueChange("");
11238
+ setComboboxValue("");
11239
+ }
11240
+ }
11241
+ );
11242
+ };
11243
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11244
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11245
+ const onSubmit = async () => {
11246
+ setIsSubmitting(true);
11247
+ let requestSucceeded = false;
11248
+ await requestOrderEdit(void 0, {
11249
+ onError: (e) => {
11250
+ toast.error(e.message);
11251
+ },
11252
+ onSuccess: () => {
11253
+ requestSucceeded = true;
11254
+ }
11255
+ });
11256
+ if (!requestSucceeded) {
11257
+ setIsSubmitting(false);
11258
+ return;
11259
+ }
11260
+ await confirmOrderEdit(void 0, {
11261
+ onError: (e) => {
11262
+ toast.error(e.message);
11263
+ },
11264
+ onSuccess: () => {
11265
+ handleSuccess();
11266
+ },
11267
+ onSettled: () => {
11268
+ setIsSubmitting(false);
11269
+ }
11270
+ });
11271
+ };
11272
+ if (isError) {
11273
+ throw error;
11274
+ }
11275
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11276
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11277
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11278
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11279
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11280
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11281
+ ] }),
11282
+ /* @__PURE__ */ jsx(
11283
+ Combobox,
11284
+ {
11285
+ id: "promotion-combobox",
11286
+ "aria-describedby": "promotion-combobox-hint",
11287
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
11288
+ fetchNextPage: comboboxData.fetchNextPage,
11289
+ options: comboboxData.options,
11290
+ onSearchValueChange: comboboxData.onSearchValueChange,
11291
+ searchValue: comboboxData.searchValue,
11292
+ disabled: comboboxData.disabled || isAddingPromotions,
11293
+ onChange: add,
11294
+ value: comboboxValue
11295
+ }
11296
+ )
11297
+ ] }),
11298
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11299
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11300
+ PromotionItem,
11301
+ {
11302
+ promotion,
11303
+ orderId: preview.id,
11304
+ isLoading: isPending
11305
+ },
11306
+ promotion.id
11307
+ )) })
11308
+ ] }) }),
11309
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11310
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11311
+ /* @__PURE__ */ jsx(
11312
+ Button,
11313
+ {
11314
+ size: "small",
11315
+ type: "submit",
11316
+ isLoading: isSubmitting || isAddingPromotions,
11317
+ children: "Save"
11318
+ }
11319
+ )
11374
11320
  ] }) })
11375
11321
  ] });
11376
11322
  };
11377
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11378
- function getDefaultValues(metadata) {
11379
- if (!metadata || !Object.keys(metadata).length) {
11380
- return [
11323
+ const PromotionItem = ({
11324
+ promotion,
11325
+ orderId,
11326
+ isLoading
11327
+ }) => {
11328
+ var _a;
11329
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11330
+ const onRemove = async () => {
11331
+ removePromotions(
11381
11332
  {
11382
- key: "",
11383
- value: "",
11384
- disabled: false
11333
+ promo_codes: [promotion.code]
11334
+ },
11335
+ {
11336
+ onError: (e) => {
11337
+ toast.error(e.message);
11338
+ }
11385
11339
  }
11386
- ];
11340
+ );
11341
+ };
11342
+ const displayValue = getDisplayValue(promotion);
11343
+ return /* @__PURE__ */ jsxs(
11344
+ "div",
11345
+ {
11346
+ className: clx(
11347
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11348
+ {
11349
+ "animate-pulse": isLoading
11350
+ }
11351
+ ),
11352
+ children: [
11353
+ /* @__PURE__ */ jsxs("div", { children: [
11354
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11355
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11356
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11357
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11358
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11359
+ ] }),
11360
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11361
+ ] })
11362
+ ] }),
11363
+ /* @__PURE__ */ jsx(
11364
+ IconButton,
11365
+ {
11366
+ size: "small",
11367
+ type: "button",
11368
+ variant: "transparent",
11369
+ onClick: onRemove,
11370
+ isLoading: isPending || isLoading,
11371
+ children: /* @__PURE__ */ jsx(XMark, {})
11372
+ }
11373
+ )
11374
+ ]
11375
+ },
11376
+ promotion.id
11377
+ );
11378
+ };
11379
+ function getDisplayValue(promotion) {
11380
+ var _a, _b, _c, _d;
11381
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11382
+ if (!value) {
11383
+ return null;
11387
11384
  }
11388
- return Object.entries(metadata).map(([key, value]) => {
11389
- if (!EDITABLE_TYPES.includes(typeof value)) {
11390
- return {
11391
- key,
11392
- value,
11393
- disabled: true
11394
- };
11395
- }
11396
- let stringValue = value;
11397
- if (typeof value !== "string") {
11398
- stringValue = JSON.stringify(value);
11385
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11386
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11387
+ if (!currency) {
11388
+ return null;
11399
11389
  }
11400
- return {
11401
- key,
11402
- value: stringValue,
11403
- original_key: key
11404
- };
11405
- });
11390
+ return getLocaleAmount(value, currency);
11391
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11392
+ return formatPercentage(value);
11393
+ }
11394
+ return null;
11406
11395
  }
11407
- function parseValues(values) {
11408
- const metadata = values.metadata;
11409
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11410
- if (isEmpty) {
11411
- return null;
11396
+ const formatter = new Intl.NumberFormat([], {
11397
+ style: "percent",
11398
+ minimumFractionDigits: 2
11399
+ });
11400
+ const formatPercentage = (value, isPercentageValue = false) => {
11401
+ let val = value || 0;
11402
+ if (!isPercentageValue) {
11403
+ val = val / 100;
11412
11404
  }
11413
- const update = {};
11414
- metadata.forEach((field) => {
11415
- let key = field.key;
11416
- let value = field.value;
11417
- const disabled = field.disabled;
11418
- if (!key || !value) {
11419
- return;
11420
- }
11421
- if (disabled) {
11422
- update[key] = value;
11423
- return;
11405
+ return formatter.format(val);
11406
+ };
11407
+ function getPromotionIds(items, shippingMethods) {
11408
+ const promotionIds = /* @__PURE__ */ new Set();
11409
+ for (const item of items) {
11410
+ if (item.adjustments) {
11411
+ for (const adjustment of item.adjustments) {
11412
+ if (adjustment.promotion_id) {
11413
+ promotionIds.add(adjustment.promotion_id);
11414
+ }
11415
+ }
11424
11416
  }
11425
- key = key.trim();
11426
- value = value.trim();
11427
- if (value === "true") {
11428
- update[key] = true;
11429
- } else if (value === "false") {
11430
- update[key] = false;
11431
- } else {
11432
- const parsedNumber = parseFloat(value);
11433
- if (!isNaN(parsedNumber)) {
11434
- update[key] = parsedNumber;
11435
- } else {
11436
- update[key] = value;
11417
+ }
11418
+ for (const shippingMethod of shippingMethods) {
11419
+ if (shippingMethod.adjustments) {
11420
+ for (const adjustment of shippingMethod.adjustments) {
11421
+ if (adjustment.promotion_id) {
11422
+ promotionIds.add(adjustment.promotion_id);
11423
+ }
11437
11424
  }
11438
11425
  }
11439
- });
11440
- return update;
11441
- }
11442
- function getHasUneditableRows(metadata) {
11443
- if (!metadata) {
11444
- return false;
11445
11426
  }
11446
- return Object.values(metadata).some(
11447
- (value) => !EDITABLE_TYPES.includes(typeof value)
11448
- );
11427
+ return Array.from(promotionIds);
11449
11428
  }
11450
11429
  const SalesChannel = () => {
11451
11430
  const { id } = useParams();
@@ -11475,7 +11454,7 @@ const SalesChannelForm = ({ order }) => {
11475
11454
  defaultValues: {
11476
11455
  sales_channel_id: order.sales_channel_id || ""
11477
11456
  },
11478
- resolver: zodResolver(schema$2)
11457
+ resolver: zodResolver(schema$3)
11479
11458
  });
11480
11459
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11481
11460
  const { handleSuccess } = useRouteModal();
@@ -11550,7 +11529,7 @@ const SalesChannelField = ({ control, order }) => {
11550
11529
  }
11551
11530
  );
11552
11531
  };
11553
- const schema$2 = objectType({
11532
+ const schema$3 = objectType({
11554
11533
  sales_channel_id: stringType().min(1)
11555
11534
  });
11556
11535
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
@@ -12383,7 +12362,7 @@ const TransferOwnershipForm = ({ order }) => {
12383
12362
  defaultValues: {
12384
12363
  customer_id: order.customer_id || ""
12385
12364
  },
12386
- resolver: zodResolver(schema$1)
12365
+ resolver: zodResolver(schema$2)
12387
12366
  });
12388
12367
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12389
12368
  const { handleSuccess } = useRouteModal();
@@ -12833,7 +12812,7 @@ const Illustration = () => {
12833
12812
  }
12834
12813
  );
12835
12814
  };
12836
- const schema$1 = objectType({
12815
+ const schema$2 = objectType({
12837
12816
  customer_id: stringType().min(1)
12838
12817
  });
12839
12818
  const ShippingAddress = () => {
@@ -12868,7 +12847,7 @@ const ShippingAddressForm = ({ order }) => {
12868
12847
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12869
12848
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12870
12849
  },
12871
- resolver: zodResolver(schema)
12850
+ resolver: zodResolver(schema$1)
12872
12851
  });
12873
12852
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12874
12853
  const { handleSuccess } = useRouteModal();
@@ -13038,7 +13017,28 @@ const ShippingAddressForm = ({ order }) => {
13038
13017
  }
13039
13018
  ) });
13040
13019
  };
13041
- const schema = addressSchema;
13020
+ const schema$1 = addressSchema;
13021
+ const CustomItems = () => {
13022
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
13023
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
13024
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
13025
+ ] });
13026
+ };
13027
+ const CustomItemsForm = () => {
13028
+ const form = useForm({
13029
+ resolver: zodResolver(schema)
13030
+ });
13031
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
13032
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
13033
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13034
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13035
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
13036
+ ] }) })
13037
+ ] }) });
13038
+ };
13039
+ const schema = objectType({
13040
+ email: stringType().email()
13041
+ });
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
13044
13044
  routes: [
@@ -13063,10 +13063,6 @@ const routeModule = {
13063
13063
  Component: BillingAddress,
13064
13064
  path: "/draft-orders/:id/billing-address"
13065
13065
  },
13066
- {
13067
- Component: CustomItems,
13068
- path: "/draft-orders/:id/custom-items"
13069
- },
13070
13066
  {
13071
13067
  Component: Email,
13072
13068
  path: "/draft-orders/:id/email"
@@ -13075,14 +13071,14 @@ const routeModule = {
13075
13071
  Component: Items,
13076
13072
  path: "/draft-orders/:id/items"
13077
13073
  },
13078
- {
13079
- Component: Promotions,
13080
- path: "/draft-orders/:id/promotions"
13081
- },
13082
13074
  {
13083
13075
  Component: Metadata,
13084
13076
  path: "/draft-orders/:id/metadata"
13085
13077
  },
13078
+ {
13079
+ Component: Promotions,
13080
+ path: "/draft-orders/:id/promotions"
13081
+ },
13086
13082
  {
13087
13083
  Component: SalesChannel,
13088
13084
  path: "/draft-orders/:id/sales-channel"
@@ -13098,6 +13094,10 @@ const routeModule = {
13098
13094
  {
13099
13095
  Component: ShippingAddress,
13100
13096
  path: "/draft-orders/:id/shipping-address"
13097
+ },
13098
+ {
13099
+ Component: CustomItems,
13100
+ path: "/draft-orders/:id/custom-items"
13101
13101
  }
13102
13102
  ]
13103
13103
  }