@medusajs/draft-order 2.11.4-preview-20251106180145 → 2.11.4-preview-20251106210130

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