@medusajs/draft-order 2.22.2-preview-20261029142542 → 2.22.2

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.
@@ -6186,6 +6186,74 @@ const CustomItemsForm = () => {
6186
6186
  const schema$4 = z.object({
6187
6187
  email: z.string().email()
6188
6188
  });
6189
+ const Email = () => {
6190
+ const { id } = useParams();
6191
+ const { order, isPending, isError, error } = useOrder(id, {
6192
+ fields: "+email"
6193
+ });
6194
+ if (isError) {
6195
+ throw error;
6196
+ }
6197
+ const isReady = !isPending && !!order;
6198
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
6199
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
6200
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
6201
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
6202
+ ] }),
6203
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
6204
+ ] });
6205
+ };
6206
+ const EmailForm = ({ order }) => {
6207
+ const form = useForm({
6208
+ defaultValues: {
6209
+ email: order.email ?? ""
6210
+ },
6211
+ resolver: zodResolver(schema$3)
6212
+ });
6213
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
6214
+ const { handleSuccess } = useRouteModal();
6215
+ const onSubmit = form.handleSubmit(async (data) => {
6216
+ await mutateAsync(
6217
+ { email: data.email },
6218
+ {
6219
+ onSuccess: () => {
6220
+ handleSuccess();
6221
+ },
6222
+ onError: (error) => {
6223
+ toast.error(error.message);
6224
+ }
6225
+ }
6226
+ );
6227
+ });
6228
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
6229
+ KeyboundForm,
6230
+ {
6231
+ className: "flex flex-1 flex-col overflow-hidden",
6232
+ onSubmit,
6233
+ children: [
6234
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
6235
+ Form$2.Field,
6236
+ {
6237
+ control: form.control,
6238
+ name: "email",
6239
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
6240
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
6241
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
6242
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
6243
+ ] })
6244
+ }
6245
+ ) }),
6246
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
6247
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
6248
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
6249
+ ] }) })
6250
+ ]
6251
+ }
6252
+ ) });
6253
+ };
6254
+ const schema$3 = z.object({
6255
+ email: z.string().email()
6256
+ });
6189
6257
  const NumberInput = forwardRef(
6190
6258
  ({
6191
6259
  value,
@@ -7205,74 +7273,6 @@ const customItemSchema = z.object({
7205
7273
  quantity: z.number(),
7206
7274
  unit_price: z.union([z.number(), z.string()])
7207
7275
  });
7208
- const Email = () => {
7209
- const { id } = useParams();
7210
- const { order, isPending, isError, error } = useOrder(id, {
7211
- fields: "+email"
7212
- });
7213
- if (isError) {
7214
- throw error;
7215
- }
7216
- const isReady = !isPending && !!order;
7217
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
7218
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
7219
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
7220
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
7221
- ] }),
7222
- isReady && /* @__PURE__ */ jsx(EmailForm, { order })
7223
- ] });
7224
- };
7225
- const EmailForm = ({ order }) => {
7226
- const form = useForm({
7227
- defaultValues: {
7228
- email: order.email ?? ""
7229
- },
7230
- resolver: zodResolver(schema$3)
7231
- });
7232
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
7233
- const { handleSuccess } = useRouteModal();
7234
- const onSubmit = form.handleSubmit(async (data) => {
7235
- await mutateAsync(
7236
- { email: data.email },
7237
- {
7238
- onSuccess: () => {
7239
- handleSuccess();
7240
- },
7241
- onError: (error) => {
7242
- toast.error(error.message);
7243
- }
7244
- }
7245
- );
7246
- });
7247
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
7248
- KeyboundForm,
7249
- {
7250
- className: "flex flex-1 flex-col overflow-hidden",
7251
- onSubmit,
7252
- children: [
7253
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
7254
- Form$2.Field,
7255
- {
7256
- control: form.control,
7257
- name: "email",
7258
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
7259
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
7260
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
7261
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
7262
- ] })
7263
- }
7264
- ) }),
7265
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
7266
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7267
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
7268
- ] }) })
7269
- ]
7270
- }
7271
- ) });
7272
- };
7273
- const schema$3 = z.object({
7274
- email: z.string().email()
7275
- });
7276
7276
  const InlineTip = forwardRef(
7277
7277
  ({ variant = "tip", label, className, children, ...props }, ref) => {
7278
7278
  const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
@@ -7623,112 +7623,281 @@ function getHasUneditableRows(metadata) {
7623
7623
  (value) => !EDITABLE_TYPES.includes(typeof value)
7624
7624
  );
7625
7625
  }
7626
- const SalesChannel = () => {
7627
- const { id } = useParams();
7628
- const { draft_order, isPending, isError, error } = useDraftOrder(
7626
+ const PROMOTION_QUERY_KEY = "promotions";
7627
+ const promotionsQueryKeys = {
7628
+ list: (query2) => [
7629
+ PROMOTION_QUERY_KEY,
7630
+ query2 ? query2 : void 0
7631
+ ],
7632
+ detail: (id, query2) => [
7633
+ PROMOTION_QUERY_KEY,
7629
7634
  id,
7635
+ query2 ? query2 : void 0
7636
+ ]
7637
+ };
7638
+ const usePromotions = (query2, options) => {
7639
+ const { data, ...rest } = useQuery({
7640
+ queryKey: promotionsQueryKeys.list(query2),
7641
+ queryFn: async () => sdk.admin.promotion.list(query2),
7642
+ ...options
7643
+ });
7644
+ return { ...data, ...rest };
7645
+ };
7646
+ const Promotions = () => {
7647
+ const { id } = useParams();
7648
+ const {
7649
+ order: preview,
7650
+ isError: isPreviewError,
7651
+ error: previewError
7652
+ } = useOrderPreview(id, void 0);
7653
+ useInitiateOrderEdit({ preview });
7654
+ const { onCancel } = useCancelOrderEdit({ preview });
7655
+ if (isPreviewError) {
7656
+ throw previewError;
7657
+ }
7658
+ const isReady = !!preview;
7659
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
7660
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
7661
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
7662
+ ] });
7663
+ };
7664
+ const PromotionForm = ({ preview }) => {
7665
+ const { items, shipping_methods } = preview;
7666
+ const [isSubmitting, setIsSubmitting] = useState(false);
7667
+ const [comboboxValue, setComboboxValue] = useState("");
7668
+ const { handleSuccess } = useRouteModal();
7669
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
7670
+ const promoIds = getPromotionIds(items, shipping_methods);
7671
+ const { promotions, isPending, isError, error } = usePromotions(
7630
7672
  {
7631
- fields: "+sales_channel_id"
7673
+ id: promoIds
7632
7674
  },
7633
7675
  {
7634
- enabled: !!id
7676
+ enabled: !!promoIds.length
7635
7677
  }
7636
7678
  );
7637
- if (isError) {
7638
- throw error;
7639
- }
7640
- const ISrEADY = !!draft_order && !isPending;
7641
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
7642
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
7643
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
7644
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
7645
- ] }),
7646
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
7647
- ] });
7648
- };
7649
- const SalesChannelForm = ({ order }) => {
7650
- const form = useForm({
7651
- defaultValues: {
7652
- sales_channel_id: order.sales_channel_id || ""
7679
+ const comboboxData = useComboboxData({
7680
+ queryKey: ["promotions", "combobox", promoIds],
7681
+ queryFn: async (params) => {
7682
+ return await sdk.admin.promotion.list({
7683
+ ...params,
7684
+ id: {
7685
+ $nin: promoIds
7686
+ }
7687
+ });
7653
7688
  },
7654
- resolver: zodResolver(schema$2)
7689
+ getOptions: (data) => {
7690
+ return data.promotions.map((promotion) => ({
7691
+ label: promotion.code,
7692
+ value: promotion.code
7693
+ }));
7694
+ }
7655
7695
  });
7656
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
7657
- const { handleSuccess } = useRouteModal();
7658
- const onSubmit = form.handleSubmit(async (data) => {
7659
- await mutateAsync(
7696
+ const add = async (value) => {
7697
+ if (!value) {
7698
+ return;
7699
+ }
7700
+ addPromotions(
7660
7701
  {
7661
- sales_channel_id: data.sales_channel_id
7702
+ promo_codes: [value]
7662
7703
  },
7663
7704
  {
7664
- onSuccess: () => {
7665
- toast.success("Sales channel updated");
7666
- handleSuccess();
7705
+ onError: (e) => {
7706
+ toast.error(e.message);
7707
+ comboboxData.onSearchValueChange("");
7708
+ setComboboxValue("");
7667
7709
  },
7668
- onError: (error) => {
7669
- toast.error(error.message);
7710
+ onSuccess: () => {
7711
+ comboboxData.onSearchValueChange("");
7712
+ setComboboxValue("");
7670
7713
  }
7671
7714
  }
7672
7715
  );
7673
- });
7674
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
7675
- KeyboundForm,
7676
- {
7677
- className: "flex flex-1 flex-col overflow-hidden",
7678
- onSubmit,
7679
- children: [
7680
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
7681
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
7682
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7683
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
7684
- ] }) })
7685
- ]
7716
+ };
7717
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
7718
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
7719
+ const onSubmit = async () => {
7720
+ setIsSubmitting(true);
7721
+ let requestSucceeded = false;
7722
+ await requestOrderEdit(void 0, {
7723
+ onError: (e) => {
7724
+ toast.error(e.message);
7725
+ },
7726
+ onSuccess: () => {
7727
+ requestSucceeded = true;
7728
+ }
7729
+ });
7730
+ if (!requestSucceeded) {
7731
+ setIsSubmitting(false);
7732
+ return;
7686
7733
  }
7687
- ) });
7734
+ await confirmOrderEdit(void 0, {
7735
+ onError: (e) => {
7736
+ toast.error(e.message);
7737
+ },
7738
+ onSuccess: () => {
7739
+ handleSuccess();
7740
+ },
7741
+ onSettled: () => {
7742
+ setIsSubmitting(false);
7743
+ }
7744
+ });
7745
+ };
7746
+ if (isError) {
7747
+ throw error;
7748
+ }
7749
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
7750
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
7751
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
7752
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
7753
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
7754
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
7755
+ ] }),
7756
+ /* @__PURE__ */ jsx(
7757
+ Combobox,
7758
+ {
7759
+ id: "promotion-combobox",
7760
+ "aria-describedby": "promotion-combobox-hint",
7761
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
7762
+ fetchNextPage: comboboxData.fetchNextPage,
7763
+ options: comboboxData.options,
7764
+ onSearchValueChange: comboboxData.onSearchValueChange,
7765
+ searchValue: comboboxData.searchValue,
7766
+ disabled: comboboxData.disabled || isAddingPromotions,
7767
+ onChange: add,
7768
+ value: comboboxValue
7769
+ }
7770
+ )
7771
+ ] }),
7772
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
7773
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions?.map((promotion) => /* @__PURE__ */ jsx(
7774
+ PromotionItem,
7775
+ {
7776
+ promotion,
7777
+ orderId: preview.id,
7778
+ isLoading: isPending
7779
+ },
7780
+ promotion.id
7781
+ )) })
7782
+ ] }) }),
7783
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
7784
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7785
+ /* @__PURE__ */ jsx(
7786
+ Button,
7787
+ {
7788
+ size: "small",
7789
+ type: "submit",
7790
+ isLoading: isSubmitting || isAddingPromotions,
7791
+ children: "Save"
7792
+ }
7793
+ )
7794
+ ] }) })
7795
+ ] });
7688
7796
  };
7689
- const SalesChannelField = ({ control, order }) => {
7690
- const salesChannels = useComboboxData({
7691
- queryFn: async (params) => {
7692
- return await sdk.admin.salesChannel.list(params);
7693
- },
7694
- queryKey: ["sales-channels"],
7695
- getOptions: (data) => {
7696
- return data.sales_channels.map((salesChannel) => ({
7697
- label: salesChannel.name,
7698
- value: salesChannel.id
7699
- }));
7700
- },
7701
- defaultValue: order.sales_channel_id || void 0
7702
- });
7703
- return /* @__PURE__ */ jsx(
7704
- Form$2.Field,
7705
- {
7706
- control,
7707
- name: "sales_channel_id",
7708
- render: ({ field }) => {
7709
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
7710
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
7711
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
7712
- Combobox,
7713
- {
7714
- options: salesChannels.options,
7715
- fetchNextPage: salesChannels.fetchNextPage,
7716
- isFetchingNextPage: salesChannels.isFetchingNextPage,
7717
- searchValue: salesChannels.searchValue,
7718
- onSearchValueChange: salesChannels.onSearchValueChange,
7719
- placeholder: "Select sales channel",
7720
- ...field
7721
- }
7722
- ) }),
7723
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
7724
- ] });
7797
+ const PromotionItem = ({
7798
+ promotion,
7799
+ orderId,
7800
+ isLoading
7801
+ }) => {
7802
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
7803
+ const onRemove = async () => {
7804
+ removePromotions(
7805
+ {
7806
+ promo_codes: [promotion.code]
7807
+ },
7808
+ {
7809
+ onError: (e) => {
7810
+ toast.error(e.message);
7811
+ }
7725
7812
  }
7726
- }
7813
+ );
7814
+ };
7815
+ const displayValue = getDisplayValue(promotion);
7816
+ return /* @__PURE__ */ jsxs(
7817
+ "div",
7818
+ {
7819
+ className: clx(
7820
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
7821
+ {
7822
+ "animate-pulse": isLoading
7823
+ }
7824
+ ),
7825
+ children: [
7826
+ /* @__PURE__ */ jsxs("div", { children: [
7827
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
7828
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
7829
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
7830
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
7831
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
7832
+ ] }),
7833
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: promotion.application_method?.allocation })
7834
+ ] })
7835
+ ] }),
7836
+ /* @__PURE__ */ jsx(
7837
+ IconButton,
7838
+ {
7839
+ size: "small",
7840
+ type: "button",
7841
+ variant: "transparent",
7842
+ onClick: onRemove,
7843
+ isLoading: isPending || isLoading,
7844
+ children: /* @__PURE__ */ jsx(XMark, {})
7845
+ }
7846
+ )
7847
+ ]
7848
+ },
7849
+ promotion.id
7727
7850
  );
7728
7851
  };
7729
- const schema$2 = z.object({
7730
- sales_channel_id: z.string().min(1)
7852
+ function getDisplayValue(promotion) {
7853
+ const value = promotion.application_method?.value;
7854
+ if (!value) {
7855
+ return null;
7856
+ }
7857
+ if (promotion.application_method?.type === "fixed") {
7858
+ const currency = promotion.application_method?.currency_code;
7859
+ if (!currency) {
7860
+ return null;
7861
+ }
7862
+ return getLocaleAmount(value, currency);
7863
+ } else if (promotion.application_method?.type === "percentage") {
7864
+ return formatPercentage(value);
7865
+ }
7866
+ return null;
7867
+ }
7868
+ const formatter = new Intl.NumberFormat([], {
7869
+ style: "percent",
7870
+ minimumFractionDigits: 2
7731
7871
  });
7872
+ const formatPercentage = (value, isPercentageValue = false) => {
7873
+ let val = value || 0;
7874
+ if (!isPercentageValue) {
7875
+ val = val / 100;
7876
+ }
7877
+ return formatter.format(val);
7878
+ };
7879
+ function getPromotionIds(items, shippingMethods) {
7880
+ const promotionIds = /* @__PURE__ */ new Set();
7881
+ for (const item of items) {
7882
+ if (item.adjustments) {
7883
+ for (const adjustment of item.adjustments) {
7884
+ if (adjustment.promotion_id) {
7885
+ promotionIds.add(adjustment.promotion_id);
7886
+ }
7887
+ }
7888
+ }
7889
+ }
7890
+ for (const shippingMethod of shippingMethods) {
7891
+ if (shippingMethod.adjustments) {
7892
+ for (const adjustment of shippingMethod.adjustments) {
7893
+ if (adjustment.promotion_id) {
7894
+ promotionIds.add(adjustment.promotion_id);
7895
+ }
7896
+ }
7897
+ }
7898
+ }
7899
+ return Array.from(promotionIds);
7900
+ }
7732
7901
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
7733
7902
  const Shipping = () => {
7734
7903
  const { id } = useParams();
@@ -8554,7 +8723,7 @@ const ShippingAddressForm = ({ order }) => {
8554
8723
  postal_code: order.shipping_address?.postal_code ?? "",
8555
8724
  phone: order.shipping_address?.phone ?? ""
8556
8725
  },
8557
- resolver: zodResolver(schema$1)
8726
+ resolver: zodResolver(schema$2)
8558
8727
  });
8559
8728
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
8560
8729
  const { handleSuccess } = useRouteModal();
@@ -8724,7 +8893,7 @@ const ShippingAddressForm = ({ order }) => {
8724
8893
  }
8725
8894
  ) });
8726
8895
  };
8727
- const schema$1 = addressSchema;
8896
+ const schema$2 = addressSchema;
8728
8897
  const TransferOwnership = () => {
8729
8898
  const { id } = useParams();
8730
8899
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -8747,7 +8916,7 @@ const TransferOwnershipForm = ({ order }) => {
8747
8916
  defaultValues: {
8748
8917
  customer_id: order.customer_id || ""
8749
8918
  },
8750
- resolver: zodResolver(schema)
8919
+ resolver: zodResolver(schema$1)
8751
8920
  });
8752
8921
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
8753
8922
  const { handleSuccess } = useRouteModal();
@@ -9199,284 +9368,115 @@ const Illustration = () => {
9199
9368
  }
9200
9369
  );
9201
9370
  };
9202
- const schema = z.object({
9371
+ const schema$1 = z.object({
9203
9372
  customer_id: z.string().min(1)
9204
9373
  });
9205
- const PROMOTION_QUERY_KEY = "promotions";
9206
- const promotionsQueryKeys = {
9207
- list: (query2) => [
9208
- PROMOTION_QUERY_KEY,
9209
- query2 ? query2 : void 0
9210
- ],
9211
- detail: (id, query2) => [
9212
- PROMOTION_QUERY_KEY,
9374
+ const SalesChannel = () => {
9375
+ const { id } = useParams();
9376
+ const { draft_order, isPending, isError, error } = useDraftOrder(
9213
9377
  id,
9214
- query2 ? query2 : void 0
9215
- ]
9378
+ {
9379
+ fields: "+sales_channel_id"
9380
+ },
9381
+ {
9382
+ enabled: !!id
9383
+ }
9384
+ );
9385
+ if (isError) {
9386
+ throw error;
9387
+ }
9388
+ const ISrEADY = !!draft_order && !isPending;
9389
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9390
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9391
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
9392
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
9393
+ ] }),
9394
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
9395
+ ] });
9216
9396
  };
9217
- const usePromotions = (query2, options) => {
9218
- const { data, ...rest } = useQuery({
9219
- queryKey: promotionsQueryKeys.list(query2),
9220
- queryFn: async () => sdk.admin.promotion.list(query2),
9221
- ...options
9222
- });
9223
- return { ...data, ...rest };
9224
- };
9225
- const Promotions = () => {
9226
- const { id } = useParams();
9227
- const {
9228
- order: preview,
9229
- isError: isPreviewError,
9230
- error: previewError
9231
- } = useOrderPreview(id, void 0);
9232
- useInitiateOrderEdit({ preview });
9233
- const { onCancel } = useCancelOrderEdit({ preview });
9234
- if (isPreviewError) {
9235
- throw previewError;
9236
- }
9237
- const isReady = !!preview;
9238
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
9239
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
9240
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
9241
- ] });
9242
- };
9243
- const PromotionForm = ({ preview }) => {
9244
- const { items, shipping_methods } = preview;
9245
- const [isSubmitting, setIsSubmitting] = useState(false);
9246
- const [comboboxValue, setComboboxValue] = useState("");
9247
- const { handleSuccess } = useRouteModal();
9248
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
9249
- const promoIds = getPromotionIds(items, shipping_methods);
9250
- const { promotions, isPending, isError, error } = usePromotions(
9251
- {
9252
- id: promoIds
9253
- },
9254
- {
9255
- enabled: !!promoIds.length
9256
- }
9257
- );
9258
- const comboboxData = useComboboxData({
9259
- queryKey: ["promotions", "combobox", promoIds],
9260
- queryFn: async (params) => {
9261
- return await sdk.admin.promotion.list({
9262
- ...params,
9263
- id: {
9264
- $nin: promoIds
9265
- }
9266
- });
9397
+ const SalesChannelForm = ({ order }) => {
9398
+ const form = useForm({
9399
+ defaultValues: {
9400
+ sales_channel_id: order.sales_channel_id || ""
9267
9401
  },
9268
- getOptions: (data) => {
9269
- return data.promotions.map((promotion) => ({
9270
- label: promotion.code,
9271
- value: promotion.code
9272
- }));
9273
- }
9402
+ resolver: zodResolver(schema)
9274
9403
  });
9275
- const add = async (value) => {
9276
- if (!value) {
9277
- return;
9278
- }
9279
- addPromotions(
9404
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9405
+ const { handleSuccess } = useRouteModal();
9406
+ const onSubmit = form.handleSubmit(async (data) => {
9407
+ await mutateAsync(
9280
9408
  {
9281
- promo_codes: [value]
9409
+ sales_channel_id: data.sales_channel_id
9282
9410
  },
9283
9411
  {
9284
- onError: (e) => {
9285
- toast.error(e.message);
9286
- comboboxData.onSearchValueChange("");
9287
- setComboboxValue("");
9288
- },
9289
9412
  onSuccess: () => {
9290
- comboboxData.onSearchValueChange("");
9291
- setComboboxValue("");
9292
- }
9293
- }
9294
- );
9295
- };
9296
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
9297
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
9298
- const onSubmit = async () => {
9299
- setIsSubmitting(true);
9300
- let requestSucceeded = false;
9301
- await requestOrderEdit(void 0, {
9302
- onError: (e) => {
9303
- toast.error(e.message);
9304
- },
9305
- onSuccess: () => {
9306
- requestSucceeded = true;
9307
- }
9308
- });
9309
- if (!requestSucceeded) {
9310
- setIsSubmitting(false);
9311
- return;
9312
- }
9313
- await confirmOrderEdit(void 0, {
9314
- onError: (e) => {
9315
- toast.error(e.message);
9316
- },
9317
- onSuccess: () => {
9318
- handleSuccess();
9319
- },
9320
- onSettled: () => {
9321
- setIsSubmitting(false);
9322
- }
9323
- });
9324
- };
9325
- if (isError) {
9326
- throw error;
9327
- }
9328
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
9329
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
9330
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
9331
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
9332
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
9333
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
9334
- ] }),
9335
- /* @__PURE__ */ jsx(
9336
- Combobox,
9337
- {
9338
- id: "promotion-combobox",
9339
- "aria-describedby": "promotion-combobox-hint",
9340
- isFetchingNextPage: comboboxData.isFetchingNextPage,
9341
- fetchNextPage: comboboxData.fetchNextPage,
9342
- options: comboboxData.options,
9343
- onSearchValueChange: comboboxData.onSearchValueChange,
9344
- searchValue: comboboxData.searchValue,
9345
- disabled: comboboxData.disabled || isAddingPromotions,
9346
- onChange: add,
9347
- value: comboboxValue
9348
- }
9349
- )
9350
- ] }),
9351
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
9352
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions?.map((promotion) => /* @__PURE__ */ jsx(
9353
- PromotionItem,
9354
- {
9355
- promotion,
9356
- orderId: preview.id,
9357
- isLoading: isPending
9413
+ toast.success("Sales channel updated");
9414
+ handleSuccess();
9358
9415
  },
9359
- promotion.id
9360
- )) })
9361
- ] }) }),
9362
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9363
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9364
- /* @__PURE__ */ jsx(
9365
- Button,
9366
- {
9367
- size: "small",
9368
- type: "submit",
9369
- isLoading: isSubmitting || isAddingPromotions,
9370
- children: "Save"
9371
- }
9372
- )
9373
- ] }) })
9374
- ] });
9375
- };
9376
- const PromotionItem = ({
9377
- promotion,
9378
- orderId,
9379
- isLoading
9380
- }) => {
9381
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
9382
- const onRemove = async () => {
9383
- removePromotions(
9384
- {
9385
- promo_codes: [promotion.code]
9386
- },
9387
- {
9388
- onError: (e) => {
9389
- toast.error(e.message);
9416
+ onError: (error) => {
9417
+ toast.error(error.message);
9390
9418
  }
9391
9419
  }
9392
9420
  );
9393
- };
9394
- const displayValue = getDisplayValue(promotion);
9395
- return /* @__PURE__ */ jsxs(
9396
- "div",
9421
+ });
9422
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9423
+ KeyboundForm,
9397
9424
  {
9398
- className: clx(
9399
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
9400
- {
9401
- "animate-pulse": isLoading
9402
- }
9403
- ),
9425
+ className: "flex flex-1 flex-col overflow-hidden",
9426
+ onSubmit,
9404
9427
  children: [
9405
- /* @__PURE__ */ jsxs("div", { children: [
9406
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
9407
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
9408
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
9409
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
9410
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
9411
- ] }),
9412
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: promotion.application_method?.allocation })
9413
- ] })
9414
- ] }),
9415
- /* @__PURE__ */ jsx(
9416
- IconButton,
9417
- {
9418
- size: "small",
9419
- type: "button",
9420
- variant: "transparent",
9421
- onClick: onRemove,
9422
- isLoading: isPending || isLoading,
9423
- children: /* @__PURE__ */ jsx(XMark, {})
9424
- }
9425
- )
9428
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
9429
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9430
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9431
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9432
+ ] }) })
9426
9433
  ]
9427
- },
9428
- promotion.id
9429
- );
9430
- };
9431
- function getDisplayValue(promotion) {
9432
- const value = promotion.application_method?.value;
9433
- if (!value) {
9434
- return null;
9435
- }
9436
- if (promotion.application_method?.type === "fixed") {
9437
- const currency = promotion.application_method?.currency_code;
9438
- if (!currency) {
9439
- return null;
9440
9434
  }
9441
- return getLocaleAmount(value, currency);
9442
- } else if (promotion.application_method?.type === "percentage") {
9443
- return formatPercentage(value);
9444
- }
9445
- return null;
9446
- }
9447
- const formatter = new Intl.NumberFormat([], {
9448
- style: "percent",
9449
- minimumFractionDigits: 2
9450
- });
9451
- const formatPercentage = (value, isPercentageValue = false) => {
9452
- let val = value || 0;
9453
- if (!isPercentageValue) {
9454
- val = val / 100;
9455
- }
9456
- return formatter.format(val);
9435
+ ) });
9457
9436
  };
9458
- function getPromotionIds(items, shippingMethods) {
9459
- const promotionIds = /* @__PURE__ */ new Set();
9460
- for (const item of items) {
9461
- if (item.adjustments) {
9462
- for (const adjustment of item.adjustments) {
9463
- if (adjustment.promotion_id) {
9464
- promotionIds.add(adjustment.promotion_id);
9465
- }
9466
- }
9467
- }
9468
- }
9469
- for (const shippingMethod of shippingMethods) {
9470
- if (shippingMethod.adjustments) {
9471
- for (const adjustment of shippingMethod.adjustments) {
9472
- if (adjustment.promotion_id) {
9473
- promotionIds.add(adjustment.promotion_id);
9474
- }
9437
+ const SalesChannelField = ({ control, order }) => {
9438
+ const salesChannels = useComboboxData({
9439
+ queryFn: async (params) => {
9440
+ return await sdk.admin.salesChannel.list(params);
9441
+ },
9442
+ queryKey: ["sales-channels"],
9443
+ getOptions: (data) => {
9444
+ return data.sales_channels.map((salesChannel) => ({
9445
+ label: salesChannel.name,
9446
+ value: salesChannel.id
9447
+ }));
9448
+ },
9449
+ defaultValue: order.sales_channel_id || void 0
9450
+ });
9451
+ return /* @__PURE__ */ jsx(
9452
+ Form$2.Field,
9453
+ {
9454
+ control,
9455
+ name: "sales_channel_id",
9456
+ render: ({ field }) => {
9457
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9458
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
9459
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9460
+ Combobox,
9461
+ {
9462
+ options: salesChannels.options,
9463
+ fetchNextPage: salesChannels.fetchNextPage,
9464
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
9465
+ searchValue: salesChannels.searchValue,
9466
+ onSearchValueChange: salesChannels.onSearchValueChange,
9467
+ placeholder: "Select sales channel",
9468
+ ...field
9469
+ }
9470
+ ) }),
9471
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9472
+ ] });
9475
9473
  }
9476
9474
  }
9477
- }
9478
- return Array.from(promotionIds);
9479
- }
9475
+ );
9476
+ };
9477
+ const schema = z.object({
9478
+ sales_channel_id: z.string().min(1)
9479
+ });
9480
9480
  const widgetModule = { widgets: [] };
9481
9481
  const routeModule = {
9482
9482
  routes: [
@@ -9505,21 +9505,21 @@ const routeModule = {
9505
9505
  Component: CustomItems,
9506
9506
  path: "/draft-orders/:id/custom-items"
9507
9507
  },
9508
- {
9509
- Component: Items,
9510
- path: "/draft-orders/:id/items"
9511
- },
9512
9508
  {
9513
9509
  Component: Email,
9514
9510
  path: "/draft-orders/:id/email"
9515
9511
  },
9512
+ {
9513
+ Component: Items,
9514
+ path: "/draft-orders/:id/items"
9515
+ },
9516
9516
  {
9517
9517
  Component: Metadata,
9518
9518
  path: "/draft-orders/:id/metadata"
9519
9519
  },
9520
9520
  {
9521
- Component: SalesChannel,
9522
- path: "/draft-orders/:id/sales-channel"
9521
+ Component: Promotions,
9522
+ path: "/draft-orders/:id/promotions"
9523
9523
  },
9524
9524
  {
9525
9525
  Component: Shipping,
@@ -9534,8 +9534,8 @@ const routeModule = {
9534
9534
  path: "/draft-orders/:id/transfer-ownership"
9535
9535
  },
9536
9536
  {
9537
- Component: Promotions,
9538
- path: "/draft-orders/:id/promotions"
9537
+ Component: SalesChannel,
9538
+ path: "/draft-orders/:id/sales-channel"
9539
9539
  }
9540
9540
  ]
9541
9541
  }