@medusajs/draft-order 2.10.4-preview-20251005120155 → 2.10.4-preview-20251005180152

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.
@@ -10630,6 +10630,283 @@ const customItemSchema = objectType({
10630
10630
  quantity: numberType(),
10631
10631
  unit_price: unionType([numberType(), stringType()])
10632
10632
  });
10633
+ const PROMOTION_QUERY_KEY = "promotions";
10634
+ const promotionsQueryKeys = {
10635
+ list: (query2) => [
10636
+ PROMOTION_QUERY_KEY,
10637
+ query2 ? query2 : void 0
10638
+ ],
10639
+ detail: (id, query2) => [
10640
+ PROMOTION_QUERY_KEY,
10641
+ id,
10642
+ query2 ? query2 : void 0
10643
+ ]
10644
+ };
10645
+ const usePromotions = (query2, options) => {
10646
+ const { data, ...rest } = useQuery({
10647
+ queryKey: promotionsQueryKeys.list(query2),
10648
+ queryFn: async () => sdk.admin.promotion.list(query2),
10649
+ ...options
10650
+ });
10651
+ return { ...data, ...rest };
10652
+ };
10653
+ const Promotions = () => {
10654
+ const { id } = useParams();
10655
+ const {
10656
+ order: preview,
10657
+ isError: isPreviewError,
10658
+ error: previewError
10659
+ } = useOrderPreview(id, void 0);
10660
+ useInitiateOrderEdit({ preview });
10661
+ const { onCancel } = useCancelOrderEdit({ preview });
10662
+ if (isPreviewError) {
10663
+ throw previewError;
10664
+ }
10665
+ const isReady = !!preview;
10666
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10667
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10668
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10669
+ ] });
10670
+ };
10671
+ const PromotionForm = ({ preview }) => {
10672
+ const { items, shipping_methods } = preview;
10673
+ const [isSubmitting, setIsSubmitting] = useState(false);
10674
+ const [comboboxValue, setComboboxValue] = useState("");
10675
+ const { handleSuccess } = useRouteModal();
10676
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10677
+ const promoIds = getPromotionIds(items, shipping_methods);
10678
+ const { promotions, isPending, isError, error } = usePromotions(
10679
+ {
10680
+ id: promoIds
10681
+ },
10682
+ {
10683
+ enabled: !!promoIds.length
10684
+ }
10685
+ );
10686
+ const comboboxData = useComboboxData({
10687
+ queryKey: ["promotions", "combobox", promoIds],
10688
+ queryFn: async (params) => {
10689
+ return await sdk.admin.promotion.list({
10690
+ ...params,
10691
+ id: {
10692
+ $nin: promoIds
10693
+ }
10694
+ });
10695
+ },
10696
+ getOptions: (data) => {
10697
+ return data.promotions.map((promotion) => ({
10698
+ label: promotion.code,
10699
+ value: promotion.code
10700
+ }));
10701
+ }
10702
+ });
10703
+ const add = async (value) => {
10704
+ if (!value) {
10705
+ return;
10706
+ }
10707
+ addPromotions(
10708
+ {
10709
+ promo_codes: [value]
10710
+ },
10711
+ {
10712
+ onError: (e) => {
10713
+ toast.error(e.message);
10714
+ comboboxData.onSearchValueChange("");
10715
+ setComboboxValue("");
10716
+ },
10717
+ onSuccess: () => {
10718
+ comboboxData.onSearchValueChange("");
10719
+ setComboboxValue("");
10720
+ }
10721
+ }
10722
+ );
10723
+ };
10724
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10725
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10726
+ const onSubmit = async () => {
10727
+ setIsSubmitting(true);
10728
+ let requestSucceeded = false;
10729
+ await requestOrderEdit(void 0, {
10730
+ onError: (e) => {
10731
+ toast.error(e.message);
10732
+ },
10733
+ onSuccess: () => {
10734
+ requestSucceeded = true;
10735
+ }
10736
+ });
10737
+ if (!requestSucceeded) {
10738
+ setIsSubmitting(false);
10739
+ return;
10740
+ }
10741
+ await confirmOrderEdit(void 0, {
10742
+ onError: (e) => {
10743
+ toast.error(e.message);
10744
+ },
10745
+ onSuccess: () => {
10746
+ handleSuccess();
10747
+ },
10748
+ onSettled: () => {
10749
+ setIsSubmitting(false);
10750
+ }
10751
+ });
10752
+ };
10753
+ if (isError) {
10754
+ throw error;
10755
+ }
10756
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10757
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
10758
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
10759
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10760
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10761
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10762
+ ] }),
10763
+ /* @__PURE__ */ jsx(
10764
+ Combobox,
10765
+ {
10766
+ id: "promotion-combobox",
10767
+ "aria-describedby": "promotion-combobox-hint",
10768
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10769
+ fetchNextPage: comboboxData.fetchNextPage,
10770
+ options: comboboxData.options,
10771
+ onSearchValueChange: comboboxData.onSearchValueChange,
10772
+ searchValue: comboboxData.searchValue,
10773
+ disabled: comboboxData.disabled || isAddingPromotions,
10774
+ onChange: add,
10775
+ value: comboboxValue
10776
+ }
10777
+ )
10778
+ ] }),
10779
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10780
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
10781
+ PromotionItem,
10782
+ {
10783
+ promotion,
10784
+ orderId: preview.id,
10785
+ isLoading: isPending
10786
+ },
10787
+ promotion.id
10788
+ )) })
10789
+ ] }) }),
10790
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10791
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10792
+ /* @__PURE__ */ jsx(
10793
+ Button,
10794
+ {
10795
+ size: "small",
10796
+ type: "submit",
10797
+ isLoading: isSubmitting || isAddingPromotions,
10798
+ children: "Save"
10799
+ }
10800
+ )
10801
+ ] }) })
10802
+ ] });
10803
+ };
10804
+ const PromotionItem = ({
10805
+ promotion,
10806
+ orderId,
10807
+ isLoading
10808
+ }) => {
10809
+ var _a;
10810
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10811
+ const onRemove = async () => {
10812
+ removePromotions(
10813
+ {
10814
+ promo_codes: [promotion.code]
10815
+ },
10816
+ {
10817
+ onError: (e) => {
10818
+ toast.error(e.message);
10819
+ }
10820
+ }
10821
+ );
10822
+ };
10823
+ const displayValue = getDisplayValue(promotion);
10824
+ return /* @__PURE__ */ jsxs(
10825
+ "div",
10826
+ {
10827
+ className: clx(
10828
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
10829
+ {
10830
+ "animate-pulse": isLoading
10831
+ }
10832
+ ),
10833
+ children: [
10834
+ /* @__PURE__ */ jsxs("div", { children: [
10835
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
10836
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
10837
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
10838
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
10839
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
10840
+ ] }),
10841
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
10842
+ ] })
10843
+ ] }),
10844
+ /* @__PURE__ */ jsx(
10845
+ IconButton,
10846
+ {
10847
+ size: "small",
10848
+ type: "button",
10849
+ variant: "transparent",
10850
+ onClick: onRemove,
10851
+ isLoading: isPending || isLoading,
10852
+ children: /* @__PURE__ */ jsx(XMark, {})
10853
+ }
10854
+ )
10855
+ ]
10856
+ },
10857
+ promotion.id
10858
+ );
10859
+ };
10860
+ function getDisplayValue(promotion) {
10861
+ var _a, _b, _c, _d;
10862
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
10863
+ if (!value) {
10864
+ return null;
10865
+ }
10866
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
10867
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
10868
+ if (!currency) {
10869
+ return null;
10870
+ }
10871
+ return getLocaleAmount(value, currency);
10872
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
10873
+ return formatPercentage(value);
10874
+ }
10875
+ return null;
10876
+ }
10877
+ const formatter = new Intl.NumberFormat([], {
10878
+ style: "percent",
10879
+ minimumFractionDigits: 2
10880
+ });
10881
+ const formatPercentage = (value, isPercentageValue = false) => {
10882
+ let val = value || 0;
10883
+ if (!isPercentageValue) {
10884
+ val = val / 100;
10885
+ }
10886
+ return formatter.format(val);
10887
+ };
10888
+ function getPromotionIds(items, shippingMethods) {
10889
+ const promotionIds = /* @__PURE__ */ new Set();
10890
+ for (const item of items) {
10891
+ if (item.adjustments) {
10892
+ for (const adjustment of item.adjustments) {
10893
+ if (adjustment.promotion_id) {
10894
+ promotionIds.add(adjustment.promotion_id);
10895
+ }
10896
+ }
10897
+ }
10898
+ }
10899
+ for (const shippingMethod of shippingMethods) {
10900
+ if (shippingMethod.adjustments) {
10901
+ for (const adjustment of shippingMethod.adjustments) {
10902
+ if (adjustment.promotion_id) {
10903
+ promotionIds.add(adjustment.promotion_id);
10904
+ }
10905
+ }
10906
+ }
10907
+ }
10908
+ return Array.from(promotionIds);
10909
+ }
10633
10910
  const InlineTip = forwardRef(
10634
10911
  ({ variant = "tip", label, className, children, ...props }, ref) => {
10635
10912
  const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
@@ -10980,283 +11257,112 @@ function getHasUneditableRows(metadata) {
10980
11257
  (value) => !EDITABLE_TYPES.includes(typeof value)
10981
11258
  );
10982
11259
  }
10983
- const PROMOTION_QUERY_KEY = "promotions";
10984
- const promotionsQueryKeys = {
10985
- list: (query2) => [
10986
- PROMOTION_QUERY_KEY,
10987
- query2 ? query2 : void 0
10988
- ],
10989
- detail: (id, query2) => [
10990
- PROMOTION_QUERY_KEY,
10991
- id,
10992
- query2 ? query2 : void 0
10993
- ]
10994
- };
10995
- const usePromotions = (query2, options) => {
10996
- const { data, ...rest } = useQuery({
10997
- queryKey: promotionsQueryKeys.list(query2),
10998
- queryFn: async () => sdk.admin.promotion.list(query2),
10999
- ...options
11000
- });
11001
- return { ...data, ...rest };
11002
- };
11003
- const Promotions = () => {
11260
+ const SalesChannel = () => {
11004
11261
  const { id } = useParams();
11005
- const {
11006
- order: preview,
11007
- isError: isPreviewError,
11008
- error: previewError
11009
- } = useOrderPreview(id, void 0);
11010
- useInitiateOrderEdit({ preview });
11011
- const { onCancel } = useCancelOrderEdit({ preview });
11012
- if (isPreviewError) {
11013
- throw previewError;
11014
- }
11015
- const isReady = !!preview;
11016
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11017
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11018
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11019
- ] });
11020
- };
11021
- const PromotionForm = ({ preview }) => {
11022
- const { items, shipping_methods } = preview;
11023
- const [isSubmitting, setIsSubmitting] = useState(false);
11024
- const [comboboxValue, setComboboxValue] = useState("");
11025
- const { handleSuccess } = useRouteModal();
11026
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11027
- const promoIds = getPromotionIds(items, shipping_methods);
11028
- const { promotions, isPending, isError, error } = usePromotions(
11262
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11263
+ id,
11029
11264
  {
11030
- id: promoIds
11265
+ fields: "+sales_channel_id"
11031
11266
  },
11032
11267
  {
11033
- enabled: !!promoIds.length
11268
+ enabled: !!id
11034
11269
  }
11035
11270
  );
11036
- const comboboxData = useComboboxData({
11037
- queryKey: ["promotions", "combobox", promoIds],
11038
- queryFn: async (params) => {
11039
- return await sdk.admin.promotion.list({
11040
- ...params,
11041
- id: {
11042
- $nin: promoIds
11043
- }
11044
- });
11045
- },
11046
- getOptions: (data) => {
11047
- return data.promotions.map((promotion) => ({
11048
- label: promotion.code,
11049
- value: promotion.code
11050
- }));
11051
- }
11052
- });
11053
- const add = async (value) => {
11054
- if (!value) {
11055
- return;
11056
- }
11057
- addPromotions(
11058
- {
11059
- promo_codes: [value]
11060
- },
11061
- {
11062
- onError: (e) => {
11063
- toast.error(e.message);
11064
- comboboxData.onSearchValueChange("");
11065
- setComboboxValue("");
11066
- },
11067
- onSuccess: () => {
11068
- comboboxData.onSearchValueChange("");
11069
- setComboboxValue("");
11070
- }
11071
- }
11072
- );
11073
- };
11074
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11075
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11076
- const onSubmit = async () => {
11077
- setIsSubmitting(true);
11078
- let requestSucceeded = false;
11079
- await requestOrderEdit(void 0, {
11080
- onError: (e) => {
11081
- toast.error(e.message);
11082
- },
11083
- onSuccess: () => {
11084
- requestSucceeded = true;
11085
- }
11086
- });
11087
- if (!requestSucceeded) {
11088
- setIsSubmitting(false);
11089
- return;
11090
- }
11091
- await confirmOrderEdit(void 0, {
11092
- onError: (e) => {
11093
- toast.error(e.message);
11094
- },
11095
- onSuccess: () => {
11096
- handleSuccess();
11097
- },
11098
- onSettled: () => {
11099
- setIsSubmitting(false);
11100
- }
11101
- });
11102
- };
11103
11271
  if (isError) {
11104
11272
  throw error;
11105
11273
  }
11106
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11107
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11108
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11109
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11110
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11111
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11112
- ] }),
11113
- /* @__PURE__ */ jsx(
11114
- Combobox,
11115
- {
11116
- id: "promotion-combobox",
11117
- "aria-describedby": "promotion-combobox-hint",
11118
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11119
- fetchNextPage: comboboxData.fetchNextPage,
11120
- options: comboboxData.options,
11121
- onSearchValueChange: comboboxData.onSearchValueChange,
11122
- searchValue: comboboxData.searchValue,
11123
- disabled: comboboxData.disabled || isAddingPromotions,
11124
- onChange: add,
11125
- value: comboboxValue
11126
- }
11127
- )
11128
- ] }),
11129
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11130
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11131
- PromotionItem,
11132
- {
11133
- promotion,
11134
- orderId: preview.id,
11135
- isLoading: isPending
11136
- },
11137
- promotion.id
11138
- )) })
11139
- ] }) }),
11140
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11141
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11142
- /* @__PURE__ */ jsx(
11143
- Button,
11144
- {
11145
- size: "small",
11146
- type: "submit",
11147
- isLoading: isSubmitting || isAddingPromotions,
11148
- children: "Save"
11149
- }
11150
- )
11151
- ] }) })
11274
+ const ISrEADY = !!draft_order && !isPending;
11275
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11276
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11277
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11278
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11279
+ ] }),
11280
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11152
11281
  ] });
11153
11282
  };
11154
- const PromotionItem = ({
11155
- promotion,
11156
- orderId,
11157
- isLoading
11158
- }) => {
11159
- var _a;
11160
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11161
- const onRemove = async () => {
11162
- removePromotions(
11283
+ const SalesChannelForm = ({ order }) => {
11284
+ const form = useForm({
11285
+ defaultValues: {
11286
+ sales_channel_id: order.sales_channel_id || ""
11287
+ },
11288
+ resolver: zodResolver(schema$3)
11289
+ });
11290
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11291
+ const { handleSuccess } = useRouteModal();
11292
+ const onSubmit = form.handleSubmit(async (data) => {
11293
+ await mutateAsync(
11163
11294
  {
11164
- promo_codes: [promotion.code]
11295
+ sales_channel_id: data.sales_channel_id
11165
11296
  },
11166
11297
  {
11167
- onError: (e) => {
11168
- toast.error(e.message);
11298
+ onSuccess: () => {
11299
+ toast.success("Sales channel updated");
11300
+ handleSuccess();
11301
+ },
11302
+ onError: (error) => {
11303
+ toast.error(error.message);
11169
11304
  }
11170
11305
  }
11171
11306
  );
11172
- };
11173
- const displayValue = getDisplayValue(promotion);
11174
- return /* @__PURE__ */ jsxs(
11175
- "div",
11307
+ });
11308
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11309
+ KeyboundForm,
11176
11310
  {
11177
- className: clx(
11178
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11179
- {
11180
- "animate-pulse": isLoading
11181
- }
11182
- ),
11311
+ className: "flex flex-1 flex-col overflow-hidden",
11312
+ onSubmit,
11183
11313
  children: [
11184
- /* @__PURE__ */ jsxs("div", { children: [
11185
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11186
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11187
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11188
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11189
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11190
- ] }),
11191
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11192
- ] })
11193
- ] }),
11194
- /* @__PURE__ */ jsx(
11195
- IconButton,
11196
- {
11197
- size: "small",
11198
- type: "button",
11199
- variant: "transparent",
11200
- onClick: onRemove,
11201
- isLoading: isPending || isLoading,
11202
- children: /* @__PURE__ */ jsx(XMark, {})
11203
- }
11204
- )
11314
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11315
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11316
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11317
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11318
+ ] }) })
11205
11319
  ]
11206
- },
11207
- promotion.id
11208
- );
11209
- };
11210
- function getDisplayValue(promotion) {
11211
- var _a, _b, _c, _d;
11212
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11213
- if (!value) {
11214
- return null;
11215
- }
11216
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11217
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11218
- if (!currency) {
11219
- return null;
11220
11320
  }
11221
- return getLocaleAmount(value, currency);
11222
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11223
- return formatPercentage(value);
11224
- }
11225
- return null;
11226
- }
11227
- const formatter = new Intl.NumberFormat([], {
11228
- style: "percent",
11229
- minimumFractionDigits: 2
11230
- });
11231
- const formatPercentage = (value, isPercentageValue = false) => {
11232
- let val = value || 0;
11233
- if (!isPercentageValue) {
11234
- val = val / 100;
11235
- }
11236
- return formatter.format(val);
11321
+ ) });
11237
11322
  };
11238
- function getPromotionIds(items, shippingMethods) {
11239
- const promotionIds = /* @__PURE__ */ new Set();
11240
- for (const item of items) {
11241
- if (item.adjustments) {
11242
- for (const adjustment of item.adjustments) {
11243
- if (adjustment.promotion_id) {
11244
- promotionIds.add(adjustment.promotion_id);
11245
- }
11246
- }
11247
- }
11248
- }
11249
- for (const shippingMethod of shippingMethods) {
11250
- if (shippingMethod.adjustments) {
11251
- for (const adjustment of shippingMethod.adjustments) {
11252
- if (adjustment.promotion_id) {
11253
- promotionIds.add(adjustment.promotion_id);
11254
- }
11323
+ const SalesChannelField = ({ control, order }) => {
11324
+ const salesChannels = useComboboxData({
11325
+ queryFn: async (params) => {
11326
+ return await sdk.admin.salesChannel.list(params);
11327
+ },
11328
+ queryKey: ["sales-channels"],
11329
+ getOptions: (data) => {
11330
+ return data.sales_channels.map((salesChannel) => ({
11331
+ label: salesChannel.name,
11332
+ value: salesChannel.id
11333
+ }));
11334
+ },
11335
+ defaultValue: order.sales_channel_id || void 0
11336
+ });
11337
+ return /* @__PURE__ */ jsx(
11338
+ Form$2.Field,
11339
+ {
11340
+ control,
11341
+ name: "sales_channel_id",
11342
+ render: ({ field }) => {
11343
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11344
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11345
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11346
+ Combobox,
11347
+ {
11348
+ options: salesChannels.options,
11349
+ fetchNextPage: salesChannels.fetchNextPage,
11350
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11351
+ searchValue: salesChannels.searchValue,
11352
+ onSearchValueChange: salesChannels.onSearchValueChange,
11353
+ placeholder: "Select sales channel",
11354
+ ...field
11355
+ }
11356
+ ) }),
11357
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11358
+ ] });
11255
11359
  }
11256
11360
  }
11257
- }
11258
- return Array.from(promotionIds);
11259
- }
11361
+ );
11362
+ };
11363
+ const schema$3 = objectType({
11364
+ sales_channel_id: stringType().min(1)
11365
+ });
11260
11366
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
11261
11367
  const Shipping = () => {
11262
11368
  var _a;
@@ -12096,7 +12202,7 @@ const ShippingAddressForm = ({ order }) => {
12096
12202
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12097
12203
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12098
12204
  },
12099
- resolver: zodResolver(schema$3)
12205
+ resolver: zodResolver(schema$2)
12100
12206
  });
12101
12207
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12102
12208
  const { handleSuccess } = useRouteModal();
@@ -12266,7 +12372,7 @@ const ShippingAddressForm = ({ order }) => {
12266
12372
  }
12267
12373
  ) });
12268
12374
  };
12269
- const schema$3 = addressSchema;
12375
+ const schema$2 = addressSchema;
12270
12376
  const TransferOwnership = () => {
12271
12377
  const { id } = useParams();
12272
12378
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12290,7 +12396,7 @@ const TransferOwnershipForm = ({ order }) => {
12290
12396
  defaultValues: {
12291
12397
  customer_id: order.customer_id || ""
12292
12398
  },
12293
- resolver: zodResolver(schema$2)
12399
+ resolver: zodResolver(schema$1)
12294
12400
  });
12295
12401
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12296
12402
  const { handleSuccess } = useRouteModal();
@@ -12740,7 +12846,7 @@ const Illustration = () => {
12740
12846
  }
12741
12847
  );
12742
12848
  };
12743
- const schema$2 = objectType({
12849
+ const schema$1 = objectType({
12744
12850
  customer_id: stringType().min(1)
12745
12851
  });
12746
12852
  const BillingAddress = () => {
@@ -12775,7 +12881,7 @@ const BillingAddressForm = ({ order }) => {
12775
12881
  postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
12776
12882
  phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
12777
12883
  },
12778
- resolver: zodResolver(schema$1)
12884
+ resolver: zodResolver(schema)
12779
12885
  });
12780
12886
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12781
12887
  const { handleSuccess } = useRouteModal();
@@ -12932,113 +13038,7 @@ const BillingAddressForm = ({ order }) => {
12932
13038
  }
12933
13039
  ) });
12934
13040
  };
12935
- const schema$1 = addressSchema;
12936
- const SalesChannel = () => {
12937
- const { id } = useParams();
12938
- const { draft_order, isPending, isError, error } = useDraftOrder(
12939
- id,
12940
- {
12941
- fields: "+sales_channel_id"
12942
- },
12943
- {
12944
- enabled: !!id
12945
- }
12946
- );
12947
- if (isError) {
12948
- throw error;
12949
- }
12950
- const ISrEADY = !!draft_order && !isPending;
12951
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12952
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12953
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
12954
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12955
- ] }),
12956
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
12957
- ] });
12958
- };
12959
- const SalesChannelForm = ({ order }) => {
12960
- const form = useForm({
12961
- defaultValues: {
12962
- sales_channel_id: order.sales_channel_id || ""
12963
- },
12964
- resolver: zodResolver(schema)
12965
- });
12966
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12967
- const { handleSuccess } = useRouteModal();
12968
- const onSubmit = form.handleSubmit(async (data) => {
12969
- await mutateAsync(
12970
- {
12971
- sales_channel_id: data.sales_channel_id
12972
- },
12973
- {
12974
- onSuccess: () => {
12975
- toast.success("Sales channel updated");
12976
- handleSuccess();
12977
- },
12978
- onError: (error) => {
12979
- toast.error(error.message);
12980
- }
12981
- }
12982
- );
12983
- });
12984
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12985
- KeyboundForm,
12986
- {
12987
- className: "flex flex-1 flex-col overflow-hidden",
12988
- onSubmit,
12989
- children: [
12990
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
12991
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12992
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12993
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12994
- ] }) })
12995
- ]
12996
- }
12997
- ) });
12998
- };
12999
- const SalesChannelField = ({ control, order }) => {
13000
- const salesChannels = useComboboxData({
13001
- queryFn: async (params) => {
13002
- return await sdk.admin.salesChannel.list(params);
13003
- },
13004
- queryKey: ["sales-channels"],
13005
- getOptions: (data) => {
13006
- return data.sales_channels.map((salesChannel) => ({
13007
- label: salesChannel.name,
13008
- value: salesChannel.id
13009
- }));
13010
- },
13011
- defaultValue: order.sales_channel_id || void 0
13012
- });
13013
- return /* @__PURE__ */ jsx(
13014
- Form$2.Field,
13015
- {
13016
- control,
13017
- name: "sales_channel_id",
13018
- render: ({ field }) => {
13019
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13020
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
13021
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
13022
- Combobox,
13023
- {
13024
- options: salesChannels.options,
13025
- fetchNextPage: salesChannels.fetchNextPage,
13026
- isFetchingNextPage: salesChannels.isFetchingNextPage,
13027
- searchValue: salesChannels.searchValue,
13028
- onSearchValueChange: salesChannels.onSearchValueChange,
13029
- placeholder: "Select sales channel",
13030
- ...field
13031
- }
13032
- ) }),
13033
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13034
- ] });
13035
- }
13036
- }
13037
- );
13038
- };
13039
- const schema = objectType({
13040
- sales_channel_id: stringType().min(1)
13041
- });
13041
+ const schema = addressSchema;
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
13044
13044
  routes: [
@@ -13071,13 +13071,17 @@ const routeModule = {
13071
13071
  Component: Items,
13072
13072
  path: "/draft-orders/:id/items"
13073
13073
  },
13074
+ {
13075
+ Component: Promotions,
13076
+ path: "/draft-orders/:id/promotions"
13077
+ },
13074
13078
  {
13075
13079
  Component: Metadata,
13076
13080
  path: "/draft-orders/:id/metadata"
13077
13081
  },
13078
13082
  {
13079
- Component: Promotions,
13080
- path: "/draft-orders/:id/promotions"
13083
+ Component: SalesChannel,
13084
+ path: "/draft-orders/:id/sales-channel"
13081
13085
  },
13082
13086
  {
13083
13087
  Component: Shipping,
@@ -13094,10 +13098,6 @@ const routeModule = {
13094
13098
  {
13095
13099
  Component: BillingAddress,
13096
13100
  path: "/draft-orders/:id/billing-address"
13097
- },
13098
- {
13099
- Component: SalesChannel,
13100
- path: "/draft-orders/:id/sales-channel"
13101
13101
  }
13102
13102
  ]
13103
13103
  }