@medusajs/draft-order 2.12.0-preview-20251103180145 → 2.12.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.
@@ -9,7 +9,7 @@ import Medusa from "@medusajs/js-sdk";
9
9
  import { format, formatDistance, sub, subDays, subMonths } from "date-fns";
10
10
  import { enUS } from "date-fns/locale";
11
11
  import { zodResolver } from "@hookform/resolvers/zod";
12
- import { FormProvider, Controller, useFormContext, useFormState, useForm, useWatch, useFieldArray } from "react-hook-form";
12
+ import { FormProvider, useFormContext, useFormState, Controller, useForm, useWatch, useFieldArray } from "react-hook-form";
13
13
  import { Slot, Collapsible, Accordion } from "radix-ui";
14
14
  import { ComboboxProvider, Combobox as Combobox$1, ComboboxDisclosure, ComboboxPopover, ComboboxItem, ComboboxItemCheck, ComboboxItemValue, Separator } from "@ariakit/react";
15
15
  import { matchSorter } from "match-sorter";
@@ -345,10 +345,14 @@ const DataTableAction = ({
345
345
  }
346
346
  return /* @__PURE__ */ jsx(Button, { ...buttonProps, onClick: props.onClick, children: label });
347
347
  };
348
+ const backendUrl = __BACKEND_URL__ ?? "/";
349
+ const authType = __AUTH_TYPE__ ?? "session";
350
+ const jwtTokenStorageKey = __JWT_TOKEN_STORAGE_KEY__ || void 0;
348
351
  const sdk = new Medusa({
349
- baseUrl: __BACKEND_URL__ || "/",
352
+ baseUrl: backendUrl,
350
353
  auth: {
351
- type: "session"
354
+ type: authType,
355
+ jwtTokenStorageKey
352
356
  }
353
357
  });
354
358
  const CUSTOMER_QUERY_KEY = "customers";
@@ -4647,9 +4651,6 @@ ZodReadonly.create = (type, params) => {
4647
4651
  ...processCreateParams(params)
4648
4652
  });
4649
4653
  };
4650
- ({
4651
- object: ZodObject.lazycreate
4652
- });
4653
4654
  var ZodFirstPartyTypeKind;
4654
4655
  (function(ZodFirstPartyTypeKind2) {
4655
4656
  ZodFirstPartyTypeKind2["ZodString"] = "ZodString";
@@ -4696,7 +4697,6 @@ const anyType = ZodAny.create;
4696
4697
  ZodNever.create;
4697
4698
  const arrayType = ZodArray.create;
4698
4699
  const objectType = ZodObject.create;
4699
- ZodObject.strictCreate;
4700
4700
  const unionType = ZodUnion.create;
4701
4701
  ZodIntersection.create;
4702
4702
  ZodTuple.create;
@@ -8722,7 +8722,7 @@ const Contact = ({ order }) => {
8722
8722
  children: phone
8723
8723
  }
8724
8724
  ),
8725
- /* @__PURE__ */ jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsx(Copy, { content: email, className: "text-ui-fg-muted" }) })
8725
+ /* @__PURE__ */ jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsx(Copy, { content: phone, className: "text-ui-fg-muted" }) })
8726
8726
  ] })
8727
8727
  ] })
8728
8728
  ] });
@@ -9755,95 +9755,6 @@ const BillingAddressForm = ({ order }) => {
9755
9755
  ) });
9756
9756
  };
9757
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
- });
9847
9758
  const NumberInput = forwardRef(
9848
9759
  ({
9849
9760
  value,
@@ -10818,536 +10729,186 @@ const customItemSchema = objectType({
10818
10729
  quantity: numberType(),
10819
10730
  unit_price: unionType([numberType(), stringType()])
10820
10731
  });
10821
- const InlineTip = forwardRef(
10822
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10823
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10824
- return /* @__PURE__ */ jsxs(
10825
- "div",
10826
- {
10827
- ref,
10828
- className: clx(
10829
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10830
- className
10831
- ),
10832
- ...props,
10833
- children: [
10834
- /* @__PURE__ */ jsx(
10835
- "div",
10836
- {
10837
- role: "presentation",
10838
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10839
- "bg-ui-tag-orange-icon": variant === "warning"
10840
- })
10841
- }
10842
- ),
10843
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
10844
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10845
- labelValue,
10846
- ":"
10847
- ] }),
10848
- " ",
10849
- children
10850
- ] })
10851
- ]
10852
- }
10853
- );
10854
- }
10855
- );
10856
- InlineTip.displayName = "InlineTip";
10857
- const MetadataFieldSchema = objectType({
10858
- key: stringType(),
10859
- disabled: booleanType().optional(),
10860
- value: anyType()
10861
- });
10862
- const MetadataSchema = objectType({
10863
- metadata: arrayType(MetadataFieldSchema)
10864
- });
10865
- const Metadata = () => {
10866
- const { id } = useParams();
10867
- const { order, isPending, isError, error } = useOrder(id, {
10868
- fields: "metadata"
10732
+ const PROMOTION_QUERY_KEY = "promotions";
10733
+ const promotionsQueryKeys = {
10734
+ list: (query2) => [
10735
+ PROMOTION_QUERY_KEY,
10736
+ query2 ? query2 : void 0
10737
+ ],
10738
+ detail: (id, query2) => [
10739
+ PROMOTION_QUERY_KEY,
10740
+ id,
10741
+ query2 ? query2 : void 0
10742
+ ]
10743
+ };
10744
+ const usePromotions = (query2, options) => {
10745
+ const { data, ...rest } = useQuery({
10746
+ queryKey: promotionsQueryKeys.list(query2),
10747
+ queryFn: async () => sdk.admin.promotion.list(query2),
10748
+ ...options
10869
10749
  });
10870
- if (isError) {
10871
- throw error;
10750
+ return { ...data, ...rest };
10751
+ };
10752
+ const Promotions = () => {
10753
+ const { id } = useParams();
10754
+ const {
10755
+ order: preview,
10756
+ isError: isPreviewError,
10757
+ error: previewError
10758
+ } = useOrderPreview(id, void 0);
10759
+ useInitiateOrderEdit({ preview });
10760
+ const { onCancel } = useCancelOrderEdit({ preview });
10761
+ if (isPreviewError) {
10762
+ throw previewError;
10872
10763
  }
10873
- const isReady = !isPending && !!order;
10874
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10875
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10876
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
10877
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10878
- ] }),
10879
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10764
+ const isReady = !!preview;
10765
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10766
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10767
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10880
10768
  ] });
10881
10769
  };
10882
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10883
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10884
- const MetadataForm = ({ orderId, metadata }) => {
10770
+ const PromotionForm = ({ preview }) => {
10771
+ const { items, shipping_methods } = preview;
10772
+ const [isSubmitting, setIsSubmitting] = useState(false);
10773
+ const [comboboxValue, setComboboxValue] = useState("");
10885
10774
  const { handleSuccess } = useRouteModal();
10886
- const hasUneditableRows = getHasUneditableRows(metadata);
10887
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10888
- const form = useForm({
10889
- defaultValues: {
10890
- metadata: getDefaultValues(metadata)
10775
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10776
+ const promoIds = getPromotionIds(items, shipping_methods);
10777
+ const { promotions, isPending, isError, error } = usePromotions(
10778
+ {
10779
+ id: promoIds
10891
10780
  },
10892
- resolver: zodResolver(MetadataSchema)
10781
+ {
10782
+ enabled: !!promoIds.length
10783
+ }
10784
+ );
10785
+ const comboboxData = useComboboxData({
10786
+ queryKey: ["promotions", "combobox", promoIds],
10787
+ queryFn: async (params) => {
10788
+ return await sdk.admin.promotion.list({
10789
+ ...params,
10790
+ id: {
10791
+ $nin: promoIds
10792
+ }
10793
+ });
10794
+ },
10795
+ getOptions: (data) => {
10796
+ return data.promotions.map((promotion) => ({
10797
+ label: promotion.code,
10798
+ value: promotion.code
10799
+ }));
10800
+ }
10893
10801
  });
10894
- const handleSubmit = form.handleSubmit(async (data) => {
10895
- const parsedData = parseValues(data);
10896
- await mutateAsync(
10802
+ const add = async (value) => {
10803
+ if (!value) {
10804
+ return;
10805
+ }
10806
+ addPromotions(
10897
10807
  {
10898
- metadata: parsedData
10808
+ promo_codes: [value]
10899
10809
  },
10900
10810
  {
10901
- onSuccess: () => {
10902
- toast.success("Metadata updated");
10903
- handleSuccess();
10811
+ onError: (e) => {
10812
+ toast.error(e.message);
10813
+ comboboxData.onSearchValueChange("");
10814
+ setComboboxValue("");
10904
10815
  },
10905
- onError: (error) => {
10906
- toast.error(error.message);
10816
+ onSuccess: () => {
10817
+ comboboxData.onSearchValueChange("");
10818
+ setComboboxValue("");
10907
10819
  }
10908
10820
  }
10909
10821
  );
10910
- });
10911
- const { fields, insert, remove } = useFieldArray({
10912
- control: form.control,
10913
- name: "metadata"
10914
- });
10915
- function deleteRow(index) {
10916
- remove(index);
10917
- if (fields.length === 1) {
10918
- insert(0, {
10919
- key: "",
10920
- value: "",
10921
- disabled: false
10922
- });
10923
- }
10924
- }
10925
- function insertRow(index, position) {
10926
- insert(index + (position === "above" ? 0 : 1), {
10927
- key: "",
10928
- value: "",
10929
- disabled: false
10822
+ };
10823
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10824
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10825
+ const onSubmit = async () => {
10826
+ setIsSubmitting(true);
10827
+ let requestSucceeded = false;
10828
+ await requestOrderEdit(void 0, {
10829
+ onError: (e) => {
10830
+ toast.error(e.message);
10831
+ },
10832
+ onSuccess: () => {
10833
+ requestSucceeded = true;
10834
+ }
10835
+ });
10836
+ if (!requestSucceeded) {
10837
+ setIsSubmitting(false);
10838
+ return;
10839
+ }
10840
+ await confirmOrderEdit(void 0, {
10841
+ onError: (e) => {
10842
+ toast.error(e.message);
10843
+ },
10844
+ onSuccess: () => {
10845
+ handleSuccess();
10846
+ },
10847
+ onSettled: () => {
10848
+ setIsSubmitting(false);
10849
+ }
10930
10850
  });
10851
+ };
10852
+ if (isError) {
10853
+ throw error;
10931
10854
  }
10932
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10933
- KeyboundForm,
10934
- {
10935
- onSubmit: handleSubmit,
10936
- className: "flex flex-1 flex-col overflow-hidden",
10937
- children: [
10938
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10939
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10940
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10941
- /* @__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" }) }),
10942
- /* @__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" }) })
10943
- ] }),
10944
- fields.map((field, index) => {
10945
- const isDisabled = field.disabled || false;
10946
- let placeholder = "-";
10947
- if (typeof field.value === "object") {
10948
- placeholder = "{ ... }";
10949
- }
10950
- if (Array.isArray(field.value)) {
10951
- placeholder = "[ ... ]";
10952
- }
10953
- return /* @__PURE__ */ jsx(
10954
- ConditionalTooltip,
10955
- {
10956
- showTooltip: isDisabled,
10957
- content: "This row is disabled because it contains non-primitive data.",
10958
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
10959
- /* @__PURE__ */ jsxs(
10960
- "div",
10961
- {
10962
- className: clx("grid grid-cols-2 divide-x", {
10963
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10964
- }),
10965
- children: [
10966
- /* @__PURE__ */ jsx(
10967
- Form$2.Field,
10968
- {
10969
- control: form.control,
10970
- name: `metadata.${index}.key`,
10971
- render: ({ field: field2 }) => {
10972
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10973
- GridInput,
10974
- {
10975
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10976
- ...field2,
10977
- disabled: isDisabled,
10978
- placeholder: "Key"
10979
- }
10980
- ) }) });
10981
- }
10982
- }
10983
- ),
10984
- /* @__PURE__ */ jsx(
10985
- Form$2.Field,
10986
- {
10987
- control: form.control,
10988
- name: `metadata.${index}.value`,
10989
- render: ({ field: { value, ...field2 } }) => {
10990
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10991
- GridInput,
10992
- {
10993
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
10994
- ...field2,
10995
- value: isDisabled ? placeholder : value,
10996
- disabled: isDisabled,
10997
- placeholder: "Value"
10998
- }
10999
- ) }) });
11000
- }
11001
- }
11002
- )
11003
- ]
11004
- }
11005
- ),
11006
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
11007
- /* @__PURE__ */ jsx(
11008
- DropdownMenu.Trigger,
11009
- {
11010
- className: clx(
11011
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11012
- {
11013
- hidden: isDisabled
11014
- }
11015
- ),
11016
- disabled: isDisabled,
11017
- asChild: true,
11018
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
11019
- }
11020
- ),
11021
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
11022
- /* @__PURE__ */ jsxs(
11023
- DropdownMenu.Item,
11024
- {
11025
- className: "gap-x-2",
11026
- onClick: () => insertRow(index, "above"),
11027
- children: [
11028
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
11029
- "Insert row above"
11030
- ]
11031
- }
11032
- ),
11033
- /* @__PURE__ */ jsxs(
11034
- DropdownMenu.Item,
11035
- {
11036
- className: "gap-x-2",
11037
- onClick: () => insertRow(index, "below"),
11038
- children: [
11039
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
11040
- "Insert row below"
11041
- ]
11042
- }
11043
- ),
11044
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
11045
- /* @__PURE__ */ jsxs(
11046
- DropdownMenu.Item,
11047
- {
11048
- className: "gap-x-2",
11049
- onClick: () => deleteRow(index),
11050
- children: [
11051
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
11052
- "Delete row"
11053
- ]
11054
- }
11055
- )
11056
- ] })
11057
- ] })
11058
- ] })
11059
- },
11060
- field.id
11061
- );
11062
- })
11063
- ] }),
11064
- 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." })
10855
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10856
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
10857
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
10858
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10859
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10860
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11065
10861
  ] }),
11066
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11067
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11068
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11069
- ] }) })
11070
- ]
11071
- }
11072
- ) });
11073
- };
11074
- const GridInput = forwardRef(({ className, ...props }, ref) => {
11075
- return /* @__PURE__ */ jsx(
11076
- "input",
11077
- {
11078
- ref,
11079
- ...props,
11080
- autoComplete: "off",
11081
- className: clx(
11082
- "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",
11083
- className
10862
+ /* @__PURE__ */ jsx(
10863
+ Combobox,
10864
+ {
10865
+ id: "promotion-combobox",
10866
+ "aria-describedby": "promotion-combobox-hint",
10867
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10868
+ fetchNextPage: comboboxData.fetchNextPage,
10869
+ options: comboboxData.options,
10870
+ onSearchValueChange: comboboxData.onSearchValueChange,
10871
+ searchValue: comboboxData.searchValue,
10872
+ disabled: comboboxData.disabled || isAddingPromotions,
10873
+ onChange: add,
10874
+ value: comboboxValue
10875
+ }
10876
+ )
10877
+ ] }),
10878
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10879
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
10880
+ PromotionItem,
10881
+ {
10882
+ promotion,
10883
+ orderId: preview.id,
10884
+ isLoading: isPending
10885
+ },
10886
+ promotion.id
10887
+ )) })
10888
+ ] }) }),
10889
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10890
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10891
+ /* @__PURE__ */ jsx(
10892
+ Button,
10893
+ {
10894
+ size: "small",
10895
+ type: "submit",
10896
+ isLoading: isSubmitting || isAddingPromotions,
10897
+ children: "Save"
10898
+ }
11084
10899
  )
11085
- }
11086
- );
11087
- });
11088
- GridInput.displayName = "MetadataForm.GridInput";
11089
- const PlaceholderInner = () => {
11090
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11091
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11092
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11093
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11094
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11095
10900
  ] }) })
11096
10901
  ] });
11097
10902
  };
11098
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11099
- function getDefaultValues(metadata) {
11100
- if (!metadata || !Object.keys(metadata).length) {
11101
- return [
11102
- {
11103
- key: "",
11104
- value: "",
11105
- disabled: false
11106
- }
11107
- ];
11108
- }
11109
- return Object.entries(metadata).map(([key, value]) => {
11110
- if (!EDITABLE_TYPES.includes(typeof value)) {
11111
- return {
11112
- key,
11113
- value,
11114
- disabled: true
11115
- };
11116
- }
11117
- let stringValue = value;
11118
- if (typeof value !== "string") {
11119
- stringValue = JSON.stringify(value);
11120
- }
11121
- return {
11122
- key,
11123
- value: stringValue,
11124
- original_key: key
11125
- };
11126
- });
11127
- }
11128
- function parseValues(values) {
11129
- const metadata = values.metadata;
11130
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11131
- if (isEmpty) {
11132
- return null;
11133
- }
11134
- const update = {};
11135
- metadata.forEach((field) => {
11136
- let key = field.key;
11137
- let value = field.value;
11138
- const disabled = field.disabled;
11139
- if (!key || !value) {
11140
- return;
11141
- }
11142
- if (disabled) {
11143
- update[key] = value;
11144
- return;
11145
- }
11146
- key = key.trim();
11147
- value = value.trim();
11148
- if (value === "true") {
11149
- update[key] = true;
11150
- } else if (value === "false") {
11151
- update[key] = false;
11152
- } else {
11153
- const parsedNumber = parseFloat(value);
11154
- if (!isNaN(parsedNumber)) {
11155
- update[key] = parsedNumber;
11156
- } else {
11157
- update[key] = value;
11158
- }
11159
- }
11160
- });
11161
- return update;
11162
- }
11163
- function getHasUneditableRows(metadata) {
11164
- if (!metadata) {
11165
- return false;
11166
- }
11167
- return Object.values(metadata).some(
11168
- (value) => !EDITABLE_TYPES.includes(typeof value)
11169
- );
11170
- }
11171
- const PROMOTION_QUERY_KEY = "promotions";
11172
- const promotionsQueryKeys = {
11173
- list: (query2) => [
11174
- PROMOTION_QUERY_KEY,
11175
- query2 ? query2 : void 0
11176
- ],
11177
- detail: (id, query2) => [
11178
- PROMOTION_QUERY_KEY,
11179
- id,
11180
- query2 ? query2 : void 0
11181
- ]
11182
- };
11183
- const usePromotions = (query2, options) => {
11184
- const { data, ...rest } = useQuery({
11185
- queryKey: promotionsQueryKeys.list(query2),
11186
- queryFn: async () => sdk.admin.promotion.list(query2),
11187
- ...options
11188
- });
11189
- return { ...data, ...rest };
11190
- };
11191
- const Promotions = () => {
11192
- const { id } = useParams();
11193
- const {
11194
- order: preview,
11195
- isError: isPreviewError,
11196
- error: previewError
11197
- } = useOrderPreview(id, void 0);
11198
- useInitiateOrderEdit({ preview });
11199
- const { onCancel } = useCancelOrderEdit({ preview });
11200
- if (isPreviewError) {
11201
- throw previewError;
11202
- }
11203
- const isReady = !!preview;
11204
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11205
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11206
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11207
- ] });
11208
- };
11209
- const PromotionForm = ({ preview }) => {
11210
- const { items, shipping_methods } = preview;
11211
- const [isSubmitting, setIsSubmitting] = useState(false);
11212
- const [comboboxValue, setComboboxValue] = useState("");
11213
- const { handleSuccess } = useRouteModal();
11214
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11215
- const promoIds = getPromotionIds(items, shipping_methods);
11216
- const { promotions, isPending, isError, error } = usePromotions(
11217
- {
11218
- id: promoIds
11219
- },
11220
- {
11221
- enabled: !!promoIds.length
11222
- }
11223
- );
11224
- const comboboxData = useComboboxData({
11225
- queryKey: ["promotions", "combobox", promoIds],
11226
- queryFn: async (params) => {
11227
- return await sdk.admin.promotion.list({
11228
- ...params,
11229
- id: {
11230
- $nin: promoIds
11231
- }
11232
- });
11233
- },
11234
- getOptions: (data) => {
11235
- return data.promotions.map((promotion) => ({
11236
- label: promotion.code,
11237
- value: promotion.code
11238
- }));
11239
- }
11240
- });
11241
- const add = async (value) => {
11242
- if (!value) {
11243
- return;
11244
- }
11245
- addPromotions(
11246
- {
11247
- promo_codes: [value]
11248
- },
11249
- {
11250
- onError: (e) => {
11251
- toast.error(e.message);
11252
- comboboxData.onSearchValueChange("");
11253
- setComboboxValue("");
11254
- },
11255
- onSuccess: () => {
11256
- comboboxData.onSearchValueChange("");
11257
- setComboboxValue("");
11258
- }
11259
- }
11260
- );
11261
- };
11262
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11263
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11264
- const onSubmit = async () => {
11265
- setIsSubmitting(true);
11266
- let requestSucceeded = false;
11267
- await requestOrderEdit(void 0, {
11268
- onError: (e) => {
11269
- toast.error(e.message);
11270
- },
11271
- onSuccess: () => {
11272
- requestSucceeded = true;
11273
- }
11274
- });
11275
- if (!requestSucceeded) {
11276
- setIsSubmitting(false);
11277
- return;
11278
- }
11279
- await confirmOrderEdit(void 0, {
11280
- onError: (e) => {
11281
- toast.error(e.message);
11282
- },
11283
- onSuccess: () => {
11284
- handleSuccess();
11285
- },
11286
- onSettled: () => {
11287
- setIsSubmitting(false);
11288
- }
11289
- });
11290
- };
11291
- if (isError) {
11292
- throw error;
11293
- }
11294
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11295
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11296
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11297
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11298
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11299
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11300
- ] }),
11301
- /* @__PURE__ */ jsx(
11302
- Combobox,
11303
- {
11304
- id: "promotion-combobox",
11305
- "aria-describedby": "promotion-combobox-hint",
11306
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11307
- fetchNextPage: comboboxData.fetchNextPage,
11308
- options: comboboxData.options,
11309
- onSearchValueChange: comboboxData.onSearchValueChange,
11310
- searchValue: comboboxData.searchValue,
11311
- disabled: comboboxData.disabled || isAddingPromotions,
11312
- onChange: add,
11313
- value: comboboxValue
11314
- }
11315
- )
11316
- ] }),
11317
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11318
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11319
- PromotionItem,
11320
- {
11321
- promotion,
11322
- orderId: preview.id,
11323
- isLoading: isPending
11324
- },
11325
- promotion.id
11326
- )) })
11327
- ] }) }),
11328
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11329
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11330
- /* @__PURE__ */ jsx(
11331
- Button,
11332
- {
11333
- size: "small",
11334
- type: "submit",
11335
- isLoading: isSubmitting || isAddingPromotions,
11336
- children: "Save"
11337
- }
11338
- )
11339
- ] }) })
11340
- ] });
11341
- };
11342
- const PromotionItem = ({
11343
- promotion,
11344
- orderId,
11345
- isLoading
11346
- }) => {
11347
- var _a;
11348
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11349
- const onRemove = async () => {
11350
- removePromotions(
10903
+ const PromotionItem = ({
10904
+ promotion,
10905
+ orderId,
10906
+ isLoading
10907
+ }) => {
10908
+ var _a;
10909
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10910
+ const onRemove = async () => {
10911
+ removePromotions(
11351
10912
  {
11352
10913
  promo_codes: [promotion.code]
11353
10914
  },
@@ -11445,15 +11006,83 @@ function getPromotionIds(items, shippingMethods) {
11445
11006
  }
11446
11007
  return Array.from(promotionIds);
11447
11008
  }
11448
- const SalesChannel = () => {
11009
+ const Email = () => {
11449
11010
  const { id } = useParams();
11450
- const { draft_order, isPending, isError, error } = useDraftOrder(
11451
- id,
11452
- {
11453
- fields: "+sales_channel_id"
11454
- },
11455
- {
11456
- enabled: !!id
11011
+ const { order, isPending, isError, error } = useOrder(id, {
11012
+ fields: "+email"
11013
+ });
11014
+ if (isError) {
11015
+ throw error;
11016
+ }
11017
+ const isReady = !isPending && !!order;
11018
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11019
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11020
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
11021
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
11022
+ ] }),
11023
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
11024
+ ] });
11025
+ };
11026
+ const EmailForm = ({ order }) => {
11027
+ const form = useForm({
11028
+ defaultValues: {
11029
+ email: order.email ?? ""
11030
+ },
11031
+ resolver: zodResolver(schema$4)
11032
+ });
11033
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11034
+ const { handleSuccess } = useRouteModal();
11035
+ const onSubmit = form.handleSubmit(async (data) => {
11036
+ await mutateAsync(
11037
+ { email: data.email },
11038
+ {
11039
+ onSuccess: () => {
11040
+ handleSuccess();
11041
+ },
11042
+ onError: (error) => {
11043
+ toast.error(error.message);
11044
+ }
11045
+ }
11046
+ );
11047
+ });
11048
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11049
+ KeyboundForm,
11050
+ {
11051
+ className: "flex flex-1 flex-col overflow-hidden",
11052
+ onSubmit,
11053
+ children: [
11054
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
11055
+ Form$2.Field,
11056
+ {
11057
+ control: form.control,
11058
+ name: "email",
11059
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11060
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
11061
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11062
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11063
+ ] })
11064
+ }
11065
+ ) }),
11066
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11067
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11068
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11069
+ ] }) })
11070
+ ]
11071
+ }
11072
+ ) });
11073
+ };
11074
+ const schema$4 = objectType({
11075
+ email: stringType().email()
11076
+ });
11077
+ const SalesChannel = () => {
11078
+ const { id } = useParams();
11079
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11080
+ id,
11081
+ {
11082
+ fields: "+sales_channel_id"
11083
+ },
11084
+ {
11085
+ enabled: !!id
11457
11086
  }
11458
11087
  );
11459
11088
  if (isError) {
@@ -11473,7 +11102,7 @@ const SalesChannelForm = ({ order }) => {
11473
11102
  defaultValues: {
11474
11103
  sales_channel_id: order.sales_channel_id || ""
11475
11104
  },
11476
- resolver: zodResolver(schema$2)
11105
+ resolver: zodResolver(schema$3)
11477
11106
  });
11478
11107
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11479
11108
  const { handleSuccess } = useRouteModal();
@@ -11548,129 +11177,332 @@ const SalesChannelField = ({ control, order }) => {
11548
11177
  }
11549
11178
  );
11550
11179
  };
11551
- const schema$2 = objectType({
11180
+ const schema$3 = objectType({
11552
11181
  sales_channel_id: stringType().min(1)
11553
11182
  });
11554
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11555
- const Shipping = () => {
11556
- var _a;
11183
+ const ShippingAddress = () => {
11557
11184
  const { id } = useParams();
11558
11185
  const { order, isPending, isError, error } = useOrder(id, {
11559
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11186
+ fields: "+shipping_address"
11560
11187
  });
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
11188
  if (isError) {
11570
11189
  throw error;
11571
11190
  }
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 px-6 py-16", 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
- ] }) });
11191
+ const isReady = !isPending && !!order;
11192
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11193
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11194
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
11195
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11196
+ ] }),
11197
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
11198
+ ] });
11588
11199
  };
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.*"
11200
+ const ShippingAddressForm = ({ order }) => {
11201
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11202
+ const form = useForm({
11203
+ defaultValues: {
11204
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11205
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11206
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11207
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11208
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11209
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11210
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11211
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11212
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11213
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11599
11214
  },
11600
- {
11601
- enabled: appliedShippingOptionIds.length > 0
11602
- }
11603
- );
11604
- const uniqueShippingProfiles = useMemo(() => {
11605
- const profiles = /* @__PURE__ */ new Map();
11606
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11607
- profiles.set(profile.id, profile);
11608
- });
11609
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11610
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11611
- });
11612
- return Array.from(profiles.values());
11613
- }, [order.items, shipping_options]);
11215
+ resolver: zodResolver(schema$2)
11216
+ });
11217
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11614
11218
  const { handleSuccess } = useRouteModal();
11615
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11616
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11617
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11618
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11619
- const onSubmit = async () => {
11620
- setIsSubmitting(true);
11621
- let requestSucceeded = false;
11622
- await requestOrderEdit(void 0, {
11623
- onError: (e) => {
11624
- toast.error(`Failed to request order edit: ${e.message}`);
11625
- },
11626
- onSuccess: () => {
11627
- requestSucceeded = true;
11628
- }
11629
- });
11630
- if (!requestSucceeded) {
11631
- setIsSubmitting(false);
11632
- return;
11633
- }
11634
- await confirmOrderEdit(void 0, {
11635
- onError: (e) => {
11636
- toast.error(`Failed to confirm order edit: ${e.message}`);
11637
- },
11638
- onSuccess: () => {
11639
- handleSuccess();
11219
+ const onSubmit = form.handleSubmit(async (data) => {
11220
+ await mutateAsync(
11221
+ {
11222
+ shipping_address: {
11223
+ first_name: data.first_name,
11224
+ last_name: data.last_name,
11225
+ company: data.company,
11226
+ address_1: data.address_1,
11227
+ address_2: data.address_2,
11228
+ city: data.city,
11229
+ province: data.province,
11230
+ country_code: data.country_code,
11231
+ postal_code: data.postal_code,
11232
+ phone: data.phone
11233
+ }
11640
11234
  },
11641
- onSettled: () => {
11642
- setIsSubmitting(false);
11643
- }
11644
- });
11645
- };
11646
- const onKeydown = useCallback(
11647
- (e) => {
11648
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11649
- if (data || isSubmitting) {
11650
- return;
11235
+ {
11236
+ onSuccess: () => {
11237
+ handleSuccess();
11238
+ },
11239
+ onError: (error) => {
11240
+ toast.error(error.message);
11651
11241
  }
11652
- onSubmit();
11653
11242
  }
11654
- },
11655
- [data, isSubmitting, onSubmit]
11656
- );
11657
- useEffect(() => {
11658
- document.addEventListener("keydown", onKeydown);
11659
- return () => {
11660
- document.removeEventListener("keydown", onKeydown);
11661
- };
11662
- }, [onKeydown]);
11663
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11664
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11665
- /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11666
- /* @__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 px-6 py-16", children: [
11667
- /* @__PURE__ */ jsxs("div", { children: [
11668
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11669
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
11670
- ] }),
11671
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11672
- /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
11673
- /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between px-4 py-2", children: [
11243
+ );
11244
+ });
11245
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11246
+ KeyboundForm,
11247
+ {
11248
+ className: "flex flex-1 flex-col overflow-hidden",
11249
+ onSubmit,
11250
+ children: [
11251
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
11252
+ /* @__PURE__ */ jsx(
11253
+ Form$2.Field,
11254
+ {
11255
+ control: form.control,
11256
+ name: "country_code",
11257
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11258
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
11259
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
11260
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11261
+ ] })
11262
+ }
11263
+ ),
11264
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11265
+ /* @__PURE__ */ jsx(
11266
+ Form$2.Field,
11267
+ {
11268
+ control: form.control,
11269
+ name: "first_name",
11270
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11271
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
11272
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11273
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11274
+ ] })
11275
+ }
11276
+ ),
11277
+ /* @__PURE__ */ jsx(
11278
+ Form$2.Field,
11279
+ {
11280
+ control: form.control,
11281
+ name: "last_name",
11282
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11283
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
11284
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11285
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11286
+ ] })
11287
+ }
11288
+ )
11289
+ ] }),
11290
+ /* @__PURE__ */ jsx(
11291
+ Form$2.Field,
11292
+ {
11293
+ control: form.control,
11294
+ name: "company",
11295
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11296
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
11297
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11298
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11299
+ ] })
11300
+ }
11301
+ ),
11302
+ /* @__PURE__ */ jsx(
11303
+ Form$2.Field,
11304
+ {
11305
+ control: form.control,
11306
+ name: "address_1",
11307
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11308
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
11309
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11310
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11311
+ ] })
11312
+ }
11313
+ ),
11314
+ /* @__PURE__ */ jsx(
11315
+ Form$2.Field,
11316
+ {
11317
+ control: form.control,
11318
+ name: "address_2",
11319
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11320
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11321
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11322
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11323
+ ] })
11324
+ }
11325
+ ),
11326
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11327
+ /* @__PURE__ */ jsx(
11328
+ Form$2.Field,
11329
+ {
11330
+ control: form.control,
11331
+ name: "postal_code",
11332
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11333
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
11334
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11335
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11336
+ ] })
11337
+ }
11338
+ ),
11339
+ /* @__PURE__ */ jsx(
11340
+ Form$2.Field,
11341
+ {
11342
+ control: form.control,
11343
+ name: "city",
11344
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11345
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
11346
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11347
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11348
+ ] })
11349
+ }
11350
+ )
11351
+ ] }),
11352
+ /* @__PURE__ */ jsx(
11353
+ Form$2.Field,
11354
+ {
11355
+ control: form.control,
11356
+ name: "province",
11357
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11358
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11359
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11360
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11361
+ ] })
11362
+ }
11363
+ ),
11364
+ /* @__PURE__ */ jsx(
11365
+ Form$2.Field,
11366
+ {
11367
+ control: form.control,
11368
+ name: "phone",
11369
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11370
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
11371
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11372
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11373
+ ] })
11374
+ }
11375
+ )
11376
+ ] }) }),
11377
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11378
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11379
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11380
+ ] }) })
11381
+ ]
11382
+ }
11383
+ ) });
11384
+ };
11385
+ const schema$2 = addressSchema;
11386
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
11387
+ const Shipping = () => {
11388
+ var _a;
11389
+ const { id } = useParams();
11390
+ const { order, isPending, isError, error } = useOrder(id, {
11391
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11392
+ });
11393
+ const {
11394
+ order: preview,
11395
+ isPending: isPreviewPending,
11396
+ isError: isPreviewError,
11397
+ error: previewError
11398
+ } = useOrderPreview(id);
11399
+ useInitiateOrderEdit({ preview });
11400
+ const { onCancel } = useCancelOrderEdit({ preview });
11401
+ if (isError) {
11402
+ throw error;
11403
+ }
11404
+ if (isPreviewError) {
11405
+ throw previewError;
11406
+ }
11407
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11408
+ const isReady = preview && !isPreviewPending && order && !isPending;
11409
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11410
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11411
+ /* @__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 px-6 py-16", children: [
11412
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11413
+ /* @__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." }) })
11414
+ ] }) }) }),
11415
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11416
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11417
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11418
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11419
+ ] }) });
11420
+ };
11421
+ const ShippingForm = ({ preview, order }) => {
11422
+ var _a;
11423
+ const { setIsOpen } = useStackedModal();
11424
+ const [isSubmitting, setIsSubmitting] = useState(false);
11425
+ const [data, setData] = useState(null);
11426
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11427
+ const { shipping_options } = useShippingOptions(
11428
+ {
11429
+ id: appliedShippingOptionIds,
11430
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11431
+ },
11432
+ {
11433
+ enabled: appliedShippingOptionIds.length > 0
11434
+ }
11435
+ );
11436
+ const uniqueShippingProfiles = useMemo(() => {
11437
+ const profiles = /* @__PURE__ */ new Map();
11438
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
11439
+ profiles.set(profile.id, profile);
11440
+ });
11441
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11442
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
11443
+ });
11444
+ return Array.from(profiles.values());
11445
+ }, [order.items, shipping_options]);
11446
+ const { handleSuccess } = useRouteModal();
11447
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11448
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11449
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11450
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11451
+ const onSubmit = async () => {
11452
+ setIsSubmitting(true);
11453
+ let requestSucceeded = false;
11454
+ await requestOrderEdit(void 0, {
11455
+ onError: (e) => {
11456
+ toast.error(`Failed to request order edit: ${e.message}`);
11457
+ },
11458
+ onSuccess: () => {
11459
+ requestSucceeded = true;
11460
+ }
11461
+ });
11462
+ if (!requestSucceeded) {
11463
+ setIsSubmitting(false);
11464
+ return;
11465
+ }
11466
+ await confirmOrderEdit(void 0, {
11467
+ onError: (e) => {
11468
+ toast.error(`Failed to confirm order edit: ${e.message}`);
11469
+ },
11470
+ onSuccess: () => {
11471
+ handleSuccess();
11472
+ },
11473
+ onSettled: () => {
11474
+ setIsSubmitting(false);
11475
+ }
11476
+ });
11477
+ };
11478
+ const onKeydown = useCallback(
11479
+ (e) => {
11480
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11481
+ if (data || isSubmitting) {
11482
+ return;
11483
+ }
11484
+ onSubmit();
11485
+ }
11486
+ },
11487
+ [data, isSubmitting, onSubmit]
11488
+ );
11489
+ useEffect(() => {
11490
+ document.addEventListener("keydown", onKeydown);
11491
+ return () => {
11492
+ document.removeEventListener("keydown", onKeydown);
11493
+ };
11494
+ }, [onKeydown]);
11495
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11496
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11497
+ /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11498
+ /* @__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 px-6 py-16", children: [
11499
+ /* @__PURE__ */ jsxs("div", { children: [
11500
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11501
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
11502
+ ] }),
11503
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11504
+ /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
11505
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-between px-4 py-2", children: [
11674
11506
  /* @__PURE__ */ jsx(
11675
11507
  Text,
11676
11508
  {
@@ -11969,598 +11801,766 @@ const ShippingForm = ({ preview, order }) => {
11969
11801
  ] }) })
11970
11802
  ] });
11971
11803
  };
11972
- const StackedModalTrigger = ({
11804
+ const StackedModalTrigger = ({
11805
+ shippingProfileId,
11806
+ shippingOption,
11807
+ shippingMethod,
11808
+ setData,
11809
+ children
11810
+ }) => {
11811
+ const { setIsOpen, getIsOpen } = useStackedModal();
11812
+ const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
11813
+ const onToggle = () => {
11814
+ if (isOpen) {
11815
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11816
+ setData(null);
11817
+ } else {
11818
+ setIsOpen(STACKED_FOCUS_MODAL_ID, true);
11819
+ setData({
11820
+ shippingProfileId,
11821
+ shippingOption,
11822
+ shippingMethod
11823
+ });
11824
+ }
11825
+ };
11826
+ return /* @__PURE__ */ jsx(
11827
+ Button,
11828
+ {
11829
+ size: "small",
11830
+ variant: "secondary",
11831
+ onClick: onToggle,
11832
+ className: "text-ui-fg-primary shrink-0",
11833
+ children
11834
+ }
11835
+ );
11836
+ };
11837
+ const ShippingProfileForm = ({
11838
+ data,
11839
+ order,
11840
+ preview
11841
+ }) => {
11842
+ var _a, _b, _c, _d, _e, _f;
11843
+ const { setIsOpen } = useStackedModal();
11844
+ const form = useForm({
11845
+ resolver: zodResolver(shippingMethodSchema),
11846
+ defaultValues: {
11847
+ location_id: (_d = (_c = (_b = (_a = data.shippingOption) == null ? void 0 : _a.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.id,
11848
+ shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
11849
+ custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
11850
+ }
11851
+ });
11852
+ const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
11853
+ const {
11854
+ mutateAsync: updateShippingMethod,
11855
+ isPending: isUpdatingShippingMethod
11856
+ } = useDraftOrderUpdateShippingMethod(order.id);
11857
+ const onSubmit = form.handleSubmit(async (values) => {
11858
+ if (isEqual(values, form.formState.defaultValues)) {
11859
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11860
+ return;
11861
+ }
11862
+ if (data.shippingMethod) {
11863
+ await updateShippingMethod(
11864
+ {
11865
+ method_id: data.shippingMethod.id,
11866
+ shipping_option_id: values.shipping_option_id,
11867
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
11868
+ },
11869
+ {
11870
+ onError: (e) => {
11871
+ toast.error(e.message);
11872
+ },
11873
+ onSuccess: () => {
11874
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11875
+ }
11876
+ }
11877
+ );
11878
+ return;
11879
+ }
11880
+ await addShippingMethod(
11881
+ {
11882
+ shipping_option_id: values.shipping_option_id,
11883
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
11884
+ },
11885
+ {
11886
+ onError: (e) => {
11887
+ toast.error(e.message);
11888
+ },
11889
+ onSuccess: () => {
11890
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11891
+ }
11892
+ }
11893
+ );
11894
+ });
11895
+ return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
11896
+ KeyboundForm,
11897
+ {
11898
+ className: "flex h-full flex-col overflow-hidden",
11899
+ onSubmit,
11900
+ children: [
11901
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
11902
+ /* @__PURE__ */ jsx(StackedFocusModal.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 px-6 py-16", children: [
11903
+ /* @__PURE__ */ jsxs("div", { children: [
11904
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11905
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a shipping method for the selected shipping profile. You can see the items that will be shipped using this method in the preview below." }) })
11906
+ ] }),
11907
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11908
+ /* @__PURE__ */ jsx(
11909
+ LocationField,
11910
+ {
11911
+ control: form.control,
11912
+ setValue: form.setValue
11913
+ }
11914
+ ),
11915
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11916
+ /* @__PURE__ */ jsx(
11917
+ ShippingOptionField,
11918
+ {
11919
+ shippingProfileId: data.shippingProfileId,
11920
+ preview,
11921
+ control: form.control
11922
+ }
11923
+ ),
11924
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11925
+ /* @__PURE__ */ jsx(
11926
+ CustomAmountField,
11927
+ {
11928
+ control: form.control,
11929
+ currencyCode: order.currency_code
11930
+ }
11931
+ ),
11932
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11933
+ /* @__PURE__ */ jsx(
11934
+ ItemsPreview,
11935
+ {
11936
+ order,
11937
+ shippingProfileId: data.shippingProfileId
11938
+ }
11939
+ )
11940
+ ] }) }) }),
11941
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
11942
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11943
+ /* @__PURE__ */ jsx(
11944
+ Button,
11945
+ {
11946
+ size: "small",
11947
+ type: "submit",
11948
+ isLoading: isPending || isUpdatingShippingMethod,
11949
+ children: data.shippingMethod ? "Update" : "Add"
11950
+ }
11951
+ )
11952
+ ] }) })
11953
+ ]
11954
+ }
11955
+ ) }) });
11956
+ };
11957
+ const shippingMethodSchema = objectType({
11958
+ location_id: stringType(),
11959
+ shipping_option_id: stringType(),
11960
+ custom_amount: unionType([numberType(), stringType()]).optional()
11961
+ });
11962
+ const ItemsPreview = ({ order, shippingProfileId }) => {
11963
+ const matches = order.items.filter(
11964
+ (item) => {
11965
+ var _a, _b, _c;
11966
+ return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
11967
+ }
11968
+ );
11969
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
11970
+ /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11971
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
11972
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
11973
+ ] }) }),
11974
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
11975
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-muted grid grid-cols-2 gap-3 px-4 py-2", children: [
11976
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
11977
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
11978
+ ] }),
11979
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxs(
11980
+ "div",
11981
+ {
11982
+ className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-2 items-center gap-3 rounded-lg px-4 py-2",
11983
+ children: [
11984
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
11985
+ /* @__PURE__ */ jsx(
11986
+ Thumbnail,
11987
+ {
11988
+ thumbnail: item.thumbnail,
11989
+ alt: item.product_title ?? void 0
11990
+ }
11991
+ ),
11992
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11993
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
11994
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
11995
+ /* @__PURE__ */ jsxs(
11996
+ Text,
11997
+ {
11998
+ size: "small",
11999
+ leading: "compact",
12000
+ className: "text-ui-fg-subtle",
12001
+ children: [
12002
+ "(",
12003
+ item.variant_title,
12004
+ ")"
12005
+ ]
12006
+ }
12007
+ )
12008
+ ] }),
12009
+ /* @__PURE__ */ jsx(
12010
+ Text,
12011
+ {
12012
+ size: "small",
12013
+ leading: "compact",
12014
+ className: "text-ui-fg-subtle",
12015
+ children: item.variant_sku
12016
+ }
12017
+ )
12018
+ ] })
12019
+ ] }),
12020
+ /* @__PURE__ */ jsxs(
12021
+ Text,
12022
+ {
12023
+ size: "small",
12024
+ leading: "compact",
12025
+ className: "text-ui-fg-subtle",
12026
+ children: [
12027
+ item.quantity,
12028
+ "x"
12029
+ ]
12030
+ }
12031
+ )
12032
+ ]
12033
+ },
12034
+ item.id
12035
+ )) : /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12036
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12037
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12038
+ 'No items found for "',
12039
+ query,
12040
+ '".'
12041
+ ] })
12042
+ ] }) })
12043
+ ] })
12044
+ ] });
12045
+ };
12046
+ const LocationField = ({ control, setValue }) => {
12047
+ const locations = useComboboxData({
12048
+ queryKey: ["locations"],
12049
+ queryFn: async (params) => {
12050
+ return await sdk.admin.stockLocation.list(params);
12051
+ },
12052
+ getOptions: (data) => {
12053
+ return data.stock_locations.map((location) => ({
12054
+ label: location.name,
12055
+ value: location.id
12056
+ }));
12057
+ }
12058
+ });
12059
+ return /* @__PURE__ */ jsx(
12060
+ Form$2.Field,
12061
+ {
12062
+ control,
12063
+ name: "location_id",
12064
+ render: ({ field: { onChange, ...field } }) => {
12065
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12066
+ /* @__PURE__ */ jsxs("div", { children: [
12067
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
12068
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12069
+ ] }),
12070
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12071
+ Combobox,
12072
+ {
12073
+ options: locations.options,
12074
+ fetchNextPage: locations.fetchNextPage,
12075
+ isFetchingNextPage: locations.isFetchingNextPage,
12076
+ searchValue: locations.searchValue,
12077
+ onSearchValueChange: locations.onSearchValueChange,
12078
+ placeholder: "Select location",
12079
+ onChange: (value) => {
12080
+ setValue("shipping_option_id", "", {
12081
+ shouldDirty: true,
12082
+ shouldTouch: true
12083
+ });
12084
+ onChange(value);
12085
+ },
12086
+ ...field
12087
+ }
12088
+ ) })
12089
+ ] }) });
12090
+ }
12091
+ }
12092
+ );
12093
+ };
12094
+ const ShippingOptionField = ({
11973
12095
  shippingProfileId,
11974
- shippingOption,
11975
- shippingMethod,
11976
- setData,
11977
- children
12096
+ preview,
12097
+ control
11978
12098
  }) => {
11979
- const { setIsOpen, getIsOpen } = useStackedModal();
11980
- const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
11981
- const onToggle = () => {
11982
- if (isOpen) {
11983
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11984
- setData(null);
11985
- } else {
11986
- setIsOpen(STACKED_FOCUS_MODAL_ID, true);
11987
- setData({
11988
- shippingProfileId,
11989
- shippingOption,
11990
- shippingMethod
12099
+ var _a;
12100
+ const locationId = useWatch({ control, name: "location_id" });
12101
+ const shippingOptions = useComboboxData({
12102
+ queryKey: ["shipping_options", locationId, shippingProfileId],
12103
+ queryFn: async (params) => {
12104
+ return await sdk.admin.shippingOption.list({
12105
+ ...params,
12106
+ stock_location_id: locationId,
12107
+ shipping_profile_id: shippingProfileId
11991
12108
  });
11992
- }
11993
- };
12109
+ },
12110
+ getOptions: (data) => {
12111
+ return data.shipping_options.map((option) => {
12112
+ var _a2;
12113
+ if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12114
+ (r) => r.attribute === "is_return" && r.value === "true"
12115
+ )) {
12116
+ return void 0;
12117
+ }
12118
+ return {
12119
+ label: option.name,
12120
+ value: option.id
12121
+ };
12122
+ }).filter(Boolean);
12123
+ },
12124
+ enabled: !!locationId && !!shippingProfileId,
12125
+ defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12126
+ });
12127
+ const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
11994
12128
  return /* @__PURE__ */ jsx(
11995
- Button,
12129
+ Form$2.Field,
11996
12130
  {
11997
- size: "small",
11998
- variant: "secondary",
11999
- onClick: onToggle,
12000
- className: "text-ui-fg-primary shrink-0",
12001
- children
12131
+ control,
12132
+ name: "shipping_option_id",
12133
+ render: ({ field }) => {
12134
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12135
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12136
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12137
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12138
+ ] }),
12139
+ /* @__PURE__ */ jsx(
12140
+ ConditionalTooltip,
12141
+ {
12142
+ content: tooltipContent,
12143
+ showTooltip: !locationId || !shippingProfileId,
12144
+ children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12145
+ Combobox,
12146
+ {
12147
+ options: shippingOptions.options,
12148
+ fetchNextPage: shippingOptions.fetchNextPage,
12149
+ isFetchingNextPage: shippingOptions.isFetchingNextPage,
12150
+ searchValue: shippingOptions.searchValue,
12151
+ onSearchValueChange: shippingOptions.onSearchValueChange,
12152
+ placeholder: "Select shipping option",
12153
+ ...field,
12154
+ disabled: !locationId || !shippingProfileId
12155
+ }
12156
+ ) }) })
12157
+ }
12158
+ )
12159
+ ] }) });
12160
+ }
12002
12161
  }
12003
12162
  );
12004
12163
  };
12005
- const ShippingProfileForm = ({
12006
- data,
12007
- order,
12008
- preview
12164
+ const CustomAmountField = ({
12165
+ control,
12166
+ currencyCode
12009
12167
  }) => {
12010
- var _a, _b, _c, _d, _e, _f;
12011
- const { setIsOpen } = useStackedModal();
12168
+ return /* @__PURE__ */ jsx(
12169
+ Form$2.Field,
12170
+ {
12171
+ control,
12172
+ name: "custom_amount",
12173
+ render: ({ field: { onChange, ...field } }) => {
12174
+ return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12175
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12176
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12177
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12178
+ ] }),
12179
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12180
+ CurrencyInput,
12181
+ {
12182
+ ...field,
12183
+ onValueChange: (value) => onChange(value),
12184
+ symbol: getNativeSymbol(currencyCode),
12185
+ code: currencyCode
12186
+ }
12187
+ ) })
12188
+ ] });
12189
+ }
12190
+ }
12191
+ );
12192
+ };
12193
+ const InlineTip = forwardRef(
12194
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
12195
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
12196
+ return /* @__PURE__ */ jsxs(
12197
+ "div",
12198
+ {
12199
+ ref,
12200
+ className: clx(
12201
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
12202
+ className
12203
+ ),
12204
+ ...props,
12205
+ children: [
12206
+ /* @__PURE__ */ jsx(
12207
+ "div",
12208
+ {
12209
+ role: "presentation",
12210
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
12211
+ "bg-ui-tag-orange-icon": variant === "warning"
12212
+ })
12213
+ }
12214
+ ),
12215
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
12216
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
12217
+ labelValue,
12218
+ ":"
12219
+ ] }),
12220
+ " ",
12221
+ children
12222
+ ] })
12223
+ ]
12224
+ }
12225
+ );
12226
+ }
12227
+ );
12228
+ InlineTip.displayName = "InlineTip";
12229
+ const MetadataFieldSchema = objectType({
12230
+ key: stringType(),
12231
+ disabled: booleanType().optional(),
12232
+ value: anyType()
12233
+ });
12234
+ const MetadataSchema = objectType({
12235
+ metadata: arrayType(MetadataFieldSchema)
12236
+ });
12237
+ const Metadata = () => {
12238
+ const { id } = useParams();
12239
+ const { order, isPending, isError, error } = useOrder(id, {
12240
+ fields: "metadata"
12241
+ });
12242
+ if (isError) {
12243
+ throw error;
12244
+ }
12245
+ const isReady = !isPending && !!order;
12246
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12247
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12248
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
12249
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
12250
+ ] }),
12251
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
12252
+ ] });
12253
+ };
12254
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
12255
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
12256
+ const MetadataForm = ({ orderId, metadata }) => {
12257
+ const { handleSuccess } = useRouteModal();
12258
+ const hasUneditableRows = getHasUneditableRows(metadata);
12259
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
12012
12260
  const form = useForm({
12013
- resolver: zodResolver(shippingMethodSchema),
12014
12261
  defaultValues: {
12015
- location_id: (_d = (_c = (_b = (_a = data.shippingOption) == null ? void 0 : _a.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.id,
12016
- shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
12017
- custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
12018
- }
12262
+ metadata: getDefaultValues(metadata)
12263
+ },
12264
+ resolver: zodResolver(MetadataSchema)
12019
12265
  });
12020
- const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
12021
- const {
12022
- mutateAsync: updateShippingMethod,
12023
- isPending: isUpdatingShippingMethod
12024
- } = useDraftOrderUpdateShippingMethod(order.id);
12025
- const onSubmit = form.handleSubmit(async (values) => {
12026
- if (isEqual(values, form.formState.defaultValues)) {
12027
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12028
- return;
12029
- }
12030
- if (data.shippingMethod) {
12031
- await updateShippingMethod(
12032
- {
12033
- method_id: data.shippingMethod.id,
12034
- shipping_option_id: values.shipping_option_id,
12035
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12036
- },
12037
- {
12038
- onError: (e) => {
12039
- toast.error(e.message);
12040
- },
12041
- onSuccess: () => {
12042
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12043
- }
12044
- }
12045
- );
12046
- return;
12047
- }
12048
- await addShippingMethod(
12049
- {
12050
- shipping_option_id: values.shipping_option_id,
12051
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12052
- },
12266
+ const handleSubmit = form.handleSubmit(async (data) => {
12267
+ const parsedData = parseValues(data);
12268
+ await mutateAsync(
12269
+ {
12270
+ metadata: parsedData
12271
+ },
12053
12272
  {
12054
- onError: (e) => {
12055
- toast.error(e.message);
12056
- },
12057
12273
  onSuccess: () => {
12058
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12274
+ toast.success("Metadata updated");
12275
+ handleSuccess();
12276
+ },
12277
+ onError: (error) => {
12278
+ toast.error(error.message);
12059
12279
  }
12060
12280
  }
12061
12281
  );
12062
12282
  });
12063
- return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
12283
+ const { fields, insert, remove } = useFieldArray({
12284
+ control: form.control,
12285
+ name: "metadata"
12286
+ });
12287
+ function deleteRow(index) {
12288
+ remove(index);
12289
+ if (fields.length === 1) {
12290
+ insert(0, {
12291
+ key: "",
12292
+ value: "",
12293
+ disabled: false
12294
+ });
12295
+ }
12296
+ }
12297
+ function insertRow(index, position) {
12298
+ insert(index + (position === "above" ? 0 : 1), {
12299
+ key: "",
12300
+ value: "",
12301
+ disabled: false
12302
+ });
12303
+ }
12304
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12064
12305
  KeyboundForm,
12065
12306
  {
12066
- className: "flex h-full flex-col overflow-hidden",
12067
- onSubmit,
12307
+ onSubmit: handleSubmit,
12308
+ className: "flex flex-1 flex-col overflow-hidden",
12068
12309
  children: [
12069
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12070
- /* @__PURE__ */ jsx(StackedFocusModal.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 px-6 py-16", children: [
12071
- /* @__PURE__ */ jsxs("div", { children: [
12072
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12073
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a shipping method for the selected shipping profile. You can see the items that will be shipped using this method in the preview below." }) })
12074
- ] }),
12075
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12076
- /* @__PURE__ */ jsx(
12077
- LocationField,
12078
- {
12079
- control: form.control,
12080
- setValue: form.setValue
12081
- }
12082
- ),
12083
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12084
- /* @__PURE__ */ jsx(
12085
- ShippingOptionField,
12086
- {
12087
- shippingProfileId: data.shippingProfileId,
12088
- preview,
12089
- control: form.control
12090
- }
12091
- ),
12092
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12093
- /* @__PURE__ */ jsx(
12094
- CustomAmountField,
12095
- {
12096
- control: form.control,
12097
- currencyCode: order.currency_code
12098
- }
12099
- ),
12100
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12101
- /* @__PURE__ */ jsx(
12102
- ItemsPreview,
12103
- {
12104
- order,
12105
- shippingProfileId: data.shippingProfileId
12106
- }
12107
- )
12108
- ] }) }) }),
12109
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12110
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12111
- /* @__PURE__ */ jsx(
12112
- Button,
12113
- {
12114
- size: "small",
12115
- type: "submit",
12116
- isLoading: isPending || isUpdatingShippingMethod,
12117
- children: data.shippingMethod ? "Update" : "Add"
12118
- }
12119
- )
12120
- ] }) })
12121
- ]
12122
- }
12123
- ) }) });
12124
- };
12125
- const shippingMethodSchema = objectType({
12126
- location_id: stringType(),
12127
- shipping_option_id: stringType(),
12128
- custom_amount: unionType([numberType(), stringType()]).optional()
12129
- });
12130
- const ItemsPreview = ({ order, shippingProfileId }) => {
12131
- const matches = order.items.filter(
12132
- (item) => {
12133
- var _a, _b, _c;
12134
- return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
12135
- }
12136
- );
12137
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12138
- /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12139
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12140
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12141
- ] }) }),
12142
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12143
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-muted grid grid-cols-2 gap-3 px-4 py-2", children: [
12144
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12145
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
12146
- ] }),
12147
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxs(
12148
- "div",
12149
- {
12150
- className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-2 items-center gap-3 rounded-lg px-4 py-2",
12151
- children: [
12152
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12153
- /* @__PURE__ */ jsx(
12154
- Thumbnail,
12155
- {
12156
- thumbnail: item.thumbnail,
12157
- alt: item.product_title ?? void 0
12158
- }
12159
- ),
12160
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12161
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12162
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12163
- /* @__PURE__ */ jsxs(
12164
- Text,
12165
- {
12166
- size: "small",
12167
- leading: "compact",
12168
- className: "text-ui-fg-subtle",
12169
- children: [
12170
- "(",
12171
- item.variant_title,
12172
- ")"
12173
- ]
12174
- }
12175
- )
12176
- ] }),
12177
- /* @__PURE__ */ jsx(
12178
- Text,
12179
- {
12180
- size: "small",
12181
- leading: "compact",
12182
- className: "text-ui-fg-subtle",
12183
- children: item.variant_sku
12184
- }
12185
- )
12186
- ] })
12310
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
12311
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
12312
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
12313
+ /* @__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" }) }),
12314
+ /* @__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" }) })
12187
12315
  ] }),
12188
- /* @__PURE__ */ jsxs(
12189
- Text,
12190
- {
12191
- size: "small",
12192
- leading: "compact",
12193
- className: "text-ui-fg-subtle",
12194
- children: [
12195
- item.quantity,
12196
- "x"
12197
- ]
12316
+ fields.map((field, index) => {
12317
+ const isDisabled = field.disabled || false;
12318
+ let placeholder = "-";
12319
+ if (typeof field.value === "object") {
12320
+ placeholder = "{ ... }";
12198
12321
  }
12199
- )
12200
- ]
12201
- },
12202
- item.id
12203
- )) : /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12204
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12205
- /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12206
- 'No items found for "',
12207
- query,
12208
- '".'
12209
- ] })
12210
- ] }) })
12211
- ] })
12212
- ] });
12213
- };
12214
- const LocationField = ({ control, setValue }) => {
12215
- const locations = useComboboxData({
12216
- queryKey: ["locations"],
12217
- queryFn: async (params) => {
12218
- return await sdk.admin.stockLocation.list(params);
12219
- },
12220
- getOptions: (data) => {
12221
- return data.stock_locations.map((location) => ({
12222
- label: location.name,
12223
- value: location.id
12224
- }));
12225
- }
12226
- });
12227
- return /* @__PURE__ */ jsx(
12228
- Form$2.Field,
12229
- {
12230
- control,
12231
- name: "location_id",
12232
- render: ({ field: { onChange, ...field } }) => {
12233
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12234
- /* @__PURE__ */ jsxs("div", { children: [
12235
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
12236
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12322
+ if (Array.isArray(field.value)) {
12323
+ placeholder = "[ ... ]";
12324
+ }
12325
+ return /* @__PURE__ */ jsx(
12326
+ ConditionalTooltip,
12327
+ {
12328
+ showTooltip: isDisabled,
12329
+ content: "This row is disabled because it contains non-primitive data.",
12330
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
12331
+ /* @__PURE__ */ jsxs(
12332
+ "div",
12333
+ {
12334
+ className: clx("grid grid-cols-2 divide-x", {
12335
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
12336
+ }),
12337
+ children: [
12338
+ /* @__PURE__ */ jsx(
12339
+ Form$2.Field,
12340
+ {
12341
+ control: form.control,
12342
+ name: `metadata.${index}.key`,
12343
+ render: ({ field: field2 }) => {
12344
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12345
+ GridInput,
12346
+ {
12347
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
12348
+ ...field2,
12349
+ disabled: isDisabled,
12350
+ placeholder: "Key"
12351
+ }
12352
+ ) }) });
12353
+ }
12354
+ }
12355
+ ),
12356
+ /* @__PURE__ */ jsx(
12357
+ Form$2.Field,
12358
+ {
12359
+ control: form.control,
12360
+ name: `metadata.${index}.value`,
12361
+ render: ({ field: { value, ...field2 } }) => {
12362
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12363
+ GridInput,
12364
+ {
12365
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
12366
+ ...field2,
12367
+ value: isDisabled ? placeholder : value,
12368
+ disabled: isDisabled,
12369
+ placeholder: "Value"
12370
+ }
12371
+ ) }) });
12372
+ }
12373
+ }
12374
+ )
12375
+ ]
12376
+ }
12377
+ ),
12378
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
12379
+ /* @__PURE__ */ jsx(
12380
+ DropdownMenu.Trigger,
12381
+ {
12382
+ className: clx(
12383
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
12384
+ {
12385
+ hidden: isDisabled
12386
+ }
12387
+ ),
12388
+ disabled: isDisabled,
12389
+ asChild: true,
12390
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
12391
+ }
12392
+ ),
12393
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
12394
+ /* @__PURE__ */ jsxs(
12395
+ DropdownMenu.Item,
12396
+ {
12397
+ className: "gap-x-2",
12398
+ onClick: () => insertRow(index, "above"),
12399
+ children: [
12400
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
12401
+ "Insert row above"
12402
+ ]
12403
+ }
12404
+ ),
12405
+ /* @__PURE__ */ jsxs(
12406
+ DropdownMenu.Item,
12407
+ {
12408
+ className: "gap-x-2",
12409
+ onClick: () => insertRow(index, "below"),
12410
+ children: [
12411
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
12412
+ "Insert row below"
12413
+ ]
12414
+ }
12415
+ ),
12416
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
12417
+ /* @__PURE__ */ jsxs(
12418
+ DropdownMenu.Item,
12419
+ {
12420
+ className: "gap-x-2",
12421
+ onClick: () => deleteRow(index),
12422
+ children: [
12423
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
12424
+ "Delete row"
12425
+ ]
12426
+ }
12427
+ )
12428
+ ] })
12429
+ ] })
12430
+ ] })
12431
+ },
12432
+ field.id
12433
+ );
12434
+ })
12237
12435
  ] }),
12238
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12239
- Combobox,
12240
- {
12241
- options: locations.options,
12242
- fetchNextPage: locations.fetchNextPage,
12243
- isFetchingNextPage: locations.isFetchingNextPage,
12244
- searchValue: locations.searchValue,
12245
- onSearchValueChange: locations.onSearchValueChange,
12246
- placeholder: "Select location",
12247
- onChange: (value) => {
12248
- setValue("shipping_option_id", "", {
12249
- shouldDirty: true,
12250
- shouldTouch: true
12251
- });
12252
- onChange(value);
12253
- },
12254
- ...field
12255
- }
12256
- ) })
12257
- ] }) });
12258
- }
12436
+ 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." })
12437
+ ] }),
12438
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12439
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12440
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12441
+ ] }) })
12442
+ ]
12259
12443
  }
12260
- );
12444
+ ) });
12261
12445
  };
12262
- const ShippingOptionField = ({
12263
- shippingProfileId,
12264
- preview,
12265
- control
12266
- }) => {
12267
- var _a;
12268
- const locationId = useWatch({ control, name: "location_id" });
12269
- const shippingOptions = useComboboxData({
12270
- queryKey: ["shipping_options", locationId, shippingProfileId],
12271
- queryFn: async (params) => {
12272
- return await sdk.admin.shippingOption.list({
12273
- ...params,
12274
- stock_location_id: locationId,
12275
- shipping_profile_id: shippingProfileId
12276
- });
12277
- },
12278
- getOptions: (data) => {
12279
- return data.shipping_options.map((option) => {
12280
- var _a2;
12281
- if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12282
- (r) => r.attribute === "is_return" && r.value === "true"
12283
- )) {
12284
- return void 0;
12285
- }
12286
- return {
12287
- label: option.name,
12288
- value: option.id
12289
- };
12290
- }).filter(Boolean);
12291
- },
12292
- enabled: !!locationId && !!shippingProfileId,
12293
- defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12294
- });
12295
- const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12446
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
12296
12447
  return /* @__PURE__ */ jsx(
12297
- Form$2.Field,
12448
+ "input",
12298
12449
  {
12299
- control,
12300
- name: "shipping_option_id",
12301
- render: ({ field }) => {
12302
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12303
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12304
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12305
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12306
- ] }),
12307
- /* @__PURE__ */ jsx(
12308
- ConditionalTooltip,
12309
- {
12310
- content: tooltipContent,
12311
- showTooltip: !locationId || !shippingProfileId,
12312
- children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12313
- Combobox,
12314
- {
12315
- options: shippingOptions.options,
12316
- fetchNextPage: shippingOptions.fetchNextPage,
12317
- isFetchingNextPage: shippingOptions.isFetchingNextPage,
12318
- searchValue: shippingOptions.searchValue,
12319
- onSearchValueChange: shippingOptions.onSearchValueChange,
12320
- placeholder: "Select shipping option",
12321
- ...field,
12322
- disabled: !locationId || !shippingProfileId
12323
- }
12324
- ) }) })
12325
- }
12326
- )
12327
- ] }) });
12328
- }
12450
+ ref,
12451
+ ...props,
12452
+ autoComplete: "off",
12453
+ className: clx(
12454
+ "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",
12455
+ className
12456
+ )
12329
12457
  }
12330
12458
  );
12459
+ });
12460
+ GridInput.displayName = "MetadataForm.GridInput";
12461
+ const PlaceholderInner = () => {
12462
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
12463
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
12464
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12465
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
12466
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
12467
+ ] }) })
12468
+ ] });
12331
12469
  };
12332
- const CustomAmountField = ({
12333
- control,
12334
- currencyCode
12335
- }) => {
12336
- return /* @__PURE__ */ jsx(
12337
- Form$2.Field,
12338
- {
12339
- control,
12340
- name: "custom_amount",
12341
- render: ({ field: { onChange, ...field } }) => {
12342
- return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12343
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12344
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12345
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12346
- ] }),
12347
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12348
- CurrencyInput,
12349
- {
12350
- ...field,
12351
- onValueChange: (value) => onChange(value),
12352
- symbol: getNativeSymbol(currencyCode),
12353
- code: currencyCode
12354
- }
12355
- ) })
12356
- ] });
12470
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
12471
+ function getDefaultValues(metadata) {
12472
+ if (!metadata || !Object.keys(metadata).length) {
12473
+ return [
12474
+ {
12475
+ key: "",
12476
+ value: "",
12477
+ disabled: false
12478
+ }
12479
+ ];
12480
+ }
12481
+ return Object.entries(metadata).map(([key, value]) => {
12482
+ if (!EDITABLE_TYPES.includes(typeof value)) {
12483
+ return {
12484
+ key,
12485
+ value,
12486
+ disabled: true
12487
+ };
12488
+ }
12489
+ let stringValue = value;
12490
+ if (typeof value !== "string") {
12491
+ stringValue = JSON.stringify(value);
12492
+ }
12493
+ return {
12494
+ key,
12495
+ value: stringValue,
12496
+ original_key: key
12497
+ };
12498
+ });
12499
+ }
12500
+ function parseValues(values) {
12501
+ const metadata = values.metadata;
12502
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
12503
+ if (isEmpty) {
12504
+ return null;
12505
+ }
12506
+ const update = {};
12507
+ metadata.forEach((field) => {
12508
+ let key = field.key;
12509
+ let value = field.value;
12510
+ const disabled = field.disabled;
12511
+ if (!key || !value) {
12512
+ return;
12513
+ }
12514
+ if (disabled) {
12515
+ update[key] = value;
12516
+ return;
12517
+ }
12518
+ key = key.trim();
12519
+ value = value.trim();
12520
+ if (value === "true") {
12521
+ update[key] = true;
12522
+ } else if (value === "false") {
12523
+ update[key] = false;
12524
+ } else {
12525
+ const parsedNumber = parseFloat(value);
12526
+ if (!isNaN(parsedNumber)) {
12527
+ update[key] = parsedNumber;
12528
+ } else {
12529
+ update[key] = value;
12357
12530
  }
12358
12531
  }
12359
- );
12360
- };
12361
- const ShippingAddress = () => {
12362
- const { id } = useParams();
12363
- const { order, isPending, isError, error } = useOrder(id, {
12364
- fields: "+shipping_address"
12365
12532
  });
12366
- if (isError) {
12367
- throw error;
12533
+ return update;
12534
+ }
12535
+ function getHasUneditableRows(metadata) {
12536
+ if (!metadata) {
12537
+ return false;
12368
12538
  }
12369
- const isReady = !isPending && !!order;
12539
+ return Object.values(metadata).some(
12540
+ (value) => !EDITABLE_TYPES.includes(typeof value)
12541
+ );
12542
+ }
12543
+ const CustomItems = () => {
12370
12544
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12371
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12372
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12373
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12374
- ] }),
12375
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12545
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
12546
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
12376
12547
  ] });
12377
12548
  };
12378
- const ShippingAddressForm = ({ order }) => {
12379
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12549
+ const CustomItemsForm = () => {
12380
12550
  const form = useForm({
12381
- defaultValues: {
12382
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12383
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12384
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12385
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12386
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12387
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12388
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12389
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12390
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12391
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12392
- },
12393
12551
  resolver: zodResolver(schema$1)
12394
12552
  });
12395
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12396
- const { handleSuccess } = useRouteModal();
12397
- const onSubmit = form.handleSubmit(async (data) => {
12398
- await mutateAsync(
12399
- {
12400
- shipping_address: {
12401
- first_name: data.first_name,
12402
- last_name: data.last_name,
12403
- company: data.company,
12404
- address_1: data.address_1,
12405
- address_2: data.address_2,
12406
- city: data.city,
12407
- province: data.province,
12408
- country_code: data.country_code,
12409
- postal_code: data.postal_code,
12410
- phone: data.phone
12411
- }
12412
- },
12413
- {
12414
- onSuccess: () => {
12415
- handleSuccess();
12416
- },
12417
- onError: (error) => {
12418
- toast.error(error.message);
12419
- }
12420
- }
12421
- );
12422
- });
12423
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12424
- KeyboundForm,
12425
- {
12426
- className: "flex flex-1 flex-col overflow-hidden",
12427
- onSubmit,
12428
- children: [
12429
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
12430
- /* @__PURE__ */ jsx(
12431
- Form$2.Field,
12432
- {
12433
- control: form.control,
12434
- name: "country_code",
12435
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12436
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12437
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12438
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12439
- ] })
12440
- }
12441
- ),
12442
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12443
- /* @__PURE__ */ jsx(
12444
- Form$2.Field,
12445
- {
12446
- control: form.control,
12447
- name: "first_name",
12448
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12449
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12450
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12451
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12452
- ] })
12453
- }
12454
- ),
12455
- /* @__PURE__ */ jsx(
12456
- Form$2.Field,
12457
- {
12458
- control: form.control,
12459
- name: "last_name",
12460
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12461
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12462
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12463
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12464
- ] })
12465
- }
12466
- )
12467
- ] }),
12468
- /* @__PURE__ */ jsx(
12469
- Form$2.Field,
12470
- {
12471
- control: form.control,
12472
- name: "company",
12473
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12474
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12475
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12476
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12477
- ] })
12478
- }
12479
- ),
12480
- /* @__PURE__ */ jsx(
12481
- Form$2.Field,
12482
- {
12483
- control: form.control,
12484
- name: "address_1",
12485
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12486
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12487
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12488
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12489
- ] })
12490
- }
12491
- ),
12492
- /* @__PURE__ */ jsx(
12493
- Form$2.Field,
12494
- {
12495
- control: form.control,
12496
- name: "address_2",
12497
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12498
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12499
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12500
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12501
- ] })
12502
- }
12503
- ),
12504
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12505
- /* @__PURE__ */ jsx(
12506
- Form$2.Field,
12507
- {
12508
- control: form.control,
12509
- name: "postal_code",
12510
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12511
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12512
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12513
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12514
- ] })
12515
- }
12516
- ),
12517
- /* @__PURE__ */ jsx(
12518
- Form$2.Field,
12519
- {
12520
- control: form.control,
12521
- name: "city",
12522
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12523
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12524
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12525
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12526
- ] })
12527
- }
12528
- )
12529
- ] }),
12530
- /* @__PURE__ */ jsx(
12531
- Form$2.Field,
12532
- {
12533
- control: form.control,
12534
- name: "province",
12535
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12536
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12537
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12538
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12539
- ] })
12540
- }
12541
- ),
12542
- /* @__PURE__ */ jsx(
12543
- Form$2.Field,
12544
- {
12545
- control: form.control,
12546
- name: "phone",
12547
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12548
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12549
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12550
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12551
- ] })
12552
- }
12553
- )
12554
- ] }) }),
12555
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12556
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12557
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12558
- ] }) })
12559
- ]
12560
- }
12561
- ) });
12553
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
12554
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
12555
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12556
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12557
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
12558
+ ] }) })
12559
+ ] }) });
12562
12560
  };
12563
- const schema$1 = addressSchema;
12561
+ const schema$1 = objectType({
12562
+ email: stringType().email()
12563
+ });
12564
12564
  const TransferOwnership = () => {
12565
12565
  const { id } = useParams();
12566
12566
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -13061,37 +13061,37 @@ const routeModule = {
13061
13061
  Component: BillingAddress,
13062
13062
  path: "/draft-orders/:id/billing-address"
13063
13063
  },
13064
- {
13065
- Component: CustomItems,
13066
- path: "/draft-orders/:id/custom-items"
13067
- },
13068
- {
13069
- Component: Email,
13070
- path: "/draft-orders/:id/email"
13071
- },
13072
13064
  {
13073
13065
  Component: Items,
13074
13066
  path: "/draft-orders/:id/items"
13075
13067
  },
13076
- {
13077
- Component: Metadata,
13078
- path: "/draft-orders/:id/metadata"
13079
- },
13080
13068
  {
13081
13069
  Component: Promotions,
13082
13070
  path: "/draft-orders/:id/promotions"
13083
13071
  },
13072
+ {
13073
+ Component: Email,
13074
+ path: "/draft-orders/:id/email"
13075
+ },
13084
13076
  {
13085
13077
  Component: SalesChannel,
13086
13078
  path: "/draft-orders/:id/sales-channel"
13087
13079
  },
13080
+ {
13081
+ Component: ShippingAddress,
13082
+ path: "/draft-orders/:id/shipping-address"
13083
+ },
13088
13084
  {
13089
13085
  Component: Shipping,
13090
13086
  path: "/draft-orders/:id/shipping"
13091
13087
  },
13092
13088
  {
13093
- Component: ShippingAddress,
13094
- path: "/draft-orders/:id/shipping-address"
13089
+ Component: Metadata,
13090
+ path: "/draft-orders/:id/metadata"
13091
+ },
13092
+ {
13093
+ Component: CustomItems,
13094
+ path: "/draft-orders/:id/custom-items"
13095
13095
  },
13096
13096
  {
13097
13097
  Component: TransferOwnership,
@@ -13107,7 +13107,9 @@ const menuItemModule = {
13107
13107
  label: config.label,
13108
13108
  icon: void 0,
13109
13109
  path: "/draft-orders",
13110
- nested: "/orders"
13110
+ nested: "/orders",
13111
+ rank: void 0,
13112
+ translationNs: void 0
13111
13113
  }
13112
13114
  ]
13113
13115
  };