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