@medusajs/draft-order 2.10.2-snapshot-20250903100537 → 2.10.2-snapshot-20250903132457

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.
@@ -9554,27 +9554,6 @@ const ID = () => {
9554
9554
  /* @__PURE__ */ jsx(Outlet, {})
9555
9555
  ] });
9556
9556
  };
9557
- const CustomItems = () => {
9558
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9559
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9560
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9561
- ] });
9562
- };
9563
- const CustomItemsForm = () => {
9564
- const form = useForm({
9565
- resolver: zodResolver(schema$5)
9566
- });
9567
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9568
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9569
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9570
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9571
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9572
- ] }) })
9573
- ] }) });
9574
- };
9575
- const schema$5 = objectType({
9576
- email: stringType().email()
9577
- });
9578
9557
  const BillingAddress = () => {
9579
9558
  const { id } = useParams();
9580
9559
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9607,7 +9586,7 @@ const BillingAddressForm = ({ order }) => {
9607
9586
  postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9608
9587
  phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9609
9588
  },
9610
- resolver: zodResolver(schema$4)
9589
+ resolver: zodResolver(schema$5)
9611
9590
  });
9612
9591
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9613
9592
  const { handleSuccess } = useRouteModal();
@@ -9764,7 +9743,7 @@ const BillingAddressForm = ({ order }) => {
9764
9743
  }
9765
9744
  ) });
9766
9745
  };
9767
- const schema$4 = addressSchema;
9746
+ const schema$5 = addressSchema;
9768
9747
  const Email = () => {
9769
9748
  const { id } = useParams();
9770
9749
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9787,7 +9766,7 @@ const EmailForm = ({ order }) => {
9787
9766
  defaultValues: {
9788
9767
  email: order.email ?? ""
9789
9768
  },
9790
- resolver: zodResolver(schema$3)
9769
+ resolver: zodResolver(schema$4)
9791
9770
  });
9792
9771
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9793
9772
  const { handleSuccess } = useRouteModal();
@@ -9830,7 +9809,7 @@ const EmailForm = ({ order }) => {
9830
9809
  }
9831
9810
  ) });
9832
9811
  };
9833
- const schema$3 = objectType({
9812
+ const schema$4 = objectType({
9834
9813
  email: stringType().email()
9835
9814
  });
9836
9815
  const InlineTip = forwardRef(
@@ -10183,6 +10162,26 @@ function getHasUneditableRows(metadata) {
10183
10162
  (value) => !EDITABLE_TYPES.includes(typeof value)
10184
10163
  );
10185
10164
  }
10165
+ const PROMOTION_QUERY_KEY = "promotions";
10166
+ const promotionsQueryKeys = {
10167
+ list: (query2) => [
10168
+ PROMOTION_QUERY_KEY,
10169
+ query2 ? query2 : void 0
10170
+ ],
10171
+ detail: (id, query2) => [
10172
+ PROMOTION_QUERY_KEY,
10173
+ id,
10174
+ query2 ? query2 : void 0
10175
+ ]
10176
+ };
10177
+ const usePromotions = (query2, options) => {
10178
+ const { data, ...rest } = useQuery({
10179
+ queryKey: promotionsQueryKeys.list(query2),
10180
+ queryFn: async () => sdk.admin.promotion.list(query2),
10181
+ ...options
10182
+ });
10183
+ return { ...data, ...rest };
10184
+ };
10186
10185
  const useCancelOrderEdit = ({ preview }) => {
10187
10186
  const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10188
10187
  const onCancel = useCallback(async () => {
@@ -10229,80 +10228,85 @@ const useInitiateOrderEdit = ({
10229
10228
  run();
10230
10229
  }, [preview, navigate, mutateAsync]);
10231
10230
  };
10232
- function convertNumber(value) {
10233
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10234
- }
10235
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
10236
- const Shipping = () => {
10237
- var _a;
10231
+ const Promotions = () => {
10238
10232
  const { id } = useParams();
10239
- const { order, isPending, isError, error } = useOrder(id, {
10240
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
10241
- });
10242
10233
  const {
10243
10234
  order: preview,
10244
- isPending: isPreviewPending,
10245
10235
  isError: isPreviewError,
10246
10236
  error: previewError
10247
- } = useOrderPreview(id);
10237
+ } = useOrderPreview(id, void 0);
10248
10238
  useInitiateOrderEdit({ preview });
10249
10239
  const { onCancel } = useCancelOrderEdit({ preview });
10250
- if (isError) {
10251
- throw error;
10252
- }
10253
10240
  if (isPreviewError) {
10254
10241
  throw previewError;
10255
10242
  }
10256
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
10257
- const isReady = preview && !isPreviewPending && order && !isPending;
10258
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
10259
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10260
- /* @__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: [
10261
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10262
- /* @__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." }) })
10263
- ] }) }) }),
10264
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
10265
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
10266
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
10267
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10268
- ] }) });
10243
+ const isReady = !!preview;
10244
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10245
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10246
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10247
+ ] });
10269
10248
  };
10270
- const ShippingForm = ({ preview, order }) => {
10271
- var _a;
10272
- const { setIsOpen } = useStackedModal();
10249
+ const PromotionForm = ({ preview }) => {
10250
+ const { items, shipping_methods } = preview;
10273
10251
  const [isSubmitting, setIsSubmitting] = useState(false);
10274
- const [data, setData] = useState(null);
10275
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
10276
- const { shipping_options } = useShippingOptions(
10252
+ const [comboboxValue, setComboboxValue] = useState("");
10253
+ const { handleSuccess } = useRouteModal();
10254
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10255
+ const promoCodes = getPromotionCodes(items, shipping_methods);
10256
+ const { promotions, isPending, isError, error } = usePromotions(
10277
10257
  {
10278
- id: appliedShippingOptionIds,
10279
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
10258
+ code: promoCodes
10280
10259
  },
10281
10260
  {
10282
- enabled: appliedShippingOptionIds.length > 0
10261
+ enabled: !!promoCodes.length
10283
10262
  }
10284
10263
  );
10285
- const uniqueShippingProfiles = useMemo(() => {
10286
- const profiles = /* @__PURE__ */ new Map();
10287
- getUniqueShippingProfiles(order.items).forEach((profile) => {
10288
- profiles.set(profile.id, profile);
10289
- });
10290
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
10291
- profiles.set(option.shipping_profile_id, option.shipping_profile);
10292
- });
10293
- return Array.from(profiles.values());
10294
- }, [order.items, shipping_options]);
10295
- const { handleSuccess } = useRouteModal();
10264
+ const comboboxData = useComboboxData({
10265
+ queryKey: ["promotions", "combobox", promoCodes],
10266
+ queryFn: async (params) => {
10267
+ return await sdk.admin.promotion.list({
10268
+ ...params,
10269
+ code: {
10270
+ $nin: promoCodes
10271
+ }
10272
+ });
10273
+ },
10274
+ getOptions: (data) => {
10275
+ return data.promotions.map((promotion) => ({
10276
+ label: promotion.code,
10277
+ value: promotion.code
10278
+ }));
10279
+ }
10280
+ });
10281
+ const add = async (value) => {
10282
+ if (!value) {
10283
+ return;
10284
+ }
10285
+ addPromotions(
10286
+ {
10287
+ promo_codes: [value]
10288
+ },
10289
+ {
10290
+ onError: (e) => {
10291
+ toast.error(e.message);
10292
+ comboboxData.onSearchValueChange("");
10293
+ setComboboxValue("");
10294
+ },
10295
+ onSuccess: () => {
10296
+ comboboxData.onSearchValueChange("");
10297
+ setComboboxValue("");
10298
+ }
10299
+ }
10300
+ );
10301
+ };
10296
10302
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10297
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10298
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
10299
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
10303
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10300
10304
  const onSubmit = async () => {
10301
10305
  setIsSubmitting(true);
10302
10306
  let requestSucceeded = false;
10303
10307
  await requestOrderEdit(void 0, {
10304
10308
  onError: (e) => {
10305
- toast.error(`Failed to request order edit: ${e.message}`);
10309
+ toast.error(e.message);
10306
10310
  },
10307
10311
  onSuccess: () => {
10308
10312
  requestSucceeded = true;
@@ -10314,7 +10318,7 @@ const ShippingForm = ({ preview, order }) => {
10314
10318
  }
10315
10319
  await confirmOrderEdit(void 0, {
10316
10320
  onError: (e) => {
10317
- toast.error(`Failed to confirm order edit: ${e.message}`);
10321
+ toast.error(e.message);
10318
10322
  },
10319
10323
  onSuccess: () => {
10320
10324
  handleSuccess();
@@ -10324,1104 +10328,1079 @@ const ShippingForm = ({ preview, order }) => {
10324
10328
  }
10325
10329
  });
10326
10330
  };
10327
- const onKeydown = useCallback(
10328
- (e) => {
10329
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10330
- if (data || isSubmitting) {
10331
- return;
10331
+ if (isError) {
10332
+ throw error;
10333
+ }
10334
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10335
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
10336
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
10337
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10338
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10339
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10340
+ ] }),
10341
+ /* @__PURE__ */ jsx(
10342
+ Combobox,
10343
+ {
10344
+ id: "promotion-combobox",
10345
+ "aria-describedby": "promotion-combobox-hint",
10346
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10347
+ fetchNextPage: comboboxData.fetchNextPage,
10348
+ options: comboboxData.options,
10349
+ onSearchValueChange: comboboxData.onSearchValueChange,
10350
+ searchValue: comboboxData.searchValue,
10351
+ disabled: comboboxData.disabled || isAddingPromotions,
10352
+ onChange: add,
10353
+ value: comboboxValue
10354
+ }
10355
+ )
10356
+ ] }),
10357
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10358
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
10359
+ PromotionItem,
10360
+ {
10361
+ promotion,
10362
+ orderId: preview.id,
10363
+ isLoading: isPending
10364
+ },
10365
+ promotion.id
10366
+ )) })
10367
+ ] }) }),
10368
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10369
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10370
+ /* @__PURE__ */ jsx(
10371
+ Button,
10372
+ {
10373
+ size: "small",
10374
+ type: "submit",
10375
+ isLoading: isSubmitting || isAddingPromotions,
10376
+ children: "Save"
10377
+ }
10378
+ )
10379
+ ] }) })
10380
+ ] });
10381
+ };
10382
+ const PromotionItem = ({
10383
+ promotion,
10384
+ orderId,
10385
+ isLoading
10386
+ }) => {
10387
+ var _a;
10388
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10389
+ const onRemove = async () => {
10390
+ removePromotions(
10391
+ {
10392
+ promo_codes: [promotion.code]
10393
+ },
10394
+ {
10395
+ onError: (e) => {
10396
+ toast.error(e.message);
10332
10397
  }
10333
- onSubmit();
10334
10398
  }
10335
- },
10336
- [data, isSubmitting, onSubmit]
10337
- );
10338
- useEffect(() => {
10339
- document.addEventListener("keydown", onKeydown);
10340
- return () => {
10341
- document.removeEventListener("keydown", onKeydown);
10342
- };
10343
- }, [onKeydown]);
10344
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10345
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10346
- /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
10347
- /* @__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: [
10399
+ );
10400
+ };
10401
+ const displayValue = getDisplayValue(promotion);
10402
+ return /* @__PURE__ */ jsxs(
10403
+ "div",
10404
+ {
10405
+ className: clx(
10406
+ "px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between",
10407
+ {
10408
+ "animate-pulse": isLoading
10409
+ }
10410
+ ),
10411
+ children: [
10348
10412
  /* @__PURE__ */ jsxs("div", { children: [
10349
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10350
- /* @__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." }) })
10351
- ] }),
10352
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10353
- /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
10354
- /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
10355
- /* @__PURE__ */ jsx(
10356
- Text,
10357
- {
10358
- size: "xsmall",
10359
- weight: "plus",
10360
- className: "text-ui-fg-muted",
10361
- children: "Shipping profile"
10362
- }
10363
- ),
10364
- /* @__PURE__ */ jsx(
10365
- Text,
10366
- {
10367
- size: "xsmall",
10368
- weight: "plus",
10369
- className: "text-ui-fg-muted",
10370
- children: "Action"
10371
- }
10372
- )
10373
- ] }),
10374
- /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
10375
- var _a2, _b, _c, _d, _e, _f, _g;
10376
- const items = getItemsWithShippingProfile(
10377
- profile.id,
10378
- order.items
10379
- );
10380
- const hasItems = items.length > 0;
10381
- const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
10382
- (option) => option.shipping_profile_id === profile.id
10383
- );
10384
- const shippingMethod = preview.shipping_methods.find(
10385
- (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
10386
- );
10387
- const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
10388
- (action) => action.action === "SHIPPING_ADD"
10389
- );
10390
- return /* @__PURE__ */ jsxs(
10391
- Accordion.Item,
10392
- {
10393
- value: profile.id,
10394
- className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
10395
- children: [
10396
- /* @__PURE__ */ jsxs("div", { className: "px-3 py-2 flex items-center justify-between gap-3", children: [
10397
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full overflow-hidden", children: [
10398
- /* @__PURE__ */ jsx(Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(
10399
- IconButton,
10400
- {
10401
- size: "2xsmall",
10402
- variant: "transparent",
10403
- className: "group/trigger",
10404
- disabled: !hasItems,
10405
- children: /* @__PURE__ */ jsx(TriangleRightMini, { className: "group-data-[state=open]/trigger:rotate-90 transition-transform" })
10406
- }
10407
- ) }),
10408
- !shippingOption ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10409
- /* @__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" }) }) }),
10410
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col flex-1", children: [
10411
- /* @__PURE__ */ jsx(
10412
- Text,
10413
- {
10414
- size: "small",
10415
- weight: "plus",
10416
- leading: "compact",
10417
- children: profile.name
10418
- }
10419
- ),
10420
- /* @__PURE__ */ jsxs(
10421
- Text,
10422
- {
10423
- size: "small",
10424
- leading: "compact",
10425
- className: "text-ui-fg-subtle",
10426
- children: [
10427
- items.length,
10428
- " ",
10429
- pluralize(items.length, "items", "item")
10430
- ]
10431
- }
10432
- )
10433
- ] })
10434
- ] }) : /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-[5px] max-sm:flex-col max-sm:items-start flex-1 w-full overflow-hidden", children: [
10435
- /* @__PURE__ */ jsx(
10436
- Tooltip,
10437
- {
10438
- content: /* @__PURE__ */ jsx("ul", { children: items.map((item) => {
10439
- var _a3, _b2, _c2;
10440
- return /* @__PURE__ */ jsx(
10441
- "li",
10442
- {
10443
- 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})`
10444
- },
10445
- item.id
10446
- );
10447
- }) }),
10448
- children: /* @__PURE__ */ jsxs(
10449
- Badge,
10450
- {
10451
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
10452
- size: "xsmall",
10453
- children: [
10454
- /* @__PURE__ */ jsx(Shopping, { className: "shrink-0" }),
10455
- /* @__PURE__ */ jsxs("span", { className: "truncate", children: [
10456
- items.reduce(
10457
- (acc, item) => acc + item.quantity,
10458
- 0
10459
- ),
10460
- "x",
10461
- " ",
10462
- pluralize(items.length, "items", "item")
10463
- ] })
10464
- ]
10465
- }
10466
- )
10467
- }
10468
- ),
10469
- /* @__PURE__ */ jsx(
10470
- Tooltip,
10471
- {
10472
- content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
10473
- children: /* @__PURE__ */ jsxs(
10474
- Badge,
10475
- {
10476
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
10477
- size: "xsmall",
10478
- children: [
10479
- /* @__PURE__ */ jsx(Buildings, { className: "shrink-0" }),
10480
- /* @__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 })
10481
- ]
10482
- }
10483
- )
10484
- }
10485
- ),
10486
- /* @__PURE__ */ jsx(Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxs(
10487
- Badge,
10488
- {
10489
- className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
10490
- size: "xsmall",
10491
- children: [
10492
- /* @__PURE__ */ jsx(TruckFast, { className: "shrink-0" }),
10493
- /* @__PURE__ */ jsx("span", { className: "truncate", children: shippingOption.name })
10494
- ]
10495
- }
10496
- ) })
10497
- ] })
10498
- ] }),
10499
- shippingOption ? /* @__PURE__ */ jsx(
10500
- ActionMenu,
10501
- {
10502
- groups: [
10503
- {
10504
- actions: [
10505
- hasItems ? {
10506
- label: "Edit shipping option",
10507
- icon: /* @__PURE__ */ jsx(Channels, {}),
10508
- onClick: () => {
10509
- setIsOpen(
10510
- STACKED_FOCUS_MODAL_ID,
10511
- true
10512
- );
10513
- setData({
10514
- shippingProfileId: profile.id,
10515
- shippingOption,
10516
- shippingMethod
10517
- });
10518
- }
10519
- } : void 0,
10520
- {
10521
- label: "Remove shipping option",
10522
- icon: /* @__PURE__ */ jsx(Trash, {}),
10523
- onClick: () => {
10524
- if (shippingMethod) {
10525
- if (addShippingMethodAction) {
10526
- removeActionShippingMethod(
10527
- addShippingMethodAction.id
10528
- );
10529
- } else {
10530
- removeShippingMethod(
10531
- shippingMethod.id
10532
- );
10533
- }
10534
- }
10535
- }
10536
- }
10537
- ].filter(Boolean)
10538
- }
10539
- ]
10540
- }
10541
- ) : /* @__PURE__ */ jsx(
10542
- StackedModalTrigger$1,
10543
- {
10544
- shippingProfileId: profile.id,
10545
- shippingOption,
10546
- shippingMethod,
10547
- setData,
10548
- children: "Add shipping option"
10549
- }
10550
- )
10551
- ] }),
10552
- /* @__PURE__ */ jsxs(Accordion.Content, { children: [
10553
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10554
- items.map((item, idx) => {
10555
- var _a3, _b2, _c2, _d2, _e2;
10556
- return /* @__PURE__ */ jsxs("div", { children: [
10557
- /* @__PURE__ */ jsxs(
10558
- "div",
10559
- {
10560
- className: "px-3 flex items-center gap-x-3",
10561
- children: [
10562
- /* @__PURE__ */ jsx("div", { className: "w-5 h-[56px] flex flex-col justify-center items-center", children: /* @__PURE__ */ jsx(
10563
- Divider,
10564
- {
10565
- variant: "dashed",
10566
- orientation: "vertical"
10567
- }
10568
- ) }),
10569
- /* @__PURE__ */ jsxs("div", { className: "py-2 flex items-center gap-x-3", children: [
10570
- /* @__PURE__ */ jsx("div", { className: "size-7 flex items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxs(
10571
- Text,
10572
- {
10573
- size: "small",
10574
- leading: "compact",
10575
- className: "text-ui-fg-subtle",
10576
- children: [
10577
- item.quantity,
10578
- "x"
10579
- ]
10580
- }
10581
- ) }),
10582
- /* @__PURE__ */ jsx(Thumbnail, { thumbnail: item.thumbnail }),
10583
- /* @__PURE__ */ jsxs("div", { children: [
10584
- /* @__PURE__ */ jsxs(
10585
- Text,
10586
- {
10587
- size: "small",
10588
- leading: "compact",
10589
- weight: "plus",
10590
- children: [
10591
- (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
10592
- " (",
10593
- (_c2 = item.variant) == null ? void 0 : _c2.title,
10594
- ")"
10595
- ]
10596
- }
10597
- ),
10598
- /* @__PURE__ */ jsx(
10599
- Text,
10600
- {
10601
- size: "small",
10602
- leading: "compact",
10603
- className: "text-ui-fg-subtle",
10604
- children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
10605
- }
10606
- )
10607
- ] })
10608
- ] })
10609
- ]
10610
- },
10611
- item.id
10612
- ),
10613
- idx !== items.length - 1 && /* @__PURE__ */ jsx(Divider, { variant: "dashed" })
10614
- ] }, item.id);
10615
- })
10616
- ] })
10617
- ]
10618
- },
10619
- profile.id
10620
- );
10621
- }) })
10622
- ] }) })
10623
- ] }) }),
10624
- /* @__PURE__ */ jsx(
10625
- StackedFocusModal,
10626
- {
10627
- id: STACKED_FOCUS_MODAL_ID,
10628
- onOpenChangeCallback: (open) => {
10629
- if (!open) {
10630
- setData(null);
10631
- }
10632
- return open;
10633
- },
10634
- children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
10635
- }
10636
- )
10637
- ] }),
10638
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
10639
- /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10640
- /* @__PURE__ */ jsx(
10641
- Button,
10642
- {
10643
- size: "small",
10644
- type: "button",
10645
- isLoading: isSubmitting,
10646
- onClick: onSubmit,
10647
- children: "Save"
10648
- }
10649
- )
10650
- ] }) })
10651
- ] });
10413
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
10414
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5 text-ui-fg-subtle", children: [
10415
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
10416
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
10417
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
10418
+ ] }),
10419
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
10420
+ ] })
10421
+ ] }),
10422
+ /* @__PURE__ */ jsx(
10423
+ IconButton,
10424
+ {
10425
+ size: "small",
10426
+ type: "button",
10427
+ variant: "transparent",
10428
+ onClick: onRemove,
10429
+ isLoading: isPending || isLoading,
10430
+ children: /* @__PURE__ */ jsx(XMark, {})
10431
+ }
10432
+ )
10433
+ ]
10434
+ },
10435
+ promotion.id
10436
+ );
10652
10437
  };
10653
- const StackedModalTrigger$1 = ({
10654
- shippingProfileId,
10655
- shippingOption,
10656
- shippingMethod,
10657
- setData,
10658
- children
10659
- }) => {
10660
- const { setIsOpen, getIsOpen } = useStackedModal();
10661
- const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
10662
- const onToggle = () => {
10663
- if (isOpen) {
10664
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
10665
- setData(null);
10666
- } else {
10667
- setIsOpen(STACKED_FOCUS_MODAL_ID, true);
10668
- setData({
10669
- shippingProfileId,
10670
- shippingOption,
10671
- shippingMethod
10672
- });
10438
+ function getDisplayValue(promotion) {
10439
+ var _a, _b, _c, _d;
10440
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
10441
+ if (!value) {
10442
+ return null;
10443
+ }
10444
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
10445
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
10446
+ if (!currency) {
10447
+ return null;
10673
10448
  }
10674
- };
10675
- return /* @__PURE__ */ jsx(
10676
- Button,
10449
+ return getLocaleAmount(value, currency);
10450
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
10451
+ return formatPercentage(value);
10452
+ }
10453
+ return null;
10454
+ }
10455
+ const formatter = new Intl.NumberFormat([], {
10456
+ style: "percent",
10457
+ minimumFractionDigits: 2
10458
+ });
10459
+ const formatPercentage = (value, isPercentageValue = false) => {
10460
+ let val = value || 0;
10461
+ if (!isPercentageValue) {
10462
+ val = val / 100;
10463
+ }
10464
+ return formatter.format(val);
10465
+ };
10466
+ function getPromotionCodes(items, shippingMethods) {
10467
+ const codes = /* @__PURE__ */ new Set();
10468
+ for (const item of items) {
10469
+ if (item.adjustments) {
10470
+ for (const adjustment of item.adjustments) {
10471
+ if (adjustment.code) {
10472
+ codes.add(adjustment.code);
10473
+ }
10474
+ }
10475
+ }
10476
+ }
10477
+ for (const shippingMethod of shippingMethods) {
10478
+ if (shippingMethod.adjustments) {
10479
+ for (const adjustment of shippingMethod.adjustments) {
10480
+ if (adjustment.code) {
10481
+ codes.add(adjustment.code);
10482
+ }
10483
+ }
10484
+ }
10485
+ }
10486
+ return Array.from(codes);
10487
+ }
10488
+ const SalesChannel = () => {
10489
+ const { id } = useParams();
10490
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10491
+ id,
10677
10492
  {
10678
- size: "small",
10679
- variant: "secondary",
10680
- onClick: onToggle,
10681
- className: "text-ui-fg-primary shrink-0",
10682
- children
10493
+ fields: "+sales_channel_id"
10494
+ },
10495
+ {
10496
+ enabled: !!id
10683
10497
  }
10684
10498
  );
10499
+ if (isError) {
10500
+ throw error;
10501
+ }
10502
+ const ISrEADY = !!draft_order && !isPending;
10503
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10504
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10505
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
10506
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
10507
+ ] }),
10508
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
10509
+ ] });
10685
10510
  };
10686
- const ShippingProfileForm = ({
10687
- data,
10688
- order,
10689
- preview
10690
- }) => {
10691
- var _a, _b, _c, _d, _e, _f;
10692
- const { setIsOpen } = useStackedModal();
10511
+ const SalesChannelForm = ({ order }) => {
10693
10512
  const form = useForm({
10694
- resolver: zodResolver(shippingMethodSchema),
10695
10513
  defaultValues: {
10696
- 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,
10697
- shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
10698
- custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
10699
- }
10514
+ sales_channel_id: order.sales_channel_id || ""
10515
+ },
10516
+ resolver: zodResolver(schema$3)
10700
10517
  });
10701
- const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
10702
- const {
10703
- mutateAsync: updateShippingMethod,
10704
- isPending: isUpdatingShippingMethod
10705
- } = useDraftOrderUpdateShippingMethod(order.id);
10706
- const onSubmit = form.handleSubmit(async (values) => {
10707
- if (isEqual(values, form.formState.defaultValues)) {
10708
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
10709
- return;
10710
- }
10711
- if (data.shippingMethod) {
10712
- await updateShippingMethod(
10713
- {
10714
- method_id: data.shippingMethod.id,
10715
- shipping_option_id: values.shipping_option_id,
10716
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
10717
- },
10718
- {
10719
- onError: (e) => {
10720
- toast.error(e.message);
10721
- },
10722
- onSuccess: () => {
10723
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
10724
- }
10725
- }
10726
- );
10727
- return;
10728
- }
10729
- await addShippingMethod(
10518
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10519
+ const { handleSuccess } = useRouteModal();
10520
+ const onSubmit = form.handleSubmit(async (data) => {
10521
+ await mutateAsync(
10730
10522
  {
10731
- shipping_option_id: values.shipping_option_id,
10732
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
10523
+ sales_channel_id: data.sales_channel_id
10733
10524
  },
10734
10525
  {
10735
- onError: (e) => {
10736
- toast.error(e.message);
10737
- },
10738
10526
  onSuccess: () => {
10739
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
10527
+ toast.success("Sales channel updated");
10528
+ handleSuccess();
10529
+ },
10530
+ onError: (error) => {
10531
+ toast.error(error.message);
10740
10532
  }
10741
10533
  }
10742
10534
  );
10743
10535
  });
10744
- return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
10536
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10745
10537
  KeyboundForm,
10746
10538
  {
10747
- className: "flex h-full flex-col overflow-hidden",
10539
+ className: "flex flex-1 flex-col overflow-hidden",
10748
10540
  onSubmit,
10749
10541
  children: [
10750
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
10751
- /* @__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: [
10752
- /* @__PURE__ */ jsxs("div", { children: [
10753
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10754
- /* @__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." }) })
10755
- ] }),
10756
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10757
- /* @__PURE__ */ jsx(
10758
- LocationField,
10759
- {
10760
- control: form.control,
10761
- setValue: form.setValue
10762
- }
10763
- ),
10764
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10765
- /* @__PURE__ */ jsx(
10766
- ShippingOptionField,
10767
- {
10768
- shippingProfileId: data.shippingProfileId,
10769
- preview,
10770
- control: form.control
10771
- }
10772
- ),
10773
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10774
- /* @__PURE__ */ jsx(
10775
- CustomAmountField,
10776
- {
10777
- control: form.control,
10778
- currencyCode: order.currency_code
10779
- }
10780
- ),
10781
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10782
- /* @__PURE__ */ jsx(
10783
- ItemsPreview,
10784
- {
10785
- order,
10786
- shippingProfileId: data.shippingProfileId
10787
- }
10788
- )
10789
- ] }) }) }),
10790
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
10791
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10792
- /* @__PURE__ */ jsx(
10793
- Button,
10794
- {
10795
- size: "small",
10796
- type: "submit",
10797
- isLoading: isPending || isUpdatingShippingMethod,
10798
- children: data.shippingMethod ? "Update" : "Add"
10799
- }
10800
- )
10542
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
10543
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10544
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10545
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10801
10546
  ] }) })
10802
10547
  ]
10803
10548
  }
10804
- ) }) });
10549
+ ) });
10805
10550
  };
10806
- const shippingMethodSchema = objectType({
10807
- location_id: stringType(),
10808
- shipping_option_id: stringType(),
10809
- custom_amount: unionType([numberType(), stringType()]).optional()
10551
+ const SalesChannelField = ({ control, order }) => {
10552
+ const salesChannels = useComboboxData({
10553
+ queryFn: async (params) => {
10554
+ return await sdk.admin.salesChannel.list(params);
10555
+ },
10556
+ queryKey: ["sales-channels"],
10557
+ getOptions: (data) => {
10558
+ return data.sales_channels.map((salesChannel) => ({
10559
+ label: salesChannel.name,
10560
+ value: salesChannel.id
10561
+ }));
10562
+ },
10563
+ defaultValue: order.sales_channel_id || void 0
10564
+ });
10565
+ return /* @__PURE__ */ jsx(
10566
+ Form$2.Field,
10567
+ {
10568
+ control,
10569
+ name: "sales_channel_id",
10570
+ render: ({ field }) => {
10571
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
10572
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
10573
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10574
+ Combobox,
10575
+ {
10576
+ options: salesChannels.options,
10577
+ fetchNextPage: salesChannels.fetchNextPage,
10578
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
10579
+ searchValue: salesChannels.searchValue,
10580
+ onSearchValueChange: salesChannels.onSearchValueChange,
10581
+ placeholder: "Select sales channel",
10582
+ ...field
10583
+ }
10584
+ ) }),
10585
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10586
+ ] });
10587
+ }
10588
+ }
10589
+ );
10590
+ };
10591
+ const schema$3 = objectType({
10592
+ sales_channel_id: stringType().min(1)
10810
10593
  });
10811
- const ItemsPreview = ({ order, shippingProfileId }) => {
10812
- const matches = order.items.filter(
10813
- (item) => {
10814
- var _a, _b, _c;
10815
- return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
10594
+ function convertNumber(value) {
10595
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10596
+ }
10597
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
10598
+ const Shipping = () => {
10599
+ var _a;
10600
+ const { id } = useParams();
10601
+ const { order, isPending, isError, error } = useOrder(id, {
10602
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
10603
+ });
10604
+ const {
10605
+ order: preview,
10606
+ isPending: isPreviewPending,
10607
+ isError: isPreviewError,
10608
+ error: previewError
10609
+ } = useOrderPreview(id);
10610
+ useInitiateOrderEdit({ preview });
10611
+ const { onCancel } = useCancelOrderEdit({ preview });
10612
+ if (isError) {
10613
+ throw error;
10614
+ }
10615
+ if (isPreviewError) {
10616
+ throw previewError;
10617
+ }
10618
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
10619
+ const isReady = preview && !isPreviewPending && order && !isPending;
10620
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
10621
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10622
+ /* @__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: [
10623
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10624
+ /* @__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." }) })
10625
+ ] }) }) }),
10626
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
10627
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
10628
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
10629
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10630
+ ] }) });
10631
+ };
10632
+ const ShippingForm = ({ preview, order }) => {
10633
+ var _a;
10634
+ const { setIsOpen } = useStackedModal();
10635
+ const [isSubmitting, setIsSubmitting] = useState(false);
10636
+ const [data, setData] = useState(null);
10637
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
10638
+ const { shipping_options } = useShippingOptions(
10639
+ {
10640
+ id: appliedShippingOptionIds,
10641
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
10642
+ },
10643
+ {
10644
+ enabled: appliedShippingOptionIds.length > 0
10645
+ }
10646
+ );
10647
+ const uniqueShippingProfiles = useMemo(() => {
10648
+ const profiles = /* @__PURE__ */ new Map();
10649
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
10650
+ profiles.set(profile.id, profile);
10651
+ });
10652
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
10653
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
10654
+ });
10655
+ return Array.from(profiles.values());
10656
+ }, [order.items, shipping_options]);
10657
+ const { handleSuccess } = useRouteModal();
10658
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10659
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10660
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
10661
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
10662
+ const onSubmit = async () => {
10663
+ setIsSubmitting(true);
10664
+ let requestSucceeded = false;
10665
+ await requestOrderEdit(void 0, {
10666
+ onError: (e) => {
10667
+ toast.error(`Failed to request order edit: ${e.message}`);
10668
+ },
10669
+ onSuccess: () => {
10670
+ requestSucceeded = true;
10671
+ }
10672
+ });
10673
+ if (!requestSucceeded) {
10674
+ setIsSubmitting(false);
10675
+ return;
10816
10676
  }
10677
+ await confirmOrderEdit(void 0, {
10678
+ onError: (e) => {
10679
+ toast.error(`Failed to confirm order edit: ${e.message}`);
10680
+ },
10681
+ onSuccess: () => {
10682
+ handleSuccess();
10683
+ },
10684
+ onSettled: () => {
10685
+ setIsSubmitting(false);
10686
+ }
10687
+ });
10688
+ };
10689
+ const onKeydown = useCallback(
10690
+ (e) => {
10691
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10692
+ if (data || isSubmitting) {
10693
+ return;
10694
+ }
10695
+ onSubmit();
10696
+ }
10697
+ },
10698
+ [data, isSubmitting, onSubmit]
10817
10699
  );
10818
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
10819
- /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10820
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
10821
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
10822
- ] }) }),
10823
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10824
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
10825
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
10826
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
10827
- ] }),
10828
- /* @__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(
10829
- "div",
10830
- {
10831
- className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
10832
- children: [
10833
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10834
- /* @__PURE__ */ jsx(
10835
- Thumbnail,
10836
- {
10837
- thumbnail: item.thumbnail,
10838
- alt: item.product_title ?? void 0
10839
- }
10840
- ),
10841
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10842
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
10843
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10844
- /* @__PURE__ */ jsxs(
10845
- Text,
10846
- {
10847
- size: "small",
10848
- leading: "compact",
10849
- className: "text-ui-fg-subtle",
10850
- children: [
10851
- "(",
10852
- item.variant_title,
10853
- ")"
10854
- ]
10855
- }
10856
- )
10857
- ] }),
10858
- /* @__PURE__ */ jsx(
10859
- Text,
10860
- {
10861
- size: "small",
10862
- leading: "compact",
10863
- className: "text-ui-fg-subtle",
10864
- children: item.variant_sku
10865
- }
10866
- )
10867
- ] })
10868
- ] }),
10869
- /* @__PURE__ */ jsxs(
10700
+ useEffect(() => {
10701
+ document.addEventListener("keydown", onKeydown);
10702
+ return () => {
10703
+ document.removeEventListener("keydown", onKeydown);
10704
+ };
10705
+ }, [onKeydown]);
10706
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10707
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10708
+ /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
10709
+ /* @__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: [
10710
+ /* @__PURE__ */ jsxs("div", { children: [
10711
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10712
+ /* @__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." }) })
10713
+ ] }),
10714
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10715
+ /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
10716
+ /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
10717
+ /* @__PURE__ */ jsx(
10870
10718
  Text,
10871
10719
  {
10872
- size: "small",
10873
- leading: "compact",
10874
- className: "text-ui-fg-subtle",
10720
+ size: "xsmall",
10721
+ weight: "plus",
10722
+ className: "text-ui-fg-muted",
10723
+ children: "Shipping profile"
10724
+ }
10725
+ ),
10726
+ /* @__PURE__ */ jsx(
10727
+ Text,
10728
+ {
10729
+ size: "xsmall",
10730
+ weight: "plus",
10731
+ className: "text-ui-fg-muted",
10732
+ children: "Action"
10733
+ }
10734
+ )
10735
+ ] }),
10736
+ /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
10737
+ var _a2, _b, _c, _d, _e, _f, _g;
10738
+ const items = getItemsWithShippingProfile(
10739
+ profile.id,
10740
+ order.items
10741
+ );
10742
+ const hasItems = items.length > 0;
10743
+ const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
10744
+ (option) => option.shipping_profile_id === profile.id
10745
+ );
10746
+ const shippingMethod = preview.shipping_methods.find(
10747
+ (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
10748
+ );
10749
+ const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
10750
+ (action) => action.action === "SHIPPING_ADD"
10751
+ );
10752
+ return /* @__PURE__ */ jsxs(
10753
+ Accordion.Item,
10754
+ {
10755
+ value: profile.id,
10756
+ className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
10875
10757
  children: [
10876
- item.quantity,
10877
- "x"
10758
+ /* @__PURE__ */ jsxs("div", { className: "px-3 py-2 flex items-center justify-between gap-3", children: [
10759
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full overflow-hidden", children: [
10760
+ /* @__PURE__ */ jsx(Accordion.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(
10761
+ IconButton,
10762
+ {
10763
+ size: "2xsmall",
10764
+ variant: "transparent",
10765
+ className: "group/trigger",
10766
+ disabled: !hasItems,
10767
+ children: /* @__PURE__ */ jsx(TriangleRightMini, { className: "group-data-[state=open]/trigger:rotate-90 transition-transform" })
10768
+ }
10769
+ ) }),
10770
+ !shippingOption ? /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10771
+ /* @__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" }) }) }),
10772
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col flex-1", children: [
10773
+ /* @__PURE__ */ jsx(
10774
+ Text,
10775
+ {
10776
+ size: "small",
10777
+ weight: "plus",
10778
+ leading: "compact",
10779
+ children: profile.name
10780
+ }
10781
+ ),
10782
+ /* @__PURE__ */ jsxs(
10783
+ Text,
10784
+ {
10785
+ size: "small",
10786
+ leading: "compact",
10787
+ className: "text-ui-fg-subtle",
10788
+ children: [
10789
+ items.length,
10790
+ " ",
10791
+ pluralize(items.length, "items", "item")
10792
+ ]
10793
+ }
10794
+ )
10795
+ ] })
10796
+ ] }) : /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-[5px] max-sm:flex-col max-sm:items-start flex-1 w-full overflow-hidden", children: [
10797
+ /* @__PURE__ */ jsx(
10798
+ Tooltip,
10799
+ {
10800
+ content: /* @__PURE__ */ jsx("ul", { children: items.map((item) => {
10801
+ var _a3, _b2, _c2;
10802
+ return /* @__PURE__ */ jsx(
10803
+ "li",
10804
+ {
10805
+ 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})`
10806
+ },
10807
+ item.id
10808
+ );
10809
+ }) }),
10810
+ children: /* @__PURE__ */ jsxs(
10811
+ Badge,
10812
+ {
10813
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
10814
+ size: "xsmall",
10815
+ children: [
10816
+ /* @__PURE__ */ jsx(Shopping, { className: "shrink-0" }),
10817
+ /* @__PURE__ */ jsxs("span", { className: "truncate", children: [
10818
+ items.reduce(
10819
+ (acc, item) => acc + item.quantity,
10820
+ 0
10821
+ ),
10822
+ "x",
10823
+ " ",
10824
+ pluralize(items.length, "items", "item")
10825
+ ] })
10826
+ ]
10827
+ }
10828
+ )
10829
+ }
10830
+ ),
10831
+ /* @__PURE__ */ jsx(
10832
+ Tooltip,
10833
+ {
10834
+ content: (_d = (_c = (_b = shippingOption.service_zone) == null ? void 0 : _b.fulfillment_set) == null ? void 0 : _c.location) == null ? void 0 : _d.name,
10835
+ children: /* @__PURE__ */ jsxs(
10836
+ Badge,
10837
+ {
10838
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
10839
+ size: "xsmall",
10840
+ children: [
10841
+ /* @__PURE__ */ jsx(Buildings, { className: "shrink-0" }),
10842
+ /* @__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 })
10843
+ ]
10844
+ }
10845
+ )
10846
+ }
10847
+ ),
10848
+ /* @__PURE__ */ jsx(Tooltip, { content: shippingOption.name, children: /* @__PURE__ */ jsxs(
10849
+ Badge,
10850
+ {
10851
+ className: "flex items-center gap-x-[3px] overflow-hidden cursor-default",
10852
+ size: "xsmall",
10853
+ children: [
10854
+ /* @__PURE__ */ jsx(TruckFast, { className: "shrink-0" }),
10855
+ /* @__PURE__ */ jsx("span", { className: "truncate", children: shippingOption.name })
10856
+ ]
10857
+ }
10858
+ ) })
10859
+ ] })
10860
+ ] }),
10861
+ shippingOption ? /* @__PURE__ */ jsx(
10862
+ ActionMenu,
10863
+ {
10864
+ groups: [
10865
+ {
10866
+ actions: [
10867
+ hasItems ? {
10868
+ label: "Edit shipping option",
10869
+ icon: /* @__PURE__ */ jsx(Channels, {}),
10870
+ onClick: () => {
10871
+ setIsOpen(
10872
+ STACKED_FOCUS_MODAL_ID,
10873
+ true
10874
+ );
10875
+ setData({
10876
+ shippingProfileId: profile.id,
10877
+ shippingOption,
10878
+ shippingMethod
10879
+ });
10880
+ }
10881
+ } : void 0,
10882
+ {
10883
+ label: "Remove shipping option",
10884
+ icon: /* @__PURE__ */ jsx(Trash, {}),
10885
+ onClick: () => {
10886
+ if (shippingMethod) {
10887
+ if (addShippingMethodAction) {
10888
+ removeActionShippingMethod(
10889
+ addShippingMethodAction.id
10890
+ );
10891
+ } else {
10892
+ removeShippingMethod(
10893
+ shippingMethod.id
10894
+ );
10895
+ }
10896
+ }
10897
+ }
10898
+ }
10899
+ ].filter(Boolean)
10900
+ }
10901
+ ]
10902
+ }
10903
+ ) : /* @__PURE__ */ jsx(
10904
+ StackedModalTrigger$1,
10905
+ {
10906
+ shippingProfileId: profile.id,
10907
+ shippingOption,
10908
+ shippingMethod,
10909
+ setData,
10910
+ children: "Add shipping option"
10911
+ }
10912
+ )
10913
+ ] }),
10914
+ /* @__PURE__ */ jsxs(Accordion.Content, { children: [
10915
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10916
+ items.map((item, idx) => {
10917
+ var _a3, _b2, _c2, _d2, _e2;
10918
+ return /* @__PURE__ */ jsxs("div", { children: [
10919
+ /* @__PURE__ */ jsxs(
10920
+ "div",
10921
+ {
10922
+ className: "px-3 flex items-center gap-x-3",
10923
+ children: [
10924
+ /* @__PURE__ */ jsx("div", { className: "w-5 h-[56px] flex flex-col justify-center items-center", children: /* @__PURE__ */ jsx(
10925
+ Divider,
10926
+ {
10927
+ variant: "dashed",
10928
+ orientation: "vertical"
10929
+ }
10930
+ ) }),
10931
+ /* @__PURE__ */ jsxs("div", { className: "py-2 flex items-center gap-x-3", children: [
10932
+ /* @__PURE__ */ jsx("div", { className: "size-7 flex items-center justify-center tabular-nums", children: /* @__PURE__ */ jsxs(
10933
+ Text,
10934
+ {
10935
+ size: "small",
10936
+ leading: "compact",
10937
+ className: "text-ui-fg-subtle",
10938
+ children: [
10939
+ item.quantity,
10940
+ "x"
10941
+ ]
10942
+ }
10943
+ ) }),
10944
+ /* @__PURE__ */ jsx(Thumbnail, { thumbnail: item.thumbnail }),
10945
+ /* @__PURE__ */ jsxs("div", { children: [
10946
+ /* @__PURE__ */ jsxs(
10947
+ Text,
10948
+ {
10949
+ size: "small",
10950
+ leading: "compact",
10951
+ weight: "plus",
10952
+ children: [
10953
+ (_b2 = (_a3 = item.variant) == null ? void 0 : _a3.product) == null ? void 0 : _b2.title,
10954
+ " (",
10955
+ (_c2 = item.variant) == null ? void 0 : _c2.title,
10956
+ ")"
10957
+ ]
10958
+ }
10959
+ ),
10960
+ /* @__PURE__ */ jsx(
10961
+ Text,
10962
+ {
10963
+ size: "small",
10964
+ leading: "compact",
10965
+ className: "text-ui-fg-subtle",
10966
+ children: (_e2 = (_d2 = item.variant) == null ? void 0 : _d2.options) == null ? void 0 : _e2.map((option) => option.value).join(" · ")
10967
+ }
10968
+ )
10969
+ ] })
10970
+ ] })
10971
+ ]
10972
+ },
10973
+ item.id
10974
+ ),
10975
+ idx !== items.length - 1 && /* @__PURE__ */ jsx(Divider, { variant: "dashed" })
10976
+ ] }, item.id);
10977
+ })
10978
+ ] })
10878
10979
  ]
10879
- }
10880
- )
10881
- ]
10882
- },
10883
- item.id
10884
- )) : /* @__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: [
10885
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10886
- /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
10887
- 'No items found for "',
10888
- query,
10889
- '".'
10890
- ] })
10891
- ] }) })
10892
- ] })
10893
- ] });
10894
- };
10895
- const LocationField = ({ control, setValue }) => {
10896
- const locations = useComboboxData({
10897
- queryKey: ["locations"],
10898
- queryFn: async (params) => {
10899
- return await sdk.admin.stockLocation.list(params);
10900
- },
10901
- getOptions: (data) => {
10902
- return data.stock_locations.map((location) => ({
10903
- label: location.name,
10904
- value: location.id
10905
- }));
10906
- }
10907
- });
10908
- return /* @__PURE__ */ jsx(
10909
- Form$2.Field,
10910
- {
10911
- control,
10912
- name: "location_id",
10913
- render: ({ field: { onChange, ...field } }) => {
10914
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10915
- /* @__PURE__ */ jsxs("div", { children: [
10916
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
10917
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
10918
- ] }),
10919
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10920
- Combobox,
10921
- {
10922
- options: locations.options,
10923
- fetchNextPage: locations.fetchNextPage,
10924
- isFetchingNextPage: locations.isFetchingNextPage,
10925
- searchValue: locations.searchValue,
10926
- onSearchValueChange: locations.onSearchValueChange,
10927
- placeholder: "Select location",
10928
- onChange: (value) => {
10929
- setValue("shipping_option_id", "", {
10930
- shouldDirty: true,
10931
- shouldTouch: true
10932
- });
10933
- onChange(value);
10934
10980
  },
10935
- ...field
10936
- }
10937
- ) })
10938
- ] }) });
10939
- }
10940
- }
10941
- );
10942
- };
10943
- const ShippingOptionField = ({
10944
- shippingProfileId,
10945
- preview,
10946
- control
10947
- }) => {
10948
- var _a;
10949
- const locationId = useWatch({ control, name: "location_id" });
10950
- const shippingOptions = useComboboxData({
10951
- queryKey: ["shipping_options", locationId, shippingProfileId],
10952
- queryFn: async (params) => {
10953
- return await sdk.admin.shippingOption.list({
10954
- ...params,
10955
- stock_location_id: locationId,
10956
- shipping_profile_id: shippingProfileId
10957
- });
10958
- },
10959
- getOptions: (data) => {
10960
- return data.shipping_options.map((option) => {
10961
- var _a2;
10962
- if ((_a2 = option.rules) == null ? void 0 : _a2.find(
10963
- (r) => r.attribute === "is_return" && r.value === "true"
10964
- )) {
10965
- return void 0;
10966
- }
10967
- return {
10968
- label: option.name,
10969
- value: option.id
10970
- };
10971
- }).filter(Boolean);
10972
- },
10973
- enabled: !!locationId && !!shippingProfileId,
10974
- defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
10975
- });
10976
- const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
10977
- return /* @__PURE__ */ jsx(
10978
- Form$2.Field,
10979
- {
10980
- control,
10981
- name: "shipping_option_id",
10982
- render: ({ field }) => {
10983
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10984
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10985
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
10986
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
10987
- ] }),
10988
- /* @__PURE__ */ jsx(
10989
- ConditionalTooltip,
10990
- {
10991
- content: tooltipContent,
10992
- showTooltip: !locationId || !shippingProfileId,
10993
- children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10994
- Combobox,
10995
- {
10996
- options: shippingOptions.options,
10997
- fetchNextPage: shippingOptions.fetchNextPage,
10998
- isFetchingNextPage: shippingOptions.isFetchingNextPage,
10999
- searchValue: shippingOptions.searchValue,
11000
- onSearchValueChange: shippingOptions.onSearchValueChange,
11001
- placeholder: "Select shipping option",
11002
- ...field,
11003
- disabled: !locationId || !shippingProfileId
11004
- }
11005
- ) }) })
10981
+ profile.id
10982
+ );
10983
+ }) })
10984
+ ] }) })
10985
+ ] }) }),
10986
+ /* @__PURE__ */ jsx(
10987
+ StackedFocusModal,
10988
+ {
10989
+ id: STACKED_FOCUS_MODAL_ID,
10990
+ onOpenChangeCallback: (open) => {
10991
+ if (!open) {
10992
+ setData(null);
11006
10993
  }
11007
- )
11008
- ] }) });
11009
- }
11010
- }
11011
- );
10994
+ return open;
10995
+ },
10996
+ children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
10997
+ }
10998
+ )
10999
+ ] }),
11000
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
11001
+ /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11002
+ /* @__PURE__ */ jsx(
11003
+ Button,
11004
+ {
11005
+ size: "small",
11006
+ type: "button",
11007
+ isLoading: isSubmitting,
11008
+ onClick: onSubmit,
11009
+ children: "Save"
11010
+ }
11011
+ )
11012
+ ] }) })
11013
+ ] });
11012
11014
  };
11013
- const CustomAmountField = ({
11014
- control,
11015
- currencyCode
11015
+ const StackedModalTrigger$1 = ({
11016
+ shippingProfileId,
11017
+ shippingOption,
11018
+ shippingMethod,
11019
+ setData,
11020
+ children
11016
11021
  }) => {
11022
+ const { setIsOpen, getIsOpen } = useStackedModal();
11023
+ const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
11024
+ const onToggle = () => {
11025
+ if (isOpen) {
11026
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11027
+ setData(null);
11028
+ } else {
11029
+ setIsOpen(STACKED_FOCUS_MODAL_ID, true);
11030
+ setData({
11031
+ shippingProfileId,
11032
+ shippingOption,
11033
+ shippingMethod
11034
+ });
11035
+ }
11036
+ };
11017
11037
  return /* @__PURE__ */ jsx(
11018
- Form$2.Field,
11038
+ Button,
11019
11039
  {
11020
- control,
11021
- name: "custom_amount",
11022
- render: ({ field: { onChange, ...field } }) => {
11023
- return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11024
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11025
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
11026
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
11027
- ] }),
11028
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11029
- CurrencyInput,
11030
- {
11031
- ...field,
11032
- onValueChange: (value) => onChange(value),
11033
- symbol: getNativeSymbol(currencyCode),
11034
- code: currencyCode
11035
- }
11036
- ) })
11037
- ] });
11038
- }
11040
+ size: "small",
11041
+ variant: "secondary",
11042
+ onClick: onToggle,
11043
+ className: "text-ui-fg-primary shrink-0",
11044
+ children
11039
11045
  }
11040
11046
  );
11041
11047
  };
11042
- const PROMOTION_QUERY_KEY = "promotions";
11043
- const promotionsQueryKeys = {
11044
- list: (query2) => [
11045
- PROMOTION_QUERY_KEY,
11046
- query2 ? query2 : void 0
11047
- ],
11048
- detail: (id, query2) => [
11049
- PROMOTION_QUERY_KEY,
11050
- id,
11051
- query2 ? query2 : void 0
11052
- ]
11053
- };
11054
- const usePromotions = (query2, options) => {
11055
- const { data, ...rest } = useQuery({
11056
- queryKey: promotionsQueryKeys.list(query2),
11057
- queryFn: async () => sdk.admin.promotion.list(query2),
11058
- ...options
11059
- });
11060
- return { ...data, ...rest };
11061
- };
11062
- const Promotions = () => {
11063
- const { id } = useParams();
11064
- const {
11065
- order: preview,
11066
- isError: isPreviewError,
11067
- error: previewError
11068
- } = useOrderPreview(id, void 0);
11069
- useInitiateOrderEdit({ preview });
11070
- const { onCancel } = useCancelOrderEdit({ preview });
11071
- if (isPreviewError) {
11072
- throw previewError;
11073
- }
11074
- const isReady = !!preview;
11075
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11076
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11077
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11078
- ] });
11079
- };
11080
- const PromotionForm = ({ preview }) => {
11081
- const { items, shipping_methods } = preview;
11082
- const [isSubmitting, setIsSubmitting] = useState(false);
11083
- const [comboboxValue, setComboboxValue] = useState("");
11084
- const { handleSuccess } = useRouteModal();
11085
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11086
- const promoCodes = getPromotionCodes(items, shipping_methods);
11087
- const { promotions, isPending, isError, error } = usePromotions(
11088
- {
11089
- code: promoCodes
11090
- },
11091
- {
11092
- enabled: !!promoCodes.length
11093
- }
11094
- );
11095
- const comboboxData = useComboboxData({
11096
- queryKey: ["promotions", "combobox", promoCodes],
11097
- queryFn: async (params) => {
11098
- return await sdk.admin.promotion.list({
11099
- ...params,
11100
- code: {
11101
- $nin: promoCodes
11102
- }
11103
- });
11104
- },
11105
- getOptions: (data) => {
11106
- return data.promotions.map((promotion) => ({
11107
- label: promotion.code,
11108
- value: promotion.code
11109
- }));
11048
+ const ShippingProfileForm = ({
11049
+ data,
11050
+ order,
11051
+ preview
11052
+ }) => {
11053
+ var _a, _b, _c, _d, _e, _f;
11054
+ const { setIsOpen } = useStackedModal();
11055
+ const form = useForm({
11056
+ resolver: zodResolver(shippingMethodSchema),
11057
+ defaultValues: {
11058
+ 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,
11059
+ shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
11060
+ custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
11110
11061
  }
11111
11062
  });
11112
- const add = async (value) => {
11113
- if (!value) {
11114
- return;
11115
- }
11116
- addPromotions(
11117
- {
11118
- promo_codes: [value]
11119
- },
11120
- {
11121
- onError: (e) => {
11122
- toast.error(e.message);
11123
- comboboxData.onSearchValueChange("");
11124
- setComboboxValue("");
11125
- },
11126
- onSuccess: () => {
11127
- comboboxData.onSearchValueChange("");
11128
- setComboboxValue("");
11129
- }
11130
- }
11131
- );
11132
- };
11133
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11134
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11135
- const onSubmit = async () => {
11136
- setIsSubmitting(true);
11137
- let requestSucceeded = false;
11138
- await requestOrderEdit(void 0, {
11139
- onError: (e) => {
11140
- toast.error(e.message);
11141
- },
11142
- onSuccess: () => {
11143
- requestSucceeded = true;
11144
- }
11145
- });
11146
- if (!requestSucceeded) {
11147
- setIsSubmitting(false);
11063
+ const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
11064
+ const {
11065
+ mutateAsync: updateShippingMethod,
11066
+ isPending: isUpdatingShippingMethod
11067
+ } = useDraftOrderUpdateShippingMethod(order.id);
11068
+ const onSubmit = form.handleSubmit(async (values) => {
11069
+ if (isEqual(values, form.formState.defaultValues)) {
11070
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11148
11071
  return;
11149
11072
  }
11150
- await confirmOrderEdit(void 0, {
11151
- onError: (e) => {
11152
- toast.error(e.message);
11153
- },
11154
- onSuccess: () => {
11155
- handleSuccess();
11156
- },
11157
- onSettled: () => {
11158
- setIsSubmitting(false);
11159
- }
11160
- });
11161
- };
11162
- if (isError) {
11163
- throw error;
11164
- }
11165
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11166
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11167
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11168
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11169
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11170
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11171
- ] }),
11172
- /* @__PURE__ */ jsx(
11173
- Combobox,
11174
- {
11175
- id: "promotion-combobox",
11176
- "aria-describedby": "promotion-combobox-hint",
11177
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11178
- fetchNextPage: comboboxData.fetchNextPage,
11179
- options: comboboxData.options,
11180
- onSearchValueChange: comboboxData.onSearchValueChange,
11181
- searchValue: comboboxData.searchValue,
11182
- disabled: comboboxData.disabled || isAddingPromotions,
11183
- onChange: add,
11184
- value: comboboxValue
11185
- }
11186
- )
11187
- ] }),
11188
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11189
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11190
- PromotionItem,
11073
+ if (data.shippingMethod) {
11074
+ await updateShippingMethod(
11191
11075
  {
11192
- promotion,
11193
- orderId: preview.id,
11194
- isLoading: isPending
11076
+ method_id: data.shippingMethod.id,
11077
+ shipping_option_id: values.shipping_option_id,
11078
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
11195
11079
  },
11196
- promotion.id
11197
- )) })
11198
- ] }) }),
11199
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11200
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11201
- /* @__PURE__ */ jsx(
11202
- Button,
11203
11080
  {
11204
- size: "small",
11205
- type: "submit",
11206
- isLoading: isSubmitting || isAddingPromotions,
11207
- children: "Save"
11081
+ onError: (e) => {
11082
+ toast.error(e.message);
11083
+ },
11084
+ onSuccess: () => {
11085
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11086
+ }
11208
11087
  }
11209
- )
11210
- ] }) })
11211
- ] });
11212
- };
11213
- const PromotionItem = ({
11214
- promotion,
11215
- orderId,
11216
- isLoading
11217
- }) => {
11218
- var _a;
11219
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11220
- const onRemove = async () => {
11221
- removePromotions(
11088
+ );
11089
+ return;
11090
+ }
11091
+ await addShippingMethod(
11222
11092
  {
11223
- promo_codes: [promotion.code]
11093
+ shipping_option_id: values.shipping_option_id,
11094
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
11224
11095
  },
11225
11096
  {
11226
11097
  onError: (e) => {
11227
11098
  toast.error(e.message);
11099
+ },
11100
+ onSuccess: () => {
11101
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11228
11102
  }
11229
11103
  }
11230
11104
  );
11231
- };
11232
- const displayValue = getDisplayValue(promotion);
11233
- return /* @__PURE__ */ jsxs(
11234
- "div",
11105
+ });
11106
+ return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
11107
+ KeyboundForm,
11235
11108
  {
11236
- className: clx(
11237
- "px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between",
11238
- {
11239
- "animate-pulse": isLoading
11240
- }
11241
- ),
11109
+ className: "flex h-full flex-col overflow-hidden",
11110
+ onSubmit,
11242
11111
  children: [
11243
- /* @__PURE__ */ jsxs("div", { children: [
11244
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11245
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5 text-ui-fg-subtle", children: [
11246
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11247
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11248
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11249
- ] }),
11250
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11251
- ] })
11252
- ] }),
11253
- /* @__PURE__ */ jsx(
11254
- IconButton,
11255
- {
11256
- size: "small",
11257
- type: "button",
11258
- variant: "transparent",
11259
- onClick: onRemove,
11260
- isLoading: isPending || isLoading,
11261
- children: /* @__PURE__ */ jsx(XMark, {})
11262
- }
11263
- )
11112
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
11113
+ /* @__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: [
11114
+ /* @__PURE__ */ jsxs("div", { children: [
11115
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11116
+ /* @__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." }) })
11117
+ ] }),
11118
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11119
+ /* @__PURE__ */ jsx(
11120
+ LocationField,
11121
+ {
11122
+ control: form.control,
11123
+ setValue: form.setValue
11124
+ }
11125
+ ),
11126
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11127
+ /* @__PURE__ */ jsx(
11128
+ ShippingOptionField,
11129
+ {
11130
+ shippingProfileId: data.shippingProfileId,
11131
+ preview,
11132
+ control: form.control
11133
+ }
11134
+ ),
11135
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11136
+ /* @__PURE__ */ jsx(
11137
+ CustomAmountField,
11138
+ {
11139
+ control: form.control,
11140
+ currencyCode: order.currency_code
11141
+ }
11142
+ ),
11143
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11144
+ /* @__PURE__ */ jsx(
11145
+ ItemsPreview,
11146
+ {
11147
+ order,
11148
+ shippingProfileId: data.shippingProfileId
11149
+ }
11150
+ )
11151
+ ] }) }) }),
11152
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
11153
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11154
+ /* @__PURE__ */ jsx(
11155
+ Button,
11156
+ {
11157
+ size: "small",
11158
+ type: "submit",
11159
+ isLoading: isPending || isUpdatingShippingMethod,
11160
+ children: data.shippingMethod ? "Update" : "Add"
11161
+ }
11162
+ )
11163
+ ] }) })
11264
11164
  ]
11265
- },
11266
- promotion.id
11267
- );
11268
- };
11269
- function getDisplayValue(promotion) {
11270
- var _a, _b, _c, _d;
11271
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11272
- if (!value) {
11273
- return null;
11274
- }
11275
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11276
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11277
- if (!currency) {
11278
- return null;
11279
11165
  }
11280
- return getLocaleAmount(value, currency);
11281
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11282
- return formatPercentage(value);
11283
- }
11284
- return null;
11285
- }
11286
- const formatter = new Intl.NumberFormat([], {
11287
- style: "percent",
11288
- minimumFractionDigits: 2
11289
- });
11290
- const formatPercentage = (value, isPercentageValue = false) => {
11291
- let val = value || 0;
11292
- if (!isPercentageValue) {
11293
- val = val / 100;
11294
- }
11295
- return formatter.format(val);
11166
+ ) }) });
11296
11167
  };
11297
- function getPromotionCodes(items, shippingMethods) {
11298
- const codes = /* @__PURE__ */ new Set();
11299
- for (const item of items) {
11300
- if (item.adjustments) {
11301
- for (const adjustment of item.adjustments) {
11302
- if (adjustment.code) {
11303
- codes.add(adjustment.code);
11304
- }
11305
- }
11306
- }
11307
- }
11308
- for (const shippingMethod of shippingMethods) {
11309
- if (shippingMethod.adjustments) {
11310
- for (const adjustment of shippingMethod.adjustments) {
11311
- if (adjustment.code) {
11312
- codes.add(adjustment.code);
11313
- }
11314
- }
11315
- }
11316
- }
11317
- return Array.from(codes);
11318
- }
11319
- const SalesChannel = () => {
11320
- const { id } = useParams();
11321
- const { draft_order, isPending, isError, error } = useDraftOrder(
11322
- id,
11323
- {
11324
- fields: "+sales_channel_id"
11325
- },
11326
- {
11327
- enabled: !!id
11168
+ const shippingMethodSchema = objectType({
11169
+ location_id: stringType(),
11170
+ shipping_option_id: stringType(),
11171
+ custom_amount: unionType([numberType(), stringType()]).optional()
11172
+ });
11173
+ const ItemsPreview = ({ order, shippingProfileId }) => {
11174
+ const matches = order.items.filter(
11175
+ (item) => {
11176
+ var _a, _b, _c;
11177
+ return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
11328
11178
  }
11329
11179
  );
11330
- if (isError) {
11331
- throw error;
11332
- }
11333
- const ISrEADY = !!draft_order && !isPending;
11334
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11335
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11336
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11337
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11338
- ] }),
11339
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11180
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
11181
+ /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11182
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
11183
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
11184
+ ] }) }),
11185
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
11186
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
11187
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
11188
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
11189
+ ] }),
11190
+ /* @__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(
11191
+ "div",
11192
+ {
11193
+ className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
11194
+ children: [
11195
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
11196
+ /* @__PURE__ */ jsx(
11197
+ Thumbnail,
11198
+ {
11199
+ thumbnail: item.thumbnail,
11200
+ alt: item.product_title ?? void 0
11201
+ }
11202
+ ),
11203
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11204
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
11205
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
11206
+ /* @__PURE__ */ jsxs(
11207
+ Text,
11208
+ {
11209
+ size: "small",
11210
+ leading: "compact",
11211
+ className: "text-ui-fg-subtle",
11212
+ children: [
11213
+ "(",
11214
+ item.variant_title,
11215
+ ")"
11216
+ ]
11217
+ }
11218
+ )
11219
+ ] }),
11220
+ /* @__PURE__ */ jsx(
11221
+ Text,
11222
+ {
11223
+ size: "small",
11224
+ leading: "compact",
11225
+ className: "text-ui-fg-subtle",
11226
+ children: item.variant_sku
11227
+ }
11228
+ )
11229
+ ] })
11230
+ ] }),
11231
+ /* @__PURE__ */ jsxs(
11232
+ Text,
11233
+ {
11234
+ size: "small",
11235
+ leading: "compact",
11236
+ className: "text-ui-fg-subtle",
11237
+ children: [
11238
+ item.quantity,
11239
+ "x"
11240
+ ]
11241
+ }
11242
+ )
11243
+ ]
11244
+ },
11245
+ item.id
11246
+ )) : /* @__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: [
11247
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
11248
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
11249
+ 'No items found for "',
11250
+ query,
11251
+ '".'
11252
+ ] })
11253
+ ] }) })
11254
+ ] })
11340
11255
  ] });
11341
11256
  };
11342
- const SalesChannelForm = ({ order }) => {
11343
- const form = useForm({
11344
- defaultValues: {
11345
- sales_channel_id: order.sales_channel_id || ""
11257
+ const LocationField = ({ control, setValue }) => {
11258
+ const locations = useComboboxData({
11259
+ queryKey: ["locations"],
11260
+ queryFn: async (params) => {
11261
+ return await sdk.admin.stockLocation.list(params);
11346
11262
  },
11347
- resolver: zodResolver(schema$2)
11348
- });
11349
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11350
- const { handleSuccess } = useRouteModal();
11351
- const onSubmit = form.handleSubmit(async (data) => {
11352
- await mutateAsync(
11353
- {
11354
- sales_channel_id: data.sales_channel_id
11355
- },
11356
- {
11357
- onSuccess: () => {
11358
- toast.success("Sales channel updated");
11359
- handleSuccess();
11360
- },
11361
- onError: (error) => {
11362
- toast.error(error.message);
11363
- }
11364
- }
11365
- );
11263
+ getOptions: (data) => {
11264
+ return data.stock_locations.map((location) => ({
11265
+ label: location.name,
11266
+ value: location.id
11267
+ }));
11268
+ }
11366
11269
  });
11367
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11368
- KeyboundForm,
11270
+ return /* @__PURE__ */ jsx(
11271
+ Form$2.Field,
11369
11272
  {
11370
- className: "flex flex-1 flex-col overflow-hidden",
11371
- onSubmit,
11372
- children: [
11373
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11374
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11375
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11376
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11377
- ] }) })
11378
- ]
11273
+ control,
11274
+ name: "location_id",
11275
+ render: ({ field: { onChange, ...field } }) => {
11276
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11277
+ /* @__PURE__ */ jsxs("div", { children: [
11278
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
11279
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
11280
+ ] }),
11281
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11282
+ Combobox,
11283
+ {
11284
+ options: locations.options,
11285
+ fetchNextPage: locations.fetchNextPage,
11286
+ isFetchingNextPage: locations.isFetchingNextPage,
11287
+ searchValue: locations.searchValue,
11288
+ onSearchValueChange: locations.onSearchValueChange,
11289
+ placeholder: "Select location",
11290
+ onChange: (value) => {
11291
+ setValue("shipping_option_id", "", {
11292
+ shouldDirty: true,
11293
+ shouldTouch: true
11294
+ });
11295
+ onChange(value);
11296
+ },
11297
+ ...field
11298
+ }
11299
+ ) })
11300
+ ] }) });
11301
+ }
11379
11302
  }
11380
- ) });
11303
+ );
11381
11304
  };
11382
- const SalesChannelField = ({ control, order }) => {
11383
- const salesChannels = useComboboxData({
11305
+ const ShippingOptionField = ({
11306
+ shippingProfileId,
11307
+ preview,
11308
+ control
11309
+ }) => {
11310
+ var _a;
11311
+ const locationId = useWatch({ control, name: "location_id" });
11312
+ const shippingOptions = useComboboxData({
11313
+ queryKey: ["shipping_options", locationId, shippingProfileId],
11384
11314
  queryFn: async (params) => {
11385
- return await sdk.admin.salesChannel.list(params);
11315
+ return await sdk.admin.shippingOption.list({
11316
+ ...params,
11317
+ stock_location_id: locationId,
11318
+ shipping_profile_id: shippingProfileId
11319
+ });
11386
11320
  },
11387
- queryKey: ["sales-channels"],
11388
11321
  getOptions: (data) => {
11389
- return data.sales_channels.map((salesChannel) => ({
11390
- label: salesChannel.name,
11391
- value: salesChannel.id
11392
- }));
11322
+ return data.shipping_options.map((option) => {
11323
+ var _a2;
11324
+ if ((_a2 = option.rules) == null ? void 0 : _a2.find(
11325
+ (r) => r.attribute === "is_return" && r.value === "true"
11326
+ )) {
11327
+ return void 0;
11328
+ }
11329
+ return {
11330
+ label: option.name,
11331
+ value: option.id
11332
+ };
11333
+ }).filter(Boolean);
11393
11334
  },
11394
- defaultValue: order.sales_channel_id || void 0
11335
+ enabled: !!locationId && !!shippingProfileId,
11336
+ defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
11395
11337
  });
11338
+ const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
11396
11339
  return /* @__PURE__ */ jsx(
11397
11340
  Form$2.Field,
11398
11341
  {
11399
11342
  control,
11400
- name: "sales_channel_id",
11343
+ name: "shipping_option_id",
11401
11344
  render: ({ field }) => {
11402
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11403
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11345
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11346
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11347
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
11348
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
11349
+ ] }),
11350
+ /* @__PURE__ */ jsx(
11351
+ ConditionalTooltip,
11352
+ {
11353
+ content: tooltipContent,
11354
+ showTooltip: !locationId || !shippingProfileId,
11355
+ children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11356
+ Combobox,
11357
+ {
11358
+ options: shippingOptions.options,
11359
+ fetchNextPage: shippingOptions.fetchNextPage,
11360
+ isFetchingNextPage: shippingOptions.isFetchingNextPage,
11361
+ searchValue: shippingOptions.searchValue,
11362
+ onSearchValueChange: shippingOptions.onSearchValueChange,
11363
+ placeholder: "Select shipping option",
11364
+ ...field,
11365
+ disabled: !locationId || !shippingProfileId
11366
+ }
11367
+ ) }) })
11368
+ }
11369
+ )
11370
+ ] }) });
11371
+ }
11372
+ }
11373
+ );
11374
+ };
11375
+ const CustomAmountField = ({
11376
+ control,
11377
+ currencyCode
11378
+ }) => {
11379
+ return /* @__PURE__ */ jsx(
11380
+ Form$2.Field,
11381
+ {
11382
+ control,
11383
+ name: "custom_amount",
11384
+ render: ({ field: { onChange, ...field } }) => {
11385
+ return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11386
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11387
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
11388
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
11389
+ ] }),
11404
11390
  /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11405
- Combobox,
11391
+ CurrencyInput,
11406
11392
  {
11407
- options: salesChannels.options,
11408
- fetchNextPage: salesChannels.fetchNextPage,
11409
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11410
- searchValue: salesChannels.searchValue,
11411
- onSearchValueChange: salesChannels.onSearchValueChange,
11412
- placeholder: "Select sales channel",
11413
- ...field
11393
+ ...field,
11394
+ onValueChange: (value) => onChange(value),
11395
+ symbol: getNativeSymbol(currencyCode),
11396
+ code: currencyCode
11414
11397
  }
11415
- ) }),
11416
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11398
+ ) })
11417
11399
  ] });
11418
11400
  }
11419
11401
  }
11420
11402
  );
11421
11403
  };
11422
- const schema$2 = objectType({
11423
- sales_channel_id: stringType().min(1)
11424
- });
11425
11404
  const ShippingAddress = () => {
11426
11405
  const { id } = useParams();
11427
11406
  const { order, isPending, isError, error } = useOrder(id, {
@@ -11454,7 +11433,7 @@ const ShippingAddressForm = ({ order }) => {
11454
11433
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11455
11434
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11456
11435
  },
11457
- resolver: zodResolver(schema$1)
11436
+ resolver: zodResolver(schema$2)
11458
11437
  });
11459
11438
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11460
11439
  const { handleSuccess } = useRouteModal();
@@ -11624,7 +11603,7 @@ const ShippingAddressForm = ({ order }) => {
11624
11603
  }
11625
11604
  ) });
11626
11605
  };
11627
- const schema$1 = addressSchema;
11606
+ const schema$2 = addressSchema;
11628
11607
  const TransferOwnership = () => {
11629
11608
  const { id } = useParams();
11630
11609
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -11648,7 +11627,7 @@ const TransferOwnershipForm = ({ order }) => {
11648
11627
  defaultValues: {
11649
11628
  customer_id: order.customer_id || ""
11650
11629
  },
11651
- resolver: zodResolver(schema)
11630
+ resolver: zodResolver(schema$1)
11652
11631
  });
11653
11632
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11654
11633
  const { handleSuccess } = useRouteModal();
@@ -12098,7 +12077,7 @@ const Illustration = () => {
12098
12077
  }
12099
12078
  );
12100
12079
  };
12101
- const schema = objectType({
12080
+ const schema$1 = objectType({
12102
12081
  customer_id: stringType().min(1)
12103
12082
  });
12104
12083
  const NumberInput = forwardRef(
@@ -13026,6 +13005,27 @@ const customItemSchema = objectType({
13026
13005
  quantity: numberType(),
13027
13006
  unit_price: unionType([numberType(), stringType()])
13028
13007
  });
13008
+ const CustomItems = () => {
13009
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
13010
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
13011
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
13012
+ ] });
13013
+ };
13014
+ const CustomItemsForm = () => {
13015
+ const form = useForm({
13016
+ resolver: zodResolver(schema)
13017
+ });
13018
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
13019
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
13020
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13021
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13022
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
13023
+ ] }) })
13024
+ ] }) });
13025
+ };
13026
+ const schema = objectType({
13027
+ email: stringType().email()
13028
+ });
13029
13029
  const widgetModule = { widgets: [] };
13030
13030
  const routeModule = {
13031
13031
  routes: [
@@ -13046,10 +13046,6 @@ const routeModule = {
13046
13046
  handle,
13047
13047
  loader,
13048
13048
  children: [
13049
- {
13050
- Component: CustomItems,
13051
- path: "/draft-orders/:id/custom-items"
13052
- },
13053
13049
  {
13054
13050
  Component: BillingAddress,
13055
13051
  path: "/draft-orders/:id/billing-address"
@@ -13062,10 +13058,6 @@ const routeModule = {
13062
13058
  Component: Metadata,
13063
13059
  path: "/draft-orders/:id/metadata"
13064
13060
  },
13065
- {
13066
- Component: Shipping,
13067
- path: "/draft-orders/:id/shipping"
13068
- },
13069
13061
  {
13070
13062
  Component: Promotions,
13071
13063
  path: "/draft-orders/:id/promotions"
@@ -13074,6 +13066,10 @@ const routeModule = {
13074
13066
  Component: SalesChannel,
13075
13067
  path: "/draft-orders/:id/sales-channel"
13076
13068
  },
13069
+ {
13070
+ Component: Shipping,
13071
+ path: "/draft-orders/:id/shipping"
13072
+ },
13077
13073
  {
13078
13074
  Component: ShippingAddress,
13079
13075
  path: "/draft-orders/:id/shipping-address"
@@ -13085,6 +13081,10 @@ const routeModule = {
13085
13081
  {
13086
13082
  Component: Items,
13087
13083
  path: "/draft-orders/:id/items"
13084
+ },
13085
+ {
13086
+ Component: CustomItems,
13087
+ path: "/draft-orders/:id/custom-items"
13088
13088
  }
13089
13089
  ]
13090
13090
  }