@medusajs/draft-order 2.10.4-preview-20251007060201 → 2.10.4-preview-20251008000316

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.
@@ -9757,95 +9757,6 @@ const BillingAddressForm = ({ order }) => {
9757
9757
  ) });
9758
9758
  };
9759
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
- });
9849
9760
  const NumberInput = forwardRef(
9850
9761
  ({
9851
9762
  value,
@@ -11447,1016 +11358,1122 @@ function getPromotionIds(items, shippingMethods) {
11447
11358
  }
11448
11359
  return Array.from(promotionIds);
11449
11360
  }
11450
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11451
- const Shipping = () => {
11452
- var _a;
11361
+ const SalesChannel = () => {
11453
11362
  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(
11363
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11364
+ id,
11492
11365
  {
11493
- id: appliedShippingOptionIds,
11494
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11366
+ fields: "+sales_channel_id"
11495
11367
  },
11496
11368
  {
11497
- enabled: appliedShippingOptionIds.length > 0
11369
+ enabled: !!id
11498
11370
  }
11499
11371
  );
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]);
11372
+ if (isError) {
11373
+ throw error;
11374
+ }
11375
+ const ISrEADY = !!draft_order && !isPending;
11376
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11377
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11378
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11379
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11380
+ ] }),
11381
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11382
+ ] });
11383
+ };
11384
+ const SalesChannelForm = ({ order }) => {
11385
+ const form = useForm({
11386
+ defaultValues: {
11387
+ sales_channel_id: order.sales_channel_id || ""
11388
+ },
11389
+ resolver: zodResolver(schema$4)
11390
+ });
11391
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11510
11392
  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();
11393
+ const onSubmit = form.handleSubmit(async (data) => {
11394
+ await mutateAsync(
11395
+ {
11396
+ sales_channel_id: data.sales_channel_id
11536
11397
  },
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;
11398
+ {
11399
+ onSuccess: () => {
11400
+ toast.success("Sales channel updated");
11401
+ handleSuccess();
11402
+ },
11403
+ onError: (error) => {
11404
+ toast.error(error.message);
11547
11405
  }
11548
- onSubmit();
11549
11406
  }
11407
+ );
11408
+ });
11409
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11410
+ KeyboundForm,
11411
+ {
11412
+ className: "flex flex-1 flex-col overflow-hidden",
11413
+ onSubmit,
11414
+ children: [
11415
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11416
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11417
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11418
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11419
+ ] }) })
11420
+ ]
11421
+ }
11422
+ ) });
11423
+ };
11424
+ const SalesChannelField = ({ control, order }) => {
11425
+ const salesChannels = useComboboxData({
11426
+ queryFn: async (params) => {
11427
+ return await sdk.admin.salesChannel.list(params);
11550
11428
  },
11551
- [data, isSubmitting, onSubmit]
11552
- );
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: [
11570
- /* @__PURE__ */ jsx(
11571
- Text,
11429
+ queryKey: ["sales-channels"],
11430
+ getOptions: (data) => {
11431
+ return data.sales_channels.map((salesChannel) => ({
11432
+ label: salesChannel.name,
11433
+ value: salesChannel.id
11434
+ }));
11435
+ },
11436
+ defaultValue: order.sales_channel_id || void 0
11437
+ });
11438
+ return /* @__PURE__ */ jsx(
11439
+ Form$2.Field,
11440
+ {
11441
+ control,
11442
+ name: "sales_channel_id",
11443
+ render: ({ field }) => {
11444
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11445
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11446
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11447
+ Combobox,
11448
+ {
11449
+ options: salesChannels.options,
11450
+ fetchNextPage: salesChannels.fetchNextPage,
11451
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11452
+ searchValue: salesChannels.searchValue,
11453
+ onSearchValueChange: salesChannels.onSearchValueChange,
11454
+ placeholder: "Select sales channel",
11455
+ ...field
11456
+ }
11457
+ ) }),
11458
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11459
+ ] });
11460
+ }
11461
+ }
11462
+ );
11463
+ };
11464
+ const schema$4 = objectType({
11465
+ sales_channel_id: stringType().min(1)
11466
+ });
11467
+ const ShippingAddress = () => {
11468
+ const { id } = useParams();
11469
+ const { order, isPending, isError, error } = useOrder(id, {
11470
+ fields: "+shipping_address"
11471
+ });
11472
+ if (isError) {
11473
+ throw error;
11474
+ }
11475
+ const isReady = !isPending && !!order;
11476
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11477
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11478
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
11479
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11480
+ ] }),
11481
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
11482
+ ] });
11483
+ };
11484
+ const ShippingAddressForm = ({ order }) => {
11485
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11486
+ const form = useForm({
11487
+ defaultValues: {
11488
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11489
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11490
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11491
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11492
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11493
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11494
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11495
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11496
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11497
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11498
+ },
11499
+ resolver: zodResolver(schema$3)
11500
+ });
11501
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11502
+ const { handleSuccess } = useRouteModal();
11503
+ const onSubmit = form.handleSubmit(async (data) => {
11504
+ await mutateAsync(
11505
+ {
11506
+ shipping_address: {
11507
+ first_name: data.first_name,
11508
+ last_name: data.last_name,
11509
+ company: data.company,
11510
+ address_1: data.address_1,
11511
+ address_2: data.address_2,
11512
+ city: data.city,
11513
+ province: data.province,
11514
+ country_code: data.country_code,
11515
+ postal_code: data.postal_code,
11516
+ phone: data.phone
11517
+ }
11518
+ },
11519
+ {
11520
+ onSuccess: () => {
11521
+ handleSuccess();
11522
+ },
11523
+ onError: (error) => {
11524
+ toast.error(error.message);
11525
+ }
11526
+ }
11527
+ );
11528
+ });
11529
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11530
+ KeyboundForm,
11531
+ {
11532
+ className: "flex flex-1 flex-col overflow-hidden",
11533
+ onSubmit,
11534
+ children: [
11535
+ /* @__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: [
11536
+ /* @__PURE__ */ jsx(
11537
+ Form$2.Field,
11538
+ {
11539
+ control: form.control,
11540
+ name: "country_code",
11541
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11542
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
11543
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
11544
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11545
+ ] })
11546
+ }
11547
+ ),
11548
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11549
+ /* @__PURE__ */ jsx(
11550
+ Form$2.Field,
11572
11551
  {
11573
- size: "xsmall",
11574
- weight: "plus",
11575
- className: "text-ui-fg-muted",
11576
- children: "Shipping profile"
11552
+ control: form.control,
11553
+ name: "first_name",
11554
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11555
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
11556
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11557
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11558
+ ] })
11577
11559
  }
11578
11560
  ),
11579
11561
  /* @__PURE__ */ jsx(
11580
- Text,
11562
+ Form$2.Field,
11581
11563
  {
11582
- size: "xsmall",
11583
- weight: "plus",
11584
- className: "text-ui-fg-muted",
11585
- children: "Action"
11564
+ control: form.control,
11565
+ name: "last_name",
11566
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11567
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
11568
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11569
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11570
+ ] })
11586
11571
  }
11587
11572
  )
11588
11573
  ] }),
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,
11574
+ /* @__PURE__ */ jsx(
11575
+ Form$2.Field,
11576
+ {
11577
+ control: form.control,
11578
+ name: "company",
11579
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11580
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
11581
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11582
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11583
+ ] })
11584
+ }
11585
+ ),
11586
+ /* @__PURE__ */ jsx(
11587
+ Form$2.Field,
11588
+ {
11589
+ control: form.control,
11590
+ name: "address_1",
11591
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11592
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
11593
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11594
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11595
+ ] })
11596
+ }
11597
+ ),
11598
+ /* @__PURE__ */ jsx(
11599
+ Form$2.Field,
11600
+ {
11601
+ control: form.control,
11602
+ name: "address_2",
11603
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11604
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11605
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11606
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11607
+ ] })
11608
+ }
11609
+ ),
11610
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11611
+ /* @__PURE__ */ jsx(
11612
+ Form$2.Field,
11607
11613
  {
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." }) })
11614
+ control: form.control,
11615
+ name: "postal_code",
11616
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11617
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
11618
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11619
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11620
+ ] })
11621
+ }
11622
+ ),
11623
+ /* @__PURE__ */ jsx(
11624
+ Form$2.Field,
11625
+ {
11626
+ control: form.control,
11627
+ name: "city",
11628
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11629
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
11630
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11631
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11632
+ ] })
11633
+ }
11634
+ )
11970
11635
  ] }),
11971
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11972
11636
  /* @__PURE__ */ jsx(
11973
- LocationField,
11974
- {
11975
- control: form.control,
11976
- setValue: form.setValue
11977
- }
11978
- ),
11979
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11980
- /* @__PURE__ */ jsx(
11981
- ShippingOptionField,
11982
- {
11983
- shippingProfileId: data.shippingProfileId,
11984
- preview,
11985
- control: form.control
11986
- }
11987
- ),
11988
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11989
- /* @__PURE__ */ jsx(
11990
- CustomAmountField,
11637
+ Form$2.Field,
11991
11638
  {
11992
11639
  control: form.control,
11993
- currencyCode: order.currency_code
11994
- }
11995
- ),
11996
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11997
- /* @__PURE__ */ jsx(
11998
- ItemsPreview,
11999
- {
12000
- order,
12001
- shippingProfileId: data.shippingProfileId
11640
+ name: "province",
11641
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11642
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11643
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11644
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11645
+ ] })
12002
11646
  }
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" }) }),
11647
+ ),
12007
11648
  /* @__PURE__ */ jsx(
12008
- Button,
11649
+ Form$2.Field,
12009
11650
  {
12010
- size: "small",
12011
- type: "submit",
12012
- isLoading: isPending || isUpdatingShippingMethod,
12013
- children: data.shippingMethod ? "Update" : "Add"
11651
+ control: form.control,
11652
+ name: "phone",
11653
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11654
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
11655
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11656
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11657
+ ] })
12014
11658
  }
12015
11659
  )
11660
+ ] }) }),
11661
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11662
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11663
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12016
11664
  ] }) })
12017
11665
  ]
12018
11666
  }
12019
- ) }) });
11667
+ ) });
12020
11668
  };
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;
11669
+ const schema$3 = addressSchema;
11670
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
11671
+ const Shipping = () => {
11672
+ var _a;
11673
+ const { id } = useParams();
11674
+ const { order, isPending, isError, error } = useOrder(id, {
11675
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11676
+ });
11677
+ const {
11678
+ order: preview,
11679
+ isPending: isPreviewPending,
11680
+ isError: isPreviewError,
11681
+ error: previewError
11682
+ } = useOrderPreview(id);
11683
+ useInitiateOrderEdit({ preview });
11684
+ const { onCancel } = useCancelOrderEdit({ preview });
11685
+ if (isError) {
11686
+ throw error;
11687
+ }
11688
+ if (isPreviewError) {
11689
+ throw previewError;
11690
+ }
11691
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11692
+ const isReady = preview && !isPreviewPending && order && !isPending;
11693
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11694
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11695
+ /* @__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: [
11696
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11697
+ /* @__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." }) })
11698
+ ] }) }) }),
11699
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11700
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11701
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11702
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11703
+ ] }) });
11704
+ };
11705
+ const ShippingForm = ({ preview, order }) => {
11706
+ var _a;
11707
+ const { setIsOpen } = useStackedModal();
11708
+ const [isSubmitting, setIsSubmitting] = useState(false);
11709
+ const [data, setData] = useState(null);
11710
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11711
+ const { shipping_options } = useShippingOptions(
11712
+ {
11713
+ id: appliedShippingOptionIds,
11714
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11715
+ },
11716
+ {
11717
+ enabled: appliedShippingOptionIds.length > 0
12031
11718
  }
12032
11719
  );
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(
11720
+ const uniqueShippingProfiles = useMemo(() => {
11721
+ const profiles = /* @__PURE__ */ new Map();
11722
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
11723
+ profiles.set(profile.id, profile);
11724
+ });
11725
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11726
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
11727
+ });
11728
+ return Array.from(profiles.values());
11729
+ }, [order.items, shipping_options]);
11730
+ const { handleSuccess } = useRouteModal();
11731
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11732
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11733
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11734
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11735
+ const onSubmit = async () => {
11736
+ setIsSubmitting(true);
11737
+ let requestSucceeded = false;
11738
+ await requestOrderEdit(void 0, {
11739
+ onError: (e) => {
11740
+ toast.error(`Failed to request order edit: ${e.message}`);
11741
+ },
11742
+ onSuccess: () => {
11743
+ requestSucceeded = true;
11744
+ }
11745
+ });
11746
+ if (!requestSucceeded) {
11747
+ setIsSubmitting(false);
11748
+ return;
11749
+ }
11750
+ await confirmOrderEdit(void 0, {
11751
+ onError: (e) => {
11752
+ toast.error(`Failed to confirm order edit: ${e.message}`);
11753
+ },
11754
+ onSuccess: () => {
11755
+ handleSuccess();
11756
+ },
11757
+ onSettled: () => {
11758
+ setIsSubmitting(false);
11759
+ }
11760
+ });
11761
+ };
11762
+ const onKeydown = useCallback(
11763
+ (e) => {
11764
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11765
+ if (data || isSubmitting) {
11766
+ return;
11767
+ }
11768
+ onSubmit();
11769
+ }
11770
+ },
11771
+ [data, isSubmitting, onSubmit]
11772
+ );
11773
+ useEffect(() => {
11774
+ document.addEventListener("keydown", onKeydown);
11775
+ return () => {
11776
+ document.removeEventListener("keydown", onKeydown);
11777
+ };
11778
+ }, [onKeydown]);
11779
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11780
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11781
+ /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11782
+ /* @__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: [
11783
+ /* @__PURE__ */ jsxs("div", { children: [
11784
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11785
+ /* @__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." }) })
11786
+ ] }),
11787
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11788
+ /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11789
+ /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11790
+ /* @__PURE__ */ jsx(
12085
11791
  Text,
12086
11792
  {
12087
- size: "small",
12088
- leading: "compact",
12089
- className: "text-ui-fg-subtle",
12090
- children: [
12091
- item.quantity,
12092
- "x"
12093
- ]
11793
+ size: "xsmall",
11794
+ weight: "plus",
11795
+ className: "text-ui-fg-muted",
11796
+ children: "Shipping profile"
12094
11797
  }
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." })
11798
+ ),
11799
+ /* @__PURE__ */ jsx(
11800
+ Text,
11801
+ {
11802
+ size: "xsmall",
11803
+ weight: "plus",
11804
+ className: "text-ui-fg-muted",
11805
+ children: "Action"
11806
+ }
11807
+ )
12133
11808
  ] }),
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);
11809
+ /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
11810
+ var _a2, _b, _c, _d, _e, _f, _g;
11811
+ const items = getItemsWithShippingProfile(
11812
+ profile.id,
11813
+ order.items
11814
+ );
11815
+ const hasItems = items.length > 0;
11816
+ const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
11817
+ (option) => option.shipping_profile_id === profile.id
11818
+ );
11819
+ const shippingMethod = preview.shipping_methods.find(
11820
+ (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
11821
+ );
11822
+ const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
11823
+ (action) => action.action === "SHIPPING_ADD"
11824
+ );
11825
+ return /* @__PURE__ */ jsxs(
11826
+ Accordion.Item,
11827
+ {
11828
+ value: profile.id,
11829
+ className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
11830
+ children: [
11831
+ /* @__PURE__ */ jsxs("div", { className: "px-3 py-2 flex items-center justify-between gap-3", children: [
11832
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full overflow-hidden", children: [
11833
+ /* @__PURE__ */ jsx(Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(
11834
+ IconButton,
11835
+ {
11836
+ size: "2xsmall",
11837
+ variant: "transparent",
11838
+ className: "group/trigger",
11839
+ disabled: !hasItems,
11840
+ children: /* @__PURE__ */ jsx(TriangleRightMini, { className: "group-data-[state=open]/trigger:rotate-90 transition-transform" })
11841
+ }
11842
+ ) }),
11843
+ !shippingOption ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
11844
+ /* @__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" }) }) }),
11845
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col flex-1", children: [
11846
+ /* @__PURE__ */ jsx(
11847
+ Text,
11848
+ {
11849
+ size: "small",
11850
+ weight: "plus",
11851
+ leading: "compact",
11852
+ children: profile.name
11853
+ }
11854
+ ),
11855
+ /* @__PURE__ */ jsxs(
11856
+ Text,
11857
+ {
11858
+ size: "small",
11859
+ leading: "compact",
11860
+ className: "text-ui-fg-subtle",
11861
+ children: [
11862
+ items.length,
11863
+ " ",
11864
+ pluralize(items.length, "items", "item")
11865
+ ]
11866
+ }
11867
+ )
11868
+ ] })
11869
+ ] }) : /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-[5px] max-sm:flex-col max-sm:items-start flex-1 w-full overflow-hidden", children: [
11870
+ /* @__PURE__ */ jsx(
11871
+ Tooltip,
11872
+ {
11873
+ content: /* @__PURE__ */ jsx("ul", { children: items.map((item) => {
11874
+ var _a3, _b2, _c2;
11875
+ return /* @__PURE__ */ jsx(
11876
+ "li",
11877
+ {
11878
+ 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})`
11879
+ },
11880
+ item.id
11881
+ );
11882
+ }) }),
11883
+ children: /* @__PURE__ */ jsxs(
11884
+ Badge,
11885
+ {
11886
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
11887
+ size: "xsmall",
11888
+ children: [
11889
+ /* @__PURE__ */ jsx(Shopping, { className: "shrink-0" }),
11890
+ /* @__PURE__ */ jsxs("span", { className: "truncate", children: [
11891
+ items.reduce(
11892
+ (acc, item) => acc + item.quantity,
11893
+ 0
11894
+ ),
11895
+ "x",
11896
+ " ",
11897
+ pluralize(items.length, "items", "item")
11898
+ ] })
11899
+ ]
11900
+ }
11901
+ )
11902
+ }
11903
+ ),
11904
+ /* @__PURE__ */ jsx(
11905
+ Tooltip,
11906
+ {
11907
+ content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
11908
+ children: /* @__PURE__ */ jsxs(
11909
+ Badge,
11910
+ {
11911
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
11912
+ size: "xsmall",
11913
+ children: [
11914
+ /* @__PURE__ */ jsx(Buildings, { className: "shrink-0" }),
11915
+ /* @__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 })
11916
+ ]
11917
+ }
11918
+ )
11919
+ }
11920
+ ),
11921
+ /* @__PURE__ */ jsx(Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxs(
11922
+ Badge,
11923
+ {
11924
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
11925
+ size: "xsmall",
11926
+ children: [
11927
+ /* @__PURE__ */ jsx(TruckFast, { className: "shrink-0" }),
11928
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: shippingOption.name })
11929
+ ]
11930
+ }
11931
+ ) })
11932
+ ] })
11933
+ ] }),
11934
+ shippingOption ? /* @__PURE__ */ jsx(
11935
+ ActionMenu,
11936
+ {
11937
+ groups: [
11938
+ {
11939
+ actions: [
11940
+ hasItems ? {
11941
+ label: "Edit shipping option",
11942
+ icon: /* @__PURE__ */ jsx(Channels, {}),
11943
+ onClick: () => {
11944
+ setIsOpen(
11945
+ STACKED_FOCUS_MODAL_ID,
11946
+ true
11947
+ );
11948
+ setData({
11949
+ shippingProfileId: profile.id,
11950
+ shippingOption,
11951
+ shippingMethod
11952
+ });
11953
+ }
11954
+ } : void 0,
11955
+ {
11956
+ label: "Remove shipping option",
11957
+ icon: /* @__PURE__ */ jsx(Trash, {}),
11958
+ onClick: () => {
11959
+ if (shippingMethod) {
11960
+ if (addShippingMethodAction) {
11961
+ removeActionShippingMethod(
11962
+ addShippingMethodAction.id
11963
+ );
11964
+ } else {
11965
+ removeShippingMethod(
11966
+ shippingMethod.id
11967
+ );
11968
+ }
11969
+ }
11970
+ }
11971
+ }
11972
+ ].filter(Boolean)
11973
+ }
11974
+ ]
11975
+ }
11976
+ ) : /* @__PURE__ */ jsx(
11977
+ StackedModalTrigger,
11978
+ {
11979
+ shippingProfileId: profile.id,
11980
+ shippingOption,
11981
+ shippingMethod,
11982
+ setData,
11983
+ children: "Add shipping option"
11984
+ }
11985
+ )
11986
+ ] }),
11987
+ /* @__PURE__ */ jsxs(Accordion.Content, { children: [
11988
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11989
+ items.map((item, idx) => {
11990
+ var _a3, _b2, _c2, _d2, _e2;
11991
+ return /* @__PURE__ */ jsxs("div", { children: [
11992
+ /* @__PURE__ */ jsxs(
11993
+ "div",
11994
+ {
11995
+ className: "px-3 flex items-center gap-x-3",
11996
+ children: [
11997
+ /* @__PURE__ */ jsx("div", { className: "w-5 h-[56px] flex flex-col justify-center items-center", children: /* @__PURE__ */ jsx(
11998
+ Divider,
11999
+ {
12000
+ variant: "dashed",
12001
+ orientation: "vertical"
12002
+ }
12003
+ ) }),
12004
+ /* @__PURE__ */ jsxs("div", { className: "py-2 flex items-center gap-x-3", children: [
12005
+ /* @__PURE__ */ jsx("div", { className: "size-7 flex items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxs(
12006
+ Text,
12007
+ {
12008
+ size: "small",
12009
+ leading: "compact",
12010
+ className: "text-ui-fg-subtle",
12011
+ children: [
12012
+ item.quantity,
12013
+ "x"
12014
+ ]
12015
+ }
12016
+ ) }),
12017
+ /* @__PURE__ */ jsx(Thumbnail, { thumbnail: item.thumbnail }),
12018
+ /* @__PURE__ */ jsxs("div", { children: [
12019
+ /* @__PURE__ */ jsxs(
12020
+ Text,
12021
+ {
12022
+ size: "small",
12023
+ leading: "compact",
12024
+ weight: "plus",
12025
+ children: [
12026
+ (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
12027
+ " (",
12028
+ (_c2 = item.variant) == null ? void 0 : _c2.title,
12029
+ ")"
12030
+ ]
12031
+ }
12032
+ ),
12033
+ /* @__PURE__ */ jsx(
12034
+ Text,
12035
+ {
12036
+ size: "small",
12037
+ leading: "compact",
12038
+ className: "text-ui-fg-subtle",
12039
+ children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
12040
+ }
12041
+ )
12042
+ ] })
12043
+ ] })
12044
+ ]
12045
+ },
12046
+ item.id
12047
+ ),
12048
+ idx !== items.length - 1 && /* @__PURE__ */ jsx(Divider, { variant: "dashed" })
12049
+ ] }, item.id);
12050
+ })
12051
+ ] })
12052
+ ]
12149
12053
  },
12150
- ...field
12054
+ profile.id
12055
+ );
12056
+ }) })
12057
+ ] }) })
12058
+ ] }) }),
12059
+ /* @__PURE__ */ jsx(
12060
+ StackedFocusModal,
12061
+ {
12062
+ id: STACKED_FOCUS_MODAL_ID,
12063
+ onOpenChangeCallback: (open) => {
12064
+ if (!open) {
12065
+ setData(null);
12151
12066
  }
12152
- ) })
12153
- ] }) });
12154
- }
12155
- }
12156
- );
12067
+ return open;
12068
+ },
12069
+ children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
12070
+ }
12071
+ )
12072
+ ] }),
12073
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12074
+ /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12075
+ /* @__PURE__ */ jsx(
12076
+ Button,
12077
+ {
12078
+ size: "small",
12079
+ type: "button",
12080
+ isLoading: isSubmitting,
12081
+ onClick: onSubmit,
12082
+ children: "Save"
12083
+ }
12084
+ )
12085
+ ] }) })
12086
+ ] });
12157
12087
  };
12158
- const ShippingOptionField = ({
12088
+ const StackedModalTrigger = ({
12159
12089
  shippingProfileId,
12160
- preview,
12161
- control
12090
+ shippingOption,
12091
+ shippingMethod,
12092
+ setData,
12093
+ children
12162
12094
  }) => {
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
12095
+ const { setIsOpen, getIsOpen } = useStackedModal();
12096
+ const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
12097
+ const onToggle = () => {
12098
+ if (isOpen) {
12099
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12100
+ setData(null);
12101
+ } else {
12102
+ setIsOpen(STACKED_FOCUS_MODAL_ID, true);
12103
+ setData({
12104
+ shippingProfileId,
12105
+ shippingOption,
12106
+ shippingMethod
12172
12107
  });
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;
12181
- }
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
12108
  }
12226
- );
12227
- };
12228
- const CustomAmountField = ({
12229
- control,
12230
- currencyCode
12231
- }) => {
12109
+ };
12232
12110
  return /* @__PURE__ */ jsx(
12233
- Form$2.Field,
12111
+ Button,
12234
12112
  {
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
- }
12113
+ size: "small",
12114
+ variant: "secondary",
12115
+ onClick: onToggle,
12116
+ className: "text-ui-fg-primary shrink-0",
12117
+ children
12254
12118
  }
12255
12119
  );
12256
12120
  };
12257
- const ShippingAddress = () => {
12258
- const { id } = useParams();
12259
- const { order, isPending, isError, error } = useOrder(id, {
12260
- fields: "+shipping_address"
12261
- });
12262
- if (isError) {
12263
- throw error;
12264
- }
12265
- const isReady = !isPending && !!order;
12266
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12267
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12268
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12269
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12270
- ] }),
12271
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12272
- ] });
12273
- };
12274
- const ShippingAddressForm = ({ order }) => {
12275
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12121
+ const ShippingProfileForm = ({
12122
+ data,
12123
+ order,
12124
+ preview
12125
+ }) => {
12126
+ var _a, _b, _c, _d, _e, _f;
12127
+ const { setIsOpen } = useStackedModal();
12276
12128
  const form = useForm({
12129
+ resolver: zodResolver(shippingMethodSchema),
12277
12130
  defaultValues: {
12278
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12279
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12280
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12281
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12282
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12283
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12284
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12285
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12286
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12287
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12288
- },
12289
- resolver: zodResolver(schema$2)
12131
+ 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,
12132
+ shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
12133
+ custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
12134
+ }
12290
12135
  });
12291
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12292
- const { handleSuccess } = useRouteModal();
12293
- const onSubmit = form.handleSubmit(async (data) => {
12294
- await mutateAsync(
12295
- {
12296
- shipping_address: {
12297
- first_name: data.first_name,
12298
- last_name: data.last_name,
12299
- company: data.company,
12300
- address_1: data.address_1,
12301
- address_2: data.address_2,
12302
- city: data.city,
12303
- province: data.province,
12304
- country_code: data.country_code,
12305
- postal_code: data.postal_code,
12306
- phone: data.phone
12136
+ const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
12137
+ const {
12138
+ mutateAsync: updateShippingMethod,
12139
+ isPending: isUpdatingShippingMethod
12140
+ } = useDraftOrderUpdateShippingMethod(order.id);
12141
+ const onSubmit = form.handleSubmit(async (values) => {
12142
+ if (isEqual(values, form.formState.defaultValues)) {
12143
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12144
+ return;
12145
+ }
12146
+ if (data.shippingMethod) {
12147
+ await updateShippingMethod(
12148
+ {
12149
+ method_id: data.shippingMethod.id,
12150
+ shipping_option_id: values.shipping_option_id,
12151
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12152
+ },
12153
+ {
12154
+ onError: (e) => {
12155
+ toast.error(e.message);
12156
+ },
12157
+ onSuccess: () => {
12158
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12159
+ }
12307
12160
  }
12161
+ );
12162
+ return;
12163
+ }
12164
+ await addShippingMethod(
12165
+ {
12166
+ shipping_option_id: values.shipping_option_id,
12167
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12308
12168
  },
12309
12169
  {
12310
- onSuccess: () => {
12311
- handleSuccess();
12170
+ onError: (e) => {
12171
+ toast.error(e.message);
12312
12172
  },
12313
- onError: (error) => {
12314
- toast.error(error.message);
12173
+ onSuccess: () => {
12174
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12315
12175
  }
12316
12176
  }
12317
12177
  );
12318
12178
  });
12319
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12179
+ return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
12320
12180
  KeyboundForm,
12321
12181
  {
12322
- className: "flex flex-1 flex-col overflow-hidden",
12182
+ className: "flex h-full flex-col overflow-hidden",
12323
12183
  onSubmit,
12324
12184
  children: [
12325
- /* @__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: [
12185
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12186
+ /* @__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: [
12187
+ /* @__PURE__ */ jsxs("div", { children: [
12188
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12189
+ /* @__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." }) })
12190
+ ] }),
12191
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12326
12192
  /* @__PURE__ */ jsx(
12327
- Form$2.Field,
12193
+ LocationField,
12328
12194
  {
12329
12195
  control: form.control,
12330
- name: "country_code",
12331
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12332
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12333
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12334
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12335
- ] })
12196
+ setValue: form.setValue
12336
12197
  }
12337
12198
  ),
12338
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12339
- /* @__PURE__ */ jsx(
12340
- Form$2.Field,
12341
- {
12342
- control: form.control,
12343
- name: "first_name",
12344
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12345
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12346
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12347
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12348
- ] })
12349
- }
12350
- ),
12351
- /* @__PURE__ */ jsx(
12352
- Form$2.Field,
12353
- {
12354
- control: form.control,
12355
- name: "last_name",
12356
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12357
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12358
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12359
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12360
- ] })
12361
- }
12362
- )
12363
- ] }),
12199
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12364
12200
  /* @__PURE__ */ jsx(
12365
- Form$2.Field,
12201
+ ShippingOptionField,
12366
12202
  {
12367
- control: form.control,
12368
- name: "company",
12369
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12370
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12371
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12372
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12373
- ] })
12203
+ shippingProfileId: data.shippingProfileId,
12204
+ preview,
12205
+ control: form.control
12374
12206
  }
12375
12207
  ),
12208
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12376
12209
  /* @__PURE__ */ jsx(
12377
- Form$2.Field,
12210
+ CustomAmountField,
12378
12211
  {
12379
12212
  control: form.control,
12380
- name: "address_1",
12381
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12382
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12383
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12384
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12385
- ] })
12213
+ currencyCode: order.currency_code
12386
12214
  }
12387
12215
  ),
12216
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12388
12217
  /* @__PURE__ */ jsx(
12389
- Form$2.Field,
12218
+ ItemsPreview,
12390
12219
  {
12391
- control: form.control,
12392
- name: "address_2",
12393
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12394
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12395
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12396
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12397
- ] })
12220
+ order,
12221
+ shippingProfileId: data.shippingProfileId
12398
12222
  }
12399
- ),
12400
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12401
- /* @__PURE__ */ jsx(
12402
- Form$2.Field,
12403
- {
12404
- control: form.control,
12405
- name: "postal_code",
12406
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12407
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12408
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12409
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12410
- ] })
12411
- }
12412
- ),
12413
- /* @__PURE__ */ jsx(
12414
- Form$2.Field,
12223
+ )
12224
+ ] }) }) }),
12225
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12226
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12227
+ /* @__PURE__ */ jsx(
12228
+ Button,
12229
+ {
12230
+ size: "small",
12231
+ type: "submit",
12232
+ isLoading: isPending || isUpdatingShippingMethod,
12233
+ children: data.shippingMethod ? "Update" : "Add"
12234
+ }
12235
+ )
12236
+ ] }) })
12237
+ ]
12238
+ }
12239
+ ) }) });
12240
+ };
12241
+ const shippingMethodSchema = objectType({
12242
+ location_id: stringType(),
12243
+ shipping_option_id: stringType(),
12244
+ custom_amount: unionType([numberType(), stringType()]).optional()
12245
+ });
12246
+ const ItemsPreview = ({ order, shippingProfileId }) => {
12247
+ const matches = order.items.filter(
12248
+ (item) => {
12249
+ var _a, _b, _c;
12250
+ return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
12251
+ }
12252
+ );
12253
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12254
+ /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12255
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12256
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12257
+ ] }) }),
12258
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12259
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
12260
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12261
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
12262
+ ] }),
12263
+ /* @__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(
12264
+ "div",
12265
+ {
12266
+ className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
12267
+ children: [
12268
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12269
+ /* @__PURE__ */ jsx(
12270
+ Thumbnail,
12271
+ {
12272
+ thumbnail: item.thumbnail,
12273
+ alt: item.product_title ?? void 0
12274
+ }
12275
+ ),
12276
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12277
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12278
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12279
+ /* @__PURE__ */ jsxs(
12280
+ Text,
12281
+ {
12282
+ size: "small",
12283
+ leading: "compact",
12284
+ className: "text-ui-fg-subtle",
12285
+ children: [
12286
+ "(",
12287
+ item.variant_title,
12288
+ ")"
12289
+ ]
12290
+ }
12291
+ )
12292
+ ] }),
12293
+ /* @__PURE__ */ jsx(
12294
+ Text,
12295
+ {
12296
+ size: "small",
12297
+ leading: "compact",
12298
+ className: "text-ui-fg-subtle",
12299
+ children: item.variant_sku
12300
+ }
12301
+ )
12302
+ ] })
12303
+ ] }),
12304
+ /* @__PURE__ */ jsxs(
12305
+ Text,
12415
12306
  {
12416
- control: form.control,
12417
- name: "city",
12418
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12419
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12420
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12421
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12422
- ] })
12307
+ size: "small",
12308
+ leading: "compact",
12309
+ className: "text-ui-fg-subtle",
12310
+ children: [
12311
+ item.quantity,
12312
+ "x"
12313
+ ]
12423
12314
  }
12424
12315
  )
12316
+ ]
12317
+ },
12318
+ item.id
12319
+ )) : /* @__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: [
12320
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12321
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12322
+ 'No items found for "',
12323
+ query,
12324
+ '".'
12325
+ ] })
12326
+ ] }) })
12327
+ ] })
12328
+ ] });
12329
+ };
12330
+ const LocationField = ({ control, setValue }) => {
12331
+ const locations = useComboboxData({
12332
+ queryKey: ["locations"],
12333
+ queryFn: async (params) => {
12334
+ return await sdk.admin.stockLocation.list(params);
12335
+ },
12336
+ getOptions: (data) => {
12337
+ return data.stock_locations.map((location) => ({
12338
+ label: location.name,
12339
+ value: location.id
12340
+ }));
12341
+ }
12342
+ });
12343
+ return /* @__PURE__ */ jsx(
12344
+ Form$2.Field,
12345
+ {
12346
+ control,
12347
+ name: "location_id",
12348
+ render: ({ field: { onChange, ...field } }) => {
12349
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12350
+ /* @__PURE__ */ jsxs("div", { children: [
12351
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
12352
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12353
+ ] }),
12354
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12355
+ Combobox,
12356
+ {
12357
+ options: locations.options,
12358
+ fetchNextPage: locations.fetchNextPage,
12359
+ isFetchingNextPage: locations.isFetchingNextPage,
12360
+ searchValue: locations.searchValue,
12361
+ onSearchValueChange: locations.onSearchValueChange,
12362
+ placeholder: "Select location",
12363
+ onChange: (value) => {
12364
+ setValue("shipping_option_id", "", {
12365
+ shouldDirty: true,
12366
+ shouldTouch: true
12367
+ });
12368
+ onChange(value);
12369
+ },
12370
+ ...field
12371
+ }
12372
+ ) })
12373
+ ] }) });
12374
+ }
12375
+ }
12376
+ );
12377
+ };
12378
+ const ShippingOptionField = ({
12379
+ shippingProfileId,
12380
+ preview,
12381
+ control
12382
+ }) => {
12383
+ var _a;
12384
+ const locationId = useWatch({ control, name: "location_id" });
12385
+ const shippingOptions = useComboboxData({
12386
+ queryKey: ["shipping_options", locationId, shippingProfileId],
12387
+ queryFn: async (params) => {
12388
+ return await sdk.admin.shippingOption.list({
12389
+ ...params,
12390
+ stock_location_id: locationId,
12391
+ shipping_profile_id: shippingProfileId
12392
+ });
12393
+ },
12394
+ getOptions: (data) => {
12395
+ return data.shipping_options.map((option) => {
12396
+ var _a2;
12397
+ if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12398
+ (r) => r.attribute === "is_return" && r.value === "true"
12399
+ )) {
12400
+ return void 0;
12401
+ }
12402
+ return {
12403
+ label: option.name,
12404
+ value: option.id
12405
+ };
12406
+ }).filter(Boolean);
12407
+ },
12408
+ enabled: !!locationId && !!shippingProfileId,
12409
+ defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12410
+ });
12411
+ const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12412
+ return /* @__PURE__ */ jsx(
12413
+ Form$2.Field,
12414
+ {
12415
+ control,
12416
+ name: "shipping_option_id",
12417
+ render: ({ field }) => {
12418
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12419
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12420
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12421
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12425
12422
  ] }),
12426
12423
  /* @__PURE__ */ jsx(
12427
- Form$2.Field,
12424
+ ConditionalTooltip,
12428
12425
  {
12429
- control: form.control,
12430
- name: "province",
12431
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12432
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12433
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12434
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12435
- ] })
12426
+ content: tooltipContent,
12427
+ showTooltip: !locationId || !shippingProfileId,
12428
+ children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12429
+ Combobox,
12430
+ {
12431
+ options: shippingOptions.options,
12432
+ fetchNextPage: shippingOptions.fetchNextPage,
12433
+ isFetchingNextPage: shippingOptions.isFetchingNextPage,
12434
+ searchValue: shippingOptions.searchValue,
12435
+ onSearchValueChange: shippingOptions.onSearchValueChange,
12436
+ placeholder: "Select shipping option",
12437
+ ...field,
12438
+ disabled: !locationId || !shippingProfileId
12439
+ }
12440
+ ) }) })
12436
12441
  }
12437
- ),
12438
- /* @__PURE__ */ jsx(
12439
- Form$2.Field,
12442
+ )
12443
+ ] }) });
12444
+ }
12445
+ }
12446
+ );
12447
+ };
12448
+ const CustomAmountField = ({
12449
+ control,
12450
+ currencyCode
12451
+ }) => {
12452
+ return /* @__PURE__ */ jsx(
12453
+ Form$2.Field,
12454
+ {
12455
+ control,
12456
+ name: "custom_amount",
12457
+ render: ({ field: { onChange, ...field } }) => {
12458
+ return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12459
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12460
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12461
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12462
+ ] }),
12463
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12464
+ CurrencyInput,
12440
12465
  {
12441
- control: form.control,
12442
- name: "phone",
12443
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12444
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12445
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12446
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12447
- ] })
12466
+ ...field,
12467
+ onValueChange: (value) => onChange(value),
12468
+ symbol: getNativeSymbol(currencyCode),
12469
+ code: currencyCode
12448
12470
  }
12449
- )
12450
- ] }) }),
12451
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12452
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12453
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12454
- ] }) })
12455
- ]
12471
+ ) })
12472
+ ] });
12473
+ }
12456
12474
  }
12457
- ) });
12475
+ );
12458
12476
  };
12459
- const schema$2 = addressSchema;
12460
12477
  const TransferOwnership = () => {
12461
12478
  const { id } = useParams();
12462
12479
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12480,7 +12497,7 @@ const TransferOwnershipForm = ({ order }) => {
12480
12497
  defaultValues: {
12481
12498
  customer_id: order.customer_id || ""
12482
12499
  },
12483
- resolver: zodResolver(schema$1)
12500
+ resolver: zodResolver(schema$2)
12484
12501
  });
12485
12502
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12486
12503
  const { handleSuccess } = useRouteModal();
@@ -12930,49 +12947,40 @@ const Illustration = () => {
12930
12947
  }
12931
12948
  );
12932
12949
  };
12933
- const schema$1 = objectType({
12950
+ const schema$2 = objectType({
12934
12951
  customer_id: stringType().min(1)
12935
12952
  });
12936
- const SalesChannel = () => {
12953
+ const Email = () => {
12937
12954
  const { id } = useParams();
12938
- const { draft_order, isPending, isError, error } = useDraftOrder(
12939
- id,
12940
- {
12941
- fields: "+sales_channel_id"
12942
- },
12943
- {
12944
- enabled: !!id
12945
- }
12946
- );
12955
+ const { order, isPending, isError, error } = useOrder(id, {
12956
+ fields: "+email"
12957
+ });
12947
12958
  if (isError) {
12948
12959
  throw error;
12949
12960
  }
12950
- const ISrEADY = !!draft_order && !isPending;
12961
+ const isReady = !isPending && !!order;
12951
12962
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12952
12963
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12953
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
12954
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12964
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
12965
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
12955
12966
  ] }),
12956
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
12967
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
12957
12968
  ] });
12958
12969
  };
12959
- const SalesChannelForm = ({ order }) => {
12970
+ const EmailForm = ({ order }) => {
12960
12971
  const form = useForm({
12961
12972
  defaultValues: {
12962
- sales_channel_id: order.sales_channel_id || ""
12973
+ email: order.email ?? ""
12963
12974
  },
12964
- resolver: zodResolver(schema)
12975
+ resolver: zodResolver(schema$1)
12965
12976
  });
12966
12977
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12967
12978
  const { handleSuccess } = useRouteModal();
12968
12979
  const onSubmit = form.handleSubmit(async (data) => {
12969
12980
  await mutateAsync(
12970
- {
12971
- sales_channel_id: data.sales_channel_id
12972
- },
12981
+ { email: data.email },
12973
12982
  {
12974
12983
  onSuccess: () => {
12975
- toast.success("Sales channel updated");
12976
12984
  handleSuccess();
12977
12985
  },
12978
12986
  onError: (error) => {
@@ -12987,7 +12995,18 @@ const SalesChannelForm = ({ order }) => {
12987
12995
  className: "flex flex-1 flex-col overflow-hidden",
12988
12996
  onSubmit,
12989
12997
  children: [
12990
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
12998
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
12999
+ Form$2.Field,
13000
+ {
13001
+ control: form.control,
13002
+ name: "email",
13003
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13004
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
13005
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13006
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13007
+ ] })
13008
+ }
13009
+ ) }),
12991
13010
  /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12992
13011
  /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12993
13012
  /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
@@ -12996,48 +13015,29 @@ const SalesChannelForm = ({ order }) => {
12996
13015
  }
12997
13016
  ) });
12998
13017
  };
12999
- const SalesChannelField = ({ control, order }) => {
13000
- const salesChannels = useComboboxData({
13001
- queryFn: async (params) => {
13002
- return await sdk.admin.salesChannel.list(params);
13003
- },
13004
- queryKey: ["sales-channels"],
13005
- getOptions: (data) => {
13006
- return data.sales_channels.map((salesChannel) => ({
13007
- label: salesChannel.name,
13008
- value: salesChannel.id
13009
- }));
13010
- },
13011
- defaultValue: order.sales_channel_id || void 0
13018
+ const schema$1 = objectType({
13019
+ email: stringType().email()
13020
+ });
13021
+ const CustomItems = () => {
13022
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
13023
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
13024
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
13025
+ ] });
13026
+ };
13027
+ const CustomItemsForm = () => {
13028
+ const form = useForm({
13029
+ resolver: zodResolver(schema)
13012
13030
  });
13013
- return /* @__PURE__ */ jsx(
13014
- Form$2.Field,
13015
- {
13016
- control,
13017
- name: "sales_channel_id",
13018
- render: ({ field }) => {
13019
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13020
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
13021
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
13022
- Combobox,
13023
- {
13024
- options: salesChannels.options,
13025
- fetchNextPage: salesChannels.fetchNextPage,
13026
- isFetchingNextPage: salesChannels.isFetchingNextPage,
13027
- searchValue: salesChannels.searchValue,
13028
- onSearchValueChange: salesChannels.onSearchValueChange,
13029
- placeholder: "Select sales channel",
13030
- ...field
13031
- }
13032
- ) }),
13033
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13034
- ] });
13035
- }
13036
- }
13037
- );
13031
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
13032
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
13033
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13034
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13035
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
13036
+ ] }) })
13037
+ ] }) });
13038
13038
  };
13039
13039
  const schema = objectType({
13040
- sales_channel_id: stringType().min(1)
13040
+ email: stringType().email()
13041
13041
  });
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
@@ -13063,14 +13063,6 @@ const routeModule = {
13063
13063
  Component: BillingAddress,
13064
13064
  path: "/draft-orders/:id/billing-address"
13065
13065
  },
13066
- {
13067
- Component: CustomItems,
13068
- path: "/draft-orders/:id/custom-items"
13069
- },
13070
- {
13071
- Component: Email,
13072
- path: "/draft-orders/:id/email"
13073
- },
13074
13066
  {
13075
13067
  Component: Items,
13076
13068
  path: "/draft-orders/:id/items"
@@ -13084,20 +13076,28 @@ const routeModule = {
13084
13076
  path: "/draft-orders/:id/promotions"
13085
13077
  },
13086
13078
  {
13087
- Component: Shipping,
13088
- path: "/draft-orders/:id/shipping"
13079
+ Component: SalesChannel,
13080
+ path: "/draft-orders/:id/sales-channel"
13089
13081
  },
13090
13082
  {
13091
13083
  Component: ShippingAddress,
13092
13084
  path: "/draft-orders/:id/shipping-address"
13093
13085
  },
13086
+ {
13087
+ Component: Shipping,
13088
+ path: "/draft-orders/:id/shipping"
13089
+ },
13094
13090
  {
13095
13091
  Component: TransferOwnership,
13096
13092
  path: "/draft-orders/:id/transfer-ownership"
13097
13093
  },
13098
13094
  {
13099
- Component: SalesChannel,
13100
- path: "/draft-orders/:id/sales-channel"
13095
+ Component: Email,
13096
+ path: "/draft-orders/:id/email"
13097
+ },
13098
+ {
13099
+ Component: CustomItems,
13100
+ path: "/draft-orders/:id/custom-items"
13101
13101
  }
13102
13102
  ]
13103
13103
  }