@medusajs/draft-order 2.10.3-preview-20250916150159 → 2.10.3-preview-20250916180159

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.
@@ -11756,921 +11756,114 @@ const ShippingAddressForm = ({ order }) => {
11756
11756
  ) });
11757
11757
  };
11758
11758
  const schema$1 = addressSchema;
11759
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11760
- const Shipping = () => {
11761
- var _a;
11759
+ const TransferOwnership = () => {
11762
11760
  const { id } = useParams();
11763
- const { order, isPending, isError, error } = useOrder(id, {
11764
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11761
+ const { draft_order, isPending, isError, error } = useDraftOrder(id, {
11762
+ fields: "id,customer_id,customer.*"
11765
11763
  });
11766
- const {
11767
- order: preview,
11768
- isPending: isPreviewPending,
11769
- isError: isPreviewError,
11770
- error: previewError
11771
- } = useOrderPreview(id);
11772
- useInitiateOrderEdit({ preview });
11773
- const { onCancel } = useCancelOrderEdit({ preview });
11774
11764
  if (isError) {
11775
11765
  throw error;
11776
11766
  }
11777
- if (isPreviewError) {
11778
- throw previewError;
11779
- }
11780
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11781
- const isReady = preview && !isPreviewPending && order && !isPending;
11782
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11783
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11784
- /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11785
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11786
- /* @__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." }) })
11787
- ] }) }) }),
11788
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11789
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11790
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11791
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11792
- ] }) });
11767
+ const isReady = !isPending && !!draft_order;
11768
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11769
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11770
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
11771
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
11772
+ ] }),
11773
+ isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
11774
+ ] });
11793
11775
  };
11794
- const ShippingForm = ({ preview, order }) => {
11795
- var _a;
11796
- const { setIsOpen } = useStackedModal();
11797
- const [isSubmitting, setIsSubmitting] = useState(false);
11798
- const [data, setData] = useState(null);
11799
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11800
- const { shipping_options } = useShippingOptions(
11801
- {
11802
- id: appliedShippingOptionIds,
11803
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11776
+ const TransferOwnershipForm = ({ order }) => {
11777
+ var _a, _b;
11778
+ const form = useForm({
11779
+ defaultValues: {
11780
+ customer_id: order.customer_id || ""
11804
11781
  },
11805
- {
11806
- enabled: appliedShippingOptionIds.length > 0
11807
- }
11808
- );
11809
- const uniqueShippingProfiles = useMemo(() => {
11810
- const profiles = /* @__PURE__ */ new Map();
11811
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11812
- profiles.set(profile.id, profile);
11813
- });
11814
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11815
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11816
- });
11817
- return Array.from(profiles.values());
11818
- }, [order.items, shipping_options]);
11782
+ resolver: zodResolver(schema)
11783
+ });
11784
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11819
11785
  const { handleSuccess } = useRouteModal();
11820
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11821
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11822
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11823
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11824
- const onSubmit = async () => {
11825
- setIsSubmitting(true);
11826
- let requestSucceeded = false;
11827
- await requestOrderEdit(void 0, {
11828
- onError: (e) => {
11829
- toast.error(`Failed to request order edit: ${e.message}`);
11830
- },
11831
- onSuccess: () => {
11832
- requestSucceeded = true;
11833
- }
11834
- });
11835
- if (!requestSucceeded) {
11836
- setIsSubmitting(false);
11837
- return;
11838
- }
11839
- await confirmOrderEdit(void 0, {
11840
- onError: (e) => {
11841
- toast.error(`Failed to confirm order edit: ${e.message}`);
11842
- },
11843
- onSuccess: () => {
11844
- handleSuccess();
11845
- },
11846
- onSettled: () => {
11847
- setIsSubmitting(false);
11848
- }
11849
- });
11850
- };
11851
- const onKeydown = useCallback(
11852
- (e) => {
11853
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11854
- if (data || isSubmitting) {
11855
- return;
11786
+ const name = [(_a = order.customer) == null ? void 0 : _a.first_name, (_b = order.customer) == null ? void 0 : _b.last_name].filter(Boolean).join(" ");
11787
+ const currentCustomer = order.customer ? {
11788
+ label: name ? `${name} (${order.customer.email})` : order.customer.email,
11789
+ value: order.customer.id
11790
+ } : null;
11791
+ const onSubmit = form.handleSubmit(async (data) => {
11792
+ await mutateAsync(
11793
+ { customer_id: data.customer_id },
11794
+ {
11795
+ onSuccess: () => {
11796
+ toast.success("Customer updated");
11797
+ handleSuccess();
11798
+ },
11799
+ onError: (error) => {
11800
+ toast.error(error.message);
11856
11801
  }
11857
- onSubmit();
11858
11802
  }
11803
+ );
11804
+ });
11805
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11806
+ KeyboundForm,
11807
+ {
11808
+ className: "flex flex-1 flex-col overflow-hidden",
11809
+ onSubmit,
11810
+ children: [
11811
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: [
11812
+ /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center bg-ui-bg-component rounded-md border", children: /* @__PURE__ */ jsx(Illustration, {}) }),
11813
+ currentCustomer && /* @__PURE__ */ jsxs("div", { className: "flex flex-col space-y-3", children: [
11814
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11815
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "current-customer", children: "Current owner" }),
11816
+ /* @__PURE__ */ jsx(Hint$1, { children: "The customer that is currently associated with this draft order." })
11817
+ ] }),
11818
+ /* @__PURE__ */ jsxs(Select, { disabled: true, value: currentCustomer.value, children: [
11819
+ /* @__PURE__ */ jsx(Select.Trigger, { id: "current-customer", children: /* @__PURE__ */ jsx(Select.Value, {}) }),
11820
+ /* @__PURE__ */ jsx(Select.Content, { children: /* @__PURE__ */ jsx(Select.Item, { value: currentCustomer.value, children: currentCustomer.label }) })
11821
+ ] })
11822
+ ] }),
11823
+ /* @__PURE__ */ jsx(
11824
+ CustomerField,
11825
+ {
11826
+ control: form.control,
11827
+ currentCustomerId: order.customer_id
11828
+ }
11829
+ )
11830
+ ] }),
11831
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11832
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", children: "Cancel" }) }),
11833
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11834
+ ] }) })
11835
+ ]
11836
+ }
11837
+ ) });
11838
+ };
11839
+ const CustomerField = ({ control, currentCustomerId }) => {
11840
+ const customers = useComboboxData({
11841
+ queryFn: async (params) => {
11842
+ return await sdk.admin.customer.list({
11843
+ ...params,
11844
+ id: currentCustomerId ? { $nin: [currentCustomerId] } : void 0
11845
+ });
11859
11846
  },
11860
- [data, isSubmitting, onSubmit]
11861
- );
11862
- useEffect(() => {
11863
- document.addEventListener("keydown", onKeydown);
11864
- return () => {
11865
- document.removeEventListener("keydown", onKeydown);
11866
- };
11867
- }, [onKeydown]);
11868
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11869
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11870
- /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11871
- /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11872
- /* @__PURE__ */ jsxs("div", { children: [
11873
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11874
- /* @__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." }) })
11875
- ] }),
11876
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11877
- /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11878
- /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11879
- /* @__PURE__ */ jsx(
11880
- Text,
11881
- {
11882
- size: "xsmall",
11883
- weight: "plus",
11884
- className: "text-ui-fg-muted",
11885
- children: "Shipping profile"
11886
- }
11887
- ),
11888
- /* @__PURE__ */ jsx(
11889
- Text,
11890
- {
11891
- size: "xsmall",
11892
- weight: "plus",
11893
- className: "text-ui-fg-muted",
11894
- children: "Action"
11895
- }
11896
- )
11897
- ] }),
11898
- /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
11899
- var _a2, _b, _c, _d, _e, _f, _g;
11900
- const items = getItemsWithShippingProfile(
11901
- profile.id,
11902
- order.items
11903
- );
11904
- const hasItems = items.length > 0;
11905
- const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
11906
- (option) => option.shipping_profile_id === profile.id
11907
- );
11908
- const shippingMethod = preview.shipping_methods.find(
11909
- (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
11910
- );
11911
- const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
11912
- (action) => action.action === "SHIPPING_ADD"
11913
- );
11914
- return /* @__PURE__ */ jsxs(
11915
- Accordion.Item,
11916
- {
11917
- value: profile.id,
11918
- className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
11919
- children: [
11920
- /* @__PURE__ */ jsxs("div", { className: "px-3 py-2 flex items-center justify-between gap-3", children: [
11921
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full overflow-hidden", children: [
11922
- /* @__PURE__ */ jsx(Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(
11923
- IconButton,
11924
- {
11925
- size: "2xsmall",
11926
- variant: "transparent",
11927
- className: "group/trigger",
11928
- disabled: !hasItems,
11929
- children: /* @__PURE__ */ jsx(TriangleRightMini, { className: "group-data-[state=open]/trigger:rotate-90 transition-transform" })
11930
- }
11931
- ) }),
11932
- !shippingOption ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
11933
- /* @__PURE__ */ jsx("div", { className: "size-7 rounded-md shadow-borders-base flex items-center justify-center", children: /* @__PURE__ */ jsx("div", { className: "size-6 rounded bg-ui-bg-component-hover flex items-center justify-center", children: /* @__PURE__ */ jsx(Shopping, { className: "text-ui-fg-subtle" }) }) }),
11934
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col flex-1", children: [
11935
- /* @__PURE__ */ jsx(
11936
- Text,
11937
- {
11938
- size: "small",
11939
- weight: "plus",
11940
- leading: "compact",
11941
- children: profile.name
11942
- }
11943
- ),
11944
- /* @__PURE__ */ jsxs(
11945
- Text,
11946
- {
11947
- size: "small",
11948
- leading: "compact",
11949
- className: "text-ui-fg-subtle",
11950
- children: [
11951
- items.length,
11952
- " ",
11953
- pluralize(items.length, "items", "item")
11954
- ]
11955
- }
11956
- )
11957
- ] })
11958
- ] }) : /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-[5px] max-sm:flex-col max-sm:items-start flex-1 w-full overflow-hidden", children: [
11959
- /* @__PURE__ */ jsx(
11960
- Tooltip,
11961
- {
11962
- content: /* @__PURE__ */ jsx("ul", { children: items.map((item) => {
11963
- var _a3, _b2, _c2;
11964
- return /* @__PURE__ */ jsx(
11965
- "li",
11966
- {
11967
- children: `${item.quantity}x ${(_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title} (${(_c2 = item.variant) == null ? void 0 : _c2.title})`
11968
- },
11969
- item.id
11970
- );
11971
- }) }),
11972
- children: /* @__PURE__ */ jsxs(
11973
- Badge,
11974
- {
11975
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
11976
- size: "xsmall",
11977
- children: [
11978
- /* @__PURE__ */ jsx(Shopping, { className: "shrink-0" }),
11979
- /* @__PURE__ */ jsxs("span", { className: "truncate", children: [
11980
- items.reduce(
11981
- (acc, item) => acc + item.quantity,
11982
- 0
11983
- ),
11984
- "x",
11985
- " ",
11986
- pluralize(items.length, "items", "item")
11987
- ] })
11988
- ]
11989
- }
11990
- )
11991
- }
11992
- ),
11993
- /* @__PURE__ */ jsx(
11994
- Tooltip,
11995
- {
11996
- content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
11997
- children: /* @__PURE__ */ jsxs(
11998
- Badge,
11999
- {
12000
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
12001
- size: "xsmall",
12002
- children: [
12003
- /* @__PURE__ */ jsx(Buildings, { className: "shrink-0" }),
12004
- /* @__PURE__ */ jsx("span", { className: "truncate", children: (_g = (_f = (_e = shippingOption.service_zone) == null ? void 0 : _e.fulfillment_set) == null ? void 0 : _f.location) == null ? void 0 : _g.name })
12005
- ]
12006
- }
12007
- )
12008
- }
12009
- ),
12010
- /* @__PURE__ */ jsx(Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxs(
12011
- Badge,
12012
- {
12013
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
12014
- size: "xsmall",
12015
- children: [
12016
- /* @__PURE__ */ jsx(TruckFast, { className: "shrink-0" }),
12017
- /* @__PURE__ */ jsx("span", { className: "truncate", children: shippingOption.name })
12018
- ]
12019
- }
12020
- ) })
12021
- ] })
12022
- ] }),
12023
- shippingOption ? /* @__PURE__ */ jsx(
12024
- ActionMenu,
12025
- {
12026
- groups: [
12027
- {
12028
- actions: [
12029
- hasItems ? {
12030
- label: "Edit shipping option",
12031
- icon: /* @__PURE__ */ jsx(Channels, {}),
12032
- onClick: () => {
12033
- setIsOpen(
12034
- STACKED_FOCUS_MODAL_ID,
12035
- true
12036
- );
12037
- setData({
12038
- shippingProfileId: profile.id,
12039
- shippingOption,
12040
- shippingMethod
12041
- });
12042
- }
12043
- } : void 0,
12044
- {
12045
- label: "Remove shipping option",
12046
- icon: /* @__PURE__ */ jsx(Trash, {}),
12047
- onClick: () => {
12048
- if (shippingMethod) {
12049
- if (addShippingMethodAction) {
12050
- removeActionShippingMethod(
12051
- addShippingMethodAction.id
12052
- );
12053
- } else {
12054
- removeShippingMethod(
12055
- shippingMethod.id
12056
- );
12057
- }
12058
- }
12059
- }
12060
- }
12061
- ].filter(Boolean)
12062
- }
12063
- ]
12064
- }
12065
- ) : /* @__PURE__ */ jsx(
12066
- StackedModalTrigger,
12067
- {
12068
- shippingProfileId: profile.id,
12069
- shippingOption,
12070
- shippingMethod,
12071
- setData,
12072
- children: "Add shipping option"
12073
- }
12074
- )
12075
- ] }),
12076
- /* @__PURE__ */ jsxs(Accordion.Content, { children: [
12077
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12078
- items.map((item, idx) => {
12079
- var _a3, _b2, _c2, _d2, _e2;
12080
- return /* @__PURE__ */ jsxs("div", { children: [
12081
- /* @__PURE__ */ jsxs(
12082
- "div",
12083
- {
12084
- className: "px-3 flex items-center gap-x-3",
12085
- children: [
12086
- /* @__PURE__ */ jsx("div", { className: "w-5 h-[56px] flex flex-col justify-center items-center", children: /* @__PURE__ */ jsx(
12087
- Divider,
12088
- {
12089
- variant: "dashed",
12090
- orientation: "vertical"
12091
- }
12092
- ) }),
12093
- /* @__PURE__ */ jsxs("div", { className: "py-2 flex items-center gap-x-3", children: [
12094
- /* @__PURE__ */ jsx("div", { className: "size-7 flex items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxs(
12095
- Text,
12096
- {
12097
- size: "small",
12098
- leading: "compact",
12099
- className: "text-ui-fg-subtle",
12100
- children: [
12101
- item.quantity,
12102
- "x"
12103
- ]
12104
- }
12105
- ) }),
12106
- /* @__PURE__ */ jsx(Thumbnail, { thumbnail: item.thumbnail }),
12107
- /* @__PURE__ */ jsxs("div", { children: [
12108
- /* @__PURE__ */ jsxs(
12109
- Text,
12110
- {
12111
- size: "small",
12112
- leading: "compact",
12113
- weight: "plus",
12114
- children: [
12115
- (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
12116
- " (",
12117
- (_c2 = item.variant) == null ? void 0 : _c2.title,
12118
- ")"
12119
- ]
12120
- }
12121
- ),
12122
- /* @__PURE__ */ jsx(
12123
- Text,
12124
- {
12125
- size: "small",
12126
- leading: "compact",
12127
- className: "text-ui-fg-subtle",
12128
- children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
12129
- }
12130
- )
12131
- ] })
12132
- ] })
12133
- ]
12134
- },
12135
- item.id
12136
- ),
12137
- idx !== items.length - 1 && /* @__PURE__ */ jsx(Divider, { variant: "dashed" })
12138
- ] }, item.id);
12139
- })
12140
- ] })
12141
- ]
12142
- },
12143
- profile.id
12144
- );
12145
- }) })
12146
- ] }) })
12147
- ] }) }),
12148
- /* @__PURE__ */ jsx(
12149
- StackedFocusModal,
12150
- {
12151
- id: STACKED_FOCUS_MODAL_ID,
12152
- onOpenChangeCallback: (open) => {
12153
- if (!open) {
12154
- setData(null);
12155
- }
12156
- return open;
12157
- },
12158
- children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
12159
- }
12160
- )
12161
- ] }),
12162
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12163
- /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12164
- /* @__PURE__ */ jsx(
12165
- Button,
12166
- {
12167
- size: "small",
12168
- type: "button",
12169
- isLoading: isSubmitting,
12170
- onClick: onSubmit,
12171
- children: "Save"
12172
- }
12173
- )
12174
- ] }) })
12175
- ] });
12176
- };
12177
- const StackedModalTrigger = ({
12178
- shippingProfileId,
12179
- shippingOption,
12180
- shippingMethod,
12181
- setData,
12182
- children
12183
- }) => {
12184
- const { setIsOpen, getIsOpen } = useStackedModal();
12185
- const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
12186
- const onToggle = () => {
12187
- if (isOpen) {
12188
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12189
- setData(null);
12190
- } else {
12191
- setIsOpen(STACKED_FOCUS_MODAL_ID, true);
12192
- setData({
12193
- shippingProfileId,
12194
- shippingOption,
12195
- shippingMethod
12196
- });
12197
- }
12198
- };
12199
- return /* @__PURE__ */ jsx(
12200
- Button,
12201
- {
12202
- size: "small",
12203
- variant: "secondary",
12204
- onClick: onToggle,
12205
- className: "text-ui-fg-primary shrink-0",
12206
- children
12207
- }
12208
- );
12209
- };
12210
- const ShippingProfileForm = ({
12211
- data,
12212
- order,
12213
- preview
12214
- }) => {
12215
- var _a, _b, _c, _d, _e, _f;
12216
- const { setIsOpen } = useStackedModal();
12217
- const form = useForm({
12218
- resolver: zodResolver(shippingMethodSchema),
12219
- defaultValues: {
12220
- 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,
12221
- shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
12222
- custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
12223
- }
12224
- });
12225
- const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
12226
- const {
12227
- mutateAsync: updateShippingMethod,
12228
- isPending: isUpdatingShippingMethod
12229
- } = useDraftOrderUpdateShippingMethod(order.id);
12230
- const onSubmit = form.handleSubmit(async (values) => {
12231
- if (isEqual(values, form.formState.defaultValues)) {
12232
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12233
- return;
12234
- }
12235
- if (data.shippingMethod) {
12236
- await updateShippingMethod(
12237
- {
12238
- method_id: data.shippingMethod.id,
12239
- shipping_option_id: values.shipping_option_id,
12240
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12241
- },
12242
- {
12243
- onError: (e) => {
12244
- toast.error(e.message);
12245
- },
12246
- onSuccess: () => {
12247
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12248
- }
12249
- }
12250
- );
12251
- return;
12252
- }
12253
- await addShippingMethod(
12254
- {
12255
- shipping_option_id: values.shipping_option_id,
12256
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12257
- },
12258
- {
12259
- onError: (e) => {
12260
- toast.error(e.message);
12261
- },
12262
- onSuccess: () => {
12263
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12264
- }
12265
- }
12266
- );
12267
- });
12268
- return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
12269
- KeyboundForm,
12270
- {
12271
- className: "flex h-full flex-col overflow-hidden",
12272
- onSubmit,
12273
- children: [
12274
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12275
- /* @__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 py-16 px-6", children: [
12276
- /* @__PURE__ */ jsxs("div", { children: [
12277
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12278
- /* @__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." }) })
12279
- ] }),
12280
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12281
- /* @__PURE__ */ jsx(
12282
- LocationField,
12283
- {
12284
- control: form.control,
12285
- setValue: form.setValue
12286
- }
12287
- ),
12288
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12289
- /* @__PURE__ */ jsx(
12290
- ShippingOptionField,
12291
- {
12292
- shippingProfileId: data.shippingProfileId,
12293
- preview,
12294
- control: form.control
12295
- }
12296
- ),
12297
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12298
- /* @__PURE__ */ jsx(
12299
- CustomAmountField,
12300
- {
12301
- control: form.control,
12302
- currencyCode: order.currency_code
12303
- }
12304
- ),
12305
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12306
- /* @__PURE__ */ jsx(
12307
- ItemsPreview,
12308
- {
12309
- order,
12310
- shippingProfileId: data.shippingProfileId
12311
- }
12312
- )
12313
- ] }) }) }),
12314
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12315
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12316
- /* @__PURE__ */ jsx(
12317
- Button,
12318
- {
12319
- size: "small",
12320
- type: "submit",
12321
- isLoading: isPending || isUpdatingShippingMethod,
12322
- children: data.shippingMethod ? "Update" : "Add"
12323
- }
12324
- )
12325
- ] }) })
12326
- ]
12327
- }
12328
- ) }) });
12329
- };
12330
- const shippingMethodSchema = objectType({
12331
- location_id: stringType(),
12332
- shipping_option_id: stringType(),
12333
- custom_amount: unionType([numberType(), stringType()]).optional()
12334
- });
12335
- const ItemsPreview = ({ order, shippingProfileId }) => {
12336
- const matches = order.items.filter(
12337
- (item) => {
12338
- var _a, _b, _c;
12339
- return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
12340
- }
12341
- );
12342
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12343
- /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12344
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12345
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12346
- ] }) }),
12347
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12348
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
12349
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12350
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
12351
- ] }),
12352
- /* @__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(
12353
- "div",
12354
- {
12355
- className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
12356
- children: [
12357
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12358
- /* @__PURE__ */ jsx(
12359
- Thumbnail,
12360
- {
12361
- thumbnail: item.thumbnail,
12362
- alt: item.product_title ?? void 0
12363
- }
12364
- ),
12365
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12366
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12367
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12368
- /* @__PURE__ */ jsxs(
12369
- Text,
12370
- {
12371
- size: "small",
12372
- leading: "compact",
12373
- className: "text-ui-fg-subtle",
12374
- children: [
12375
- "(",
12376
- item.variant_title,
12377
- ")"
12378
- ]
12379
- }
12380
- )
12381
- ] }),
12382
- /* @__PURE__ */ jsx(
12383
- Text,
12384
- {
12385
- size: "small",
12386
- leading: "compact",
12387
- className: "text-ui-fg-subtle",
12388
- children: item.variant_sku
12389
- }
12390
- )
12391
- ] })
12392
- ] }),
12393
- /* @__PURE__ */ jsxs(
12394
- Text,
12395
- {
12396
- size: "small",
12397
- leading: "compact",
12398
- className: "text-ui-fg-subtle",
12399
- children: [
12400
- item.quantity,
12401
- "x"
12402
- ]
12403
- }
12404
- )
12405
- ]
12406
- },
12407
- item.id
12408
- )) : /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-x-3 bg-ui-bg-base rounded-lg p-4 shadow-elevation-card-rest flex-col gap-1", children: [
12409
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12410
- /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12411
- 'No items found for "',
12412
- query,
12413
- '".'
12414
- ] })
12415
- ] }) })
12416
- ] })
12417
- ] });
12418
- };
12419
- const LocationField = ({ control, setValue }) => {
12420
- const locations = useComboboxData({
12421
- queryKey: ["locations"],
12422
- queryFn: async (params) => {
12423
- return await sdk.admin.stockLocation.list(params);
12424
- },
12425
- getOptions: (data) => {
12426
- return data.stock_locations.map((location) => ({
12427
- label: location.name,
12428
- value: location.id
12429
- }));
12430
- }
12431
- });
12432
- return /* @__PURE__ */ jsx(
12433
- Form$2.Field,
12434
- {
12435
- control,
12436
- name: "location_id",
12437
- render: ({ field: { onChange, ...field } }) => {
12438
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12439
- /* @__PURE__ */ jsxs("div", { children: [
12440
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
12441
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12442
- ] }),
12443
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12444
- Combobox,
12445
- {
12446
- options: locations.options,
12447
- fetchNextPage: locations.fetchNextPage,
12448
- isFetchingNextPage: locations.isFetchingNextPage,
12449
- searchValue: locations.searchValue,
12450
- onSearchValueChange: locations.onSearchValueChange,
12451
- placeholder: "Select location",
12452
- onChange: (value) => {
12453
- setValue("shipping_option_id", "", {
12454
- shouldDirty: true,
12455
- shouldTouch: true
12456
- });
12457
- onChange(value);
12458
- },
12459
- ...field
12460
- }
12461
- ) })
12462
- ] }) });
12463
- }
12464
- }
12465
- );
12466
- };
12467
- const ShippingOptionField = ({
12468
- shippingProfileId,
12469
- preview,
12470
- control
12471
- }) => {
12472
- var _a;
12473
- const locationId = useWatch({ control, name: "location_id" });
12474
- const shippingOptions = useComboboxData({
12475
- queryKey: ["shipping_options", locationId, shippingProfileId],
12476
- queryFn: async (params) => {
12477
- return await sdk.admin.shippingOption.list({
12478
- ...params,
12479
- stock_location_id: locationId,
12480
- shipping_profile_id: shippingProfileId
12481
- });
12482
- },
12483
- getOptions: (data) => {
12484
- return data.shipping_options.map((option) => {
12485
- var _a2;
12486
- if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12487
- (r) => r.attribute === "is_return" && r.value === "true"
12488
- )) {
12489
- return void 0;
12490
- }
12491
- return {
12492
- label: option.name,
12493
- value: option.id
12494
- };
12495
- }).filter(Boolean);
12496
- },
12497
- enabled: !!locationId && !!shippingProfileId,
12498
- defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12499
- });
12500
- const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12501
- return /* @__PURE__ */ jsx(
12502
- Form$2.Field,
12503
- {
12504
- control,
12505
- name: "shipping_option_id",
12506
- render: ({ field }) => {
12507
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12508
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12509
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12510
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12511
- ] }),
12512
- /* @__PURE__ */ jsx(
12513
- ConditionalTooltip,
12514
- {
12515
- content: tooltipContent,
12516
- showTooltip: !locationId || !shippingProfileId,
12517
- children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12518
- Combobox,
12519
- {
12520
- options: shippingOptions.options,
12521
- fetchNextPage: shippingOptions.fetchNextPage,
12522
- isFetchingNextPage: shippingOptions.isFetchingNextPage,
12523
- searchValue: shippingOptions.searchValue,
12524
- onSearchValueChange: shippingOptions.onSearchValueChange,
12525
- placeholder: "Select shipping option",
12526
- ...field,
12527
- disabled: !locationId || !shippingProfileId
12528
- }
12529
- ) }) })
12530
- }
12531
- )
12532
- ] }) });
12533
- }
12534
- }
12535
- );
12536
- };
12537
- const CustomAmountField = ({
12538
- control,
12539
- currencyCode
12540
- }) => {
12541
- return /* @__PURE__ */ jsx(
12542
- Form$2.Field,
12543
- {
12544
- control,
12545
- name: "custom_amount",
12546
- render: ({ field: { onChange, ...field } }) => {
12547
- return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12548
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12549
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12550
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12551
- ] }),
12552
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12553
- CurrencyInput,
12554
- {
12555
- ...field,
12556
- onValueChange: (value) => onChange(value),
12557
- symbol: getNativeSymbol(currencyCode),
12558
- code: currencyCode
12559
- }
12560
- ) })
12561
- ] });
12562
- }
12563
- }
12564
- );
12565
- };
12566
- const TransferOwnership = () => {
12567
- const { id } = useParams();
12568
- const { draft_order, isPending, isError, error } = useDraftOrder(id, {
12569
- fields: "id,customer_id,customer.*"
12570
- });
12571
- if (isError) {
12572
- throw error;
12573
- }
12574
- const isReady = !isPending && !!draft_order;
12575
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12576
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12577
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
12578
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
12579
- ] }),
12580
- isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
12581
- ] });
12582
- };
12583
- const TransferOwnershipForm = ({ order }) => {
12584
- var _a, _b;
12585
- const form = useForm({
12586
- defaultValues: {
12587
- customer_id: order.customer_id || ""
12588
- },
12589
- resolver: zodResolver(schema)
12590
- });
12591
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12592
- const { handleSuccess } = useRouteModal();
12593
- const name = [(_a = order.customer) == null ? void 0 : _a.first_name, (_b = order.customer) == null ? void 0 : _b.last_name].filter(Boolean).join(" ");
12594
- const currentCustomer = order.customer ? {
12595
- label: name ? `${name} (${order.customer.email})` : order.customer.email,
12596
- value: order.customer.id
12597
- } : null;
12598
- const onSubmit = form.handleSubmit(async (data) => {
12599
- await mutateAsync(
12600
- { customer_id: data.customer_id },
12601
- {
12602
- onSuccess: () => {
12603
- toast.success("Customer updated");
12604
- handleSuccess();
12605
- },
12606
- onError: (error) => {
12607
- toast.error(error.message);
12608
- }
12609
- }
12610
- );
12611
- });
12612
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12613
- KeyboundForm,
12614
- {
12615
- className: "flex flex-1 flex-col overflow-hidden",
12616
- onSubmit,
12617
- children: [
12618
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: [
12619
- /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center bg-ui-bg-component rounded-md border", children: /* @__PURE__ */ jsx(Illustration, {}) }),
12620
- currentCustomer && /* @__PURE__ */ jsxs("div", { className: "flex flex-col space-y-3", children: [
12621
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12622
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "current-customer", children: "Current owner" }),
12623
- /* @__PURE__ */ jsx(Hint$1, { children: "The customer that is currently associated with this draft order." })
12624
- ] }),
12625
- /* @__PURE__ */ jsxs(Select, { disabled: true, value: currentCustomer.value, children: [
12626
- /* @__PURE__ */ jsx(Select.Trigger, { id: "current-customer", children: /* @__PURE__ */ jsx(Select.Value, {}) }),
12627
- /* @__PURE__ */ jsx(Select.Content, { children: /* @__PURE__ */ jsx(Select.Item, { value: currentCustomer.value, children: currentCustomer.label }) })
12628
- ] })
12629
- ] }),
12630
- /* @__PURE__ */ jsx(
12631
- CustomerField,
12632
- {
12633
- control: form.control,
12634
- currentCustomerId: order.customer_id
12635
- }
12636
- )
12637
- ] }),
12638
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12639
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", children: "Cancel" }) }),
12640
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12641
- ] }) })
12642
- ]
12643
- }
12644
- ) });
12645
- };
12646
- const CustomerField = ({ control, currentCustomerId }) => {
12647
- const customers = useComboboxData({
12648
- queryFn: async (params) => {
12649
- return await sdk.admin.customer.list({
12650
- ...params,
12651
- id: currentCustomerId ? { $nin: [currentCustomerId] } : void 0
12652
- });
12653
- },
12654
- queryKey: ["customers"],
12655
- getOptions: (data) => {
12656
- return data.customers.map((customer) => {
12657
- const name = [customer.first_name, customer.last_name].filter(Boolean).join(" ");
12658
- return {
12659
- label: name ? `${name} (${customer.email})` : customer.email,
12660
- value: customer.id
12661
- };
12662
- });
12663
- }
12664
- });
12665
- return /* @__PURE__ */ jsx(
12666
- Form$2.Field,
12667
- {
12668
- name: "customer_id",
12669
- control,
12670
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { className: "space-y-3", children: [
12671
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12672
- /* @__PURE__ */ jsx(Form$2.Label, { children: "New customer" }),
12673
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "The customer to transfer this draft order to." })
11847
+ queryKey: ["customers"],
11848
+ getOptions: (data) => {
11849
+ return data.customers.map((customer) => {
11850
+ const name = [customer.first_name, customer.last_name].filter(Boolean).join(" ");
11851
+ return {
11852
+ label: name ? `${name} (${customer.email})` : customer.email,
11853
+ value: customer.id
11854
+ };
11855
+ });
11856
+ }
11857
+ });
11858
+ return /* @__PURE__ */ jsx(
11859
+ Form$2.Field,
11860
+ {
11861
+ name: "customer_id",
11862
+ control,
11863
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { className: "space-y-3", children: [
11864
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11865
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "New customer" }),
11866
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "The customer to transfer this draft order to." })
12674
11867
  ] }),
12675
11868
  /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12676
11869
  Combobox,
@@ -12975,70 +12168,877 @@ const Illustration = () => {
12975
12168
  strokeLinecap: "round",
12976
12169
  strokeLinejoin: "round"
12977
12170
  }
12978
- ) }),
12979
- /* @__PURE__ */ jsxs("defs", { children: [
12980
- /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
12981
- "rect",
12171
+ ) }),
12172
+ /* @__PURE__ */ jsxs("defs", { children: [
12173
+ /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
12174
+ "rect",
12175
+ {
12176
+ width: "12",
12177
+ height: "12",
12178
+ fill: "white",
12179
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12180
+ }
12181
+ ) }),
12182
+ /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
12183
+ "rect",
12184
+ {
12185
+ width: "12",
12186
+ height: "12",
12187
+ fill: "white",
12188
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12189
+ }
12190
+ ) }),
12191
+ /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
12192
+ "rect",
12193
+ {
12194
+ width: "12",
12195
+ height: "12",
12196
+ fill: "white",
12197
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12198
+ }
12199
+ ) }),
12200
+ /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
12201
+ "rect",
12202
+ {
12203
+ width: "12",
12204
+ height: "12",
12205
+ fill: "white",
12206
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12207
+ }
12208
+ ) }),
12209
+ /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
12210
+ "rect",
12211
+ {
12212
+ width: "12",
12213
+ height: "12",
12214
+ fill: "white",
12215
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12216
+ }
12217
+ ) }),
12218
+ /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
12219
+ "rect",
12220
+ {
12221
+ width: "12",
12222
+ height: "12",
12223
+ fill: "white",
12224
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12225
+ }
12226
+ ) })
12227
+ ] })
12228
+ ]
12229
+ }
12230
+ );
12231
+ };
12232
+ const schema = objectType({
12233
+ customer_id: stringType().min(1)
12234
+ });
12235
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
12236
+ const Shipping = () => {
12237
+ var _a;
12238
+ const { id } = useParams();
12239
+ const { order, isPending, isError, error } = useOrder(id, {
12240
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
12241
+ });
12242
+ const {
12243
+ order: preview,
12244
+ isPending: isPreviewPending,
12245
+ isError: isPreviewError,
12246
+ error: previewError
12247
+ } = useOrderPreview(id);
12248
+ useInitiateOrderEdit({ preview });
12249
+ const { onCancel } = useCancelOrderEdit({ preview });
12250
+ if (isError) {
12251
+ throw error;
12252
+ }
12253
+ if (isPreviewError) {
12254
+ throw previewError;
12255
+ }
12256
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
12257
+ const isReady = preview && !isPreviewPending && order && !isPending;
12258
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
12259
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
12260
+ /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
12261
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12262
+ /* @__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." }) })
12263
+ ] }) }) }),
12264
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
12265
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
12266
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
12267
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
12268
+ ] }) });
12269
+ };
12270
+ const ShippingForm = ({ preview, order }) => {
12271
+ var _a;
12272
+ const { setIsOpen } = useStackedModal();
12273
+ const [isSubmitting, setIsSubmitting] = useState(false);
12274
+ const [data, setData] = useState(null);
12275
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
12276
+ const { shipping_options } = useShippingOptions(
12277
+ {
12278
+ id: appliedShippingOptionIds,
12279
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
12280
+ },
12281
+ {
12282
+ enabled: appliedShippingOptionIds.length > 0
12283
+ }
12284
+ );
12285
+ const uniqueShippingProfiles = useMemo(() => {
12286
+ const profiles = /* @__PURE__ */ new Map();
12287
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
12288
+ profiles.set(profile.id, profile);
12289
+ });
12290
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
12291
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
12292
+ });
12293
+ return Array.from(profiles.values());
12294
+ }, [order.items, shipping_options]);
12295
+ const { handleSuccess } = useRouteModal();
12296
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12297
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
12298
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
12299
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
12300
+ const onSubmit = async () => {
12301
+ setIsSubmitting(true);
12302
+ let requestSucceeded = false;
12303
+ await requestOrderEdit(void 0, {
12304
+ onError: (e) => {
12305
+ toast.error(`Failed to request order edit: ${e.message}`);
12306
+ },
12307
+ onSuccess: () => {
12308
+ requestSucceeded = true;
12309
+ }
12310
+ });
12311
+ if (!requestSucceeded) {
12312
+ setIsSubmitting(false);
12313
+ return;
12314
+ }
12315
+ await confirmOrderEdit(void 0, {
12316
+ onError: (e) => {
12317
+ toast.error(`Failed to confirm order edit: ${e.message}`);
12318
+ },
12319
+ onSuccess: () => {
12320
+ handleSuccess();
12321
+ },
12322
+ onSettled: () => {
12323
+ setIsSubmitting(false);
12324
+ }
12325
+ });
12326
+ };
12327
+ const onKeydown = useCallback(
12328
+ (e) => {
12329
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
12330
+ if (data || isSubmitting) {
12331
+ return;
12332
+ }
12333
+ onSubmit();
12334
+ }
12335
+ },
12336
+ [data, isSubmitting, onSubmit]
12337
+ );
12338
+ useEffect(() => {
12339
+ document.addEventListener("keydown", onKeydown);
12340
+ return () => {
12341
+ document.removeEventListener("keydown", onKeydown);
12342
+ };
12343
+ }, [onKeydown]);
12344
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
12345
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
12346
+ /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
12347
+ /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
12348
+ /* @__PURE__ */ jsxs("div", { children: [
12349
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12350
+ /* @__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." }) })
12351
+ ] }),
12352
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12353
+ /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
12354
+ /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
12355
+ /* @__PURE__ */ jsx(
12356
+ Text,
12357
+ {
12358
+ size: "xsmall",
12359
+ weight: "plus",
12360
+ className: "text-ui-fg-muted",
12361
+ children: "Shipping profile"
12362
+ }
12363
+ ),
12364
+ /* @__PURE__ */ jsx(
12365
+ Text,
12366
+ {
12367
+ size: "xsmall",
12368
+ weight: "plus",
12369
+ className: "text-ui-fg-muted",
12370
+ children: "Action"
12371
+ }
12372
+ )
12373
+ ] }),
12374
+ /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
12375
+ var _a2, _b, _c, _d, _e, _f, _g;
12376
+ const items = getItemsWithShippingProfile(
12377
+ profile.id,
12378
+ order.items
12379
+ );
12380
+ const hasItems = items.length > 0;
12381
+ const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
12382
+ (option) => option.shipping_profile_id === profile.id
12383
+ );
12384
+ const shippingMethod = preview.shipping_methods.find(
12385
+ (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
12386
+ );
12387
+ const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
12388
+ (action) => action.action === "SHIPPING_ADD"
12389
+ );
12390
+ return /* @__PURE__ */ jsxs(
12391
+ Accordion.Item,
12392
+ {
12393
+ value: profile.id,
12394
+ className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
12395
+ children: [
12396
+ /* @__PURE__ */ jsxs("div", { className: "px-3 py-2 flex items-center justify-between gap-3", children: [
12397
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full overflow-hidden", children: [
12398
+ /* @__PURE__ */ jsx(Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(
12399
+ IconButton,
12400
+ {
12401
+ size: "2xsmall",
12402
+ variant: "transparent",
12403
+ className: "group/trigger",
12404
+ disabled: !hasItems,
12405
+ children: /* @__PURE__ */ jsx(TriangleRightMini, { className: "group-data-[state=open]/trigger:rotate-90 transition-transform" })
12406
+ }
12407
+ ) }),
12408
+ !shippingOption ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12409
+ /* @__PURE__ */ jsx("div", { className: "size-7 rounded-md shadow-borders-base flex items-center justify-center", children: /* @__PURE__ */ jsx("div", { className: "size-6 rounded bg-ui-bg-component-hover flex items-center justify-center", children: /* @__PURE__ */ jsx(Shopping, { className: "text-ui-fg-subtle" }) }) }),
12410
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col flex-1", children: [
12411
+ /* @__PURE__ */ jsx(
12412
+ Text,
12413
+ {
12414
+ size: "small",
12415
+ weight: "plus",
12416
+ leading: "compact",
12417
+ children: profile.name
12418
+ }
12419
+ ),
12420
+ /* @__PURE__ */ jsxs(
12421
+ Text,
12422
+ {
12423
+ size: "small",
12424
+ leading: "compact",
12425
+ className: "text-ui-fg-subtle",
12426
+ children: [
12427
+ items.length,
12428
+ " ",
12429
+ pluralize(items.length, "items", "item")
12430
+ ]
12431
+ }
12432
+ )
12433
+ ] })
12434
+ ] }) : /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-[5px] max-sm:flex-col max-sm:items-start flex-1 w-full overflow-hidden", children: [
12435
+ /* @__PURE__ */ jsx(
12436
+ Tooltip,
12437
+ {
12438
+ content: /* @__PURE__ */ jsx("ul", { children: items.map((item) => {
12439
+ var _a3, _b2, _c2;
12440
+ return /* @__PURE__ */ jsx(
12441
+ "li",
12442
+ {
12443
+ children: `${item.quantity}x ${(_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title} (${(_c2 = item.variant) == null ? void 0 : _c2.title})`
12444
+ },
12445
+ item.id
12446
+ );
12447
+ }) }),
12448
+ children: /* @__PURE__ */ jsxs(
12449
+ Badge,
12450
+ {
12451
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
12452
+ size: "xsmall",
12453
+ children: [
12454
+ /* @__PURE__ */ jsx(Shopping, { className: "shrink-0" }),
12455
+ /* @__PURE__ */ jsxs("span", { className: "truncate", children: [
12456
+ items.reduce(
12457
+ (acc, item) => acc + item.quantity,
12458
+ 0
12459
+ ),
12460
+ "x",
12461
+ " ",
12462
+ pluralize(items.length, "items", "item")
12463
+ ] })
12464
+ ]
12465
+ }
12466
+ )
12467
+ }
12468
+ ),
12469
+ /* @__PURE__ */ jsx(
12470
+ Tooltip,
12471
+ {
12472
+ content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
12473
+ children: /* @__PURE__ */ jsxs(
12474
+ Badge,
12475
+ {
12476
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
12477
+ size: "xsmall",
12478
+ children: [
12479
+ /* @__PURE__ */ jsx(Buildings, { className: "shrink-0" }),
12480
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: (_g = (_f = (_e = shippingOption.service_zone) == null ? void 0 : _e.fulfillment_set) == null ? void 0 : _f.location) == null ? void 0 : _g.name })
12481
+ ]
12482
+ }
12483
+ )
12484
+ }
12485
+ ),
12486
+ /* @__PURE__ */ jsx(Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxs(
12487
+ Badge,
12488
+ {
12489
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
12490
+ size: "xsmall",
12491
+ children: [
12492
+ /* @__PURE__ */ jsx(TruckFast, { className: "shrink-0" }),
12493
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: shippingOption.name })
12494
+ ]
12495
+ }
12496
+ ) })
12497
+ ] })
12498
+ ] }),
12499
+ shippingOption ? /* @__PURE__ */ jsx(
12500
+ ActionMenu,
12501
+ {
12502
+ groups: [
12503
+ {
12504
+ actions: [
12505
+ hasItems ? {
12506
+ label: "Edit shipping option",
12507
+ icon: /* @__PURE__ */ jsx(Channels, {}),
12508
+ onClick: () => {
12509
+ setIsOpen(
12510
+ STACKED_FOCUS_MODAL_ID,
12511
+ true
12512
+ );
12513
+ setData({
12514
+ shippingProfileId: profile.id,
12515
+ shippingOption,
12516
+ shippingMethod
12517
+ });
12518
+ }
12519
+ } : void 0,
12520
+ {
12521
+ label: "Remove shipping option",
12522
+ icon: /* @__PURE__ */ jsx(Trash, {}),
12523
+ onClick: () => {
12524
+ if (shippingMethod) {
12525
+ if (addShippingMethodAction) {
12526
+ removeActionShippingMethod(
12527
+ addShippingMethodAction.id
12528
+ );
12529
+ } else {
12530
+ removeShippingMethod(
12531
+ shippingMethod.id
12532
+ );
12533
+ }
12534
+ }
12535
+ }
12536
+ }
12537
+ ].filter(Boolean)
12538
+ }
12539
+ ]
12540
+ }
12541
+ ) : /* @__PURE__ */ jsx(
12542
+ StackedModalTrigger,
12543
+ {
12544
+ shippingProfileId: profile.id,
12545
+ shippingOption,
12546
+ shippingMethod,
12547
+ setData,
12548
+ children: "Add shipping option"
12549
+ }
12550
+ )
12551
+ ] }),
12552
+ /* @__PURE__ */ jsxs(Accordion.Content, { children: [
12553
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12554
+ items.map((item, idx) => {
12555
+ var _a3, _b2, _c2, _d2, _e2;
12556
+ return /* @__PURE__ */ jsxs("div", { children: [
12557
+ /* @__PURE__ */ jsxs(
12558
+ "div",
12559
+ {
12560
+ className: "px-3 flex items-center gap-x-3",
12561
+ children: [
12562
+ /* @__PURE__ */ jsx("div", { className: "w-5 h-[56px] flex flex-col justify-center items-center", children: /* @__PURE__ */ jsx(
12563
+ Divider,
12564
+ {
12565
+ variant: "dashed",
12566
+ orientation: "vertical"
12567
+ }
12568
+ ) }),
12569
+ /* @__PURE__ */ jsxs("div", { className: "py-2 flex items-center gap-x-3", children: [
12570
+ /* @__PURE__ */ jsx("div", { className: "size-7 flex items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxs(
12571
+ Text,
12572
+ {
12573
+ size: "small",
12574
+ leading: "compact",
12575
+ className: "text-ui-fg-subtle",
12576
+ children: [
12577
+ item.quantity,
12578
+ "x"
12579
+ ]
12580
+ }
12581
+ ) }),
12582
+ /* @__PURE__ */ jsx(Thumbnail, { thumbnail: item.thumbnail }),
12583
+ /* @__PURE__ */ jsxs("div", { children: [
12584
+ /* @__PURE__ */ jsxs(
12585
+ Text,
12586
+ {
12587
+ size: "small",
12588
+ leading: "compact",
12589
+ weight: "plus",
12590
+ children: [
12591
+ (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
12592
+ " (",
12593
+ (_c2 = item.variant) == null ? void 0 : _c2.title,
12594
+ ")"
12595
+ ]
12596
+ }
12597
+ ),
12598
+ /* @__PURE__ */ jsx(
12599
+ Text,
12600
+ {
12601
+ size: "small",
12602
+ leading: "compact",
12603
+ className: "text-ui-fg-subtle",
12604
+ children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
12605
+ }
12606
+ )
12607
+ ] })
12608
+ ] })
12609
+ ]
12610
+ },
12611
+ item.id
12612
+ ),
12613
+ idx !== items.length - 1 && /* @__PURE__ */ jsx(Divider, { variant: "dashed" })
12614
+ ] }, item.id);
12615
+ })
12616
+ ] })
12617
+ ]
12618
+ },
12619
+ profile.id
12620
+ );
12621
+ }) })
12622
+ ] }) })
12623
+ ] }) }),
12624
+ /* @__PURE__ */ jsx(
12625
+ StackedFocusModal,
12626
+ {
12627
+ id: STACKED_FOCUS_MODAL_ID,
12628
+ onOpenChangeCallback: (open) => {
12629
+ if (!open) {
12630
+ setData(null);
12631
+ }
12632
+ return open;
12633
+ },
12634
+ children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
12635
+ }
12636
+ )
12637
+ ] }),
12638
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12639
+ /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12640
+ /* @__PURE__ */ jsx(
12641
+ Button,
12642
+ {
12643
+ size: "small",
12644
+ type: "button",
12645
+ isLoading: isSubmitting,
12646
+ onClick: onSubmit,
12647
+ children: "Save"
12648
+ }
12649
+ )
12650
+ ] }) })
12651
+ ] });
12652
+ };
12653
+ const StackedModalTrigger = ({
12654
+ shippingProfileId,
12655
+ shippingOption,
12656
+ shippingMethod,
12657
+ setData,
12658
+ children
12659
+ }) => {
12660
+ const { setIsOpen, getIsOpen } = useStackedModal();
12661
+ const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
12662
+ const onToggle = () => {
12663
+ if (isOpen) {
12664
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12665
+ setData(null);
12666
+ } else {
12667
+ setIsOpen(STACKED_FOCUS_MODAL_ID, true);
12668
+ setData({
12669
+ shippingProfileId,
12670
+ shippingOption,
12671
+ shippingMethod
12672
+ });
12673
+ }
12674
+ };
12675
+ return /* @__PURE__ */ jsx(
12676
+ Button,
12677
+ {
12678
+ size: "small",
12679
+ variant: "secondary",
12680
+ onClick: onToggle,
12681
+ className: "text-ui-fg-primary shrink-0",
12682
+ children
12683
+ }
12684
+ );
12685
+ };
12686
+ const ShippingProfileForm = ({
12687
+ data,
12688
+ order,
12689
+ preview
12690
+ }) => {
12691
+ var _a, _b, _c, _d, _e, _f;
12692
+ const { setIsOpen } = useStackedModal();
12693
+ const form = useForm({
12694
+ resolver: zodResolver(shippingMethodSchema),
12695
+ defaultValues: {
12696
+ 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,
12697
+ shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
12698
+ custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
12699
+ }
12700
+ });
12701
+ const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
12702
+ const {
12703
+ mutateAsync: updateShippingMethod,
12704
+ isPending: isUpdatingShippingMethod
12705
+ } = useDraftOrderUpdateShippingMethod(order.id);
12706
+ const onSubmit = form.handleSubmit(async (values) => {
12707
+ if (isEqual(values, form.formState.defaultValues)) {
12708
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12709
+ return;
12710
+ }
12711
+ if (data.shippingMethod) {
12712
+ await updateShippingMethod(
12713
+ {
12714
+ method_id: data.shippingMethod.id,
12715
+ shipping_option_id: values.shipping_option_id,
12716
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12717
+ },
12718
+ {
12719
+ onError: (e) => {
12720
+ toast.error(e.message);
12721
+ },
12722
+ onSuccess: () => {
12723
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12724
+ }
12725
+ }
12726
+ );
12727
+ return;
12728
+ }
12729
+ await addShippingMethod(
12730
+ {
12731
+ shipping_option_id: values.shipping_option_id,
12732
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12733
+ },
12734
+ {
12735
+ onError: (e) => {
12736
+ toast.error(e.message);
12737
+ },
12738
+ onSuccess: () => {
12739
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12740
+ }
12741
+ }
12742
+ );
12743
+ });
12744
+ return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
12745
+ KeyboundForm,
12746
+ {
12747
+ className: "flex h-full flex-col overflow-hidden",
12748
+ onSubmit,
12749
+ children: [
12750
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12751
+ /* @__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 py-16 px-6", children: [
12752
+ /* @__PURE__ */ jsxs("div", { children: [
12753
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12754
+ /* @__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." }) })
12755
+ ] }),
12756
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12757
+ /* @__PURE__ */ jsx(
12758
+ LocationField,
12982
12759
  {
12983
- width: "12",
12984
- height: "12",
12985
- fill: "white",
12986
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12760
+ control: form.control,
12761
+ setValue: form.setValue
12987
12762
  }
12988
- ) }),
12989
- /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
12990
- "rect",
12763
+ ),
12764
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12765
+ /* @__PURE__ */ jsx(
12766
+ ShippingOptionField,
12991
12767
  {
12992
- width: "12",
12993
- height: "12",
12994
- fill: "white",
12995
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12768
+ shippingProfileId: data.shippingProfileId,
12769
+ preview,
12770
+ control: form.control
12996
12771
  }
12997
- ) }),
12998
- /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
12999
- "rect",
12772
+ ),
12773
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12774
+ /* @__PURE__ */ jsx(
12775
+ CustomAmountField,
13000
12776
  {
13001
- width: "12",
13002
- height: "12",
13003
- fill: "white",
13004
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12777
+ control: form.control,
12778
+ currencyCode: order.currency_code
13005
12779
  }
13006
- ) }),
13007
- /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
13008
- "rect",
12780
+ ),
12781
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12782
+ /* @__PURE__ */ jsx(
12783
+ ItemsPreview,
12784
+ {
12785
+ order,
12786
+ shippingProfileId: data.shippingProfileId
12787
+ }
12788
+ )
12789
+ ] }) }) }),
12790
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12791
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12792
+ /* @__PURE__ */ jsx(
12793
+ Button,
12794
+ {
12795
+ size: "small",
12796
+ type: "submit",
12797
+ isLoading: isPending || isUpdatingShippingMethod,
12798
+ children: data.shippingMethod ? "Update" : "Add"
12799
+ }
12800
+ )
12801
+ ] }) })
12802
+ ]
12803
+ }
12804
+ ) }) });
12805
+ };
12806
+ const shippingMethodSchema = objectType({
12807
+ location_id: stringType(),
12808
+ shipping_option_id: stringType(),
12809
+ custom_amount: unionType([numberType(), stringType()]).optional()
12810
+ });
12811
+ const ItemsPreview = ({ order, shippingProfileId }) => {
12812
+ const matches = order.items.filter(
12813
+ (item) => {
12814
+ var _a, _b, _c;
12815
+ return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
12816
+ }
12817
+ );
12818
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12819
+ /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12820
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12821
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12822
+ ] }) }),
12823
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12824
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
12825
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12826
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
12827
+ ] }),
12828
+ /* @__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(
12829
+ "div",
12830
+ {
12831
+ className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
12832
+ children: [
12833
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12834
+ /* @__PURE__ */ jsx(
12835
+ Thumbnail,
12836
+ {
12837
+ thumbnail: item.thumbnail,
12838
+ alt: item.product_title ?? void 0
12839
+ }
12840
+ ),
12841
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12842
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12843
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12844
+ /* @__PURE__ */ jsxs(
12845
+ Text,
12846
+ {
12847
+ size: "small",
12848
+ leading: "compact",
12849
+ className: "text-ui-fg-subtle",
12850
+ children: [
12851
+ "(",
12852
+ item.variant_title,
12853
+ ")"
12854
+ ]
12855
+ }
12856
+ )
12857
+ ] }),
12858
+ /* @__PURE__ */ jsx(
12859
+ Text,
12860
+ {
12861
+ size: "small",
12862
+ leading: "compact",
12863
+ className: "text-ui-fg-subtle",
12864
+ children: item.variant_sku
12865
+ }
12866
+ )
12867
+ ] })
12868
+ ] }),
12869
+ /* @__PURE__ */ jsxs(
12870
+ Text,
12871
+ {
12872
+ size: "small",
12873
+ leading: "compact",
12874
+ className: "text-ui-fg-subtle",
12875
+ children: [
12876
+ item.quantity,
12877
+ "x"
12878
+ ]
12879
+ }
12880
+ )
12881
+ ]
12882
+ },
12883
+ item.id
12884
+ )) : /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-x-3 bg-ui-bg-base rounded-lg p-4 shadow-elevation-card-rest flex-col gap-1", children: [
12885
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12886
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12887
+ 'No items found for "',
12888
+ query,
12889
+ '".'
12890
+ ] })
12891
+ ] }) })
12892
+ ] })
12893
+ ] });
12894
+ };
12895
+ const LocationField = ({ control, setValue }) => {
12896
+ const locations = useComboboxData({
12897
+ queryKey: ["locations"],
12898
+ queryFn: async (params) => {
12899
+ return await sdk.admin.stockLocation.list(params);
12900
+ },
12901
+ getOptions: (data) => {
12902
+ return data.stock_locations.map((location) => ({
12903
+ label: location.name,
12904
+ value: location.id
12905
+ }));
12906
+ }
12907
+ });
12908
+ return /* @__PURE__ */ jsx(
12909
+ Form$2.Field,
12910
+ {
12911
+ control,
12912
+ name: "location_id",
12913
+ render: ({ field: { onChange, ...field } }) => {
12914
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12915
+ /* @__PURE__ */ jsxs("div", { children: [
12916
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
12917
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12918
+ ] }),
12919
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12920
+ Combobox,
13009
12921
  {
13010
- width: "12",
13011
- height: "12",
13012
- fill: "white",
13013
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12922
+ options: locations.options,
12923
+ fetchNextPage: locations.fetchNextPage,
12924
+ isFetchingNextPage: locations.isFetchingNextPage,
12925
+ searchValue: locations.searchValue,
12926
+ onSearchValueChange: locations.onSearchValueChange,
12927
+ placeholder: "Select location",
12928
+ onChange: (value) => {
12929
+ setValue("shipping_option_id", "", {
12930
+ shouldDirty: true,
12931
+ shouldTouch: true
12932
+ });
12933
+ onChange(value);
12934
+ },
12935
+ ...field
13014
12936
  }
13015
- ) }),
13016
- /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
13017
- "rect",
12937
+ ) })
12938
+ ] }) });
12939
+ }
12940
+ }
12941
+ );
12942
+ };
12943
+ const ShippingOptionField = ({
12944
+ shippingProfileId,
12945
+ preview,
12946
+ control
12947
+ }) => {
12948
+ var _a;
12949
+ const locationId = useWatch({ control, name: "location_id" });
12950
+ const shippingOptions = useComboboxData({
12951
+ queryKey: ["shipping_options", locationId, shippingProfileId],
12952
+ queryFn: async (params) => {
12953
+ return await sdk.admin.shippingOption.list({
12954
+ ...params,
12955
+ stock_location_id: locationId,
12956
+ shipping_profile_id: shippingProfileId
12957
+ });
12958
+ },
12959
+ getOptions: (data) => {
12960
+ return data.shipping_options.map((option) => {
12961
+ var _a2;
12962
+ if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12963
+ (r) => r.attribute === "is_return" && r.value === "true"
12964
+ )) {
12965
+ return void 0;
12966
+ }
12967
+ return {
12968
+ label: option.name,
12969
+ value: option.id
12970
+ };
12971
+ }).filter(Boolean);
12972
+ },
12973
+ enabled: !!locationId && !!shippingProfileId,
12974
+ defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12975
+ });
12976
+ const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12977
+ return /* @__PURE__ */ jsx(
12978
+ Form$2.Field,
12979
+ {
12980
+ control,
12981
+ name: "shipping_option_id",
12982
+ render: ({ field }) => {
12983
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12984
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12985
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12986
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12987
+ ] }),
12988
+ /* @__PURE__ */ jsx(
12989
+ ConditionalTooltip,
13018
12990
  {
13019
- width: "12",
13020
- height: "12",
13021
- fill: "white",
13022
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12991
+ content: tooltipContent,
12992
+ showTooltip: !locationId || !shippingProfileId,
12993
+ children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12994
+ Combobox,
12995
+ {
12996
+ options: shippingOptions.options,
12997
+ fetchNextPage: shippingOptions.fetchNextPage,
12998
+ isFetchingNextPage: shippingOptions.isFetchingNextPage,
12999
+ searchValue: shippingOptions.searchValue,
13000
+ onSearchValueChange: shippingOptions.onSearchValueChange,
13001
+ placeholder: "Select shipping option",
13002
+ ...field,
13003
+ disabled: !locationId || !shippingProfileId
13004
+ }
13005
+ ) }) })
13023
13006
  }
13024
- ) }),
13025
- /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
13026
- "rect",
13007
+ )
13008
+ ] }) });
13009
+ }
13010
+ }
13011
+ );
13012
+ };
13013
+ const CustomAmountField = ({
13014
+ control,
13015
+ currencyCode
13016
+ }) => {
13017
+ return /* @__PURE__ */ jsx(
13018
+ Form$2.Field,
13019
+ {
13020
+ control,
13021
+ name: "custom_amount",
13022
+ render: ({ field: { onChange, ...field } }) => {
13023
+ return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
13024
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
13025
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
13026
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
13027
+ ] }),
13028
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
13029
+ CurrencyInput,
13027
13030
  {
13028
- width: "12",
13029
- height: "12",
13030
- fill: "white",
13031
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
13031
+ ...field,
13032
+ onValueChange: (value) => onChange(value),
13033
+ symbol: getNativeSymbol(currencyCode),
13034
+ code: currencyCode
13032
13035
  }
13033
13036
  ) })
13034
- ] })
13035
- ]
13037
+ ] });
13038
+ }
13036
13039
  }
13037
13040
  );
13038
13041
  };
13039
- const schema = objectType({
13040
- customer_id: stringType().min(1)
13041
- });
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
13044
13044
  routes: [
@@ -13091,13 +13091,13 @@ const routeModule = {
13091
13091
  Component: ShippingAddress,
13092
13092
  path: "/draft-orders/:id/shipping-address"
13093
13093
  },
13094
- {
13095
- Component: Shipping,
13096
- path: "/draft-orders/:id/shipping"
13097
- },
13098
13094
  {
13099
13095
  Component: TransferOwnership,
13100
13096
  path: "/draft-orders/:id/transfer-ownership"
13097
+ },
13098
+ {
13099
+ Component: Shipping,
13100
+ path: "/draft-orders/:id/shipping"
13101
13101
  }
13102
13102
  ]
13103
13103
  }