@medusajs/draft-order 2.11.0-preview-20251016120206 → 2.11.0-preview-20251016180154

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