@medusajs/draft-order 2.11.0-snapshot-20251020120939 → 2.11.0

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.
@@ -9565,6 +9565,27 @@ const ID = () => {
9565
9565
  /* @__PURE__ */ jsx(Outlet, {})
9566
9566
  ] });
9567
9567
  };
9568
+ const CustomItems = () => {
9569
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9570
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9571
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
9572
+ ] });
9573
+ };
9574
+ const CustomItemsForm = () => {
9575
+ const form = useForm({
9576
+ resolver: zodResolver(schema$5)
9577
+ });
9578
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9579
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9580
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9581
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9582
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9583
+ ] }) })
9584
+ ] }) });
9585
+ };
9586
+ const schema$5 = objectType({
9587
+ email: stringType().email()
9588
+ });
9568
9589
  const BillingAddress = () => {
9569
9590
  const { id } = useParams();
9570
9591
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9597,7 +9618,7 @@ const BillingAddressForm = ({ order }) => {
9597
9618
  postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9598
9619
  phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9599
9620
  },
9600
- resolver: zodResolver(schema$5)
9621
+ resolver: zodResolver(schema$4)
9601
9622
  });
9602
9623
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9603
9624
  const { handleSuccess } = useRouteModal();
@@ -9754,96 +9775,7 @@ const BillingAddressForm = ({ order }) => {
9754
9775
  }
9755
9776
  ) });
9756
9777
  };
9757
- const schema$5 = addressSchema;
9758
- const CustomItems = () => {
9759
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9760
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9761
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9762
- ] });
9763
- };
9764
- const CustomItemsForm = () => {
9765
- const form = useForm({
9766
- resolver: zodResolver(schema$4)
9767
- });
9768
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9769
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9770
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9771
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9772
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9773
- ] }) })
9774
- ] }) });
9775
- };
9776
- const schema$4 = objectType({
9777
- email: stringType().email()
9778
- });
9779
- const Email = () => {
9780
- const { id } = useParams();
9781
- const { order, isPending, isError, error } = useOrder(id, {
9782
- fields: "+email"
9783
- });
9784
- if (isError) {
9785
- throw error;
9786
- }
9787
- const isReady = !isPending && !!order;
9788
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9789
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9790
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
9791
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9792
- ] }),
9793
- isReady && /* @__PURE__ */ jsx(EmailForm, { order })
9794
- ] });
9795
- };
9796
- const EmailForm = ({ order }) => {
9797
- const form = useForm({
9798
- defaultValues: {
9799
- email: order.email ?? ""
9800
- },
9801
- resolver: zodResolver(schema$3)
9802
- });
9803
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9804
- const { handleSuccess } = useRouteModal();
9805
- const onSubmit = form.handleSubmit(async (data) => {
9806
- await mutateAsync(
9807
- { email: data.email },
9808
- {
9809
- onSuccess: () => {
9810
- handleSuccess();
9811
- },
9812
- onError: (error) => {
9813
- toast.error(error.message);
9814
- }
9815
- }
9816
- );
9817
- });
9818
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9819
- KeyboundForm,
9820
- {
9821
- className: "flex flex-1 flex-col overflow-hidden",
9822
- onSubmit,
9823
- children: [
9824
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
9825
- Form$2.Field,
9826
- {
9827
- control: form.control,
9828
- name: "email",
9829
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9830
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
9831
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9832
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9833
- ] })
9834
- }
9835
- ) }),
9836
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9837
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9838
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9839
- ] }) })
9840
- ]
9841
- }
9842
- ) });
9843
- };
9844
- const schema$3 = objectType({
9845
- email: stringType().email()
9846
- });
9778
+ const schema$4 = addressSchema;
9847
9779
  const NumberInput = forwardRef(
9848
9780
  ({
9849
9781
  value,
@@ -10818,78 +10750,252 @@ const customItemSchema = objectType({
10818
10750
  quantity: numberType(),
10819
10751
  unit_price: unionType([numberType(), stringType()])
10820
10752
  });
10821
- const PROMOTION_QUERY_KEY = "promotions";
10822
- const promotionsQueryKeys = {
10823
- list: (query2) => [
10824
- PROMOTION_QUERY_KEY,
10825
- query2 ? query2 : void 0
10826
- ],
10827
- detail: (id, query2) => [
10828
- PROMOTION_QUERY_KEY,
10829
- id,
10830
- query2 ? query2 : void 0
10831
- ]
10832
- };
10833
- const usePromotions = (query2, options) => {
10834
- const { data, ...rest } = useQuery({
10835
- queryKey: promotionsQueryKeys.list(query2),
10836
- queryFn: async () => sdk.admin.promotion.list(query2),
10837
- ...options
10838
- });
10839
- return { ...data, ...rest };
10840
- };
10841
- const Promotions = () => {
10753
+ const Email = () => {
10842
10754
  const { id } = useParams();
10843
- const {
10844
- order: preview,
10845
- isError: isPreviewError,
10846
- error: previewError
10847
- } = useOrderPreview(id, void 0);
10848
- useInitiateOrderEdit({ preview });
10849
- const { onCancel } = useCancelOrderEdit({ preview });
10850
- if (isPreviewError) {
10851
- throw previewError;
10755
+ const { order, isPending, isError, error } = useOrder(id, {
10756
+ fields: "+email"
10757
+ });
10758
+ if (isError) {
10759
+ throw error;
10852
10760
  }
10853
- const isReady = !!preview;
10854
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10855
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10856
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10761
+ const isReady = !isPending && !!order;
10762
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10763
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10764
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
10765
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
10766
+ ] }),
10767
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
10857
10768
  ] });
10858
10769
  };
10859
- const PromotionForm = ({ preview }) => {
10860
- const { items, shipping_methods } = preview;
10861
- const [isSubmitting, setIsSubmitting] = useState(false);
10862
- const [comboboxValue, setComboboxValue] = useState("");
10770
+ const EmailForm = ({ order }) => {
10771
+ const form = useForm({
10772
+ defaultValues: {
10773
+ email: order.email ?? ""
10774
+ },
10775
+ resolver: zodResolver(schema$3)
10776
+ });
10777
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10863
10778
  const { handleSuccess } = useRouteModal();
10864
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10865
- const promoIds = getPromotionIds(items, shipping_methods);
10866
- const { promotions, isPending, isError, error } = usePromotions(
10779
+ const onSubmit = form.handleSubmit(async (data) => {
10780
+ await mutateAsync(
10781
+ { email: data.email },
10782
+ {
10783
+ onSuccess: () => {
10784
+ handleSuccess();
10785
+ },
10786
+ onError: (error) => {
10787
+ toast.error(error.message);
10788
+ }
10789
+ }
10790
+ );
10791
+ });
10792
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10793
+ KeyboundForm,
10867
10794
  {
10868
- id: promoIds
10795
+ className: "flex flex-1 flex-col overflow-hidden",
10796
+ onSubmit,
10797
+ children: [
10798
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
10799
+ Form$2.Field,
10800
+ {
10801
+ control: form.control,
10802
+ name: "email",
10803
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
10804
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
10805
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10806
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10807
+ ] })
10808
+ }
10809
+ ) }),
10810
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10811
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10812
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10813
+ ] }) })
10814
+ ]
10815
+ }
10816
+ ) });
10817
+ };
10818
+ const schema$3 = objectType({
10819
+ email: stringType().email()
10820
+ });
10821
+ const SalesChannel = () => {
10822
+ const { id } = useParams();
10823
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10824
+ id,
10825
+ {
10826
+ fields: "+sales_channel_id"
10869
10827
  },
10870
10828
  {
10871
- enabled: !!promoIds.length
10829
+ enabled: !!id
10872
10830
  }
10873
10831
  );
10874
- const comboboxData = useComboboxData({
10875
- queryKey: ["promotions", "combobox", promoIds],
10876
- queryFn: async (params) => {
10877
- return await sdk.admin.promotion.list({
10878
- ...params,
10879
- id: {
10880
- $nin: promoIds
10881
- }
10882
- });
10832
+ if (isError) {
10833
+ throw error;
10834
+ }
10835
+ const ISrEADY = !!draft_order && !isPending;
10836
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10837
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10838
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
10839
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
10840
+ ] }),
10841
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
10842
+ ] });
10843
+ };
10844
+ const SalesChannelForm = ({ order }) => {
10845
+ const form = useForm({
10846
+ defaultValues: {
10847
+ sales_channel_id: order.sales_channel_id || ""
10883
10848
  },
10884
- getOptions: (data) => {
10885
- return data.promotions.map((promotion) => ({
10886
- label: promotion.code,
10887
- value: promotion.code
10888
- }));
10889
- }
10849
+ resolver: zodResolver(schema$2)
10890
10850
  });
10891
- const add = async (value) => {
10892
- if (!value) {
10851
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10852
+ const { handleSuccess } = useRouteModal();
10853
+ const onSubmit = form.handleSubmit(async (data) => {
10854
+ await mutateAsync(
10855
+ {
10856
+ sales_channel_id: data.sales_channel_id
10857
+ },
10858
+ {
10859
+ onSuccess: () => {
10860
+ toast.success("Sales channel updated");
10861
+ handleSuccess();
10862
+ },
10863
+ onError: (error) => {
10864
+ toast.error(error.message);
10865
+ }
10866
+ }
10867
+ );
10868
+ });
10869
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10870
+ KeyboundForm,
10871
+ {
10872
+ className: "flex flex-1 flex-col overflow-hidden",
10873
+ onSubmit,
10874
+ children: [
10875
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
10876
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10877
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10878
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10879
+ ] }) })
10880
+ ]
10881
+ }
10882
+ ) });
10883
+ };
10884
+ const SalesChannelField = ({ control, order }) => {
10885
+ const salesChannels = useComboboxData({
10886
+ queryFn: async (params) => {
10887
+ return await sdk.admin.salesChannel.list(params);
10888
+ },
10889
+ queryKey: ["sales-channels"],
10890
+ getOptions: (data) => {
10891
+ return data.sales_channels.map((salesChannel) => ({
10892
+ label: salesChannel.name,
10893
+ value: salesChannel.id
10894
+ }));
10895
+ },
10896
+ defaultValue: order.sales_channel_id || void 0
10897
+ });
10898
+ return /* @__PURE__ */ jsx(
10899
+ Form$2.Field,
10900
+ {
10901
+ control,
10902
+ name: "sales_channel_id",
10903
+ render: ({ field }) => {
10904
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
10905
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
10906
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10907
+ Combobox,
10908
+ {
10909
+ options: salesChannels.options,
10910
+ fetchNextPage: salesChannels.fetchNextPage,
10911
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
10912
+ searchValue: salesChannels.searchValue,
10913
+ onSearchValueChange: salesChannels.onSearchValueChange,
10914
+ placeholder: "Select sales channel",
10915
+ ...field
10916
+ }
10917
+ ) }),
10918
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10919
+ ] });
10920
+ }
10921
+ }
10922
+ );
10923
+ };
10924
+ const schema$2 = objectType({
10925
+ sales_channel_id: stringType().min(1)
10926
+ });
10927
+ const PROMOTION_QUERY_KEY = "promotions";
10928
+ const promotionsQueryKeys = {
10929
+ list: (query2) => [
10930
+ PROMOTION_QUERY_KEY,
10931
+ query2 ? query2 : void 0
10932
+ ],
10933
+ detail: (id, query2) => [
10934
+ PROMOTION_QUERY_KEY,
10935
+ id,
10936
+ query2 ? query2 : void 0
10937
+ ]
10938
+ };
10939
+ const usePromotions = (query2, options) => {
10940
+ const { data, ...rest } = useQuery({
10941
+ queryKey: promotionsQueryKeys.list(query2),
10942
+ queryFn: async () => sdk.admin.promotion.list(query2),
10943
+ ...options
10944
+ });
10945
+ return { ...data, ...rest };
10946
+ };
10947
+ const Promotions = () => {
10948
+ const { id } = useParams();
10949
+ const {
10950
+ order: preview,
10951
+ isError: isPreviewError,
10952
+ error: previewError
10953
+ } = useOrderPreview(id, void 0);
10954
+ useInitiateOrderEdit({ preview });
10955
+ const { onCancel } = useCancelOrderEdit({ preview });
10956
+ if (isPreviewError) {
10957
+ throw previewError;
10958
+ }
10959
+ const isReady = !!preview;
10960
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10961
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10962
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10963
+ ] });
10964
+ };
10965
+ const PromotionForm = ({ preview }) => {
10966
+ const { items, shipping_methods } = preview;
10967
+ const [isSubmitting, setIsSubmitting] = useState(false);
10968
+ const [comboboxValue, setComboboxValue] = useState("");
10969
+ const { handleSuccess } = useRouteModal();
10970
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10971
+ const promoIds = getPromotionIds(items, shipping_methods);
10972
+ const { promotions, isPending, isError, error } = usePromotions(
10973
+ {
10974
+ id: promoIds
10975
+ },
10976
+ {
10977
+ enabled: !!promoIds.length
10978
+ }
10979
+ );
10980
+ const comboboxData = useComboboxData({
10981
+ queryKey: ["promotions", "combobox", promoIds],
10982
+ queryFn: async (params) => {
10983
+ return await sdk.admin.promotion.list({
10984
+ ...params,
10985
+ id: {
10986
+ $nin: promoIds
10987
+ }
10988
+ });
10989
+ },
10990
+ getOptions: (data) => {
10991
+ return data.promotions.map((promotion) => ({
10992
+ label: promotion.code,
10993
+ value: promotion.code
10994
+ }));
10995
+ }
10996
+ });
10997
+ const add = async (value) => {
10998
+ if (!value) {
10893
10999
  return;
10894
11000
  }
10895
11001
  addPromotions(
@@ -11095,507 +11201,51 @@ function getPromotionIds(items, shippingMethods) {
11095
11201
  }
11096
11202
  return Array.from(promotionIds);
11097
11203
  }
11098
- const InlineTip = forwardRef(
11099
- ({ variant = "tip", label, className, children, ...props }, ref) => {
11100
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
11101
- return /* @__PURE__ */ jsxs(
11102
- "div",
11103
- {
11104
- ref,
11105
- className: clx(
11106
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
11107
- className
11108
- ),
11109
- ...props,
11110
- children: [
11111
- /* @__PURE__ */ jsx(
11112
- "div",
11113
- {
11114
- role: "presentation",
11115
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
11116
- "bg-ui-tag-orange-icon": variant === "warning"
11117
- })
11118
- }
11119
- ),
11120
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
11121
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
11122
- labelValue,
11123
- ":"
11124
- ] }),
11125
- " ",
11126
- children
11127
- ] })
11128
- ]
11129
- }
11130
- );
11131
- }
11132
- );
11133
- InlineTip.displayName = "InlineTip";
11134
- const MetadataFieldSchema = objectType({
11135
- key: stringType(),
11136
- disabled: booleanType().optional(),
11137
- value: anyType()
11138
- });
11139
- const MetadataSchema = objectType({
11140
- metadata: arrayType(MetadataFieldSchema)
11141
- });
11142
- const Metadata = () => {
11204
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
11205
+ const Shipping = () => {
11206
+ var _a;
11143
11207
  const { id } = useParams();
11144
11208
  const { order, isPending, isError, error } = useOrder(id, {
11145
- fields: "metadata"
11209
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11146
11210
  });
11211
+ const {
11212
+ order: preview,
11213
+ isPending: isPreviewPending,
11214
+ isError: isPreviewError,
11215
+ error: previewError
11216
+ } = useOrderPreview(id);
11217
+ useInitiateOrderEdit({ preview });
11218
+ const { onCancel } = useCancelOrderEdit({ preview });
11147
11219
  if (isError) {
11148
11220
  throw error;
11149
11221
  }
11150
- const isReady = !isPending && !!order;
11151
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11152
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11153
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
11154
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
11155
- ] }),
11156
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
11157
- ] });
11222
+ if (isPreviewError) {
11223
+ throw previewError;
11224
+ }
11225
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11226
+ const isReady = preview && !isPreviewPending && order && !isPending;
11227
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11228
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11229
+ /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11230
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11231
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11232
+ ] }) }) }),
11233
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11234
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11235
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11236
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11237
+ ] }) });
11158
11238
  };
11159
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
11160
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
11161
- const MetadataForm = ({ orderId, metadata }) => {
11162
- const { handleSuccess } = useRouteModal();
11163
- const hasUneditableRows = getHasUneditableRows(metadata);
11164
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
11165
- const form = useForm({
11166
- defaultValues: {
11167
- metadata: getDefaultValues(metadata)
11168
- },
11169
- resolver: zodResolver(MetadataSchema)
11170
- });
11171
- const handleSubmit = form.handleSubmit(async (data) => {
11172
- const parsedData = parseValues(data);
11173
- await mutateAsync(
11174
- {
11175
- metadata: parsedData
11176
- },
11177
- {
11178
- onSuccess: () => {
11179
- toast.success("Metadata updated");
11180
- handleSuccess();
11181
- },
11182
- onError: (error) => {
11183
- toast.error(error.message);
11184
- }
11185
- }
11186
- );
11187
- });
11188
- const { fields, insert, remove } = useFieldArray({
11189
- control: form.control,
11190
- name: "metadata"
11191
- });
11192
- function deleteRow(index) {
11193
- remove(index);
11194
- if (fields.length === 1) {
11195
- insert(0, {
11196
- key: "",
11197
- value: "",
11198
- disabled: false
11199
- });
11200
- }
11201
- }
11202
- function insertRow(index, position) {
11203
- insert(index + (position === "above" ? 0 : 1), {
11204
- key: "",
11205
- value: "",
11206
- disabled: false
11207
- });
11208
- }
11209
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11210
- KeyboundForm,
11211
- {
11212
- onSubmit: handleSubmit,
11213
- className: "flex flex-1 flex-col overflow-hidden",
11214
- children: [
11215
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
11216
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
11217
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
11218
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
11219
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
11220
- ] }),
11221
- fields.map((field, index) => {
11222
- const isDisabled = field.disabled || false;
11223
- let placeholder = "-";
11224
- if (typeof field.value === "object") {
11225
- placeholder = "{ ... }";
11226
- }
11227
- if (Array.isArray(field.value)) {
11228
- placeholder = "[ ... ]";
11229
- }
11230
- return /* @__PURE__ */ jsx(
11231
- ConditionalTooltip,
11232
- {
11233
- showTooltip: isDisabled,
11234
- content: "This row is disabled because it contains non-primitive data.",
11235
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
11236
- /* @__PURE__ */ jsxs(
11237
- "div",
11238
- {
11239
- className: clx("grid grid-cols-2 divide-x", {
11240
- "overflow-hidden rounded-b-lg": index === fields.length - 1
11241
- }),
11242
- children: [
11243
- /* @__PURE__ */ jsx(
11244
- Form$2.Field,
11245
- {
11246
- control: form.control,
11247
- name: `metadata.${index}.key`,
11248
- render: ({ field: field2 }) => {
11249
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11250
- GridInput,
11251
- {
11252
- "aria-labelledby": METADATA_KEY_LABEL_ID,
11253
- ...field2,
11254
- disabled: isDisabled,
11255
- placeholder: "Key"
11256
- }
11257
- ) }) });
11258
- }
11259
- }
11260
- ),
11261
- /* @__PURE__ */ jsx(
11262
- Form$2.Field,
11263
- {
11264
- control: form.control,
11265
- name: `metadata.${index}.value`,
11266
- render: ({ field: { value, ...field2 } }) => {
11267
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11268
- GridInput,
11269
- {
11270
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
11271
- ...field2,
11272
- value: isDisabled ? placeholder : value,
11273
- disabled: isDisabled,
11274
- placeholder: "Value"
11275
- }
11276
- ) }) });
11277
- }
11278
- }
11279
- )
11280
- ]
11281
- }
11282
- ),
11283
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
11284
- /* @__PURE__ */ jsx(
11285
- DropdownMenu.Trigger,
11286
- {
11287
- className: clx(
11288
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11289
- {
11290
- hidden: isDisabled
11291
- }
11292
- ),
11293
- disabled: isDisabled,
11294
- asChild: true,
11295
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
11296
- }
11297
- ),
11298
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
11299
- /* @__PURE__ */ jsxs(
11300
- DropdownMenu.Item,
11301
- {
11302
- className: "gap-x-2",
11303
- onClick: () => insertRow(index, "above"),
11304
- children: [
11305
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
11306
- "Insert row above"
11307
- ]
11308
- }
11309
- ),
11310
- /* @__PURE__ */ jsxs(
11311
- DropdownMenu.Item,
11312
- {
11313
- className: "gap-x-2",
11314
- onClick: () => insertRow(index, "below"),
11315
- children: [
11316
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
11317
- "Insert row below"
11318
- ]
11319
- }
11320
- ),
11321
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
11322
- /* @__PURE__ */ jsxs(
11323
- DropdownMenu.Item,
11324
- {
11325
- className: "gap-x-2",
11326
- onClick: () => deleteRow(index),
11327
- children: [
11328
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
11329
- "Delete row"
11330
- ]
11331
- }
11332
- )
11333
- ] })
11334
- ] })
11335
- ] })
11336
- },
11337
- field.id
11338
- );
11339
- })
11340
- ] }),
11341
- hasUneditableRows && /* @__PURE__ */ jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
11342
- ] }),
11343
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11344
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11345
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11346
- ] }) })
11347
- ]
11348
- }
11349
- ) });
11350
- };
11351
- const GridInput = forwardRef(({ className, ...props }, ref) => {
11352
- return /* @__PURE__ */ jsx(
11353
- "input",
11354
- {
11355
- ref,
11356
- ...props,
11357
- autoComplete: "off",
11358
- className: clx(
11359
- "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
11360
- className
11361
- )
11362
- }
11363
- );
11364
- });
11365
- GridInput.displayName = "MetadataForm.GridInput";
11366
- const PlaceholderInner = () => {
11367
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11368
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11369
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11370
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11371
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11372
- ] }) })
11373
- ] });
11374
- };
11375
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11376
- function getDefaultValues(metadata) {
11377
- if (!metadata || !Object.keys(metadata).length) {
11378
- return [
11379
- {
11380
- key: "",
11381
- value: "",
11382
- disabled: false
11383
- }
11384
- ];
11385
- }
11386
- return Object.entries(metadata).map(([key, value]) => {
11387
- if (!EDITABLE_TYPES.includes(typeof value)) {
11388
- return {
11389
- key,
11390
- value,
11391
- disabled: true
11392
- };
11393
- }
11394
- let stringValue = value;
11395
- if (typeof value !== "string") {
11396
- stringValue = JSON.stringify(value);
11397
- }
11398
- return {
11399
- key,
11400
- value: stringValue,
11401
- original_key: key
11402
- };
11403
- });
11404
- }
11405
- function parseValues(values) {
11406
- const metadata = values.metadata;
11407
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11408
- if (isEmpty) {
11409
- return null;
11410
- }
11411
- const update = {};
11412
- metadata.forEach((field) => {
11413
- let key = field.key;
11414
- let value = field.value;
11415
- const disabled = field.disabled;
11416
- if (!key || !value) {
11417
- return;
11418
- }
11419
- if (disabled) {
11420
- update[key] = value;
11421
- return;
11422
- }
11423
- key = key.trim();
11424
- value = value.trim();
11425
- if (value === "true") {
11426
- update[key] = true;
11427
- } else if (value === "false") {
11428
- update[key] = false;
11429
- } else {
11430
- const parsedNumber = parseFloat(value);
11431
- if (!isNaN(parsedNumber)) {
11432
- update[key] = parsedNumber;
11433
- } else {
11434
- update[key] = value;
11435
- }
11436
- }
11437
- });
11438
- return update;
11439
- }
11440
- function getHasUneditableRows(metadata) {
11441
- if (!metadata) {
11442
- return false;
11443
- }
11444
- return Object.values(metadata).some(
11445
- (value) => !EDITABLE_TYPES.includes(typeof value)
11446
- );
11447
- }
11448
- const SalesChannel = () => {
11449
- const { id } = useParams();
11450
- const { draft_order, isPending, isError, error } = useDraftOrder(
11451
- id,
11452
- {
11453
- fields: "+sales_channel_id"
11454
- },
11455
- {
11456
- enabled: !!id
11457
- }
11458
- );
11459
- if (isError) {
11460
- throw error;
11461
- }
11462
- const ISrEADY = !!draft_order && !isPending;
11463
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11464
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11465
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11466
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11467
- ] }),
11468
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11469
- ] });
11470
- };
11471
- const SalesChannelForm = ({ order }) => {
11472
- const form = useForm({
11473
- defaultValues: {
11474
- sales_channel_id: order.sales_channel_id || ""
11475
- },
11476
- resolver: zodResolver(schema$2)
11477
- });
11478
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11479
- const { handleSuccess } = useRouteModal();
11480
- const onSubmit = form.handleSubmit(async (data) => {
11481
- await mutateAsync(
11482
- {
11483
- sales_channel_id: data.sales_channel_id
11484
- },
11485
- {
11486
- onSuccess: () => {
11487
- toast.success("Sales channel updated");
11488
- handleSuccess();
11489
- },
11490
- onError: (error) => {
11491
- toast.error(error.message);
11492
- }
11493
- }
11494
- );
11495
- });
11496
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11497
- KeyboundForm,
11498
- {
11499
- className: "flex flex-1 flex-col overflow-hidden",
11500
- onSubmit,
11501
- children: [
11502
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11503
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11504
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11505
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11506
- ] }) })
11507
- ]
11508
- }
11509
- ) });
11510
- };
11511
- const SalesChannelField = ({ control, order }) => {
11512
- const salesChannels = useComboboxData({
11513
- queryFn: async (params) => {
11514
- return await sdk.admin.salesChannel.list(params);
11515
- },
11516
- queryKey: ["sales-channels"],
11517
- getOptions: (data) => {
11518
- return data.sales_channels.map((salesChannel) => ({
11519
- label: salesChannel.name,
11520
- value: salesChannel.id
11521
- }));
11522
- },
11523
- defaultValue: order.sales_channel_id || void 0
11524
- });
11525
- return /* @__PURE__ */ jsx(
11526
- Form$2.Field,
11527
- {
11528
- control,
11529
- name: "sales_channel_id",
11530
- render: ({ field }) => {
11531
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11532
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11533
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11534
- Combobox,
11535
- {
11536
- options: salesChannels.options,
11537
- fetchNextPage: salesChannels.fetchNextPage,
11538
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11539
- searchValue: salesChannels.searchValue,
11540
- onSearchValueChange: salesChannels.onSearchValueChange,
11541
- placeholder: "Select sales channel",
11542
- ...field
11543
- }
11544
- ) }),
11545
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11546
- ] });
11547
- }
11548
- }
11549
- );
11550
- };
11551
- const schema$2 = objectType({
11552
- sales_channel_id: stringType().min(1)
11553
- });
11554
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11555
- const Shipping = () => {
11556
- var _a;
11557
- const { id } = useParams();
11558
- const { order, isPending, isError, error } = useOrder(id, {
11559
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11560
- });
11561
- const {
11562
- order: preview,
11563
- isPending: isPreviewPending,
11564
- isError: isPreviewError,
11565
- error: previewError
11566
- } = useOrderPreview(id);
11567
- useInitiateOrderEdit({ preview });
11568
- const { onCancel } = useCancelOrderEdit({ preview });
11569
- if (isError) {
11570
- throw error;
11571
- }
11572
- if (isPreviewError) {
11573
- throw previewError;
11574
- }
11575
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11576
- const isReady = preview && !isPreviewPending && order && !isPending;
11577
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11578
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11579
- /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11580
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11581
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11582
- ] }) }) }),
11583
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11584
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11585
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11586
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11587
- ] }) });
11588
- };
11589
- const ShippingForm = ({ preview, order }) => {
11590
- var _a;
11591
- const { setIsOpen } = useStackedModal();
11592
- const [isSubmitting, setIsSubmitting] = useState(false);
11593
- const [data, setData] = useState(null);
11594
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11595
- const { shipping_options } = useShippingOptions(
11596
- {
11597
- id: appliedShippingOptionIds,
11598
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11239
+ const ShippingForm = ({ preview, order }) => {
11240
+ var _a;
11241
+ const { setIsOpen } = useStackedModal();
11242
+ const [isSubmitting, setIsSubmitting] = useState(false);
11243
+ const [data, setData] = useState(null);
11244
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11245
+ const { shipping_options } = useShippingOptions(
11246
+ {
11247
+ id: appliedShippingOptionIds,
11248
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11599
11249
  },
11600
11250
  {
11601
11251
  enabled: appliedShippingOptionIds.length > 0
@@ -13032,11 +12682,361 @@ const Illustration = () => {
13032
12682
  ] })
13033
12683
  ]
13034
12684
  }
13035
- );
12685
+ );
12686
+ };
12687
+ const schema = objectType({
12688
+ customer_id: stringType().min(1)
12689
+ });
12690
+ const InlineTip = forwardRef(
12691
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
12692
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
12693
+ return /* @__PURE__ */ jsxs(
12694
+ "div",
12695
+ {
12696
+ ref,
12697
+ className: clx(
12698
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
12699
+ className
12700
+ ),
12701
+ ...props,
12702
+ children: [
12703
+ /* @__PURE__ */ jsx(
12704
+ "div",
12705
+ {
12706
+ role: "presentation",
12707
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
12708
+ "bg-ui-tag-orange-icon": variant === "warning"
12709
+ })
12710
+ }
12711
+ ),
12712
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
12713
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
12714
+ labelValue,
12715
+ ":"
12716
+ ] }),
12717
+ " ",
12718
+ children
12719
+ ] })
12720
+ ]
12721
+ }
12722
+ );
12723
+ }
12724
+ );
12725
+ InlineTip.displayName = "InlineTip";
12726
+ const MetadataFieldSchema = objectType({
12727
+ key: stringType(),
12728
+ disabled: booleanType().optional(),
12729
+ value: anyType()
12730
+ });
12731
+ const MetadataSchema = objectType({
12732
+ metadata: arrayType(MetadataFieldSchema)
12733
+ });
12734
+ const Metadata = () => {
12735
+ const { id } = useParams();
12736
+ const { order, isPending, isError, error } = useOrder(id, {
12737
+ fields: "metadata"
12738
+ });
12739
+ if (isError) {
12740
+ throw error;
12741
+ }
12742
+ const isReady = !isPending && !!order;
12743
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12744
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12745
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
12746
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
12747
+ ] }),
12748
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
12749
+ ] });
12750
+ };
12751
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
12752
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
12753
+ const MetadataForm = ({ orderId, metadata }) => {
12754
+ const { handleSuccess } = useRouteModal();
12755
+ const hasUneditableRows = getHasUneditableRows(metadata);
12756
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
12757
+ const form = useForm({
12758
+ defaultValues: {
12759
+ metadata: getDefaultValues(metadata)
12760
+ },
12761
+ resolver: zodResolver(MetadataSchema)
12762
+ });
12763
+ const handleSubmit = form.handleSubmit(async (data) => {
12764
+ const parsedData = parseValues(data);
12765
+ await mutateAsync(
12766
+ {
12767
+ metadata: parsedData
12768
+ },
12769
+ {
12770
+ onSuccess: () => {
12771
+ toast.success("Metadata updated");
12772
+ handleSuccess();
12773
+ },
12774
+ onError: (error) => {
12775
+ toast.error(error.message);
12776
+ }
12777
+ }
12778
+ );
12779
+ });
12780
+ const { fields, insert, remove } = useFieldArray({
12781
+ control: form.control,
12782
+ name: "metadata"
12783
+ });
12784
+ function deleteRow(index) {
12785
+ remove(index);
12786
+ if (fields.length === 1) {
12787
+ insert(0, {
12788
+ key: "",
12789
+ value: "",
12790
+ disabled: false
12791
+ });
12792
+ }
12793
+ }
12794
+ function insertRow(index, position) {
12795
+ insert(index + (position === "above" ? 0 : 1), {
12796
+ key: "",
12797
+ value: "",
12798
+ disabled: false
12799
+ });
12800
+ }
12801
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12802
+ KeyboundForm,
12803
+ {
12804
+ onSubmit: handleSubmit,
12805
+ className: "flex flex-1 flex-col overflow-hidden",
12806
+ children: [
12807
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
12808
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
12809
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
12810
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
12811
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
12812
+ ] }),
12813
+ fields.map((field, index) => {
12814
+ const isDisabled = field.disabled || false;
12815
+ let placeholder = "-";
12816
+ if (typeof field.value === "object") {
12817
+ placeholder = "{ ... }";
12818
+ }
12819
+ if (Array.isArray(field.value)) {
12820
+ placeholder = "[ ... ]";
12821
+ }
12822
+ return /* @__PURE__ */ jsx(
12823
+ ConditionalTooltip,
12824
+ {
12825
+ showTooltip: isDisabled,
12826
+ content: "This row is disabled because it contains non-primitive data.",
12827
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
12828
+ /* @__PURE__ */ jsxs(
12829
+ "div",
12830
+ {
12831
+ className: clx("grid grid-cols-2 divide-x", {
12832
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
12833
+ }),
12834
+ children: [
12835
+ /* @__PURE__ */ jsx(
12836
+ Form$2.Field,
12837
+ {
12838
+ control: form.control,
12839
+ name: `metadata.${index}.key`,
12840
+ render: ({ field: field2 }) => {
12841
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12842
+ GridInput,
12843
+ {
12844
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
12845
+ ...field2,
12846
+ disabled: isDisabled,
12847
+ placeholder: "Key"
12848
+ }
12849
+ ) }) });
12850
+ }
12851
+ }
12852
+ ),
12853
+ /* @__PURE__ */ jsx(
12854
+ Form$2.Field,
12855
+ {
12856
+ control: form.control,
12857
+ name: `metadata.${index}.value`,
12858
+ render: ({ field: { value, ...field2 } }) => {
12859
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12860
+ GridInput,
12861
+ {
12862
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
12863
+ ...field2,
12864
+ value: isDisabled ? placeholder : value,
12865
+ disabled: isDisabled,
12866
+ placeholder: "Value"
12867
+ }
12868
+ ) }) });
12869
+ }
12870
+ }
12871
+ )
12872
+ ]
12873
+ }
12874
+ ),
12875
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
12876
+ /* @__PURE__ */ jsx(
12877
+ DropdownMenu.Trigger,
12878
+ {
12879
+ className: clx(
12880
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
12881
+ {
12882
+ hidden: isDisabled
12883
+ }
12884
+ ),
12885
+ disabled: isDisabled,
12886
+ asChild: true,
12887
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
12888
+ }
12889
+ ),
12890
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
12891
+ /* @__PURE__ */ jsxs(
12892
+ DropdownMenu.Item,
12893
+ {
12894
+ className: "gap-x-2",
12895
+ onClick: () => insertRow(index, "above"),
12896
+ children: [
12897
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
12898
+ "Insert row above"
12899
+ ]
12900
+ }
12901
+ ),
12902
+ /* @__PURE__ */ jsxs(
12903
+ DropdownMenu.Item,
12904
+ {
12905
+ className: "gap-x-2",
12906
+ onClick: () => insertRow(index, "below"),
12907
+ children: [
12908
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
12909
+ "Insert row below"
12910
+ ]
12911
+ }
12912
+ ),
12913
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
12914
+ /* @__PURE__ */ jsxs(
12915
+ DropdownMenu.Item,
12916
+ {
12917
+ className: "gap-x-2",
12918
+ onClick: () => deleteRow(index),
12919
+ children: [
12920
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
12921
+ "Delete row"
12922
+ ]
12923
+ }
12924
+ )
12925
+ ] })
12926
+ ] })
12927
+ ] })
12928
+ },
12929
+ field.id
12930
+ );
12931
+ })
12932
+ ] }),
12933
+ hasUneditableRows && /* @__PURE__ */ jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
12934
+ ] }),
12935
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12936
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12937
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12938
+ ] }) })
12939
+ ]
12940
+ }
12941
+ ) });
13036
12942
  };
13037
- const schema = objectType({
13038
- customer_id: stringType().min(1)
12943
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
12944
+ return /* @__PURE__ */ jsx(
12945
+ "input",
12946
+ {
12947
+ ref,
12948
+ ...props,
12949
+ autoComplete: "off",
12950
+ className: clx(
12951
+ "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
12952
+ className
12953
+ )
12954
+ }
12955
+ );
13039
12956
  });
12957
+ GridInput.displayName = "MetadataForm.GridInput";
12958
+ const PlaceholderInner = () => {
12959
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
12960
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
12961
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12962
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
12963
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
12964
+ ] }) })
12965
+ ] });
12966
+ };
12967
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
12968
+ function getDefaultValues(metadata) {
12969
+ if (!metadata || !Object.keys(metadata).length) {
12970
+ return [
12971
+ {
12972
+ key: "",
12973
+ value: "",
12974
+ disabled: false
12975
+ }
12976
+ ];
12977
+ }
12978
+ return Object.entries(metadata).map(([key, value]) => {
12979
+ if (!EDITABLE_TYPES.includes(typeof value)) {
12980
+ return {
12981
+ key,
12982
+ value,
12983
+ disabled: true
12984
+ };
12985
+ }
12986
+ let stringValue = value;
12987
+ if (typeof value !== "string") {
12988
+ stringValue = JSON.stringify(value);
12989
+ }
12990
+ return {
12991
+ key,
12992
+ value: stringValue,
12993
+ original_key: key
12994
+ };
12995
+ });
12996
+ }
12997
+ function parseValues(values) {
12998
+ const metadata = values.metadata;
12999
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
13000
+ if (isEmpty) {
13001
+ return null;
13002
+ }
13003
+ const update = {};
13004
+ metadata.forEach((field) => {
13005
+ let key = field.key;
13006
+ let value = field.value;
13007
+ const disabled = field.disabled;
13008
+ if (!key || !value) {
13009
+ return;
13010
+ }
13011
+ if (disabled) {
13012
+ update[key] = value;
13013
+ return;
13014
+ }
13015
+ key = key.trim();
13016
+ value = value.trim();
13017
+ if (value === "true") {
13018
+ update[key] = true;
13019
+ } else if (value === "false") {
13020
+ update[key] = false;
13021
+ } else {
13022
+ const parsedNumber = parseFloat(value);
13023
+ if (!isNaN(parsedNumber)) {
13024
+ update[key] = parsedNumber;
13025
+ } else {
13026
+ update[key] = value;
13027
+ }
13028
+ }
13029
+ });
13030
+ return update;
13031
+ }
13032
+ function getHasUneditableRows(metadata) {
13033
+ if (!metadata) {
13034
+ return false;
13035
+ }
13036
+ return Object.values(metadata).some(
13037
+ (value) => !EDITABLE_TYPES.includes(typeof value)
13038
+ );
13039
+ }
13040
13040
  const widgetModule = { widgets: [] };
13041
13041
  const routeModule = {
13042
13042
  routes: [
@@ -13057,34 +13057,30 @@ const routeModule = {
13057
13057
  handle,
13058
13058
  loader,
13059
13059
  children: [
13060
- {
13061
- Component: BillingAddress,
13062
- path: "/draft-orders/:id/billing-address"
13063
- },
13064
13060
  {
13065
13061
  Component: CustomItems,
13066
13062
  path: "/draft-orders/:id/custom-items"
13067
13063
  },
13068
13064
  {
13069
- Component: Email,
13070
- path: "/draft-orders/:id/email"
13065
+ Component: BillingAddress,
13066
+ path: "/draft-orders/:id/billing-address"
13071
13067
  },
13072
13068
  {
13073
13069
  Component: Items,
13074
13070
  path: "/draft-orders/:id/items"
13075
13071
  },
13076
13072
  {
13077
- Component: Promotions,
13078
- path: "/draft-orders/:id/promotions"
13079
- },
13080
- {
13081
- Component: Metadata,
13082
- path: "/draft-orders/:id/metadata"
13073
+ Component: Email,
13074
+ path: "/draft-orders/:id/email"
13083
13075
  },
13084
13076
  {
13085
13077
  Component: SalesChannel,
13086
13078
  path: "/draft-orders/:id/sales-channel"
13087
13079
  },
13080
+ {
13081
+ Component: Promotions,
13082
+ path: "/draft-orders/:id/promotions"
13083
+ },
13088
13084
  {
13089
13085
  Component: Shipping,
13090
13086
  path: "/draft-orders/:id/shipping"
@@ -13096,6 +13092,10 @@ const routeModule = {
13096
13092
  {
13097
13093
  Component: TransferOwnership,
13098
13094
  path: "/draft-orders/:id/transfer-ownership"
13095
+ },
13096
+ {
13097
+ Component: Metadata,
13098
+ path: "/draft-orders/:id/metadata"
13099
13099
  }
13100
13100
  ]
13101
13101
  }