@medusajs/draft-order 2.10.4-snapshot-20250922062130 → 2.10.4-snapshot-20250922165717

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.
@@ -9567,6 +9567,95 @@ const ID = () => {
9567
9567
  /* @__PURE__ */ jsx(Outlet, {})
9568
9568
  ] });
9569
9569
  };
9570
+ const CustomItems = () => {
9571
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9572
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9573
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
9574
+ ] });
9575
+ };
9576
+ const CustomItemsForm = () => {
9577
+ const form = useForm({
9578
+ resolver: zodResolver(schema$5)
9579
+ });
9580
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9581
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9582
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9583
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9584
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9585
+ ] }) })
9586
+ ] }) });
9587
+ };
9588
+ const schema$5 = objectType({
9589
+ email: stringType().email()
9590
+ });
9591
+ const Email = () => {
9592
+ const { id } = useParams();
9593
+ const { order, isPending, isError, error } = useOrder(id, {
9594
+ fields: "+email"
9595
+ });
9596
+ if (isError) {
9597
+ throw error;
9598
+ }
9599
+ const isReady = !isPending && !!order;
9600
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9601
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9602
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
9603
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9604
+ ] }),
9605
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
9606
+ ] });
9607
+ };
9608
+ const EmailForm = ({ order }) => {
9609
+ const form = useForm({
9610
+ defaultValues: {
9611
+ email: order.email ?? ""
9612
+ },
9613
+ resolver: zodResolver(schema$4)
9614
+ });
9615
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9616
+ const { handleSuccess } = useRouteModal();
9617
+ const onSubmit = form.handleSubmit(async (data) => {
9618
+ await mutateAsync(
9619
+ { email: data.email },
9620
+ {
9621
+ onSuccess: () => {
9622
+ handleSuccess();
9623
+ },
9624
+ onError: (error) => {
9625
+ toast.error(error.message);
9626
+ }
9627
+ }
9628
+ );
9629
+ });
9630
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9631
+ KeyboundForm,
9632
+ {
9633
+ className: "flex flex-1 flex-col overflow-hidden",
9634
+ onSubmit,
9635
+ children: [
9636
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
9637
+ Form$2.Field,
9638
+ {
9639
+ control: form.control,
9640
+ name: "email",
9641
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9642
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
9643
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9644
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9645
+ ] })
9646
+ }
9647
+ ) }),
9648
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9649
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9650
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9651
+ ] }) })
9652
+ ]
9653
+ }
9654
+ ) });
9655
+ };
9656
+ const schema$4 = objectType({
9657
+ email: stringType().email()
9658
+ });
9570
9659
  const BillingAddress = () => {
9571
9660
  const { id } = useParams();
9572
9661
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9599,7 +9688,7 @@ const BillingAddressForm = ({ order }) => {
9599
9688
  postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9600
9689
  phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9601
9690
  },
9602
- resolver: zodResolver(schema$5)
9691
+ resolver: zodResolver(schema$3)
9603
9692
  });
9604
9693
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9605
9694
  const { handleSuccess } = useRouteModal();
@@ -9756,96 +9845,7 @@ const BillingAddressForm = ({ order }) => {
9756
9845
  }
9757
9846
  ) });
9758
9847
  };
9759
- const schema$5 = addressSchema;
9760
- const CustomItems = () => {
9761
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9762
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9763
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9764
- ] });
9765
- };
9766
- const CustomItemsForm = () => {
9767
- const form = useForm({
9768
- resolver: zodResolver(schema$4)
9769
- });
9770
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9771
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9772
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9773
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9774
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9775
- ] }) })
9776
- ] }) });
9777
- };
9778
- const schema$4 = objectType({
9779
- email: stringType().email()
9780
- });
9781
- const Email = () => {
9782
- const { id } = useParams();
9783
- const { order, isPending, isError, error } = useOrder(id, {
9784
- fields: "+email"
9785
- });
9786
- if (isError) {
9787
- throw error;
9788
- }
9789
- const isReady = !isPending && !!order;
9790
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9791
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9792
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
9793
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9794
- ] }),
9795
- isReady && /* @__PURE__ */ jsx(EmailForm, { order })
9796
- ] });
9797
- };
9798
- const EmailForm = ({ order }) => {
9799
- const form = useForm({
9800
- defaultValues: {
9801
- email: order.email ?? ""
9802
- },
9803
- resolver: zodResolver(schema$3)
9804
- });
9805
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9806
- const { handleSuccess } = useRouteModal();
9807
- const onSubmit = form.handleSubmit(async (data) => {
9808
- await mutateAsync(
9809
- { email: data.email },
9810
- {
9811
- onSuccess: () => {
9812
- handleSuccess();
9813
- },
9814
- onError: (error) => {
9815
- toast.error(error.message);
9816
- }
9817
- }
9818
- );
9819
- });
9820
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9821
- KeyboundForm,
9822
- {
9823
- className: "flex flex-1 flex-col overflow-hidden",
9824
- onSubmit,
9825
- children: [
9826
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
9827
- Form$2.Field,
9828
- {
9829
- control: form.control,
9830
- name: "email",
9831
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9832
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
9833
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9834
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9835
- ] })
9836
- }
9837
- ) }),
9838
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9839
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9840
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9841
- ] }) })
9842
- ]
9843
- }
9844
- ) });
9845
- };
9846
- const schema$3 = objectType({
9847
- email: stringType().email()
9848
- });
9848
+ const schema$3 = addressSchema;
9849
9849
  const NumberInput = forwardRef(
9850
9850
  ({
9851
9851
  value,
@@ -11447,1122 +11447,1122 @@ function getPromotionIds(items, shippingMethods) {
11447
11447
  }
11448
11448
  return Array.from(promotionIds);
11449
11449
  }
11450
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11451
- const Shipping = () => {
11452
- var _a;
11450
+ const SalesChannel = () => {
11453
11451
  const { id } = useParams();
11454
- const { order, isPending, isError, error } = useOrder(id, {
11455
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11456
- });
11457
- const {
11458
- order: preview,
11459
- isPending: isPreviewPending,
11460
- isError: isPreviewError,
11461
- error: previewError
11462
- } = useOrderPreview(id);
11463
- useInitiateOrderEdit({ preview });
11464
- const { onCancel } = useCancelOrderEdit({ preview });
11465
- if (isError) {
11466
- throw error;
11467
- }
11468
- if (isPreviewError) {
11469
- throw previewError;
11470
- }
11471
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11472
- const isReady = preview && !isPreviewPending && order && !isPending;
11473
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11474
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11475
- /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11476
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11477
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11478
- ] }) }) }),
11479
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11480
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11481
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11482
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11483
- ] }) });
11484
- };
11485
- const ShippingForm = ({ preview, order }) => {
11486
- var _a;
11487
- const { setIsOpen } = useStackedModal();
11488
- const [isSubmitting, setIsSubmitting] = useState(false);
11489
- const [data, setData] = useState(null);
11490
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11491
- const { shipping_options } = useShippingOptions(
11452
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11453
+ id,
11492
11454
  {
11493
- id: appliedShippingOptionIds,
11494
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11455
+ fields: "+sales_channel_id"
11495
11456
  },
11496
11457
  {
11497
- enabled: appliedShippingOptionIds.length > 0
11458
+ enabled: !!id
11498
11459
  }
11499
11460
  );
11500
- const uniqueShippingProfiles = useMemo(() => {
11501
- const profiles = /* @__PURE__ */ new Map();
11502
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11503
- profiles.set(profile.id, profile);
11504
- });
11505
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11506
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11507
- });
11508
- return Array.from(profiles.values());
11509
- }, [order.items, shipping_options]);
11510
- const { handleSuccess } = useRouteModal();
11511
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11512
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11513
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11514
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11515
- const onSubmit = async () => {
11516
- setIsSubmitting(true);
11517
- let requestSucceeded = false;
11518
- await requestOrderEdit(void 0, {
11519
- onError: (e) => {
11520
- toast.error(`Failed to request order edit: ${e.message}`);
11521
- },
11522
- onSuccess: () => {
11523
- requestSucceeded = true;
11524
- }
11525
- });
11526
- if (!requestSucceeded) {
11527
- setIsSubmitting(false);
11528
- return;
11529
- }
11530
- await confirmOrderEdit(void 0, {
11531
- onError: (e) => {
11532
- toast.error(`Failed to confirm order edit: ${e.message}`);
11533
- },
11534
- onSuccess: () => {
11535
- handleSuccess();
11461
+ if (isError) {
11462
+ throw error;
11463
+ }
11464
+ const ISrEADY = !!draft_order && !isPending;
11465
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11466
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11467
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11468
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11469
+ ] }),
11470
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11471
+ ] });
11472
+ };
11473
+ const SalesChannelForm = ({ order }) => {
11474
+ const form = useForm({
11475
+ defaultValues: {
11476
+ sales_channel_id: order.sales_channel_id || ""
11477
+ },
11478
+ resolver: zodResolver(schema$2)
11479
+ });
11480
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11481
+ const { handleSuccess } = useRouteModal();
11482
+ const onSubmit = form.handleSubmit(async (data) => {
11483
+ await mutateAsync(
11484
+ {
11485
+ sales_channel_id: data.sales_channel_id
11536
11486
  },
11537
- onSettled: () => {
11538
- setIsSubmitting(false);
11539
- }
11540
- });
11541
- };
11542
- const onKeydown = useCallback(
11543
- (e) => {
11544
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11545
- if (data || isSubmitting) {
11546
- return;
11487
+ {
11488
+ onSuccess: () => {
11489
+ toast.success("Sales channel updated");
11490
+ handleSuccess();
11491
+ },
11492
+ onError: (error) => {
11493
+ toast.error(error.message);
11547
11494
  }
11548
- onSubmit();
11549
11495
  }
11496
+ );
11497
+ });
11498
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11499
+ KeyboundForm,
11500
+ {
11501
+ className: "flex flex-1 flex-col overflow-hidden",
11502
+ onSubmit,
11503
+ children: [
11504
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11505
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11506
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11507
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11508
+ ] }) })
11509
+ ]
11510
+ }
11511
+ ) });
11512
+ };
11513
+ const SalesChannelField = ({ control, order }) => {
11514
+ const salesChannels = useComboboxData({
11515
+ queryFn: async (params) => {
11516
+ return await sdk.admin.salesChannel.list(params);
11550
11517
  },
11551
- [data, isSubmitting, onSubmit]
11518
+ queryKey: ["sales-channels"],
11519
+ getOptions: (data) => {
11520
+ return data.sales_channels.map((salesChannel) => ({
11521
+ label: salesChannel.name,
11522
+ value: salesChannel.id
11523
+ }));
11524
+ },
11525
+ defaultValue: order.sales_channel_id || void 0
11526
+ });
11527
+ return /* @__PURE__ */ jsx(
11528
+ Form$2.Field,
11529
+ {
11530
+ control,
11531
+ name: "sales_channel_id",
11532
+ render: ({ field }) => {
11533
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11534
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11535
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11536
+ Combobox,
11537
+ {
11538
+ options: salesChannels.options,
11539
+ fetchNextPage: salesChannels.fetchNextPage,
11540
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11541
+ searchValue: salesChannels.searchValue,
11542
+ onSearchValueChange: salesChannels.onSearchValueChange,
11543
+ placeholder: "Select sales channel",
11544
+ ...field
11545
+ }
11546
+ ) }),
11547
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11548
+ ] });
11549
+ }
11550
+ }
11552
11551
  );
11553
- useEffect(() => {
11554
- document.addEventListener("keydown", onKeydown);
11555
- return () => {
11556
- document.removeEventListener("keydown", onKeydown);
11557
- };
11558
- }, [onKeydown]);
11559
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11560
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11561
- /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11562
- /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11563
- /* @__PURE__ */ jsxs("div", { children: [
11564
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11565
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
11566
- ] }),
11567
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11568
- /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11569
- /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11552
+ };
11553
+ const schema$2 = objectType({
11554
+ sales_channel_id: stringType().min(1)
11555
+ });
11556
+ const ShippingAddress = () => {
11557
+ const { id } = useParams();
11558
+ const { order, isPending, isError, error } = useOrder(id, {
11559
+ fields: "+shipping_address"
11560
+ });
11561
+ if (isError) {
11562
+ throw error;
11563
+ }
11564
+ const isReady = !isPending && !!order;
11565
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11566
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11567
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
11568
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11569
+ ] }),
11570
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
11571
+ ] });
11572
+ };
11573
+ const ShippingAddressForm = ({ order }) => {
11574
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11575
+ const form = useForm({
11576
+ defaultValues: {
11577
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11578
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11579
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11580
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11581
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11582
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11583
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11584
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11585
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11586
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11587
+ },
11588
+ resolver: zodResolver(schema$1)
11589
+ });
11590
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11591
+ const { handleSuccess } = useRouteModal();
11592
+ const onSubmit = form.handleSubmit(async (data) => {
11593
+ await mutateAsync(
11594
+ {
11595
+ shipping_address: {
11596
+ first_name: data.first_name,
11597
+ last_name: data.last_name,
11598
+ company: data.company,
11599
+ address_1: data.address_1,
11600
+ address_2: data.address_2,
11601
+ city: data.city,
11602
+ province: data.province,
11603
+ country_code: data.country_code,
11604
+ postal_code: data.postal_code,
11605
+ phone: data.phone
11606
+ }
11607
+ },
11608
+ {
11609
+ onSuccess: () => {
11610
+ handleSuccess();
11611
+ },
11612
+ onError: (error) => {
11613
+ toast.error(error.message);
11614
+ }
11615
+ }
11616
+ );
11617
+ });
11618
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11619
+ KeyboundForm,
11620
+ {
11621
+ className: "flex flex-1 flex-col overflow-hidden",
11622
+ onSubmit,
11623
+ children: [
11624
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
11625
+ /* @__PURE__ */ jsx(
11626
+ Form$2.Field,
11627
+ {
11628
+ control: form.control,
11629
+ name: "country_code",
11630
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11631
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
11632
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
11633
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11634
+ ] })
11635
+ }
11636
+ ),
11637
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11570
11638
  /* @__PURE__ */ jsx(
11571
- Text,
11639
+ Form$2.Field,
11572
11640
  {
11573
- size: "xsmall",
11574
- weight: "plus",
11575
- className: "text-ui-fg-muted",
11576
- children: "Shipping profile"
11641
+ control: form.control,
11642
+ name: "first_name",
11643
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11644
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
11645
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11646
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11647
+ ] })
11577
11648
  }
11578
11649
  ),
11579
11650
  /* @__PURE__ */ jsx(
11580
- Text,
11651
+ Form$2.Field,
11581
11652
  {
11582
- size: "xsmall",
11583
- weight: "plus",
11584
- className: "text-ui-fg-muted",
11585
- children: "Action"
11653
+ control: form.control,
11654
+ name: "last_name",
11655
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11656
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
11657
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11658
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11659
+ ] })
11586
11660
  }
11587
11661
  )
11588
11662
  ] }),
11589
- /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
11590
- var _a2, _b, _c, _d, _e, _f, _g;
11591
- const items = getItemsWithShippingProfile(
11592
- profile.id,
11593
- order.items
11594
- );
11595
- const hasItems = items.length > 0;
11596
- const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
11597
- (option) => option.shipping_profile_id === profile.id
11598
- );
11599
- const shippingMethod = preview.shipping_methods.find(
11600
- (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
11601
- );
11602
- const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
11603
- (action) => action.action === "SHIPPING_ADD"
11604
- );
11605
- return /* @__PURE__ */ jsxs(
11606
- Accordion.Item,
11607
- {
11608
- value: profile.id,
11609
- className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
11610
- children: [
11611
- /* @__PURE__ */ jsxs("div", { className: "px-3 py-2 flex items-center justify-between gap-3", children: [
11612
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full overflow-hidden", children: [
11613
- /* @__PURE__ */ jsx(Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(
11614
- IconButton,
11615
- {
11616
- size: "2xsmall",
11617
- variant: "transparent",
11618
- className: "group/trigger",
11619
- disabled: !hasItems,
11620
- children: /* @__PURE__ */ jsx(TriangleRightMini, { className: "group-data-[state=open]/trigger:rotate-90 transition-transform" })
11621
- }
11622
- ) }),
11623
- !shippingOption ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
11624
- /* @__PURE__ */ jsx("div", { className: "size-7 rounded-md shadow-borders-base flex items-center justify-center", children: /* @__PURE__ */ jsx("div", { className: "size-6 rounded bg-ui-bg-component-hover flex items-center justify-center", children: /* @__PURE__ */ jsx(Shopping, { className: "text-ui-fg-subtle" }) }) }),
11625
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col flex-1", children: [
11626
- /* @__PURE__ */ jsx(
11627
- Text,
11628
- {
11629
- size: "small",
11630
- weight: "plus",
11631
- leading: "compact",
11632
- children: profile.name
11633
- }
11634
- ),
11635
- /* @__PURE__ */ jsxs(
11636
- Text,
11637
- {
11638
- size: "small",
11639
- leading: "compact",
11640
- className: "text-ui-fg-subtle",
11641
- children: [
11642
- items.length,
11643
- " ",
11644
- pluralize(items.length, "items", "item")
11645
- ]
11646
- }
11647
- )
11648
- ] })
11649
- ] }) : /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-[5px] max-sm:flex-col max-sm:items-start flex-1 w-full overflow-hidden", children: [
11650
- /* @__PURE__ */ jsx(
11651
- Tooltip,
11652
- {
11653
- content: /* @__PURE__ */ jsx("ul", { children: items.map((item) => {
11654
- var _a3, _b2, _c2;
11655
- return /* @__PURE__ */ jsx(
11656
- "li",
11657
- {
11658
- 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})`
11659
- },
11660
- item.id
11661
- );
11662
- }) }),
11663
- children: /* @__PURE__ */ jsxs(
11664
- Badge,
11665
- {
11666
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
11667
- size: "xsmall",
11668
- children: [
11669
- /* @__PURE__ */ jsx(Shopping, { className: "shrink-0" }),
11670
- /* @__PURE__ */ jsxs("span", { className: "truncate", children: [
11671
- items.reduce(
11672
- (acc, item) => acc + item.quantity,
11673
- 0
11674
- ),
11675
- "x",
11676
- " ",
11677
- pluralize(items.length, "items", "item")
11678
- ] })
11679
- ]
11680
- }
11681
- )
11682
- }
11683
- ),
11684
- /* @__PURE__ */ jsx(
11685
- Tooltip,
11686
- {
11687
- content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
11688
- children: /* @__PURE__ */ jsxs(
11689
- Badge,
11690
- {
11691
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
11692
- size: "xsmall",
11693
- children: [
11694
- /* @__PURE__ */ jsx(Buildings, { className: "shrink-0" }),
11695
- /* @__PURE__ */ jsx("span", { className: "truncate", children: (_g = (_f = (_e = shippingOption.service_zone) == null ? void 0 : _e.fulfillment_set) == null ? void 0 : _f.location) == null ? void 0 : _g.name })
11696
- ]
11697
- }
11698
- )
11699
- }
11700
- ),
11701
- /* @__PURE__ */ jsx(Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxs(
11702
- Badge,
11703
- {
11704
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
11705
- size: "xsmall",
11706
- children: [
11707
- /* @__PURE__ */ jsx(TruckFast, { className: "shrink-0" }),
11708
- /* @__PURE__ */ jsx("span", { className: "truncate", children: shippingOption.name })
11709
- ]
11710
- }
11711
- ) })
11712
- ] })
11713
- ] }),
11714
- shippingOption ? /* @__PURE__ */ jsx(
11715
- ActionMenu,
11716
- {
11717
- groups: [
11718
- {
11719
- actions: [
11720
- hasItems ? {
11721
- label: "Edit shipping option",
11722
- icon: /* @__PURE__ */ jsx(Channels, {}),
11723
- onClick: () => {
11724
- setIsOpen(
11725
- STACKED_FOCUS_MODAL_ID,
11726
- true
11727
- );
11728
- setData({
11729
- shippingProfileId: profile.id,
11730
- shippingOption,
11731
- shippingMethod
11732
- });
11733
- }
11734
- } : void 0,
11735
- {
11736
- label: "Remove shipping option",
11737
- icon: /* @__PURE__ */ jsx(Trash, {}),
11738
- onClick: () => {
11739
- if (shippingMethod) {
11740
- if (addShippingMethodAction) {
11741
- removeActionShippingMethod(
11742
- addShippingMethodAction.id
11743
- );
11744
- } else {
11745
- removeShippingMethod(
11746
- shippingMethod.id
11747
- );
11748
- }
11749
- }
11750
- }
11751
- }
11752
- ].filter(Boolean)
11753
- }
11754
- ]
11755
- }
11756
- ) : /* @__PURE__ */ jsx(
11757
- StackedModalTrigger,
11758
- {
11759
- shippingProfileId: profile.id,
11760
- shippingOption,
11761
- shippingMethod,
11762
- setData,
11763
- children: "Add shipping option"
11764
- }
11765
- )
11766
- ] }),
11767
- /* @__PURE__ */ jsxs(Accordion.Content, { children: [
11768
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11769
- items.map((item, idx) => {
11770
- var _a3, _b2, _c2, _d2, _e2;
11771
- return /* @__PURE__ */ jsxs("div", { children: [
11772
- /* @__PURE__ */ jsxs(
11773
- "div",
11774
- {
11775
- className: "px-3 flex items-center gap-x-3",
11776
- children: [
11777
- /* @__PURE__ */ jsx("div", { className: "w-5 h-[56px] flex flex-col justify-center items-center", children: /* @__PURE__ */ jsx(
11778
- Divider,
11779
- {
11780
- variant: "dashed",
11781
- orientation: "vertical"
11782
- }
11783
- ) }),
11784
- /* @__PURE__ */ jsxs("div", { className: "py-2 flex items-center gap-x-3", children: [
11785
- /* @__PURE__ */ jsx("div", { className: "size-7 flex items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxs(
11786
- Text,
11787
- {
11788
- size: "small",
11789
- leading: "compact",
11790
- className: "text-ui-fg-subtle",
11791
- children: [
11792
- item.quantity,
11793
- "x"
11794
- ]
11795
- }
11796
- ) }),
11797
- /* @__PURE__ */ jsx(Thumbnail, { thumbnail: item.thumbnail }),
11798
- /* @__PURE__ */ jsxs("div", { children: [
11799
- /* @__PURE__ */ jsxs(
11800
- Text,
11801
- {
11802
- size: "small",
11803
- leading: "compact",
11804
- weight: "plus",
11805
- children: [
11806
- (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
11807
- " (",
11808
- (_c2 = item.variant) == null ? void 0 : _c2.title,
11809
- ")"
11810
- ]
11811
- }
11812
- ),
11813
- /* @__PURE__ */ jsx(
11814
- Text,
11815
- {
11816
- size: "small",
11817
- leading: "compact",
11818
- className: "text-ui-fg-subtle",
11819
- children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
11820
- }
11821
- )
11822
- ] })
11823
- ] })
11824
- ]
11825
- },
11826
- item.id
11827
- ),
11828
- idx !== items.length - 1 && /* @__PURE__ */ jsx(Divider, { variant: "dashed" })
11829
- ] }, item.id);
11830
- })
11831
- ] })
11832
- ]
11833
- },
11834
- profile.id
11835
- );
11836
- }) })
11837
- ] }) })
11838
- ] }) }),
11839
- /* @__PURE__ */ jsx(
11840
- StackedFocusModal,
11841
- {
11842
- id: STACKED_FOCUS_MODAL_ID,
11843
- onOpenChangeCallback: (open) => {
11844
- if (!open) {
11845
- setData(null);
11846
- }
11847
- return open;
11848
- },
11849
- children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
11850
- }
11851
- )
11852
- ] }),
11853
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
11854
- /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11855
- /* @__PURE__ */ jsx(
11856
- Button,
11857
- {
11858
- size: "small",
11859
- type: "button",
11860
- isLoading: isSubmitting,
11861
- onClick: onSubmit,
11862
- children: "Save"
11863
- }
11864
- )
11865
- ] }) })
11866
- ] });
11867
- };
11868
- const StackedModalTrigger = ({
11869
- shippingProfileId,
11870
- shippingOption,
11871
- shippingMethod,
11872
- setData,
11873
- children
11874
- }) => {
11875
- const { setIsOpen, getIsOpen } = useStackedModal();
11876
- const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
11877
- const onToggle = () => {
11878
- if (isOpen) {
11879
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11880
- setData(null);
11881
- } else {
11882
- setIsOpen(STACKED_FOCUS_MODAL_ID, true);
11883
- setData({
11884
- shippingProfileId,
11885
- shippingOption,
11886
- shippingMethod
11887
- });
11888
- }
11889
- };
11890
- return /* @__PURE__ */ jsx(
11891
- Button,
11892
- {
11893
- size: "small",
11894
- variant: "secondary",
11895
- onClick: onToggle,
11896
- className: "text-ui-fg-primary shrink-0",
11897
- children
11898
- }
11899
- );
11900
- };
11901
- const ShippingProfileForm = ({
11902
- data,
11903
- order,
11904
- preview
11905
- }) => {
11906
- var _a, _b, _c, _d, _e, _f;
11907
- const { setIsOpen } = useStackedModal();
11908
- const form = useForm({
11909
- resolver: zodResolver(shippingMethodSchema),
11910
- defaultValues: {
11911
- 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,
11912
- shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
11913
- custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
11914
- }
11915
- });
11916
- const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
11917
- const {
11918
- mutateAsync: updateShippingMethod,
11919
- isPending: isUpdatingShippingMethod
11920
- } = useDraftOrderUpdateShippingMethod(order.id);
11921
- const onSubmit = form.handleSubmit(async (values) => {
11922
- if (isEqual(values, form.formState.defaultValues)) {
11923
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11924
- return;
11925
- }
11926
- if (data.shippingMethod) {
11927
- await updateShippingMethod(
11928
- {
11929
- method_id: data.shippingMethod.id,
11930
- shipping_option_id: values.shipping_option_id,
11931
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
11932
- },
11933
- {
11934
- onError: (e) => {
11935
- toast.error(e.message);
11936
- },
11937
- onSuccess: () => {
11938
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11939
- }
11940
- }
11941
- );
11942
- return;
11943
- }
11944
- await addShippingMethod(
11945
- {
11946
- shipping_option_id: values.shipping_option_id,
11947
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
11948
- },
11949
- {
11950
- onError: (e) => {
11951
- toast.error(e.message);
11952
- },
11953
- onSuccess: () => {
11954
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11955
- }
11956
- }
11957
- );
11958
- });
11959
- return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
11960
- KeyboundForm,
11961
- {
11962
- className: "flex h-full flex-col overflow-hidden",
11963
- onSubmit,
11964
- children: [
11965
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
11966
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11967
- /* @__PURE__ */ jsxs("div", { children: [
11968
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11969
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a shipping method for the selected shipping profile. You can see the items that will be shipped using this method in the preview below." }) })
11970
- ] }),
11971
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11972
11663
  /* @__PURE__ */ jsx(
11973
- LocationField,
11664
+ Form$2.Field,
11974
11665
  {
11975
11666
  control: form.control,
11976
- setValue: form.setValue
11667
+ name: "company",
11668
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11669
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
11670
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11671
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11672
+ ] })
11977
11673
  }
11978
11674
  ),
11979
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11980
11675
  /* @__PURE__ */ jsx(
11981
- ShippingOptionField,
11676
+ Form$2.Field,
11982
11677
  {
11983
- shippingProfileId: data.shippingProfileId,
11984
- preview,
11985
- control: form.control
11678
+ control: form.control,
11679
+ name: "address_1",
11680
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11681
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
11682
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11683
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11684
+ ] })
11986
11685
  }
11987
11686
  ),
11988
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11989
11687
  /* @__PURE__ */ jsx(
11990
- CustomAmountField,
11688
+ Form$2.Field,
11991
11689
  {
11992
11690
  control: form.control,
11993
- currencyCode: order.currency_code
11691
+ name: "address_2",
11692
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11693
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11694
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11695
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11696
+ ] })
11994
11697
  }
11995
11698
  ),
11996
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11699
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11700
+ /* @__PURE__ */ jsx(
11701
+ Form$2.Field,
11702
+ {
11703
+ control: form.control,
11704
+ name: "postal_code",
11705
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11706
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
11707
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11708
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11709
+ ] })
11710
+ }
11711
+ ),
11712
+ /* @__PURE__ */ jsx(
11713
+ Form$2.Field,
11714
+ {
11715
+ control: form.control,
11716
+ name: "city",
11717
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11718
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
11719
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11720
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11721
+ ] })
11722
+ }
11723
+ )
11724
+ ] }),
11997
11725
  /* @__PURE__ */ jsx(
11998
- ItemsPreview,
11726
+ Form$2.Field,
11999
11727
  {
12000
- order,
12001
- shippingProfileId: data.shippingProfileId
11728
+ control: form.control,
11729
+ name: "province",
11730
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11731
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11732
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11733
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11734
+ ] })
12002
11735
  }
12003
- )
12004
- ] }) }) }),
12005
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12006
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11736
+ ),
12007
11737
  /* @__PURE__ */ jsx(
12008
- Button,
11738
+ Form$2.Field,
12009
11739
  {
12010
- size: "small",
12011
- type: "submit",
12012
- isLoading: isPending || isUpdatingShippingMethod,
12013
- children: data.shippingMethod ? "Update" : "Add"
11740
+ control: form.control,
11741
+ name: "phone",
11742
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11743
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
11744
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11745
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11746
+ ] })
12014
11747
  }
12015
11748
  )
11749
+ ] }) }),
11750
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11751
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11752
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12016
11753
  ] }) })
12017
11754
  ]
12018
11755
  }
12019
- ) }) });
11756
+ ) });
12020
11757
  };
12021
- const shippingMethodSchema = objectType({
12022
- location_id: stringType(),
12023
- shipping_option_id: stringType(),
12024
- custom_amount: unionType([numberType(), stringType()]).optional()
12025
- });
12026
- const ItemsPreview = ({ order, shippingProfileId }) => {
12027
- const matches = order.items.filter(
12028
- (item) => {
12029
- var _a, _b, _c;
12030
- return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
11758
+ const schema$1 = addressSchema;
11759
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
11760
+ const Shipping = () => {
11761
+ var _a;
11762
+ const { id } = useParams();
11763
+ const { order, isPending, isError, error } = useOrder(id, {
11764
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11765
+ });
11766
+ const {
11767
+ order: preview,
11768
+ isPending: isPreviewPending,
11769
+ isError: isPreviewError,
11770
+ error: previewError
11771
+ } = useOrderPreview(id);
11772
+ useInitiateOrderEdit({ preview });
11773
+ const { onCancel } = useCancelOrderEdit({ preview });
11774
+ if (isError) {
11775
+ throw error;
11776
+ }
11777
+ if (isPreviewError) {
11778
+ throw previewError;
11779
+ }
11780
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11781
+ const isReady = preview && !isPreviewPending && order && !isPending;
11782
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11783
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11784
+ /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11785
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11786
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11787
+ ] }) }) }),
11788
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11789
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11790
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11791
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11792
+ ] }) });
11793
+ };
11794
+ const ShippingForm = ({ preview, order }) => {
11795
+ var _a;
11796
+ const { setIsOpen } = useStackedModal();
11797
+ const [isSubmitting, setIsSubmitting] = useState(false);
11798
+ const [data, setData] = useState(null);
11799
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11800
+ const { shipping_options } = useShippingOptions(
11801
+ {
11802
+ id: appliedShippingOptionIds,
11803
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11804
+ },
11805
+ {
11806
+ enabled: appliedShippingOptionIds.length > 0
12031
11807
  }
12032
11808
  );
12033
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12034
- /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12035
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12036
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12037
- ] }) }),
12038
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12039
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
12040
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12041
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
12042
- ] }),
12043
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxs(
12044
- "div",
12045
- {
12046
- className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
12047
- children: [
12048
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12049
- /* @__PURE__ */ jsx(
12050
- Thumbnail,
12051
- {
12052
- thumbnail: item.thumbnail,
12053
- alt: item.product_title ?? void 0
12054
- }
12055
- ),
12056
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12057
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12058
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12059
- /* @__PURE__ */ jsxs(
12060
- Text,
12061
- {
12062
- size: "small",
12063
- leading: "compact",
12064
- className: "text-ui-fg-subtle",
12065
- children: [
12066
- "(",
12067
- item.variant_title,
12068
- ")"
12069
- ]
12070
- }
12071
- )
12072
- ] }),
12073
- /* @__PURE__ */ jsx(
12074
- Text,
12075
- {
12076
- size: "small",
12077
- leading: "compact",
12078
- className: "text-ui-fg-subtle",
12079
- children: item.variant_sku
12080
- }
12081
- )
12082
- ] })
12083
- ] }),
12084
- /* @__PURE__ */ jsxs(
11809
+ const uniqueShippingProfiles = useMemo(() => {
11810
+ const profiles = /* @__PURE__ */ new Map();
11811
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
11812
+ profiles.set(profile.id, profile);
11813
+ });
11814
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11815
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
11816
+ });
11817
+ return Array.from(profiles.values());
11818
+ }, [order.items, shipping_options]);
11819
+ const { handleSuccess } = useRouteModal();
11820
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11821
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11822
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11823
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11824
+ const onSubmit = async () => {
11825
+ setIsSubmitting(true);
11826
+ let requestSucceeded = false;
11827
+ await requestOrderEdit(void 0, {
11828
+ onError: (e) => {
11829
+ toast.error(`Failed to request order edit: ${e.message}`);
11830
+ },
11831
+ onSuccess: () => {
11832
+ requestSucceeded = true;
11833
+ }
11834
+ });
11835
+ if (!requestSucceeded) {
11836
+ setIsSubmitting(false);
11837
+ return;
11838
+ }
11839
+ await confirmOrderEdit(void 0, {
11840
+ onError: (e) => {
11841
+ toast.error(`Failed to confirm order edit: ${e.message}`);
11842
+ },
11843
+ onSuccess: () => {
11844
+ handleSuccess();
11845
+ },
11846
+ onSettled: () => {
11847
+ setIsSubmitting(false);
11848
+ }
11849
+ });
11850
+ };
11851
+ const onKeydown = useCallback(
11852
+ (e) => {
11853
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11854
+ if (data || isSubmitting) {
11855
+ return;
11856
+ }
11857
+ onSubmit();
11858
+ }
11859
+ },
11860
+ [data, isSubmitting, onSubmit]
11861
+ );
11862
+ useEffect(() => {
11863
+ document.addEventListener("keydown", onKeydown);
11864
+ return () => {
11865
+ document.removeEventListener("keydown", onKeydown);
11866
+ };
11867
+ }, [onKeydown]);
11868
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11869
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11870
+ /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11871
+ /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11872
+ /* @__PURE__ */ jsxs("div", { children: [
11873
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11874
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
11875
+ ] }),
11876
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11877
+ /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11878
+ /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11879
+ /* @__PURE__ */ jsx(
12085
11880
  Text,
12086
11881
  {
12087
- size: "small",
12088
- leading: "compact",
12089
- className: "text-ui-fg-subtle",
12090
- children: [
12091
- item.quantity,
12092
- "x"
12093
- ]
12094
- }
12095
- )
12096
- ]
12097
- },
12098
- item.id
12099
- )) : /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-x-3 bg-ui-bg-base rounded-lg p-4 shadow-elevation-card-rest flex-col gap-1", children: [
12100
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12101
- /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12102
- 'No items found for "',
12103
- query,
12104
- '".'
12105
- ] })
12106
- ] }) })
12107
- ] })
12108
- ] });
12109
- };
12110
- const LocationField = ({ control, setValue }) => {
12111
- const locations = useComboboxData({
12112
- queryKey: ["locations"],
12113
- queryFn: async (params) => {
12114
- return await sdk.admin.stockLocation.list(params);
12115
- },
12116
- getOptions: (data) => {
12117
- return data.stock_locations.map((location) => ({
12118
- label: location.name,
12119
- value: location.id
12120
- }));
12121
- }
12122
- });
12123
- return /* @__PURE__ */ jsx(
12124
- Form$2.Field,
12125
- {
12126
- control,
12127
- name: "location_id",
12128
- render: ({ field: { onChange, ...field } }) => {
12129
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12130
- /* @__PURE__ */ jsxs("div", { children: [
12131
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
12132
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
11882
+ size: "xsmall",
11883
+ weight: "plus",
11884
+ className: "text-ui-fg-muted",
11885
+ children: "Shipping profile"
11886
+ }
11887
+ ),
11888
+ /* @__PURE__ */ jsx(
11889
+ Text,
11890
+ {
11891
+ size: "xsmall",
11892
+ weight: "plus",
11893
+ className: "text-ui-fg-muted",
11894
+ children: "Action"
11895
+ }
11896
+ )
12133
11897
  ] }),
12134
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12135
- Combobox,
12136
- {
12137
- options: locations.options,
12138
- fetchNextPage: locations.fetchNextPage,
12139
- isFetchingNextPage: locations.isFetchingNextPage,
12140
- searchValue: locations.searchValue,
12141
- onSearchValueChange: locations.onSearchValueChange,
12142
- placeholder: "Select location",
12143
- onChange: (value) => {
12144
- setValue("shipping_option_id", "", {
12145
- shouldDirty: true,
12146
- shouldTouch: true
12147
- });
12148
- onChange(value);
11898
+ /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
11899
+ var _a2, _b, _c, _d, _e, _f, _g;
11900
+ const items = getItemsWithShippingProfile(
11901
+ profile.id,
11902
+ order.items
11903
+ );
11904
+ const hasItems = items.length > 0;
11905
+ const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
11906
+ (option) => option.shipping_profile_id === profile.id
11907
+ );
11908
+ const shippingMethod = preview.shipping_methods.find(
11909
+ (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
11910
+ );
11911
+ const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
11912
+ (action) => action.action === "SHIPPING_ADD"
11913
+ );
11914
+ return /* @__PURE__ */ jsxs(
11915
+ Accordion.Item,
11916
+ {
11917
+ value: profile.id,
11918
+ className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
11919
+ children: [
11920
+ /* @__PURE__ */ jsxs("div", { className: "px-3 py-2 flex items-center justify-between gap-3", children: [
11921
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full overflow-hidden", children: [
11922
+ /* @__PURE__ */ jsx(Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(
11923
+ IconButton,
11924
+ {
11925
+ size: "2xsmall",
11926
+ variant: "transparent",
11927
+ className: "group/trigger",
11928
+ disabled: !hasItems,
11929
+ children: /* @__PURE__ */ jsx(TriangleRightMini, { className: "group-data-[state=open]/trigger:rotate-90 transition-transform" })
11930
+ }
11931
+ ) }),
11932
+ !shippingOption ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
11933
+ /* @__PURE__ */ jsx("div", { className: "size-7 rounded-md shadow-borders-base flex items-center justify-center", children: /* @__PURE__ */ jsx("div", { className: "size-6 rounded bg-ui-bg-component-hover flex items-center justify-center", children: /* @__PURE__ */ jsx(Shopping, { className: "text-ui-fg-subtle" }) }) }),
11934
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col flex-1", children: [
11935
+ /* @__PURE__ */ jsx(
11936
+ Text,
11937
+ {
11938
+ size: "small",
11939
+ weight: "plus",
11940
+ leading: "compact",
11941
+ children: profile.name
11942
+ }
11943
+ ),
11944
+ /* @__PURE__ */ jsxs(
11945
+ Text,
11946
+ {
11947
+ size: "small",
11948
+ leading: "compact",
11949
+ className: "text-ui-fg-subtle",
11950
+ children: [
11951
+ items.length,
11952
+ " ",
11953
+ pluralize(items.length, "items", "item")
11954
+ ]
11955
+ }
11956
+ )
11957
+ ] })
11958
+ ] }) : /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-[5px] max-sm:flex-col max-sm:items-start flex-1 w-full overflow-hidden", children: [
11959
+ /* @__PURE__ */ jsx(
11960
+ Tooltip,
11961
+ {
11962
+ content: /* @__PURE__ */ jsx("ul", { children: items.map((item) => {
11963
+ var _a3, _b2, _c2;
11964
+ return /* @__PURE__ */ jsx(
11965
+ "li",
11966
+ {
11967
+ children: `${item.quantity}x ${(_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title} (${(_c2 = item.variant) == null ? void 0 : _c2.title})`
11968
+ },
11969
+ item.id
11970
+ );
11971
+ }) }),
11972
+ children: /* @__PURE__ */ jsxs(
11973
+ Badge,
11974
+ {
11975
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
11976
+ size: "xsmall",
11977
+ children: [
11978
+ /* @__PURE__ */ jsx(Shopping, { className: "shrink-0" }),
11979
+ /* @__PURE__ */ jsxs("span", { className: "truncate", children: [
11980
+ items.reduce(
11981
+ (acc, item) => acc + item.quantity,
11982
+ 0
11983
+ ),
11984
+ "x",
11985
+ " ",
11986
+ pluralize(items.length, "items", "item")
11987
+ ] })
11988
+ ]
11989
+ }
11990
+ )
11991
+ }
11992
+ ),
11993
+ /* @__PURE__ */ jsx(
11994
+ Tooltip,
11995
+ {
11996
+ content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
11997
+ children: /* @__PURE__ */ jsxs(
11998
+ Badge,
11999
+ {
12000
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
12001
+ size: "xsmall",
12002
+ children: [
12003
+ /* @__PURE__ */ jsx(Buildings, { className: "shrink-0" }),
12004
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: (_g = (_f = (_e = shippingOption.service_zone) == null ? void 0 : _e.fulfillment_set) == null ? void 0 : _f.location) == null ? void 0 : _g.name })
12005
+ ]
12006
+ }
12007
+ )
12008
+ }
12009
+ ),
12010
+ /* @__PURE__ */ jsx(Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxs(
12011
+ Badge,
12012
+ {
12013
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
12014
+ size: "xsmall",
12015
+ children: [
12016
+ /* @__PURE__ */ jsx(TruckFast, { className: "shrink-0" }),
12017
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: shippingOption.name })
12018
+ ]
12019
+ }
12020
+ ) })
12021
+ ] })
12022
+ ] }),
12023
+ shippingOption ? /* @__PURE__ */ jsx(
12024
+ ActionMenu,
12025
+ {
12026
+ groups: [
12027
+ {
12028
+ actions: [
12029
+ hasItems ? {
12030
+ label: "Edit shipping option",
12031
+ icon: /* @__PURE__ */ jsx(Channels, {}),
12032
+ onClick: () => {
12033
+ setIsOpen(
12034
+ STACKED_FOCUS_MODAL_ID,
12035
+ true
12036
+ );
12037
+ setData({
12038
+ shippingProfileId: profile.id,
12039
+ shippingOption,
12040
+ shippingMethod
12041
+ });
12042
+ }
12043
+ } : void 0,
12044
+ {
12045
+ label: "Remove shipping option",
12046
+ icon: /* @__PURE__ */ jsx(Trash, {}),
12047
+ onClick: () => {
12048
+ if (shippingMethod) {
12049
+ if (addShippingMethodAction) {
12050
+ removeActionShippingMethod(
12051
+ addShippingMethodAction.id
12052
+ );
12053
+ } else {
12054
+ removeShippingMethod(
12055
+ shippingMethod.id
12056
+ );
12057
+ }
12058
+ }
12059
+ }
12060
+ }
12061
+ ].filter(Boolean)
12062
+ }
12063
+ ]
12064
+ }
12065
+ ) : /* @__PURE__ */ jsx(
12066
+ StackedModalTrigger,
12067
+ {
12068
+ shippingProfileId: profile.id,
12069
+ shippingOption,
12070
+ shippingMethod,
12071
+ setData,
12072
+ children: "Add shipping option"
12073
+ }
12074
+ )
12075
+ ] }),
12076
+ /* @__PURE__ */ jsxs(Accordion.Content, { children: [
12077
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12078
+ items.map((item, idx) => {
12079
+ var _a3, _b2, _c2, _d2, _e2;
12080
+ return /* @__PURE__ */ jsxs("div", { children: [
12081
+ /* @__PURE__ */ jsxs(
12082
+ "div",
12083
+ {
12084
+ className: "px-3 flex items-center gap-x-3",
12085
+ children: [
12086
+ /* @__PURE__ */ jsx("div", { className: "w-5 h-[56px] flex flex-col justify-center items-center", children: /* @__PURE__ */ jsx(
12087
+ Divider,
12088
+ {
12089
+ variant: "dashed",
12090
+ orientation: "vertical"
12091
+ }
12092
+ ) }),
12093
+ /* @__PURE__ */ jsxs("div", { className: "py-2 flex items-center gap-x-3", children: [
12094
+ /* @__PURE__ */ jsx("div", { className: "size-7 flex items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxs(
12095
+ Text,
12096
+ {
12097
+ size: "small",
12098
+ leading: "compact",
12099
+ className: "text-ui-fg-subtle",
12100
+ children: [
12101
+ item.quantity,
12102
+ "x"
12103
+ ]
12104
+ }
12105
+ ) }),
12106
+ /* @__PURE__ */ jsx(Thumbnail, { thumbnail: item.thumbnail }),
12107
+ /* @__PURE__ */ jsxs("div", { children: [
12108
+ /* @__PURE__ */ jsxs(
12109
+ Text,
12110
+ {
12111
+ size: "small",
12112
+ leading: "compact",
12113
+ weight: "plus",
12114
+ children: [
12115
+ (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
12116
+ " (",
12117
+ (_c2 = item.variant) == null ? void 0 : _c2.title,
12118
+ ")"
12119
+ ]
12120
+ }
12121
+ ),
12122
+ /* @__PURE__ */ jsx(
12123
+ Text,
12124
+ {
12125
+ size: "small",
12126
+ leading: "compact",
12127
+ className: "text-ui-fg-subtle",
12128
+ children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
12129
+ }
12130
+ )
12131
+ ] })
12132
+ ] })
12133
+ ]
12134
+ },
12135
+ item.id
12136
+ ),
12137
+ idx !== items.length - 1 && /* @__PURE__ */ jsx(Divider, { variant: "dashed" })
12138
+ ] }, item.id);
12139
+ })
12140
+ ] })
12141
+ ]
12149
12142
  },
12150
- ...field
12143
+ profile.id
12144
+ );
12145
+ }) })
12146
+ ] }) })
12147
+ ] }) }),
12148
+ /* @__PURE__ */ jsx(
12149
+ StackedFocusModal,
12150
+ {
12151
+ id: STACKED_FOCUS_MODAL_ID,
12152
+ onOpenChangeCallback: (open) => {
12153
+ if (!open) {
12154
+ setData(null);
12151
12155
  }
12152
- ) })
12153
- ] }) });
12154
- }
12155
- }
12156
- );
12157
- };
12158
- const ShippingOptionField = ({
12159
- shippingProfileId,
12160
- preview,
12161
- control
12162
- }) => {
12163
- var _a;
12164
- const locationId = useWatch({ control, name: "location_id" });
12165
- const shippingOptions = useComboboxData({
12166
- queryKey: ["shipping_options", locationId, shippingProfileId],
12167
- queryFn: async (params) => {
12168
- return await sdk.admin.shippingOption.list({
12169
- ...params,
12170
- stock_location_id: locationId,
12171
- shipping_profile_id: shippingProfileId
12172
- });
12173
- },
12174
- getOptions: (data) => {
12175
- return data.shipping_options.map((option) => {
12176
- var _a2;
12177
- if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12178
- (r) => r.attribute === "is_return" && r.value === "true"
12179
- )) {
12180
- return void 0;
12156
+ return open;
12157
+ },
12158
+ children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
12181
12159
  }
12182
- return {
12183
- label: option.name,
12184
- value: option.id
12185
- };
12186
- }).filter(Boolean);
12187
- },
12188
- enabled: !!locationId && !!shippingProfileId,
12189
- defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12190
- });
12191
- const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12192
- return /* @__PURE__ */ jsx(
12193
- Form$2.Field,
12194
- {
12195
- control,
12196
- name: "shipping_option_id",
12197
- render: ({ field }) => {
12198
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12199
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12200
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12201
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12202
- ] }),
12203
- /* @__PURE__ */ jsx(
12204
- ConditionalTooltip,
12205
- {
12206
- content: tooltipContent,
12207
- showTooltip: !locationId || !shippingProfileId,
12208
- children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12209
- Combobox,
12210
- {
12211
- options: shippingOptions.options,
12212
- fetchNextPage: shippingOptions.fetchNextPage,
12213
- isFetchingNextPage: shippingOptions.isFetchingNextPage,
12214
- searchValue: shippingOptions.searchValue,
12215
- onSearchValueChange: shippingOptions.onSearchValueChange,
12216
- placeholder: "Select shipping option",
12217
- ...field,
12218
- disabled: !locationId || !shippingProfileId
12219
- }
12220
- ) }) })
12221
- }
12222
- )
12223
- ] }) });
12224
- }
12225
- }
12226
- );
12227
- };
12228
- const CustomAmountField = ({
12229
- control,
12230
- currencyCode
12231
- }) => {
12232
- return /* @__PURE__ */ jsx(
12233
- Form$2.Field,
12234
- {
12235
- control,
12236
- name: "custom_amount",
12237
- render: ({ field: { onChange, ...field } }) => {
12238
- return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12239
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12240
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12241
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12242
- ] }),
12243
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12244
- CurrencyInput,
12245
- {
12246
- ...field,
12247
- onValueChange: (value) => onChange(value),
12248
- symbol: getNativeSymbol(currencyCode),
12249
- code: currencyCode
12250
- }
12251
- ) })
12252
- ] });
12253
- }
12254
- }
12255
- );
12256
- };
12257
- const SalesChannel = () => {
12258
- const { id } = useParams();
12259
- const { draft_order, isPending, isError, error } = useDraftOrder(
12260
- id,
12261
- {
12262
- fields: "+sales_channel_id"
12263
- },
12264
- {
12265
- enabled: !!id
12266
- }
12267
- );
12268
- if (isError) {
12269
- throw error;
12270
- }
12271
- const ISrEADY = !!draft_order && !isPending;
12272
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12273
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12274
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
12275
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12160
+ )
12276
12161
  ] }),
12277
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
12278
- ] });
12279
- };
12280
- const SalesChannelForm = ({ order }) => {
12281
- const form = useForm({
12282
- defaultValues: {
12283
- sales_channel_id: order.sales_channel_id || ""
12284
- },
12285
- resolver: zodResolver(schema$2)
12286
- });
12287
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12288
- const { handleSuccess } = useRouteModal();
12289
- const onSubmit = form.handleSubmit(async (data) => {
12290
- await mutateAsync(
12291
- {
12292
- sales_channel_id: data.sales_channel_id
12293
- },
12294
- {
12295
- onSuccess: () => {
12296
- toast.success("Sales channel updated");
12297
- handleSuccess();
12298
- },
12299
- onError: (error) => {
12300
- toast.error(error.message);
12162
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12163
+ /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12164
+ /* @__PURE__ */ jsx(
12165
+ Button,
12166
+ {
12167
+ size: "small",
12168
+ type: "button",
12169
+ isLoading: isSubmitting,
12170
+ onClick: onSubmit,
12171
+ children: "Save"
12301
12172
  }
12302
- }
12303
- );
12304
- });
12305
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12306
- KeyboundForm,
12307
- {
12308
- className: "flex flex-1 flex-col overflow-hidden",
12309
- onSubmit,
12310
- children: [
12311
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
12312
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12313
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12314
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12315
- ] }) })
12316
- ]
12317
- }
12318
- ) });
12173
+ )
12174
+ ] }) })
12175
+ ] });
12319
12176
  };
12320
- const SalesChannelField = ({ control, order }) => {
12321
- const salesChannels = useComboboxData({
12322
- queryFn: async (params) => {
12323
- return await sdk.admin.salesChannel.list(params);
12324
- },
12325
- queryKey: ["sales-channels"],
12326
- getOptions: (data) => {
12327
- return data.sales_channels.map((salesChannel) => ({
12328
- label: salesChannel.name,
12329
- value: salesChannel.id
12330
- }));
12331
- },
12332
- defaultValue: order.sales_channel_id || void 0
12333
- });
12334
- return /* @__PURE__ */ jsx(
12335
- Form$2.Field,
12336
- {
12337
- control,
12338
- name: "sales_channel_id",
12339
- render: ({ field }) => {
12340
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12341
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
12342
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12343
- Combobox,
12344
- {
12345
- options: salesChannels.options,
12346
- fetchNextPage: salesChannels.fetchNextPage,
12347
- isFetchingNextPage: salesChannels.isFetchingNextPage,
12348
- searchValue: salesChannels.searchValue,
12349
- onSearchValueChange: salesChannels.onSearchValueChange,
12350
- placeholder: "Select sales channel",
12351
- ...field
12352
- }
12353
- ) }),
12354
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12355
- ] });
12356
- }
12177
+ const StackedModalTrigger = ({
12178
+ shippingProfileId,
12179
+ shippingOption,
12180
+ shippingMethod,
12181
+ setData,
12182
+ children
12183
+ }) => {
12184
+ const { setIsOpen, getIsOpen } = useStackedModal();
12185
+ const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
12186
+ const onToggle = () => {
12187
+ if (isOpen) {
12188
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12189
+ setData(null);
12190
+ } else {
12191
+ setIsOpen(STACKED_FOCUS_MODAL_ID, true);
12192
+ setData({
12193
+ shippingProfileId,
12194
+ shippingOption,
12195
+ shippingMethod
12196
+ });
12197
+ }
12198
+ };
12199
+ return /* @__PURE__ */ jsx(
12200
+ Button,
12201
+ {
12202
+ size: "small",
12203
+ variant: "secondary",
12204
+ onClick: onToggle,
12205
+ className: "text-ui-fg-primary shrink-0",
12206
+ children
12357
12207
  }
12358
12208
  );
12359
12209
  };
12360
- const schema$2 = objectType({
12361
- sales_channel_id: stringType().min(1)
12362
- });
12363
- const ShippingAddress = () => {
12364
- const { id } = useParams();
12365
- const { order, isPending, isError, error } = useOrder(id, {
12366
- fields: "+shipping_address"
12367
- });
12368
- if (isError) {
12369
- throw error;
12370
- }
12371
- const isReady = !isPending && !!order;
12372
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12373
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12374
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12375
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12376
- ] }),
12377
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12378
- ] });
12379
- };
12380
- const ShippingAddressForm = ({ order }) => {
12381
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12210
+ const ShippingProfileForm = ({
12211
+ data,
12212
+ order,
12213
+ preview
12214
+ }) => {
12215
+ var _a, _b, _c, _d, _e, _f;
12216
+ const { setIsOpen } = useStackedModal();
12382
12217
  const form = useForm({
12218
+ resolver: zodResolver(shippingMethodSchema),
12383
12219
  defaultValues: {
12384
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12385
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12386
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12387
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12388
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12389
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12390
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12391
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12392
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12393
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12394
- },
12395
- resolver: zodResolver(schema$1)
12220
+ location_id: (_d = (_c = (_b = (_a = data.shippingOption) == null ? void 0 : _a.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.id,
12221
+ shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
12222
+ custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
12223
+ }
12396
12224
  });
12397
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12398
- const { handleSuccess } = useRouteModal();
12399
- const onSubmit = form.handleSubmit(async (data) => {
12400
- await mutateAsync(
12401
- {
12402
- shipping_address: {
12403
- first_name: data.first_name,
12404
- last_name: data.last_name,
12405
- company: data.company,
12406
- address_1: data.address_1,
12407
- address_2: data.address_2,
12408
- city: data.city,
12409
- province: data.province,
12410
- country_code: data.country_code,
12411
- postal_code: data.postal_code,
12412
- phone: data.phone
12225
+ const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
12226
+ const {
12227
+ mutateAsync: updateShippingMethod,
12228
+ isPending: isUpdatingShippingMethod
12229
+ } = useDraftOrderUpdateShippingMethod(order.id);
12230
+ const onSubmit = form.handleSubmit(async (values) => {
12231
+ if (isEqual(values, form.formState.defaultValues)) {
12232
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12233
+ return;
12234
+ }
12235
+ if (data.shippingMethod) {
12236
+ await updateShippingMethod(
12237
+ {
12238
+ method_id: data.shippingMethod.id,
12239
+ shipping_option_id: values.shipping_option_id,
12240
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12241
+ },
12242
+ {
12243
+ onError: (e) => {
12244
+ toast.error(e.message);
12245
+ },
12246
+ onSuccess: () => {
12247
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12248
+ }
12413
12249
  }
12250
+ );
12251
+ return;
12252
+ }
12253
+ await addShippingMethod(
12254
+ {
12255
+ shipping_option_id: values.shipping_option_id,
12256
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12414
12257
  },
12415
12258
  {
12416
- onSuccess: () => {
12417
- handleSuccess();
12259
+ onError: (e) => {
12260
+ toast.error(e.message);
12418
12261
  },
12419
- onError: (error) => {
12420
- toast.error(error.message);
12262
+ onSuccess: () => {
12263
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12421
12264
  }
12422
12265
  }
12423
12266
  );
12424
12267
  });
12425
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12268
+ return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
12426
12269
  KeyboundForm,
12427
12270
  {
12428
- className: "flex flex-1 flex-col overflow-hidden",
12271
+ className: "flex h-full flex-col overflow-hidden",
12429
12272
  onSubmit,
12430
12273
  children: [
12431
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
12274
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12275
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
12276
+ /* @__PURE__ */ jsxs("div", { children: [
12277
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12278
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a shipping method for the selected shipping profile. You can see the items that will be shipped using this method in the preview below." }) })
12279
+ ] }),
12280
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12432
12281
  /* @__PURE__ */ jsx(
12433
- Form$2.Field,
12282
+ LocationField,
12434
12283
  {
12435
12284
  control: form.control,
12436
- name: "country_code",
12437
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12438
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12439
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12440
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12441
- ] })
12285
+ setValue: form.setValue
12442
12286
  }
12443
12287
  ),
12444
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12445
- /* @__PURE__ */ jsx(
12446
- Form$2.Field,
12447
- {
12448
- control: form.control,
12449
- name: "first_name",
12450
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12451
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12452
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12453
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12454
- ] })
12455
- }
12456
- ),
12457
- /* @__PURE__ */ jsx(
12458
- Form$2.Field,
12459
- {
12460
- control: form.control,
12461
- name: "last_name",
12462
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12463
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12464
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12465
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12466
- ] })
12467
- }
12468
- )
12469
- ] }),
12288
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12470
12289
  /* @__PURE__ */ jsx(
12471
- Form$2.Field,
12290
+ ShippingOptionField,
12472
12291
  {
12473
- control: form.control,
12474
- name: "company",
12475
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12476
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12477
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12478
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12479
- ] })
12292
+ shippingProfileId: data.shippingProfileId,
12293
+ preview,
12294
+ control: form.control
12480
12295
  }
12481
12296
  ),
12297
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12482
12298
  /* @__PURE__ */ jsx(
12483
- Form$2.Field,
12299
+ CustomAmountField,
12484
12300
  {
12485
12301
  control: form.control,
12486
- name: "address_1",
12487
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12488
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12489
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12490
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12491
- ] })
12302
+ currencyCode: order.currency_code
12492
12303
  }
12493
12304
  ),
12305
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12494
12306
  /* @__PURE__ */ jsx(
12495
- Form$2.Field,
12307
+ ItemsPreview,
12496
12308
  {
12497
- control: form.control,
12498
- name: "address_2",
12499
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12500
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12501
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12502
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12309
+ order,
12310
+ shippingProfileId: data.shippingProfileId
12311
+ }
12312
+ )
12313
+ ] }) }) }),
12314
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12315
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12316
+ /* @__PURE__ */ jsx(
12317
+ Button,
12318
+ {
12319
+ size: "small",
12320
+ type: "submit",
12321
+ isLoading: isPending || isUpdatingShippingMethod,
12322
+ children: data.shippingMethod ? "Update" : "Add"
12323
+ }
12324
+ )
12325
+ ] }) })
12326
+ ]
12327
+ }
12328
+ ) }) });
12329
+ };
12330
+ const shippingMethodSchema = objectType({
12331
+ location_id: stringType(),
12332
+ shipping_option_id: stringType(),
12333
+ custom_amount: unionType([numberType(), stringType()]).optional()
12334
+ });
12335
+ const ItemsPreview = ({ order, shippingProfileId }) => {
12336
+ const matches = order.items.filter(
12337
+ (item) => {
12338
+ var _a, _b, _c;
12339
+ return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
12340
+ }
12341
+ );
12342
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12343
+ /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12344
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12345
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12346
+ ] }) }),
12347
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12348
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
12349
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12350
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
12351
+ ] }),
12352
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxs(
12353
+ "div",
12354
+ {
12355
+ className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
12356
+ children: [
12357
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12358
+ /* @__PURE__ */ jsx(
12359
+ Thumbnail,
12360
+ {
12361
+ thumbnail: item.thumbnail,
12362
+ alt: item.product_title ?? void 0
12363
+ }
12364
+ ),
12365
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12366
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12367
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12368
+ /* @__PURE__ */ jsxs(
12369
+ Text,
12370
+ {
12371
+ size: "small",
12372
+ leading: "compact",
12373
+ className: "text-ui-fg-subtle",
12374
+ children: [
12375
+ "(",
12376
+ item.variant_title,
12377
+ ")"
12378
+ ]
12379
+ }
12380
+ )
12381
+ ] }),
12382
+ /* @__PURE__ */ jsx(
12383
+ Text,
12384
+ {
12385
+ size: "small",
12386
+ leading: "compact",
12387
+ className: "text-ui-fg-subtle",
12388
+ children: item.variant_sku
12389
+ }
12390
+ )
12503
12391
  ] })
12504
- }
12505
- ),
12506
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12507
- /* @__PURE__ */ jsx(
12508
- Form$2.Field,
12509
- {
12510
- control: form.control,
12511
- name: "postal_code",
12512
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12513
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12514
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12515
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12516
- ] })
12517
- }
12518
- ),
12519
- /* @__PURE__ */ jsx(
12520
- Form$2.Field,
12392
+ ] }),
12393
+ /* @__PURE__ */ jsxs(
12394
+ Text,
12521
12395
  {
12522
- control: form.control,
12523
- name: "city",
12524
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12525
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12526
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12527
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12528
- ] })
12396
+ size: "small",
12397
+ leading: "compact",
12398
+ className: "text-ui-fg-subtle",
12399
+ children: [
12400
+ item.quantity,
12401
+ "x"
12402
+ ]
12529
12403
  }
12530
12404
  )
12405
+ ]
12406
+ },
12407
+ item.id
12408
+ )) : /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-x-3 bg-ui-bg-base rounded-lg p-4 shadow-elevation-card-rest flex-col gap-1", children: [
12409
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12410
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12411
+ 'No items found for "',
12412
+ query,
12413
+ '".'
12414
+ ] })
12415
+ ] }) })
12416
+ ] })
12417
+ ] });
12418
+ };
12419
+ const LocationField = ({ control, setValue }) => {
12420
+ const locations = useComboboxData({
12421
+ queryKey: ["locations"],
12422
+ queryFn: async (params) => {
12423
+ return await sdk.admin.stockLocation.list(params);
12424
+ },
12425
+ getOptions: (data) => {
12426
+ return data.stock_locations.map((location) => ({
12427
+ label: location.name,
12428
+ value: location.id
12429
+ }));
12430
+ }
12431
+ });
12432
+ return /* @__PURE__ */ jsx(
12433
+ Form$2.Field,
12434
+ {
12435
+ control,
12436
+ name: "location_id",
12437
+ render: ({ field: { onChange, ...field } }) => {
12438
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12439
+ /* @__PURE__ */ jsxs("div", { children: [
12440
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
12441
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12531
12442
  ] }),
12532
- /* @__PURE__ */ jsx(
12533
- Form$2.Field,
12443
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12444
+ Combobox,
12534
12445
  {
12535
- control: form.control,
12536
- name: "province",
12537
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12538
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12539
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12540
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12541
- ] })
12446
+ options: locations.options,
12447
+ fetchNextPage: locations.fetchNextPage,
12448
+ isFetchingNextPage: locations.isFetchingNextPage,
12449
+ searchValue: locations.searchValue,
12450
+ onSearchValueChange: locations.onSearchValueChange,
12451
+ placeholder: "Select location",
12452
+ onChange: (value) => {
12453
+ setValue("shipping_option_id", "", {
12454
+ shouldDirty: true,
12455
+ shouldTouch: true
12456
+ });
12457
+ onChange(value);
12458
+ },
12459
+ ...field
12542
12460
  }
12543
- ),
12461
+ ) })
12462
+ ] }) });
12463
+ }
12464
+ }
12465
+ );
12466
+ };
12467
+ const ShippingOptionField = ({
12468
+ shippingProfileId,
12469
+ preview,
12470
+ control
12471
+ }) => {
12472
+ var _a;
12473
+ const locationId = useWatch({ control, name: "location_id" });
12474
+ const shippingOptions = useComboboxData({
12475
+ queryKey: ["shipping_options", locationId, shippingProfileId],
12476
+ queryFn: async (params) => {
12477
+ return await sdk.admin.shippingOption.list({
12478
+ ...params,
12479
+ stock_location_id: locationId,
12480
+ shipping_profile_id: shippingProfileId
12481
+ });
12482
+ },
12483
+ getOptions: (data) => {
12484
+ return data.shipping_options.map((option) => {
12485
+ var _a2;
12486
+ if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12487
+ (r) => r.attribute === "is_return" && r.value === "true"
12488
+ )) {
12489
+ return void 0;
12490
+ }
12491
+ return {
12492
+ label: option.name,
12493
+ value: option.id
12494
+ };
12495
+ }).filter(Boolean);
12496
+ },
12497
+ enabled: !!locationId && !!shippingProfileId,
12498
+ defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12499
+ });
12500
+ const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12501
+ return /* @__PURE__ */ jsx(
12502
+ Form$2.Field,
12503
+ {
12504
+ control,
12505
+ name: "shipping_option_id",
12506
+ render: ({ field }) => {
12507
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12508
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12509
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12510
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12511
+ ] }),
12544
12512
  /* @__PURE__ */ jsx(
12545
- Form$2.Field,
12513
+ ConditionalTooltip,
12546
12514
  {
12547
- control: form.control,
12548
- name: "phone",
12549
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12550
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12551
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12552
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12553
- ] })
12515
+ content: tooltipContent,
12516
+ showTooltip: !locationId || !shippingProfileId,
12517
+ children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12518
+ Combobox,
12519
+ {
12520
+ options: shippingOptions.options,
12521
+ fetchNextPage: shippingOptions.fetchNextPage,
12522
+ isFetchingNextPage: shippingOptions.isFetchingNextPage,
12523
+ searchValue: shippingOptions.searchValue,
12524
+ onSearchValueChange: shippingOptions.onSearchValueChange,
12525
+ placeholder: "Select shipping option",
12526
+ ...field,
12527
+ disabled: !locationId || !shippingProfileId
12528
+ }
12529
+ ) }) })
12554
12530
  }
12555
12531
  )
12556
- ] }) }),
12557
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12558
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12559
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12560
- ] }) })
12561
- ]
12532
+ ] }) });
12533
+ }
12562
12534
  }
12563
- ) });
12535
+ );
12536
+ };
12537
+ const CustomAmountField = ({
12538
+ control,
12539
+ currencyCode
12540
+ }) => {
12541
+ return /* @__PURE__ */ jsx(
12542
+ Form$2.Field,
12543
+ {
12544
+ control,
12545
+ name: "custom_amount",
12546
+ render: ({ field: { onChange, ...field } }) => {
12547
+ return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12548
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12549
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12550
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12551
+ ] }),
12552
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12553
+ CurrencyInput,
12554
+ {
12555
+ ...field,
12556
+ onValueChange: (value) => onChange(value),
12557
+ symbol: getNativeSymbol(currencyCode),
12558
+ code: currencyCode
12559
+ }
12560
+ ) })
12561
+ ] });
12562
+ }
12563
+ }
12564
+ );
12564
12565
  };
12565
- const schema$1 = addressSchema;
12566
12566
  const TransferOwnership = () => {
12567
12567
  const { id } = useParams();
12568
12568
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -13059,10 +13059,6 @@ const routeModule = {
13059
13059
  handle,
13060
13060
  loader,
13061
13061
  children: [
13062
- {
13063
- Component: BillingAddress,
13064
- path: "/draft-orders/:id/billing-address"
13065
- },
13066
13062
  {
13067
13063
  Component: CustomItems,
13068
13064
  path: "/draft-orders/:id/custom-items"
@@ -13071,6 +13067,10 @@ const routeModule = {
13071
13067
  Component: Email,
13072
13068
  path: "/draft-orders/:id/email"
13073
13069
  },
13070
+ {
13071
+ Component: BillingAddress,
13072
+ path: "/draft-orders/:id/billing-address"
13073
+ },
13074
13074
  {
13075
13075
  Component: Items,
13076
13076
  path: "/draft-orders/:id/items"
@@ -13083,10 +13083,6 @@ const routeModule = {
13083
13083
  Component: Promotions,
13084
13084
  path: "/draft-orders/:id/promotions"
13085
13085
  },
13086
- {
13087
- Component: Shipping,
13088
- path: "/draft-orders/:id/shipping"
13089
- },
13090
13086
  {
13091
13087
  Component: SalesChannel,
13092
13088
  path: "/draft-orders/:id/sales-channel"
@@ -13095,6 +13091,10 @@ const routeModule = {
13095
13091
  Component: ShippingAddress,
13096
13092
  path: "/draft-orders/:id/shipping-address"
13097
13093
  },
13094
+ {
13095
+ Component: Shipping,
13096
+ path: "/draft-orders/:id/shipping"
13097
+ },
13098
13098
  {
13099
13099
  Component: TransferOwnership,
13100
13100
  path: "/draft-orders/:id/transfer-ownership"