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

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.
@@ -11170,105 +11170,183 @@ function getHasUneditableRows(metadata) {
11170
11170
  (value) => !EDITABLE_TYPES.includes(typeof value)
11171
11171
  );
11172
11172
  }
11173
- const PROMOTION_QUERY_KEY = "promotions";
11174
- const promotionsQueryKeys = {
11175
- list: (query2) => [
11176
- PROMOTION_QUERY_KEY,
11177
- query2 ? query2 : void 0
11178
- ],
11179
- detail: (id, query2) => [
11180
- PROMOTION_QUERY_KEY,
11173
+ const SalesChannel = () => {
11174
+ const { id } = useParams();
11175
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11181
11176
  id,
11182
- query2 ? query2 : void 0
11183
- ]
11177
+ {
11178
+ fields: "+sales_channel_id"
11179
+ },
11180
+ {
11181
+ enabled: !!id
11182
+ }
11183
+ );
11184
+ if (isError) {
11185
+ throw error;
11186
+ }
11187
+ const ISrEADY = !!draft_order && !isPending;
11188
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11189
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11190
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11191
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11192
+ ] }),
11193
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11194
+ ] });
11184
11195
  };
11185
- const usePromotions = (query2, options) => {
11186
- const { data, ...rest } = useQuery({
11187
- queryKey: promotionsQueryKeys.list(query2),
11188
- queryFn: async () => sdk.admin.promotion.list(query2),
11189
- ...options
11196
+ const SalesChannelForm = ({ order }) => {
11197
+ const form = useForm({
11198
+ defaultValues: {
11199
+ sales_channel_id: order.sales_channel_id || ""
11200
+ },
11201
+ resolver: zodResolver(schema$2)
11190
11202
  });
11191
- return { ...data, ...rest };
11203
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11204
+ const { handleSuccess } = useRouteModal();
11205
+ const onSubmit = form.handleSubmit(async (data) => {
11206
+ await mutateAsync(
11207
+ {
11208
+ sales_channel_id: data.sales_channel_id
11209
+ },
11210
+ {
11211
+ onSuccess: () => {
11212
+ toast.success("Sales channel updated");
11213
+ handleSuccess();
11214
+ },
11215
+ onError: (error) => {
11216
+ toast.error(error.message);
11217
+ }
11218
+ }
11219
+ );
11220
+ });
11221
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11222
+ KeyboundForm,
11223
+ {
11224
+ className: "flex flex-1 flex-col overflow-hidden",
11225
+ onSubmit,
11226
+ children: [
11227
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11228
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11229
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11230
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11231
+ ] }) })
11232
+ ]
11233
+ }
11234
+ ) });
11192
11235
  };
11193
- const Promotions = () => {
11236
+ const SalesChannelField = ({ control, order }) => {
11237
+ const salesChannels = useComboboxData({
11238
+ queryFn: async (params) => {
11239
+ return await sdk.admin.salesChannel.list(params);
11240
+ },
11241
+ queryKey: ["sales-channels"],
11242
+ getOptions: (data) => {
11243
+ return data.sales_channels.map((salesChannel) => ({
11244
+ label: salesChannel.name,
11245
+ value: salesChannel.id
11246
+ }));
11247
+ },
11248
+ defaultValue: order.sales_channel_id || void 0
11249
+ });
11250
+ return /* @__PURE__ */ jsx(
11251
+ Form$2.Field,
11252
+ {
11253
+ control,
11254
+ name: "sales_channel_id",
11255
+ render: ({ field }) => {
11256
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11257
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11258
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11259
+ Combobox,
11260
+ {
11261
+ options: salesChannels.options,
11262
+ fetchNextPage: salesChannels.fetchNextPage,
11263
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11264
+ searchValue: salesChannels.searchValue,
11265
+ onSearchValueChange: salesChannels.onSearchValueChange,
11266
+ placeholder: "Select sales channel",
11267
+ ...field
11268
+ }
11269
+ ) }),
11270
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11271
+ ] });
11272
+ }
11273
+ }
11274
+ );
11275
+ };
11276
+ const schema$2 = objectType({
11277
+ sales_channel_id: stringType().min(1)
11278
+ });
11279
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
11280
+ const Shipping = () => {
11281
+ var _a;
11194
11282
  const { id } = useParams();
11283
+ const { order, isPending, isError, error } = useOrder(id, {
11284
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11285
+ });
11195
11286
  const {
11196
11287
  order: preview,
11288
+ isPending: isPreviewPending,
11197
11289
  isError: isPreviewError,
11198
11290
  error: previewError
11199
- } = useOrderPreview(id, void 0);
11291
+ } = useOrderPreview(id);
11200
11292
  useInitiateOrderEdit({ preview });
11201
11293
  const { onCancel } = useCancelOrderEdit({ preview });
11294
+ if (isError) {
11295
+ throw error;
11296
+ }
11202
11297
  if (isPreviewError) {
11203
11298
  throw previewError;
11204
11299
  }
11205
- const isReady = !!preview;
11206
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11207
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11208
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11209
- ] });
11300
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11301
+ const isReady = preview && !isPreviewPending && order && !isPending;
11302
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11303
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11304
+ /* @__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: [
11305
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11306
+ /* @__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." }) })
11307
+ ] }) }) }),
11308
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11309
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11310
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11311
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11312
+ ] }) });
11210
11313
  };
11211
- const PromotionForm = ({ preview }) => {
11212
- const { items, shipping_methods } = preview;
11314
+ const ShippingForm = ({ preview, order }) => {
11315
+ var _a;
11316
+ const { setIsOpen } = useStackedModal();
11213
11317
  const [isSubmitting, setIsSubmitting] = useState(false);
11214
- const [comboboxValue, setComboboxValue] = useState("");
11215
- const { handleSuccess } = useRouteModal();
11216
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11217
- const promoIds = getPromotionIds(items, shipping_methods);
11218
- const { promotions, isPending, isError, error } = usePromotions(
11318
+ const [data, setData] = useState(null);
11319
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11320
+ const { shipping_options } = useShippingOptions(
11219
11321
  {
11220
- id: promoIds
11322
+ id: appliedShippingOptionIds,
11323
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11221
11324
  },
11222
11325
  {
11223
- enabled: !!promoIds.length
11326
+ enabled: appliedShippingOptionIds.length > 0
11224
11327
  }
11225
11328
  );
11226
- const comboboxData = useComboboxData({
11227
- queryKey: ["promotions", "combobox", promoIds],
11228
- queryFn: async (params) => {
11229
- return await sdk.admin.promotion.list({
11230
- ...params,
11231
- id: {
11232
- $nin: promoIds
11233
- }
11234
- });
11235
- },
11236
- getOptions: (data) => {
11237
- return data.promotions.map((promotion) => ({
11238
- label: promotion.code,
11239
- value: promotion.code
11240
- }));
11241
- }
11242
- });
11243
- const add = async (value) => {
11244
- if (!value) {
11245
- return;
11246
- }
11247
- addPromotions(
11248
- {
11249
- promo_codes: [value]
11250
- },
11251
- {
11252
- onError: (e) => {
11253
- toast.error(e.message);
11254
- comboboxData.onSearchValueChange("");
11255
- setComboboxValue("");
11256
- },
11257
- onSuccess: () => {
11258
- comboboxData.onSearchValueChange("");
11259
- setComboboxValue("");
11260
- }
11261
- }
11262
- );
11263
- };
11329
+ const uniqueShippingProfiles = useMemo(() => {
11330
+ const profiles = /* @__PURE__ */ new Map();
11331
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
11332
+ profiles.set(profile.id, profile);
11333
+ });
11334
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11335
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
11336
+ });
11337
+ return Array.from(profiles.values());
11338
+ }, [order.items, shipping_options]);
11339
+ const { handleSuccess } = useRouteModal();
11264
11340
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11265
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11341
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11342
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11343
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11266
11344
  const onSubmit = async () => {
11267
11345
  setIsSubmitting(true);
11268
11346
  let requestSucceeded = false;
11269
11347
  await requestOrderEdit(void 0, {
11270
11348
  onError: (e) => {
11271
- toast.error(e.message);
11349
+ toast.error(`Failed to request order edit: ${e.message}`);
11272
11350
  },
11273
11351
  onSuccess: () => {
11274
11352
  requestSucceeded = true;
@@ -11280,7 +11358,7 @@ const PromotionForm = ({ preview }) => {
11280
11358
  }
11281
11359
  await confirmOrderEdit(void 0, {
11282
11360
  onError: (e) => {
11283
- toast.error(e.message);
11361
+ toast.error(`Failed to confirm order edit: ${e.message}`);
11284
11362
  },
11285
11363
  onSuccess: () => {
11286
11364
  handleSuccess();
@@ -11290,1105 +11368,71 @@ const PromotionForm = ({ preview }) => {
11290
11368
  }
11291
11369
  });
11292
11370
  };
11293
- if (isError) {
11294
- throw error;
11295
- }
11296
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11297
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11298
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11299
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11300
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11301
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11302
- ] }),
11303
- /* @__PURE__ */ jsx(
11304
- Combobox,
11305
- {
11306
- id: "promotion-combobox",
11307
- "aria-describedby": "promotion-combobox-hint",
11308
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11309
- fetchNextPage: comboboxData.fetchNextPage,
11310
- options: comboboxData.options,
11311
- onSearchValueChange: comboboxData.onSearchValueChange,
11312
- searchValue: comboboxData.searchValue,
11313
- disabled: comboboxData.disabled || isAddingPromotions,
11314
- onChange: add,
11315
- value: comboboxValue
11316
- }
11317
- )
11318
- ] }),
11319
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11320
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11321
- PromotionItem,
11322
- {
11323
- promotion,
11324
- orderId: preview.id,
11325
- isLoading: isPending
11326
- },
11327
- promotion.id
11328
- )) })
11329
- ] }) }),
11330
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11331
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11332
- /* @__PURE__ */ jsx(
11333
- Button,
11334
- {
11335
- size: "small",
11336
- type: "submit",
11337
- isLoading: isSubmitting || isAddingPromotions,
11338
- children: "Save"
11339
- }
11340
- )
11341
- ] }) })
11342
- ] });
11343
- };
11344
- const PromotionItem = ({
11345
- promotion,
11346
- orderId,
11347
- isLoading
11348
- }) => {
11349
- var _a;
11350
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11351
- const onRemove = async () => {
11352
- removePromotions(
11353
- {
11354
- promo_codes: [promotion.code]
11355
- },
11356
- {
11357
- onError: (e) => {
11358
- toast.error(e.message);
11359
- }
11360
- }
11361
- );
11362
- };
11363
- const displayValue = getDisplayValue(promotion);
11364
- return /* @__PURE__ */ jsxs(
11365
- "div",
11366
- {
11367
- className: clx(
11368
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11369
- {
11370
- "animate-pulse": isLoading
11371
- }
11372
- ),
11373
- children: [
11374
- /* @__PURE__ */ jsxs("div", { children: [
11375
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11376
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11377
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11378
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11379
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11380
- ] }),
11381
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11382
- ] })
11383
- ] }),
11384
- /* @__PURE__ */ jsx(
11385
- IconButton,
11386
- {
11387
- size: "small",
11388
- type: "button",
11389
- variant: "transparent",
11390
- onClick: onRemove,
11391
- isLoading: isPending || isLoading,
11392
- children: /* @__PURE__ */ jsx(XMark, {})
11393
- }
11394
- )
11395
- ]
11396
- },
11397
- promotion.id
11398
- );
11399
- };
11400
- function getDisplayValue(promotion) {
11401
- var _a, _b, _c, _d;
11402
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11403
- if (!value) {
11404
- return null;
11405
- }
11406
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11407
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11408
- if (!currency) {
11409
- return null;
11410
- }
11411
- return getLocaleAmount(value, currency);
11412
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11413
- return formatPercentage(value);
11414
- }
11415
- return null;
11416
- }
11417
- const formatter = new Intl.NumberFormat([], {
11418
- style: "percent",
11419
- minimumFractionDigits: 2
11420
- });
11421
- const formatPercentage = (value, isPercentageValue = false) => {
11422
- let val = value || 0;
11423
- if (!isPercentageValue) {
11424
- val = val / 100;
11425
- }
11426
- return formatter.format(val);
11427
- };
11428
- function getPromotionIds(items, shippingMethods) {
11429
- const promotionIds = /* @__PURE__ */ new Set();
11430
- for (const item of items) {
11431
- if (item.adjustments) {
11432
- for (const adjustment of item.adjustments) {
11433
- if (adjustment.promotion_id) {
11434
- promotionIds.add(adjustment.promotion_id);
11435
- }
11436
- }
11437
- }
11438
- }
11439
- for (const shippingMethod of shippingMethods) {
11440
- if (shippingMethod.adjustments) {
11441
- for (const adjustment of shippingMethod.adjustments) {
11442
- if (adjustment.promotion_id) {
11443
- promotionIds.add(adjustment.promotion_id);
11444
- }
11445
- }
11446
- }
11447
- }
11448
- return Array.from(promotionIds);
11449
- }
11450
- const SalesChannel = () => {
11451
- const { id } = useParams();
11452
- const { draft_order, isPending, isError, error } = useDraftOrder(
11453
- id,
11454
- {
11455
- fields: "+sales_channel_id"
11456
- },
11457
- {
11458
- enabled: !!id
11459
- }
11460
- );
11461
- if (isError) {
11462
- throw error;
11463
- }
11464
- const ISrEADY = !!draft_order && !isPending;
11465
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11466
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11467
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11468
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11469
- ] }),
11470
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11471
- ] });
11472
- };
11473
- const SalesChannelForm = ({ order }) => {
11474
- const form = useForm({
11475
- defaultValues: {
11476
- sales_channel_id: order.sales_channel_id || ""
11477
- },
11478
- resolver: zodResolver(schema$2)
11479
- });
11480
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11481
- const { handleSuccess } = useRouteModal();
11482
- const onSubmit = form.handleSubmit(async (data) => {
11483
- await mutateAsync(
11484
- {
11485
- sales_channel_id: data.sales_channel_id
11486
- },
11487
- {
11488
- onSuccess: () => {
11489
- toast.success("Sales channel updated");
11490
- handleSuccess();
11491
- },
11492
- onError: (error) => {
11493
- toast.error(error.message);
11371
+ const onKeydown = useCallback(
11372
+ (e) => {
11373
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11374
+ if (data || isSubmitting) {
11375
+ return;
11494
11376
  }
11377
+ onSubmit();
11495
11378
  }
11496
- );
11497
- });
11498
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11499
- KeyboundForm,
11500
- {
11501
- className: "flex flex-1 flex-col overflow-hidden",
11502
- onSubmit,
11503
- children: [
11504
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11505
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11506
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11507
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11508
- ] }) })
11509
- ]
11510
- }
11511
- ) });
11512
- };
11513
- const SalesChannelField = ({ control, order }) => {
11514
- const salesChannels = useComboboxData({
11515
- queryFn: async (params) => {
11516
- return await sdk.admin.salesChannel.list(params);
11517
- },
11518
- queryKey: ["sales-channels"],
11519
- getOptions: (data) => {
11520
- return data.sales_channels.map((salesChannel) => ({
11521
- label: salesChannel.name,
11522
- value: salesChannel.id
11523
- }));
11524
11379
  },
11525
- defaultValue: order.sales_channel_id || void 0
11526
- });
11527
- return /* @__PURE__ */ jsx(
11528
- Form$2.Field,
11529
- {
11530
- control,
11531
- name: "sales_channel_id",
11532
- render: ({ field }) => {
11533
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11534
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11535
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11536
- Combobox,
11537
- {
11538
- options: salesChannels.options,
11539
- fetchNextPage: salesChannels.fetchNextPage,
11540
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11541
- searchValue: salesChannels.searchValue,
11542
- onSearchValueChange: salesChannels.onSearchValueChange,
11543
- placeholder: "Select sales channel",
11544
- ...field
11545
- }
11546
- ) }),
11547
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11548
- ] });
11549
- }
11550
- }
11380
+ [data, isSubmitting, onSubmit]
11551
11381
  );
11552
- };
11553
- const schema$2 = objectType({
11554
- sales_channel_id: stringType().min(1)
11555
- });
11556
- const ShippingAddress = () => {
11557
- const { id } = useParams();
11558
- const { order, isPending, isError, error } = useOrder(id, {
11559
- fields: "+shipping_address"
11560
- });
11561
- if (isError) {
11562
- throw error;
11563
- }
11564
- const isReady = !isPending && !!order;
11565
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11566
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11567
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
11568
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11569
- ] }),
11570
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
11571
- ] });
11572
- };
11573
- const ShippingAddressForm = ({ order }) => {
11574
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11575
- const form = useForm({
11576
- defaultValues: {
11577
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11578
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11579
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11580
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11581
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11582
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11583
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11584
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11585
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11586
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11587
- },
11588
- resolver: zodResolver(schema$1)
11589
- });
11590
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11591
- const { handleSuccess } = useRouteModal();
11592
- const onSubmit = form.handleSubmit(async (data) => {
11593
- await mutateAsync(
11594
- {
11595
- shipping_address: {
11596
- first_name: data.first_name,
11597
- last_name: data.last_name,
11598
- company: data.company,
11599
- address_1: data.address_1,
11600
- address_2: data.address_2,
11601
- city: data.city,
11602
- province: data.province,
11603
- country_code: data.country_code,
11604
- postal_code: data.postal_code,
11605
- phone: data.phone
11606
- }
11607
- },
11608
- {
11609
- onSuccess: () => {
11610
- handleSuccess();
11611
- },
11612
- onError: (error) => {
11613
- toast.error(error.message);
11614
- }
11615
- }
11616
- );
11617
- });
11618
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11619
- KeyboundForm,
11620
- {
11621
- className: "flex flex-1 flex-col overflow-hidden",
11622
- onSubmit,
11623
- children: [
11624
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
11625
- /* @__PURE__ */ jsx(
11626
- Form$2.Field,
11627
- {
11628
- control: form.control,
11629
- name: "country_code",
11630
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11631
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
11632
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
11633
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11634
- ] })
11635
- }
11636
- ),
11637
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11382
+ useEffect(() => {
11383
+ document.addEventListener("keydown", onKeydown);
11384
+ return () => {
11385
+ document.removeEventListener("keydown", onKeydown);
11386
+ };
11387
+ }, [onKeydown]);
11388
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11389
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11390
+ /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11391
+ /* @__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: [
11392
+ /* @__PURE__ */ jsxs("div", { children: [
11393
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11394
+ /* @__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." }) })
11395
+ ] }),
11396
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11397
+ /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11398
+ /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11638
11399
  /* @__PURE__ */ jsx(
11639
- Form$2.Field,
11400
+ Text,
11640
11401
  {
11641
- control: form.control,
11642
- name: "first_name",
11643
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11644
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
11645
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11646
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11647
- ] })
11402
+ size: "xsmall",
11403
+ weight: "plus",
11404
+ className: "text-ui-fg-muted",
11405
+ children: "Shipping profile"
11648
11406
  }
11649
11407
  ),
11650
11408
  /* @__PURE__ */ jsx(
11651
- Form$2.Field,
11409
+ Text,
11652
11410
  {
11653
- control: form.control,
11654
- name: "last_name",
11655
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11656
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
11657
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11658
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11659
- ] })
11411
+ size: "xsmall",
11412
+ weight: "plus",
11413
+ className: "text-ui-fg-muted",
11414
+ children: "Action"
11660
11415
  }
11661
11416
  )
11662
11417
  ] }),
11663
- /* @__PURE__ */ jsx(
11664
- Form$2.Field,
11665
- {
11666
- control: form.control,
11667
- name: "company",
11668
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11669
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
11670
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11671
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11672
- ] })
11673
- }
11674
- ),
11675
- /* @__PURE__ */ jsx(
11676
- Form$2.Field,
11677
- {
11678
- control: form.control,
11679
- name: "address_1",
11680
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11681
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
11682
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11683
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11684
- ] })
11685
- }
11686
- ),
11687
- /* @__PURE__ */ jsx(
11688
- Form$2.Field,
11689
- {
11690
- control: form.control,
11691
- name: "address_2",
11692
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11693
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11694
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11695
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11696
- ] })
11697
- }
11698
- ),
11699
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11700
- /* @__PURE__ */ jsx(
11701
- Form$2.Field,
11702
- {
11703
- control: form.control,
11704
- name: "postal_code",
11705
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11706
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
11707
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11708
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11709
- ] })
11710
- }
11711
- ),
11712
- /* @__PURE__ */ jsx(
11713
- Form$2.Field,
11714
- {
11715
- control: form.control,
11716
- name: "city",
11717
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11718
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
11719
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11720
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11721
- ] })
11722
- }
11723
- )
11724
- ] }),
11725
- /* @__PURE__ */ jsx(
11726
- Form$2.Field,
11727
- {
11728
- control: form.control,
11729
- name: "province",
11730
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11731
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11732
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11733
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11734
- ] })
11735
- }
11736
- ),
11737
- /* @__PURE__ */ jsx(
11738
- Form$2.Field,
11739
- {
11740
- control: form.control,
11741
- name: "phone",
11742
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11743
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
11744
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11745
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11746
- ] })
11747
- }
11748
- )
11749
- ] }) }),
11750
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11751
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11752
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11753
- ] }) })
11754
- ]
11755
- }
11756
- ) });
11757
- };
11758
- const schema$1 = addressSchema;
11759
- const TransferOwnership = () => {
11760
- const { id } = useParams();
11761
- const { draft_order, isPending, isError, error } = useDraftOrder(id, {
11762
- fields: "id,customer_id,customer.*"
11763
- });
11764
- if (isError) {
11765
- throw error;
11766
- }
11767
- const isReady = !isPending && !!draft_order;
11768
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11769
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11770
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
11771
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
11772
- ] }),
11773
- isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
11774
- ] });
11775
- };
11776
- const TransferOwnershipForm = ({ order }) => {
11777
- var _a, _b;
11778
- const form = useForm({
11779
- defaultValues: {
11780
- customer_id: order.customer_id || ""
11781
- },
11782
- resolver: zodResolver(schema)
11783
- });
11784
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11785
- const { handleSuccess } = useRouteModal();
11786
- const name = [(_a = order.customer) == null ? void 0 : _a.first_name, (_b = order.customer) == null ? void 0 : _b.last_name].filter(Boolean).join(" ");
11787
- const currentCustomer = order.customer ? {
11788
- label: name ? `${name} (${order.customer.email})` : order.customer.email,
11789
- value: order.customer.id
11790
- } : null;
11791
- const onSubmit = form.handleSubmit(async (data) => {
11792
- await mutateAsync(
11793
- { customer_id: data.customer_id },
11794
- {
11795
- onSuccess: () => {
11796
- toast.success("Customer updated");
11797
- handleSuccess();
11798
- },
11799
- onError: (error) => {
11800
- toast.error(error.message);
11801
- }
11802
- }
11803
- );
11804
- });
11805
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11806
- KeyboundForm,
11807
- {
11808
- className: "flex flex-1 flex-col overflow-hidden",
11809
- onSubmit,
11810
- children: [
11811
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: [
11812
- /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center bg-ui-bg-component rounded-md border", children: /* @__PURE__ */ jsx(Illustration, {}) }),
11813
- currentCustomer && /* @__PURE__ */ jsxs("div", { className: "flex flex-col space-y-3", children: [
11814
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11815
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "current-customer", children: "Current owner" }),
11816
- /* @__PURE__ */ jsx(Hint$1, { children: "The customer that is currently associated with this draft order." })
11817
- ] }),
11818
- /* @__PURE__ */ jsxs(Select, { disabled: true, value: currentCustomer.value, children: [
11819
- /* @__PURE__ */ jsx(Select.Trigger, { id: "current-customer", children: /* @__PURE__ */ jsx(Select.Value, {}) }),
11820
- /* @__PURE__ */ jsx(Select.Content, { children: /* @__PURE__ */ jsx(Select.Item, { value: currentCustomer.value, children: currentCustomer.label }) })
11821
- ] })
11822
- ] }),
11823
- /* @__PURE__ */ jsx(
11824
- CustomerField,
11825
- {
11826
- control: form.control,
11827
- currentCustomerId: order.customer_id
11828
- }
11829
- )
11830
- ] }),
11831
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11832
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", children: "Cancel" }) }),
11833
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11834
- ] }) })
11835
- ]
11836
- }
11837
- ) });
11838
- };
11839
- const CustomerField = ({ control, currentCustomerId }) => {
11840
- const customers = useComboboxData({
11841
- queryFn: async (params) => {
11842
- return await sdk.admin.customer.list({
11843
- ...params,
11844
- id: currentCustomerId ? { $nin: [currentCustomerId] } : void 0
11845
- });
11846
- },
11847
- queryKey: ["customers"],
11848
- getOptions: (data) => {
11849
- return data.customers.map((customer) => {
11850
- const name = [customer.first_name, customer.last_name].filter(Boolean).join(" ");
11851
- return {
11852
- label: name ? `${name} (${customer.email})` : customer.email,
11853
- value: customer.id
11854
- };
11855
- });
11856
- }
11857
- });
11858
- return /* @__PURE__ */ jsx(
11859
- Form$2.Field,
11860
- {
11861
- name: "customer_id",
11862
- control,
11863
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { className: "space-y-3", children: [
11864
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11865
- /* @__PURE__ */ jsx(Form$2.Label, { children: "New customer" }),
11866
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "The customer to transfer this draft order to." })
11867
- ] }),
11868
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11869
- Combobox,
11870
- {
11871
- options: customers.options,
11872
- fetchNextPage: customers.fetchNextPage,
11873
- isFetchingNextPage: customers.isFetchingNextPage,
11874
- searchValue: customers.searchValue,
11875
- onSearchValueChange: customers.onSearchValueChange,
11876
- placeholder: "Select customer",
11877
- ...field
11878
- }
11879
- ) }),
11880
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11881
- ] })
11882
- }
11883
- );
11884
- };
11885
- const Illustration = () => {
11886
- return /* @__PURE__ */ jsxs(
11887
- "svg",
11888
- {
11889
- width: "280",
11890
- height: "180",
11891
- viewBox: "0 0 280 180",
11892
- fill: "none",
11893
- xmlns: "http://www.w3.org/2000/svg",
11894
- children: [
11895
- /* @__PURE__ */ jsx(
11896
- "rect",
11897
- {
11898
- x: "0.00428286",
11899
- y: "-0.742904",
11900
- width: "33.5",
11901
- height: "65.5",
11902
- rx: "6.75",
11903
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 189.756 88.438)",
11904
- fill: "#D4D4D8",
11905
- stroke: "#52525B",
11906
- strokeWidth: "1.5"
11907
- }
11908
- ),
11909
- /* @__PURE__ */ jsx(
11910
- "rect",
11911
- {
11912
- x: "0.00428286",
11913
- y: "-0.742904",
11914
- width: "33.5",
11915
- height: "65.5",
11916
- rx: "6.75",
11917
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 189.756 85.4381)",
11918
- fill: "white",
11919
- stroke: "#52525B",
11920
- strokeWidth: "1.5"
11921
- }
11922
- ),
11923
- /* @__PURE__ */ jsx(
11924
- "path",
11925
- {
11926
- d: "M180.579 107.142L179.126 107.959",
11927
- stroke: "#52525B",
11928
- strokeWidth: "1.5",
11929
- strokeLinecap: "round",
11930
- strokeLinejoin: "round"
11931
- }
11932
- ),
11933
- /* @__PURE__ */ jsx(
11934
- "path",
11935
- {
11936
- opacity: "0.88",
11937
- d: "M182.305 109.546L180.257 109.534",
11938
- stroke: "#52525B",
11939
- strokeWidth: "1.5",
11940
- strokeLinecap: "round",
11941
- strokeLinejoin: "round"
11942
- }
11943
- ),
11944
- /* @__PURE__ */ jsx(
11945
- "path",
11946
- {
11947
- opacity: "0.75",
11948
- d: "M180.551 111.93L179.108 111.096",
11949
- stroke: "#52525B",
11950
- strokeWidth: "1.5",
11951
- strokeLinecap: "round",
11952
- strokeLinejoin: "round"
11953
- }
11954
- ),
11955
- /* @__PURE__ */ jsx(
11956
- "path",
11957
- {
11958
- opacity: "0.63",
11959
- d: "M176.347 112.897L176.354 111.73",
11960
- stroke: "#52525B",
11961
- strokeWidth: "1.5",
11962
- strokeLinecap: "round",
11963
- strokeLinejoin: "round"
11964
- }
11965
- ),
11966
- /* @__PURE__ */ jsx(
11967
- "path",
11968
- {
11969
- opacity: "0.5",
11970
- d: "M172.153 111.881L173.606 111.064",
11971
- stroke: "#52525B",
11972
- strokeWidth: "1.5",
11973
- strokeLinecap: "round",
11974
- strokeLinejoin: "round"
11975
- }
11976
- ),
11977
- /* @__PURE__ */ jsx(
11978
- "path",
11979
- {
11980
- opacity: "0.38",
11981
- d: "M170.428 109.478L172.476 109.489",
11982
- stroke: "#52525B",
11983
- strokeWidth: "1.5",
11984
- strokeLinecap: "round",
11985
- strokeLinejoin: "round"
11986
- }
11987
- ),
11988
- /* @__PURE__ */ jsx(
11989
- "path",
11990
- {
11991
- opacity: "0.25",
11992
- d: "M172.181 107.094L173.624 107.928",
11993
- stroke: "#52525B",
11994
- strokeWidth: "1.5",
11995
- strokeLinecap: "round",
11996
- strokeLinejoin: "round"
11997
- }
11998
- ),
11999
- /* @__PURE__ */ jsx(
12000
- "path",
12001
- {
12002
- opacity: "0.13",
12003
- d: "M176.386 106.126L176.379 107.294",
12004
- stroke: "#52525B",
12005
- strokeWidth: "1.5",
12006
- strokeLinecap: "round",
12007
- strokeLinejoin: "round"
12008
- }
12009
- ),
12010
- /* @__PURE__ */ jsx(
12011
- "rect",
12012
- {
12013
- width: "12",
12014
- height: "3",
12015
- rx: "1.5",
12016
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 196.447 92.2925)",
12017
- fill: "#D4D4D8"
12018
- }
12019
- ),
12020
- /* @__PURE__ */ jsx(
12021
- "rect",
12022
- {
12023
- x: "0.00428286",
12024
- y: "-0.742904",
12025
- width: "33.5",
12026
- height: "65.5",
12027
- rx: "6.75",
12028
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 117.023 46.4147)",
12029
- fill: "#D4D4D8",
12030
- stroke: "#52525B",
12031
- strokeWidth: "1.5"
12032
- }
12033
- ),
12034
- /* @__PURE__ */ jsx(
12035
- "rect",
12036
- {
12037
- x: "0.00428286",
12038
- y: "-0.742904",
12039
- width: "33.5",
12040
- height: "65.5",
12041
- rx: "6.75",
12042
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 117.023 43.4147)",
12043
- fill: "white",
12044
- stroke: "#52525B",
12045
- strokeWidth: "1.5"
12046
- }
12047
- ),
12048
- /* @__PURE__ */ jsx(
12049
- "rect",
12050
- {
12051
- width: "12",
12052
- height: "3",
12053
- rx: "1.5",
12054
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 123.714 50.2691)",
12055
- fill: "#D4D4D8"
12056
- }
12057
- ),
12058
- /* @__PURE__ */ jsx(
12059
- "rect",
12060
- {
12061
- width: "17",
12062
- height: "3",
12063
- rx: "1.5",
12064
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 97.5557 66.958)",
12065
- fill: "#D4D4D8"
12066
- }
12067
- ),
12068
- /* @__PURE__ */ jsx(
12069
- "rect",
12070
- {
12071
- width: "12",
12072
- height: "3",
12073
- rx: "1.5",
12074
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 93.1978 69.4093)",
12075
- fill: "#D4D4D8"
12076
- }
12077
- ),
12078
- /* @__PURE__ */ jsx(
12079
- "path",
12080
- {
12081
- d: "M92.3603 63.9563C90.9277 63.1286 88.59 63.1152 87.148 63.9263C85.7059 64.7374 85.6983 66.0702 87.1308 66.8979C88.5634 67.7256 90.9011 67.7391 92.3432 66.928C93.7852 66.1168 93.7929 64.784 92.3603 63.9563ZM88.4382 66.1625C87.7221 65.7488 87.726 65.0822 88.4468 64.6767C89.1676 64.2713 90.3369 64.278 91.0529 64.6917C91.769 65.1055 91.7652 65.7721 91.0444 66.1775C90.3236 66.583 89.1543 66.5762 88.4382 66.1625Z",
12082
- fill: "#A1A1AA"
12083
- }
12084
- ),
12085
- /* @__PURE__ */ jsx(
12086
- "rect",
12087
- {
12088
- width: "17",
12089
- height: "3",
12090
- rx: "1.5",
12091
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 109.758 60.0944)",
12092
- fill: "#A1A1AA"
12093
- }
12094
- ),
12095
- /* @__PURE__ */ jsx(
12096
- "rect",
12097
- {
12098
- width: "12",
12099
- height: "3",
12100
- rx: "1.5",
12101
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 105.4 62.5457)",
12102
- fill: "#A1A1AA"
12103
- }
12104
- ),
12105
- /* @__PURE__ */ jsx(
12106
- "path",
12107
- {
12108
- d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
12109
- fill: "#52525B"
12110
- }
12111
- ),
12112
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsx(
12113
- "path",
12114
- {
12115
- d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
12116
- stroke: "#A1A1AA",
12117
- strokeWidth: "1.5",
12118
- strokeLinecap: "round",
12119
- strokeLinejoin: "round"
12120
- }
12121
- ) }),
12122
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsx(
12123
- "path",
12124
- {
12125
- d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
12126
- stroke: "#A1A1AA",
12127
- strokeWidth: "1.5",
12128
- strokeLinecap: "round",
12129
- strokeLinejoin: "round"
12130
- }
12131
- ) }),
12132
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsx(
12133
- "path",
12134
- {
12135
- d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12136
- stroke: "#A1A1AA",
12137
- strokeWidth: "1.5",
12138
- strokeLinecap: "round",
12139
- strokeLinejoin: "round"
12140
- }
12141
- ) }),
12142
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsx(
12143
- "path",
12144
- {
12145
- d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12146
- stroke: "#A1A1AA",
12147
- strokeWidth: "1.5",
12148
- strokeLinecap: "round",
12149
- strokeLinejoin: "round"
12150
- }
12151
- ) }),
12152
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsx(
12153
- "path",
12154
- {
12155
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12156
- stroke: "#A1A1AA",
12157
- strokeWidth: "1.5",
12158
- strokeLinecap: "round",
12159
- strokeLinejoin: "round"
12160
- }
12161
- ) }),
12162
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsx(
12163
- "path",
12164
- {
12165
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12166
- stroke: "#A1A1AA",
12167
- strokeWidth: "1.5",
12168
- strokeLinecap: "round",
12169
- strokeLinejoin: "round"
12170
- }
12171
- ) }),
12172
- /* @__PURE__ */ jsxs("defs", { children: [
12173
- /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
12174
- "rect",
12175
- {
12176
- width: "12",
12177
- height: "12",
12178
- fill: "white",
12179
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12180
- }
12181
- ) }),
12182
- /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
12183
- "rect",
12184
- {
12185
- width: "12",
12186
- height: "12",
12187
- fill: "white",
12188
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12189
- }
12190
- ) }),
12191
- /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
12192
- "rect",
12193
- {
12194
- width: "12",
12195
- height: "12",
12196
- fill: "white",
12197
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12198
- }
12199
- ) }),
12200
- /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
12201
- "rect",
12202
- {
12203
- width: "12",
12204
- height: "12",
12205
- fill: "white",
12206
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12207
- }
12208
- ) }),
12209
- /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
12210
- "rect",
12211
- {
12212
- width: "12",
12213
- height: "12",
12214
- fill: "white",
12215
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12216
- }
12217
- ) }),
12218
- /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
12219
- "rect",
12220
- {
12221
- width: "12",
12222
- height: "12",
12223
- fill: "white",
12224
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12225
- }
12226
- ) })
12227
- ] })
12228
- ]
12229
- }
12230
- );
12231
- };
12232
- const schema = objectType({
12233
- customer_id: stringType().min(1)
12234
- });
12235
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
12236
- const Shipping = () => {
12237
- var _a;
12238
- const { id } = useParams();
12239
- const { order, isPending, isError, error } = useOrder(id, {
12240
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
12241
- });
12242
- const {
12243
- order: preview,
12244
- isPending: isPreviewPending,
12245
- isError: isPreviewError,
12246
- error: previewError
12247
- } = useOrderPreview(id);
12248
- useInitiateOrderEdit({ preview });
12249
- const { onCancel } = useCancelOrderEdit({ preview });
12250
- if (isError) {
12251
- throw error;
12252
- }
12253
- if (isPreviewError) {
12254
- throw previewError;
12255
- }
12256
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
12257
- const isReady = preview && !isPreviewPending && order && !isPending;
12258
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
12259
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
12260
- /* @__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: [
12261
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12262
- /* @__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." }) })
12263
- ] }) }) }),
12264
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
12265
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
12266
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
12267
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
12268
- ] }) });
12269
- };
12270
- const ShippingForm = ({ preview, order }) => {
12271
- var _a;
12272
- const { setIsOpen } = useStackedModal();
12273
- const [isSubmitting, setIsSubmitting] = useState(false);
12274
- const [data, setData] = useState(null);
12275
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
12276
- const { shipping_options } = useShippingOptions(
12277
- {
12278
- id: appliedShippingOptionIds,
12279
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
12280
- },
12281
- {
12282
- enabled: appliedShippingOptionIds.length > 0
12283
- }
12284
- );
12285
- const uniqueShippingProfiles = useMemo(() => {
12286
- const profiles = /* @__PURE__ */ new Map();
12287
- getUniqueShippingProfiles(order.items).forEach((profile) => {
12288
- profiles.set(profile.id, profile);
12289
- });
12290
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
12291
- profiles.set(option.shipping_profile_id, option.shipping_profile);
12292
- });
12293
- return Array.from(profiles.values());
12294
- }, [order.items, shipping_options]);
12295
- const { handleSuccess } = useRouteModal();
12296
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12297
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
12298
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
12299
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
12300
- const onSubmit = async () => {
12301
- setIsSubmitting(true);
12302
- let requestSucceeded = false;
12303
- await requestOrderEdit(void 0, {
12304
- onError: (e) => {
12305
- toast.error(`Failed to request order edit: ${e.message}`);
12306
- },
12307
- onSuccess: () => {
12308
- requestSucceeded = true;
12309
- }
12310
- });
12311
- if (!requestSucceeded) {
12312
- setIsSubmitting(false);
12313
- return;
12314
- }
12315
- await confirmOrderEdit(void 0, {
12316
- onError: (e) => {
12317
- toast.error(`Failed to confirm order edit: ${e.message}`);
12318
- },
12319
- onSuccess: () => {
12320
- handleSuccess();
12321
- },
12322
- onSettled: () => {
12323
- setIsSubmitting(false);
12324
- }
12325
- });
12326
- };
12327
- const onKeydown = useCallback(
12328
- (e) => {
12329
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
12330
- if (data || isSubmitting) {
12331
- return;
12332
- }
12333
- onSubmit();
12334
- }
12335
- },
12336
- [data, isSubmitting, onSubmit]
12337
- );
12338
- useEffect(() => {
12339
- document.addEventListener("keydown", onKeydown);
12340
- return () => {
12341
- document.removeEventListener("keydown", onKeydown);
12342
- };
12343
- }, [onKeydown]);
12344
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
12345
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
12346
- /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
12347
- /* @__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: [
12348
- /* @__PURE__ */ jsxs("div", { children: [
12349
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12350
- /* @__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." }) })
12351
- ] }),
12352
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12353
- /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
12354
- /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
12355
- /* @__PURE__ */ jsx(
12356
- Text,
12357
- {
12358
- size: "xsmall",
12359
- weight: "plus",
12360
- className: "text-ui-fg-muted",
12361
- children: "Shipping profile"
12362
- }
12363
- ),
12364
- /* @__PURE__ */ jsx(
12365
- Text,
12366
- {
12367
- size: "xsmall",
12368
- weight: "plus",
12369
- className: "text-ui-fg-muted",
12370
- children: "Action"
12371
- }
12372
- )
12373
- ] }),
12374
- /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
12375
- var _a2, _b, _c, _d, _e, _f, _g;
12376
- const items = getItemsWithShippingProfile(
12377
- profile.id,
12378
- order.items
12379
- );
12380
- const hasItems = items.length > 0;
12381
- const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
12382
- (option) => option.shipping_profile_id === profile.id
12383
- );
12384
- const shippingMethod = preview.shipping_methods.find(
12385
- (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
12386
- );
12387
- const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
12388
- (action) => action.action === "SHIPPING_ADD"
12389
- );
12390
- return /* @__PURE__ */ jsxs(
12391
- Accordion.Item,
11418
+ /* @__PURE__ */ jsx("div", { className: "px-[5px] pb-[5px]", children: uniqueShippingProfiles.map((profile) => {
11419
+ var _a2, _b, _c, _d, _e, _f, _g;
11420
+ const items = getItemsWithShippingProfile(
11421
+ profile.id,
11422
+ order.items
11423
+ );
11424
+ const hasItems = items.length > 0;
11425
+ const shippingOption = shipping_options == null ? void 0 : shipping_options.find(
11426
+ (option) => option.shipping_profile_id === profile.id
11427
+ );
11428
+ const shippingMethod = preview.shipping_methods.find(
11429
+ (method) => method.shipping_option_id === (shippingOption == null ? void 0 : shippingOption.id)
11430
+ );
11431
+ const addShippingMethodAction = (_a2 = shippingMethod == null ? void 0 : shippingMethod.actions) == null ? void 0 : _a2.find(
11432
+ (action) => action.action === "SHIPPING_ADD"
11433
+ );
11434
+ return /* @__PURE__ */ jsxs(
11435
+ Accordion.Item,
12392
11436
  {
12393
11437
  value: profile.id,
12394
11438
  className: "bg-ui-bg-base shadow-elevation-card-rest rounded-lg",
@@ -12616,429 +11660,1385 @@ const ShippingForm = ({ preview, order }) => {
12616
11660
  ] })
12617
11661
  ]
12618
11662
  },
12619
- profile.id
12620
- );
12621
- }) })
11663
+ profile.id
11664
+ );
11665
+ }) })
11666
+ ] }) })
11667
+ ] }) }),
11668
+ /* @__PURE__ */ jsx(
11669
+ StackedFocusModal,
11670
+ {
11671
+ id: STACKED_FOCUS_MODAL_ID,
11672
+ onOpenChangeCallback: (open) => {
11673
+ if (!open) {
11674
+ setData(null);
11675
+ }
11676
+ return open;
11677
+ },
11678
+ children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
11679
+ }
11680
+ )
11681
+ ] }),
11682
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
11683
+ /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11684
+ /* @__PURE__ */ jsx(
11685
+ Button,
11686
+ {
11687
+ size: "small",
11688
+ type: "button",
11689
+ isLoading: isSubmitting,
11690
+ onClick: onSubmit,
11691
+ children: "Save"
11692
+ }
11693
+ )
11694
+ ] }) })
11695
+ ] });
11696
+ };
11697
+ const StackedModalTrigger = ({
11698
+ shippingProfileId,
11699
+ shippingOption,
11700
+ shippingMethod,
11701
+ setData,
11702
+ children
11703
+ }) => {
11704
+ const { setIsOpen, getIsOpen } = useStackedModal();
11705
+ const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
11706
+ const onToggle = () => {
11707
+ if (isOpen) {
11708
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11709
+ setData(null);
11710
+ } else {
11711
+ setIsOpen(STACKED_FOCUS_MODAL_ID, true);
11712
+ setData({
11713
+ shippingProfileId,
11714
+ shippingOption,
11715
+ shippingMethod
11716
+ });
11717
+ }
11718
+ };
11719
+ return /* @__PURE__ */ jsx(
11720
+ Button,
11721
+ {
11722
+ size: "small",
11723
+ variant: "secondary",
11724
+ onClick: onToggle,
11725
+ className: "text-ui-fg-primary shrink-0",
11726
+ children
11727
+ }
11728
+ );
11729
+ };
11730
+ const ShippingProfileForm = ({
11731
+ data,
11732
+ order,
11733
+ preview
11734
+ }) => {
11735
+ var _a, _b, _c, _d, _e, _f;
11736
+ const { setIsOpen } = useStackedModal();
11737
+ const form = useForm({
11738
+ resolver: zodResolver(shippingMethodSchema),
11739
+ defaultValues: {
11740
+ 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,
11741
+ shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
11742
+ custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
11743
+ }
11744
+ });
11745
+ const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
11746
+ const {
11747
+ mutateAsync: updateShippingMethod,
11748
+ isPending: isUpdatingShippingMethod
11749
+ } = useDraftOrderUpdateShippingMethod(order.id);
11750
+ const onSubmit = form.handleSubmit(async (values) => {
11751
+ if (isEqual(values, form.formState.defaultValues)) {
11752
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11753
+ return;
11754
+ }
11755
+ if (data.shippingMethod) {
11756
+ await updateShippingMethod(
11757
+ {
11758
+ method_id: data.shippingMethod.id,
11759
+ shipping_option_id: values.shipping_option_id,
11760
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
11761
+ },
11762
+ {
11763
+ onError: (e) => {
11764
+ toast.error(e.message);
11765
+ },
11766
+ onSuccess: () => {
11767
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11768
+ }
11769
+ }
11770
+ );
11771
+ return;
11772
+ }
11773
+ await addShippingMethod(
11774
+ {
11775
+ shipping_option_id: values.shipping_option_id,
11776
+ custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
11777
+ },
11778
+ {
11779
+ onError: (e) => {
11780
+ toast.error(e.message);
11781
+ },
11782
+ onSuccess: () => {
11783
+ setIsOpen(STACKED_FOCUS_MODAL_ID, false);
11784
+ }
11785
+ }
11786
+ );
11787
+ });
11788
+ return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
11789
+ KeyboundForm,
11790
+ {
11791
+ className: "flex h-full flex-col overflow-hidden",
11792
+ onSubmit,
11793
+ children: [
11794
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
11795
+ /* @__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: [
11796
+ /* @__PURE__ */ jsxs("div", { children: [
11797
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11798
+ /* @__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." }) })
11799
+ ] }),
11800
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11801
+ /* @__PURE__ */ jsx(
11802
+ LocationField,
11803
+ {
11804
+ control: form.control,
11805
+ setValue: form.setValue
11806
+ }
11807
+ ),
11808
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11809
+ /* @__PURE__ */ jsx(
11810
+ ShippingOptionField,
11811
+ {
11812
+ shippingProfileId: data.shippingProfileId,
11813
+ preview,
11814
+ control: form.control
11815
+ }
11816
+ ),
11817
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11818
+ /* @__PURE__ */ jsx(
11819
+ CustomAmountField,
11820
+ {
11821
+ control: form.control,
11822
+ currencyCode: order.currency_code
11823
+ }
11824
+ ),
11825
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11826
+ /* @__PURE__ */ jsx(
11827
+ ItemsPreview,
11828
+ {
11829
+ order,
11830
+ shippingProfileId: data.shippingProfileId
11831
+ }
11832
+ )
11833
+ ] }) }) }),
11834
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
11835
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11836
+ /* @__PURE__ */ jsx(
11837
+ Button,
11838
+ {
11839
+ size: "small",
11840
+ type: "submit",
11841
+ isLoading: isPending || isUpdatingShippingMethod,
11842
+ children: data.shippingMethod ? "Update" : "Add"
11843
+ }
11844
+ )
11845
+ ] }) })
11846
+ ]
11847
+ }
11848
+ ) }) });
11849
+ };
11850
+ const shippingMethodSchema = objectType({
11851
+ location_id: stringType(),
11852
+ shipping_option_id: stringType(),
11853
+ custom_amount: unionType([numberType(), stringType()]).optional()
11854
+ });
11855
+ const ItemsPreview = ({ order, shippingProfileId }) => {
11856
+ const matches = order.items.filter(
11857
+ (item) => {
11858
+ var _a, _b, _c;
11859
+ return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
11860
+ }
11861
+ );
11862
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
11863
+ /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11864
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
11865
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
11866
+ ] }) }),
11867
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
11868
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
11869
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
11870
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
11871
+ ] }),
11872
+ /* @__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(
11873
+ "div",
11874
+ {
11875
+ className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
11876
+ children: [
11877
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
11878
+ /* @__PURE__ */ jsx(
11879
+ Thumbnail,
11880
+ {
11881
+ thumbnail: item.thumbnail,
11882
+ alt: item.product_title ?? void 0
11883
+ }
11884
+ ),
11885
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11886
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
11887
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
11888
+ /* @__PURE__ */ jsxs(
11889
+ Text,
11890
+ {
11891
+ size: "small",
11892
+ leading: "compact",
11893
+ className: "text-ui-fg-subtle",
11894
+ children: [
11895
+ "(",
11896
+ item.variant_title,
11897
+ ")"
11898
+ ]
11899
+ }
11900
+ )
11901
+ ] }),
11902
+ /* @__PURE__ */ jsx(
11903
+ Text,
11904
+ {
11905
+ size: "small",
11906
+ leading: "compact",
11907
+ className: "text-ui-fg-subtle",
11908
+ children: item.variant_sku
11909
+ }
11910
+ )
11911
+ ] })
11912
+ ] }),
11913
+ /* @__PURE__ */ jsxs(
11914
+ Text,
11915
+ {
11916
+ size: "small",
11917
+ leading: "compact",
11918
+ className: "text-ui-fg-subtle",
11919
+ children: [
11920
+ item.quantity,
11921
+ "x"
11922
+ ]
11923
+ }
11924
+ )
11925
+ ]
11926
+ },
11927
+ item.id
11928
+ )) : /* @__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: [
11929
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
11930
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
11931
+ 'No items found for "',
11932
+ query,
11933
+ '".'
11934
+ ] })
11935
+ ] }) })
11936
+ ] })
11937
+ ] });
11938
+ };
11939
+ const LocationField = ({ control, setValue }) => {
11940
+ const locations = useComboboxData({
11941
+ queryKey: ["locations"],
11942
+ queryFn: async (params) => {
11943
+ return await sdk.admin.stockLocation.list(params);
11944
+ },
11945
+ getOptions: (data) => {
11946
+ return data.stock_locations.map((location) => ({
11947
+ label: location.name,
11948
+ value: location.id
11949
+ }));
11950
+ }
11951
+ });
11952
+ return /* @__PURE__ */ jsx(
11953
+ Form$2.Field,
11954
+ {
11955
+ control,
11956
+ name: "location_id",
11957
+ render: ({ field: { onChange, ...field } }) => {
11958
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11959
+ /* @__PURE__ */ jsxs("div", { children: [
11960
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
11961
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
11962
+ ] }),
11963
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11964
+ Combobox,
11965
+ {
11966
+ options: locations.options,
11967
+ fetchNextPage: locations.fetchNextPage,
11968
+ isFetchingNextPage: locations.isFetchingNextPage,
11969
+ searchValue: locations.searchValue,
11970
+ onSearchValueChange: locations.onSearchValueChange,
11971
+ placeholder: "Select location",
11972
+ onChange: (value) => {
11973
+ setValue("shipping_option_id", "", {
11974
+ shouldDirty: true,
11975
+ shouldTouch: true
11976
+ });
11977
+ onChange(value);
11978
+ },
11979
+ ...field
11980
+ }
11981
+ ) })
11982
+ ] }) });
11983
+ }
11984
+ }
11985
+ );
11986
+ };
11987
+ const ShippingOptionField = ({
11988
+ shippingProfileId,
11989
+ preview,
11990
+ control
11991
+ }) => {
11992
+ var _a;
11993
+ const locationId = useWatch({ control, name: "location_id" });
11994
+ const shippingOptions = useComboboxData({
11995
+ queryKey: ["shipping_options", locationId, shippingProfileId],
11996
+ queryFn: async (params) => {
11997
+ return await sdk.admin.shippingOption.list({
11998
+ ...params,
11999
+ stock_location_id: locationId,
12000
+ shipping_profile_id: shippingProfileId
12001
+ });
12002
+ },
12003
+ getOptions: (data) => {
12004
+ return data.shipping_options.map((option) => {
12005
+ var _a2;
12006
+ if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12007
+ (r) => r.attribute === "is_return" && r.value === "true"
12008
+ )) {
12009
+ return void 0;
12010
+ }
12011
+ return {
12012
+ label: option.name,
12013
+ value: option.id
12014
+ };
12015
+ }).filter(Boolean);
12016
+ },
12017
+ enabled: !!locationId && !!shippingProfileId,
12018
+ defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12019
+ });
12020
+ const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12021
+ return /* @__PURE__ */ jsx(
12022
+ Form$2.Field,
12023
+ {
12024
+ control,
12025
+ name: "shipping_option_id",
12026
+ render: ({ field }) => {
12027
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12028
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12029
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12030
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12031
+ ] }),
12032
+ /* @__PURE__ */ jsx(
12033
+ ConditionalTooltip,
12034
+ {
12035
+ content: tooltipContent,
12036
+ showTooltip: !locationId || !shippingProfileId,
12037
+ children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12038
+ Combobox,
12039
+ {
12040
+ options: shippingOptions.options,
12041
+ fetchNextPage: shippingOptions.fetchNextPage,
12042
+ isFetchingNextPage: shippingOptions.isFetchingNextPage,
12043
+ searchValue: shippingOptions.searchValue,
12044
+ onSearchValueChange: shippingOptions.onSearchValueChange,
12045
+ placeholder: "Select shipping option",
12046
+ ...field,
12047
+ disabled: !locationId || !shippingProfileId
12048
+ }
12049
+ ) }) })
12050
+ }
12051
+ )
12052
+ ] }) });
12053
+ }
12054
+ }
12055
+ );
12056
+ };
12057
+ const CustomAmountField = ({
12058
+ control,
12059
+ currencyCode
12060
+ }) => {
12061
+ return /* @__PURE__ */ jsx(
12062
+ Form$2.Field,
12063
+ {
12064
+ control,
12065
+ name: "custom_amount",
12066
+ render: ({ field: { onChange, ...field } }) => {
12067
+ return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12068
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12069
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12070
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12071
+ ] }),
12072
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12073
+ CurrencyInput,
12074
+ {
12075
+ ...field,
12076
+ onValueChange: (value) => onChange(value),
12077
+ symbol: getNativeSymbol(currencyCode),
12078
+ code: currencyCode
12079
+ }
12080
+ ) })
12081
+ ] });
12082
+ }
12083
+ }
12084
+ );
12085
+ };
12086
+ const ShippingAddress = () => {
12087
+ const { id } = useParams();
12088
+ const { order, isPending, isError, error } = useOrder(id, {
12089
+ fields: "+shipping_address"
12090
+ });
12091
+ if (isError) {
12092
+ throw error;
12093
+ }
12094
+ const isReady = !isPending && !!order;
12095
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12096
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12097
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12098
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12099
+ ] }),
12100
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12101
+ ] });
12102
+ };
12103
+ const ShippingAddressForm = ({ order }) => {
12104
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12105
+ const form = useForm({
12106
+ defaultValues: {
12107
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12108
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12109
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12110
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12111
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12112
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12113
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12114
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12115
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12116
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12117
+ },
12118
+ resolver: zodResolver(schema$1)
12119
+ });
12120
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12121
+ const { handleSuccess } = useRouteModal();
12122
+ const onSubmit = form.handleSubmit(async (data) => {
12123
+ await mutateAsync(
12124
+ {
12125
+ shipping_address: {
12126
+ first_name: data.first_name,
12127
+ last_name: data.last_name,
12128
+ company: data.company,
12129
+ address_1: data.address_1,
12130
+ address_2: data.address_2,
12131
+ city: data.city,
12132
+ province: data.province,
12133
+ country_code: data.country_code,
12134
+ postal_code: data.postal_code,
12135
+ phone: data.phone
12136
+ }
12137
+ },
12138
+ {
12139
+ onSuccess: () => {
12140
+ handleSuccess();
12141
+ },
12142
+ onError: (error) => {
12143
+ toast.error(error.message);
12144
+ }
12145
+ }
12146
+ );
12147
+ });
12148
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12149
+ KeyboundForm,
12150
+ {
12151
+ className: "flex flex-1 flex-col overflow-hidden",
12152
+ onSubmit,
12153
+ children: [
12154
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
12155
+ /* @__PURE__ */ jsx(
12156
+ Form$2.Field,
12157
+ {
12158
+ control: form.control,
12159
+ name: "country_code",
12160
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12161
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12162
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12163
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12164
+ ] })
12165
+ }
12166
+ ),
12167
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12168
+ /* @__PURE__ */ jsx(
12169
+ Form$2.Field,
12170
+ {
12171
+ control: form.control,
12172
+ name: "first_name",
12173
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12174
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12175
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12176
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12177
+ ] })
12178
+ }
12179
+ ),
12180
+ /* @__PURE__ */ jsx(
12181
+ Form$2.Field,
12182
+ {
12183
+ control: form.control,
12184
+ name: "last_name",
12185
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12186
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12187
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12188
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12189
+ ] })
12190
+ }
12191
+ )
12192
+ ] }),
12193
+ /* @__PURE__ */ jsx(
12194
+ Form$2.Field,
12195
+ {
12196
+ control: form.control,
12197
+ name: "company",
12198
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12199
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12200
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12201
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12202
+ ] })
12203
+ }
12204
+ ),
12205
+ /* @__PURE__ */ jsx(
12206
+ Form$2.Field,
12207
+ {
12208
+ control: form.control,
12209
+ name: "address_1",
12210
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12211
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12212
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12213
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12214
+ ] })
12215
+ }
12216
+ ),
12217
+ /* @__PURE__ */ jsx(
12218
+ Form$2.Field,
12219
+ {
12220
+ control: form.control,
12221
+ name: "address_2",
12222
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12223
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12224
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12225
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12226
+ ] })
12227
+ }
12228
+ ),
12229
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12230
+ /* @__PURE__ */ jsx(
12231
+ Form$2.Field,
12232
+ {
12233
+ control: form.control,
12234
+ name: "postal_code",
12235
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12236
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12237
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12238
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12239
+ ] })
12240
+ }
12241
+ ),
12242
+ /* @__PURE__ */ jsx(
12243
+ Form$2.Field,
12244
+ {
12245
+ control: form.control,
12246
+ name: "city",
12247
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12248
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12249
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12250
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12251
+ ] })
12252
+ }
12253
+ )
12254
+ ] }),
12255
+ /* @__PURE__ */ jsx(
12256
+ Form$2.Field,
12257
+ {
12258
+ control: form.control,
12259
+ name: "province",
12260
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12261
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12262
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12263
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12264
+ ] })
12265
+ }
12266
+ ),
12267
+ /* @__PURE__ */ jsx(
12268
+ Form$2.Field,
12269
+ {
12270
+ control: form.control,
12271
+ name: "phone",
12272
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12273
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12274
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12275
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12276
+ ] })
12277
+ }
12278
+ )
12279
+ ] }) }),
12280
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12281
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12282
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12622
12283
  ] }) })
12623
- ] }) }),
12624
- /* @__PURE__ */ jsx(
12625
- StackedFocusModal,
12626
- {
12627
- id: STACKED_FOCUS_MODAL_ID,
12628
- onOpenChangeCallback: (open) => {
12629
- if (!open) {
12630
- setData(null);
12284
+ ]
12285
+ }
12286
+ ) });
12287
+ };
12288
+ const schema$1 = addressSchema;
12289
+ const TransferOwnership = () => {
12290
+ const { id } = useParams();
12291
+ const { draft_order, isPending, isError, error } = useDraftOrder(id, {
12292
+ fields: "id,customer_id,customer.*"
12293
+ });
12294
+ if (isError) {
12295
+ throw error;
12296
+ }
12297
+ const isReady = !isPending && !!draft_order;
12298
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12299
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12300
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
12301
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
12302
+ ] }),
12303
+ isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
12304
+ ] });
12305
+ };
12306
+ const TransferOwnershipForm = ({ order }) => {
12307
+ var _a, _b;
12308
+ const form = useForm({
12309
+ defaultValues: {
12310
+ customer_id: order.customer_id || ""
12311
+ },
12312
+ resolver: zodResolver(schema)
12313
+ });
12314
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12315
+ const { handleSuccess } = useRouteModal();
12316
+ const name = [(_a = order.customer) == null ? void 0 : _a.first_name, (_b = order.customer) == null ? void 0 : _b.last_name].filter(Boolean).join(" ");
12317
+ const currentCustomer = order.customer ? {
12318
+ label: name ? `${name} (${order.customer.email})` : order.customer.email,
12319
+ value: order.customer.id
12320
+ } : null;
12321
+ const onSubmit = form.handleSubmit(async (data) => {
12322
+ await mutateAsync(
12323
+ { customer_id: data.customer_id },
12324
+ {
12325
+ onSuccess: () => {
12326
+ toast.success("Customer updated");
12327
+ handleSuccess();
12328
+ },
12329
+ onError: (error) => {
12330
+ toast.error(error.message);
12331
+ }
12332
+ }
12333
+ );
12334
+ });
12335
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12336
+ KeyboundForm,
12337
+ {
12338
+ className: "flex flex-1 flex-col overflow-hidden",
12339
+ onSubmit,
12340
+ children: [
12341
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: [
12342
+ /* @__PURE__ */ jsx("div", { className: "flex items-center justify-center bg-ui-bg-component rounded-md border", children: /* @__PURE__ */ jsx(Illustration, {}) }),
12343
+ currentCustomer && /* @__PURE__ */ jsxs("div", { className: "flex flex-col space-y-3", children: [
12344
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12345
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "current-customer", children: "Current owner" }),
12346
+ /* @__PURE__ */ jsx(Hint$1, { children: "The customer that is currently associated with this draft order." })
12347
+ ] }),
12348
+ /* @__PURE__ */ jsxs(Select, { disabled: true, value: currentCustomer.value, children: [
12349
+ /* @__PURE__ */ jsx(Select.Trigger, { id: "current-customer", children: /* @__PURE__ */ jsx(Select.Value, {}) }),
12350
+ /* @__PURE__ */ jsx(Select.Content, { children: /* @__PURE__ */ jsx(Select.Item, { value: currentCustomer.value, children: currentCustomer.label }) })
12351
+ ] })
12352
+ ] }),
12353
+ /* @__PURE__ */ jsx(
12354
+ CustomerField,
12355
+ {
12356
+ control: form.control,
12357
+ currentCustomerId: order.customer_id
12631
12358
  }
12632
- return open;
12633
- },
12634
- children: data && /* @__PURE__ */ jsx(ShippingProfileForm, { data, order, preview })
12635
- }
12636
- )
12637
- ] }),
12638
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12639
- /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12640
- /* @__PURE__ */ jsx(
12641
- Button,
12642
- {
12643
- size: "small",
12644
- type: "button",
12645
- isLoading: isSubmitting,
12646
- onClick: onSubmit,
12647
- children: "Save"
12648
- }
12649
- )
12650
- ] }) })
12651
- ] });
12359
+ )
12360
+ ] }),
12361
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12362
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { variant: "secondary", size: "small", children: "Cancel" }) }),
12363
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12364
+ ] }) })
12365
+ ]
12366
+ }
12367
+ ) });
12652
12368
  };
12653
- const StackedModalTrigger = ({
12654
- shippingProfileId,
12655
- shippingOption,
12656
- shippingMethod,
12657
- setData,
12658
- children
12659
- }) => {
12660
- const { setIsOpen, getIsOpen } = useStackedModal();
12661
- const isOpen = getIsOpen(STACKED_FOCUS_MODAL_ID);
12662
- const onToggle = () => {
12663
- if (isOpen) {
12664
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12665
- setData(null);
12666
- } else {
12667
- setIsOpen(STACKED_FOCUS_MODAL_ID, true);
12668
- setData({
12669
- shippingProfileId,
12670
- shippingOption,
12671
- shippingMethod
12369
+ const CustomerField = ({ control, currentCustomerId }) => {
12370
+ const customers = useComboboxData({
12371
+ queryFn: async (params) => {
12372
+ return await sdk.admin.customer.list({
12373
+ ...params,
12374
+ id: currentCustomerId ? { $nin: [currentCustomerId] } : void 0
12375
+ });
12376
+ },
12377
+ queryKey: ["customers"],
12378
+ getOptions: (data) => {
12379
+ return data.customers.map((customer) => {
12380
+ const name = [customer.first_name, customer.last_name].filter(Boolean).join(" ");
12381
+ return {
12382
+ label: name ? `${name} (${customer.email})` : customer.email,
12383
+ value: customer.id
12384
+ };
12672
12385
  });
12673
12386
  }
12674
- };
12387
+ });
12675
12388
  return /* @__PURE__ */ jsx(
12676
- Button,
12389
+ Form$2.Field,
12677
12390
  {
12678
- size: "small",
12679
- variant: "secondary",
12680
- onClick: onToggle,
12681
- className: "text-ui-fg-primary shrink-0",
12682
- children
12391
+ name: "customer_id",
12392
+ control,
12393
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { className: "space-y-3", children: [
12394
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12395
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "New customer" }),
12396
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "The customer to transfer this draft order to." })
12397
+ ] }),
12398
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12399
+ Combobox,
12400
+ {
12401
+ options: customers.options,
12402
+ fetchNextPage: customers.fetchNextPage,
12403
+ isFetchingNextPage: customers.isFetchingNextPage,
12404
+ searchValue: customers.searchValue,
12405
+ onSearchValueChange: customers.onSearchValueChange,
12406
+ placeholder: "Select customer",
12407
+ ...field
12408
+ }
12409
+ ) }),
12410
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12411
+ ] })
12683
12412
  }
12684
12413
  );
12685
12414
  };
12686
- const ShippingProfileForm = ({
12687
- data,
12688
- order,
12689
- preview
12690
- }) => {
12691
- var _a, _b, _c, _d, _e, _f;
12692
- const { setIsOpen } = useStackedModal();
12693
- const form = useForm({
12694
- resolver: zodResolver(shippingMethodSchema),
12695
- defaultValues: {
12696
- 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,
12697
- shipping_option_id: (_e = data.shippingOption) == null ? void 0 : _e.id,
12698
- custom_amount: (_f = data.shippingMethod) == null ? void 0 : _f.amount
12699
- }
12700
- });
12701
- const { mutateAsync: addShippingMethod, isPending } = useDraftOrderAddShippingMethod(order.id);
12702
- const {
12703
- mutateAsync: updateShippingMethod,
12704
- isPending: isUpdatingShippingMethod
12705
- } = useDraftOrderUpdateShippingMethod(order.id);
12706
- const onSubmit = form.handleSubmit(async (values) => {
12707
- if (isEqual(values, form.formState.defaultValues)) {
12708
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12709
- return;
12710
- }
12711
- if (data.shippingMethod) {
12712
- await updateShippingMethod(
12713
- {
12714
- method_id: data.shippingMethod.id,
12715
- shipping_option_id: values.shipping_option_id,
12716
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12717
- },
12718
- {
12719
- onError: (e) => {
12720
- toast.error(e.message);
12721
- },
12722
- onSuccess: () => {
12723
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12415
+ const Illustration = () => {
12416
+ return /* @__PURE__ */ jsxs(
12417
+ "svg",
12418
+ {
12419
+ width: "280",
12420
+ height: "180",
12421
+ viewBox: "0 0 280 180",
12422
+ fill: "none",
12423
+ xmlns: "http://www.w3.org/2000/svg",
12424
+ children: [
12425
+ /* @__PURE__ */ jsx(
12426
+ "rect",
12427
+ {
12428
+ x: "0.00428286",
12429
+ y: "-0.742904",
12430
+ width: "33.5",
12431
+ height: "65.5",
12432
+ rx: "6.75",
12433
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 189.756 88.438)",
12434
+ fill: "#D4D4D8",
12435
+ stroke: "#52525B",
12436
+ strokeWidth: "1.5"
12437
+ }
12438
+ ),
12439
+ /* @__PURE__ */ jsx(
12440
+ "rect",
12441
+ {
12442
+ x: "0.00428286",
12443
+ y: "-0.742904",
12444
+ width: "33.5",
12445
+ height: "65.5",
12446
+ rx: "6.75",
12447
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 189.756 85.4381)",
12448
+ fill: "white",
12449
+ stroke: "#52525B",
12450
+ strokeWidth: "1.5"
12451
+ }
12452
+ ),
12453
+ /* @__PURE__ */ jsx(
12454
+ "path",
12455
+ {
12456
+ d: "M180.579 107.142L179.126 107.959",
12457
+ stroke: "#52525B",
12458
+ strokeWidth: "1.5",
12459
+ strokeLinecap: "round",
12460
+ strokeLinejoin: "round"
12461
+ }
12462
+ ),
12463
+ /* @__PURE__ */ jsx(
12464
+ "path",
12465
+ {
12466
+ opacity: "0.88",
12467
+ d: "M182.305 109.546L180.257 109.534",
12468
+ stroke: "#52525B",
12469
+ strokeWidth: "1.5",
12470
+ strokeLinecap: "round",
12471
+ strokeLinejoin: "round"
12472
+ }
12473
+ ),
12474
+ /* @__PURE__ */ jsx(
12475
+ "path",
12476
+ {
12477
+ opacity: "0.75",
12478
+ d: "M180.551 111.93L179.108 111.096",
12479
+ stroke: "#52525B",
12480
+ strokeWidth: "1.5",
12481
+ strokeLinecap: "round",
12482
+ strokeLinejoin: "round"
12483
+ }
12484
+ ),
12485
+ /* @__PURE__ */ jsx(
12486
+ "path",
12487
+ {
12488
+ opacity: "0.63",
12489
+ d: "M176.347 112.897L176.354 111.73",
12490
+ stroke: "#52525B",
12491
+ strokeWidth: "1.5",
12492
+ strokeLinecap: "round",
12493
+ strokeLinejoin: "round"
12494
+ }
12495
+ ),
12496
+ /* @__PURE__ */ jsx(
12497
+ "path",
12498
+ {
12499
+ opacity: "0.5",
12500
+ d: "M172.153 111.881L173.606 111.064",
12501
+ stroke: "#52525B",
12502
+ strokeWidth: "1.5",
12503
+ strokeLinecap: "round",
12504
+ strokeLinejoin: "round"
12505
+ }
12506
+ ),
12507
+ /* @__PURE__ */ jsx(
12508
+ "path",
12509
+ {
12510
+ opacity: "0.38",
12511
+ d: "M170.428 109.478L172.476 109.489",
12512
+ stroke: "#52525B",
12513
+ strokeWidth: "1.5",
12514
+ strokeLinecap: "round",
12515
+ strokeLinejoin: "round"
12516
+ }
12517
+ ),
12518
+ /* @__PURE__ */ jsx(
12519
+ "path",
12520
+ {
12521
+ opacity: "0.25",
12522
+ d: "M172.181 107.094L173.624 107.928",
12523
+ stroke: "#52525B",
12524
+ strokeWidth: "1.5",
12525
+ strokeLinecap: "round",
12526
+ strokeLinejoin: "round"
12527
+ }
12528
+ ),
12529
+ /* @__PURE__ */ jsx(
12530
+ "path",
12531
+ {
12532
+ opacity: "0.13",
12533
+ d: "M176.386 106.126L176.379 107.294",
12534
+ stroke: "#52525B",
12535
+ strokeWidth: "1.5",
12536
+ strokeLinecap: "round",
12537
+ strokeLinejoin: "round"
12724
12538
  }
12725
- }
12726
- );
12727
- return;
12728
- }
12729
- await addShippingMethod(
12730
- {
12731
- shipping_option_id: values.shipping_option_id,
12732
- custom_amount: values.custom_amount ? convertNumber(values.custom_amount) : void 0
12733
- },
12734
- {
12735
- onError: (e) => {
12736
- toast.error(e.message);
12737
- },
12738
- onSuccess: () => {
12739
- setIsOpen(STACKED_FOCUS_MODAL_ID, false);
12740
- }
12741
- }
12742
- );
12743
- });
12744
- return /* @__PURE__ */ jsx(StackedFocusModal.Content, { children: /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxs(
12745
- KeyboundForm,
12746
- {
12747
- className: "flex h-full flex-col overflow-hidden",
12748
- onSubmit,
12749
- children: [
12750
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12751
- /* @__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: [
12752
- /* @__PURE__ */ jsxs("div", { children: [
12753
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
12754
- /* @__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." }) })
12755
- ] }),
12756
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12757
- /* @__PURE__ */ jsx(
12758
- LocationField,
12539
+ ),
12540
+ /* @__PURE__ */ jsx(
12541
+ "rect",
12542
+ {
12543
+ width: "12",
12544
+ height: "3",
12545
+ rx: "1.5",
12546
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 196.447 92.2925)",
12547
+ fill: "#D4D4D8"
12548
+ }
12549
+ ),
12550
+ /* @__PURE__ */ jsx(
12551
+ "rect",
12552
+ {
12553
+ x: "0.00428286",
12554
+ y: "-0.742904",
12555
+ width: "33.5",
12556
+ height: "65.5",
12557
+ rx: "6.75",
12558
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 117.023 46.4147)",
12559
+ fill: "#D4D4D8",
12560
+ stroke: "#52525B",
12561
+ strokeWidth: "1.5"
12562
+ }
12563
+ ),
12564
+ /* @__PURE__ */ jsx(
12565
+ "rect",
12566
+ {
12567
+ x: "0.00428286",
12568
+ y: "-0.742904",
12569
+ width: "33.5",
12570
+ height: "65.5",
12571
+ rx: "6.75",
12572
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 117.023 43.4147)",
12573
+ fill: "white",
12574
+ stroke: "#52525B",
12575
+ strokeWidth: "1.5"
12576
+ }
12577
+ ),
12578
+ /* @__PURE__ */ jsx(
12579
+ "rect",
12580
+ {
12581
+ width: "12",
12582
+ height: "3",
12583
+ rx: "1.5",
12584
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 123.714 50.2691)",
12585
+ fill: "#D4D4D8"
12586
+ }
12587
+ ),
12588
+ /* @__PURE__ */ jsx(
12589
+ "rect",
12590
+ {
12591
+ width: "17",
12592
+ height: "3",
12593
+ rx: "1.5",
12594
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 97.5557 66.958)",
12595
+ fill: "#D4D4D8"
12596
+ }
12597
+ ),
12598
+ /* @__PURE__ */ jsx(
12599
+ "rect",
12600
+ {
12601
+ width: "12",
12602
+ height: "3",
12603
+ rx: "1.5",
12604
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 93.1978 69.4093)",
12605
+ fill: "#D4D4D8"
12606
+ }
12607
+ ),
12608
+ /* @__PURE__ */ jsx(
12609
+ "path",
12610
+ {
12611
+ d: "M92.3603 63.9563C90.9277 63.1286 88.59 63.1152 87.148 63.9263C85.7059 64.7374 85.6983 66.0702 87.1308 66.8979C88.5634 67.7256 90.9011 67.7391 92.3432 66.928C93.7852 66.1168 93.7929 64.784 92.3603 63.9563ZM88.4382 66.1625C87.7221 65.7488 87.726 65.0822 88.4468 64.6767C89.1676 64.2713 90.3369 64.278 91.0529 64.6917C91.769 65.1055 91.7652 65.7721 91.0444 66.1775C90.3236 66.583 89.1543 66.5762 88.4382 66.1625Z",
12612
+ fill: "#A1A1AA"
12613
+ }
12614
+ ),
12615
+ /* @__PURE__ */ jsx(
12616
+ "rect",
12617
+ {
12618
+ width: "17",
12619
+ height: "3",
12620
+ rx: "1.5",
12621
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 109.758 60.0944)",
12622
+ fill: "#A1A1AA"
12623
+ }
12624
+ ),
12625
+ /* @__PURE__ */ jsx(
12626
+ "rect",
12627
+ {
12628
+ width: "12",
12629
+ height: "3",
12630
+ rx: "1.5",
12631
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 105.4 62.5457)",
12632
+ fill: "#A1A1AA"
12633
+ }
12634
+ ),
12635
+ /* @__PURE__ */ jsx(
12636
+ "path",
12637
+ {
12638
+ d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
12639
+ fill: "#52525B"
12640
+ }
12641
+ ),
12642
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsx(
12643
+ "path",
12644
+ {
12645
+ d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
12646
+ stroke: "#A1A1AA",
12647
+ strokeWidth: "1.5",
12648
+ strokeLinecap: "round",
12649
+ strokeLinejoin: "round"
12650
+ }
12651
+ ) }),
12652
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsx(
12653
+ "path",
12654
+ {
12655
+ d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
12656
+ stroke: "#A1A1AA",
12657
+ strokeWidth: "1.5",
12658
+ strokeLinecap: "round",
12659
+ strokeLinejoin: "round"
12660
+ }
12661
+ ) }),
12662
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsx(
12663
+ "path",
12664
+ {
12665
+ d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12666
+ stroke: "#A1A1AA",
12667
+ strokeWidth: "1.5",
12668
+ strokeLinecap: "round",
12669
+ strokeLinejoin: "round"
12670
+ }
12671
+ ) }),
12672
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsx(
12673
+ "path",
12674
+ {
12675
+ d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12676
+ stroke: "#A1A1AA",
12677
+ strokeWidth: "1.5",
12678
+ strokeLinecap: "round",
12679
+ strokeLinejoin: "round"
12680
+ }
12681
+ ) }),
12682
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsx(
12683
+ "path",
12684
+ {
12685
+ d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12686
+ stroke: "#A1A1AA",
12687
+ strokeWidth: "1.5",
12688
+ strokeLinecap: "round",
12689
+ strokeLinejoin: "round"
12690
+ }
12691
+ ) }),
12692
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsx(
12693
+ "path",
12694
+ {
12695
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12696
+ stroke: "#A1A1AA",
12697
+ strokeWidth: "1.5",
12698
+ strokeLinecap: "round",
12699
+ strokeLinejoin: "round"
12700
+ }
12701
+ ) }),
12702
+ /* @__PURE__ */ jsxs("defs", { children: [
12703
+ /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
12704
+ "rect",
12759
12705
  {
12760
- control: form.control,
12761
- setValue: form.setValue
12706
+ width: "12",
12707
+ height: "12",
12708
+ fill: "white",
12709
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12762
12710
  }
12763
- ),
12764
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12765
- /* @__PURE__ */ jsx(
12766
- ShippingOptionField,
12711
+ ) }),
12712
+ /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
12713
+ "rect",
12767
12714
  {
12768
- shippingProfileId: data.shippingProfileId,
12769
- preview,
12770
- control: form.control
12715
+ width: "12",
12716
+ height: "12",
12717
+ fill: "white",
12718
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12771
12719
  }
12772
- ),
12773
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12774
- /* @__PURE__ */ jsx(
12775
- CustomAmountField,
12720
+ ) }),
12721
+ /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
12722
+ "rect",
12776
12723
  {
12777
- control: form.control,
12778
- currencyCode: order.currency_code
12724
+ width: "12",
12725
+ height: "12",
12726
+ fill: "white",
12727
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12779
12728
  }
12780
- ),
12781
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12782
- /* @__PURE__ */ jsx(
12783
- ItemsPreview,
12729
+ ) }),
12730
+ /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
12731
+ "rect",
12784
12732
  {
12785
- order,
12786
- shippingProfileId: data.shippingProfileId
12733
+ width: "12",
12734
+ height: "12",
12735
+ fill: "white",
12736
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12787
12737
  }
12788
- )
12789
- ] }) }) }),
12790
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-x-2", children: [
12791
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12792
- /* @__PURE__ */ jsx(
12793
- Button,
12738
+ ) }),
12739
+ /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
12740
+ "rect",
12741
+ {
12742
+ width: "12",
12743
+ height: "12",
12744
+ fill: "white",
12745
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12746
+ }
12747
+ ) }),
12748
+ /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
12749
+ "rect",
12794
12750
  {
12795
- size: "small",
12796
- type: "submit",
12797
- isLoading: isPending || isUpdatingShippingMethod,
12798
- children: data.shippingMethod ? "Update" : "Add"
12751
+ width: "12",
12752
+ height: "12",
12753
+ fill: "white",
12754
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12799
12755
  }
12800
- )
12801
- ] }) })
12756
+ ) })
12757
+ ] })
12802
12758
  ]
12803
12759
  }
12804
- ) }) });
12760
+ );
12805
12761
  };
12806
- const shippingMethodSchema = objectType({
12807
- location_id: stringType(),
12808
- shipping_option_id: stringType(),
12809
- custom_amount: unionType([numberType(), stringType()]).optional()
12762
+ const schema = objectType({
12763
+ customer_id: stringType().min(1)
12810
12764
  });
12811
- const ItemsPreview = ({ order, shippingProfileId }) => {
12812
- const matches = order.items.filter(
12813
- (item) => {
12814
- var _a, _b, _c;
12815
- return ((_c = (_b = (_a = item.variant) == null ? void 0 : _a.product) == null ? void 0 : _b.shipping_profile) == null ? void 0 : _c.id) === shippingProfileId;
12816
- }
12817
- );
12818
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12819
- /* @__PURE__ */ jsx("div", { className: "grid grid-cols-2 items-center gap-3", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12820
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items to ship" }),
12821
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Items with the selected shipping profile." })
12822
- ] }) }),
12823
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12824
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-3 px-4 py-2 text-ui-fg-muted", children: [
12825
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12826
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) })
12827
- ] }),
12828
- /* @__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(
12829
- "div",
12830
- {
12831
- className: "grid grid-cols-2 gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center",
12832
- children: [
12833
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12834
- /* @__PURE__ */ jsx(
12835
- Thumbnail,
12836
- {
12837
- thumbnail: item.thumbnail,
12838
- alt: item.product_title ?? void 0
12839
- }
12840
- ),
12841
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12842
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12843
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12844
- /* @__PURE__ */ jsxs(
12845
- Text,
12846
- {
12847
- size: "small",
12848
- leading: "compact",
12849
- className: "text-ui-fg-subtle",
12850
- children: [
12851
- "(",
12852
- item.variant_title,
12853
- ")"
12854
- ]
12855
- }
12856
- )
12857
- ] }),
12858
- /* @__PURE__ */ jsx(
12859
- Text,
12860
- {
12861
- size: "small",
12862
- leading: "compact",
12863
- className: "text-ui-fg-subtle",
12864
- children: item.variant_sku
12865
- }
12866
- )
12867
- ] })
12868
- ] }),
12869
- /* @__PURE__ */ jsxs(
12870
- Text,
12871
- {
12872
- size: "small",
12873
- leading: "compact",
12874
- className: "text-ui-fg-subtle",
12875
- children: [
12876
- item.quantity,
12877
- "x"
12878
- ]
12879
- }
12880
- )
12881
- ]
12882
- },
12883
- item.id
12884
- )) : /* @__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: [
12885
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12886
- /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12887
- 'No items found for "',
12888
- query,
12889
- '".'
12890
- ] })
12891
- ] }) })
12892
- ] })
12765
+ const PROMOTION_QUERY_KEY = "promotions";
12766
+ const promotionsQueryKeys = {
12767
+ list: (query2) => [
12768
+ PROMOTION_QUERY_KEY,
12769
+ query2 ? query2 : void 0
12770
+ ],
12771
+ detail: (id, query2) => [
12772
+ PROMOTION_QUERY_KEY,
12773
+ id,
12774
+ query2 ? query2 : void 0
12775
+ ]
12776
+ };
12777
+ const usePromotions = (query2, options) => {
12778
+ const { data, ...rest } = useQuery({
12779
+ queryKey: promotionsQueryKeys.list(query2),
12780
+ queryFn: async () => sdk.admin.promotion.list(query2),
12781
+ ...options
12782
+ });
12783
+ return { ...data, ...rest };
12784
+ };
12785
+ const Promotions = () => {
12786
+ const { id } = useParams();
12787
+ const {
12788
+ order: preview,
12789
+ isError: isPreviewError,
12790
+ error: previewError
12791
+ } = useOrderPreview(id, void 0);
12792
+ useInitiateOrderEdit({ preview });
12793
+ const { onCancel } = useCancelOrderEdit({ preview });
12794
+ if (isPreviewError) {
12795
+ throw previewError;
12796
+ }
12797
+ const isReady = !!preview;
12798
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
12799
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
12800
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
12893
12801
  ] });
12894
12802
  };
12895
- const LocationField = ({ control, setValue }) => {
12896
- const locations = useComboboxData({
12897
- queryKey: ["locations"],
12803
+ const PromotionForm = ({ preview }) => {
12804
+ const { items, shipping_methods } = preview;
12805
+ const [isSubmitting, setIsSubmitting] = useState(false);
12806
+ const [comboboxValue, setComboboxValue] = useState("");
12807
+ const { handleSuccess } = useRouteModal();
12808
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
12809
+ const promoIds = getPromotionIds(items, shipping_methods);
12810
+ const { promotions, isPending, isError, error } = usePromotions(
12811
+ {
12812
+ id: promoIds
12813
+ },
12814
+ {
12815
+ enabled: !!promoIds.length
12816
+ }
12817
+ );
12818
+ const comboboxData = useComboboxData({
12819
+ queryKey: ["promotions", "combobox", promoIds],
12898
12820
  queryFn: async (params) => {
12899
- return await sdk.admin.stockLocation.list(params);
12821
+ return await sdk.admin.promotion.list({
12822
+ ...params,
12823
+ id: {
12824
+ $nin: promoIds
12825
+ }
12826
+ });
12900
12827
  },
12901
12828
  getOptions: (data) => {
12902
- return data.stock_locations.map((location) => ({
12903
- label: location.name,
12904
- value: location.id
12829
+ return data.promotions.map((promotion) => ({
12830
+ label: promotion.code,
12831
+ value: promotion.code
12905
12832
  }));
12906
12833
  }
12907
12834
  });
12908
- return /* @__PURE__ */ jsx(
12909
- Form$2.Field,
12910
- {
12911
- control,
12912
- name: "location_id",
12913
- render: ({ field: { onChange, ...field } }) => {
12914
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12915
- /* @__PURE__ */ jsxs("div", { children: [
12916
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Location" }),
12917
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose where you want to ship the items from." })
12918
- ] }),
12919
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12920
- Combobox,
12921
- {
12922
- options: locations.options,
12923
- fetchNextPage: locations.fetchNextPage,
12924
- isFetchingNextPage: locations.isFetchingNextPage,
12925
- searchValue: locations.searchValue,
12926
- onSearchValueChange: locations.onSearchValueChange,
12927
- placeholder: "Select location",
12928
- onChange: (value) => {
12929
- setValue("shipping_option_id", "", {
12930
- shouldDirty: true,
12931
- shouldTouch: true
12932
- });
12933
- onChange(value);
12934
- },
12935
- ...field
12936
- }
12937
- ) })
12938
- ] }) });
12835
+ const add = async (value) => {
12836
+ if (!value) {
12837
+ return;
12838
+ }
12839
+ addPromotions(
12840
+ {
12841
+ promo_codes: [value]
12842
+ },
12843
+ {
12844
+ onError: (e) => {
12845
+ toast.error(e.message);
12846
+ comboboxData.onSearchValueChange("");
12847
+ setComboboxValue("");
12848
+ },
12849
+ onSuccess: () => {
12850
+ comboboxData.onSearchValueChange("");
12851
+ setComboboxValue("");
12852
+ }
12853
+ }
12854
+ );
12855
+ };
12856
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12857
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
12858
+ const onSubmit = async () => {
12859
+ setIsSubmitting(true);
12860
+ let requestSucceeded = false;
12861
+ await requestOrderEdit(void 0, {
12862
+ onError: (e) => {
12863
+ toast.error(e.message);
12864
+ },
12865
+ onSuccess: () => {
12866
+ requestSucceeded = true;
12867
+ }
12868
+ });
12869
+ if (!requestSucceeded) {
12870
+ setIsSubmitting(false);
12871
+ return;
12872
+ }
12873
+ await confirmOrderEdit(void 0, {
12874
+ onError: (e) => {
12875
+ toast.error(e.message);
12876
+ },
12877
+ onSuccess: () => {
12878
+ handleSuccess();
12879
+ },
12880
+ onSettled: () => {
12881
+ setIsSubmitting(false);
12939
12882
  }
12940
- }
12941
- );
12883
+ });
12884
+ };
12885
+ if (isError) {
12886
+ throw error;
12887
+ }
12888
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
12889
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
12890
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
12891
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12892
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
12893
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
12894
+ ] }),
12895
+ /* @__PURE__ */ jsx(
12896
+ Combobox,
12897
+ {
12898
+ id: "promotion-combobox",
12899
+ "aria-describedby": "promotion-combobox-hint",
12900
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
12901
+ fetchNextPage: comboboxData.fetchNextPage,
12902
+ options: comboboxData.options,
12903
+ onSearchValueChange: comboboxData.onSearchValueChange,
12904
+ searchValue: comboboxData.searchValue,
12905
+ disabled: comboboxData.disabled || isAddingPromotions,
12906
+ onChange: add,
12907
+ value: comboboxValue
12908
+ }
12909
+ )
12910
+ ] }),
12911
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12912
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
12913
+ PromotionItem,
12914
+ {
12915
+ promotion,
12916
+ orderId: preview.id,
12917
+ isLoading: isPending
12918
+ },
12919
+ promotion.id
12920
+ )) })
12921
+ ] }) }),
12922
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12923
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12924
+ /* @__PURE__ */ jsx(
12925
+ Button,
12926
+ {
12927
+ size: "small",
12928
+ type: "submit",
12929
+ isLoading: isSubmitting || isAddingPromotions,
12930
+ children: "Save"
12931
+ }
12932
+ )
12933
+ ] }) })
12934
+ ] });
12942
12935
  };
12943
- const ShippingOptionField = ({
12944
- shippingProfileId,
12945
- preview,
12946
- control
12936
+ const PromotionItem = ({
12937
+ promotion,
12938
+ orderId,
12939
+ isLoading
12947
12940
  }) => {
12948
12941
  var _a;
12949
- const locationId = useWatch({ control, name: "location_id" });
12950
- const shippingOptions = useComboboxData({
12951
- queryKey: ["shipping_options", locationId, shippingProfileId],
12952
- queryFn: async (params) => {
12953
- return await sdk.admin.shippingOption.list({
12954
- ...params,
12955
- stock_location_id: locationId,
12956
- shipping_profile_id: shippingProfileId
12957
- });
12958
- },
12959
- getOptions: (data) => {
12960
- return data.shipping_options.map((option) => {
12961
- var _a2;
12962
- if ((_a2 = option.rules) == null ? void 0 : _a2.find(
12963
- (r) => r.attribute === "is_return" && r.value === "true"
12964
- )) {
12965
- return void 0;
12942
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
12943
+ const onRemove = async () => {
12944
+ removePromotions(
12945
+ {
12946
+ promo_codes: [promotion.code]
12947
+ },
12948
+ {
12949
+ onError: (e) => {
12950
+ toast.error(e.message);
12966
12951
  }
12967
- return {
12968
- label: option.name,
12969
- value: option.id
12970
- };
12971
- }).filter(Boolean);
12972
- },
12973
- enabled: !!locationId && !!shippingProfileId,
12974
- defaultValue: ((_a = preview.shipping_methods[0]) == null ? void 0 : _a.shipping_option_id) || void 0
12975
- });
12976
- const tooltipContent = !locationId && !shippingProfileId ? "Choose a location and shipping profile first." : !locationId ? "Choose a location first." : "Choose a shipping profile first.";
12977
- return /* @__PURE__ */ jsx(
12978
- Form$2.Field,
12979
- {
12980
- control,
12981
- name: "shipping_option_id",
12982
- render: ({ field }) => {
12983
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12984
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12985
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Shipping option" }),
12986
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12987
- ] }),
12988
- /* @__PURE__ */ jsx(
12989
- ConditionalTooltip,
12990
- {
12991
- content: tooltipContent,
12992
- showTooltip: !locationId || !shippingProfileId,
12993
- children: /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12994
- Combobox,
12995
- {
12996
- options: shippingOptions.options,
12997
- fetchNextPage: shippingOptions.fetchNextPage,
12998
- isFetchingNextPage: shippingOptions.isFetchingNextPage,
12999
- searchValue: shippingOptions.searchValue,
13000
- onSearchValueChange: shippingOptions.onSearchValueChange,
13001
- placeholder: "Select shipping option",
13002
- ...field,
13003
- disabled: !locationId || !shippingProfileId
13004
- }
13005
- ) }) })
13006
- }
13007
- )
13008
- ] }) });
13009
12952
  }
13010
- }
12953
+ );
12954
+ };
12955
+ const displayValue = getDisplayValue(promotion);
12956
+ return /* @__PURE__ */ jsxs(
12957
+ "div",
12958
+ {
12959
+ className: clx(
12960
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
12961
+ {
12962
+ "animate-pulse": isLoading
12963
+ }
12964
+ ),
12965
+ children: [
12966
+ /* @__PURE__ */ jsxs("div", { children: [
12967
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
12968
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
12969
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
12970
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
12971
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
12972
+ ] }),
12973
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
12974
+ ] })
12975
+ ] }),
12976
+ /* @__PURE__ */ jsx(
12977
+ IconButton,
12978
+ {
12979
+ size: "small",
12980
+ type: "button",
12981
+ variant: "transparent",
12982
+ onClick: onRemove,
12983
+ isLoading: isPending || isLoading,
12984
+ children: /* @__PURE__ */ jsx(XMark, {})
12985
+ }
12986
+ )
12987
+ ]
12988
+ },
12989
+ promotion.id
13011
12990
  );
13012
12991
  };
13013
- const CustomAmountField = ({
13014
- control,
13015
- currencyCode
13016
- }) => {
13017
- return /* @__PURE__ */ jsx(
13018
- Form$2.Field,
13019
- {
13020
- control,
13021
- name: "custom_amount",
13022
- render: ({ field: { onChange, ...field } }) => {
13023
- return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
13024
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
13025
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
13026
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
13027
- ] }),
13028
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
13029
- CurrencyInput,
13030
- {
13031
- ...field,
13032
- onValueChange: (value) => onChange(value),
13033
- symbol: getNativeSymbol(currencyCode),
13034
- code: currencyCode
13035
- }
13036
- ) })
13037
- ] });
13038
- }
12992
+ function getDisplayValue(promotion) {
12993
+ var _a, _b, _c, _d;
12994
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
12995
+ if (!value) {
12996
+ return null;
12997
+ }
12998
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
12999
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
13000
+ if (!currency) {
13001
+ return null;
13039
13002
  }
13040
- );
13003
+ return getLocaleAmount(value, currency);
13004
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
13005
+ return formatPercentage(value);
13006
+ }
13007
+ return null;
13008
+ }
13009
+ const formatter = new Intl.NumberFormat([], {
13010
+ style: "percent",
13011
+ minimumFractionDigits: 2
13012
+ });
13013
+ const formatPercentage = (value, isPercentageValue = false) => {
13014
+ let val = value || 0;
13015
+ if (!isPercentageValue) {
13016
+ val = val / 100;
13017
+ }
13018
+ return formatter.format(val);
13041
13019
  };
13020
+ function getPromotionIds(items, shippingMethods) {
13021
+ const promotionIds = /* @__PURE__ */ new Set();
13022
+ for (const item of items) {
13023
+ if (item.adjustments) {
13024
+ for (const adjustment of item.adjustments) {
13025
+ if (adjustment.promotion_id) {
13026
+ promotionIds.add(adjustment.promotion_id);
13027
+ }
13028
+ }
13029
+ }
13030
+ }
13031
+ for (const shippingMethod of shippingMethods) {
13032
+ if (shippingMethod.adjustments) {
13033
+ for (const adjustment of shippingMethod.adjustments) {
13034
+ if (adjustment.promotion_id) {
13035
+ promotionIds.add(adjustment.promotion_id);
13036
+ }
13037
+ }
13038
+ }
13039
+ }
13040
+ return Array.from(promotionIds);
13041
+ }
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
13044
13044
  routes: [
@@ -13079,14 +13079,14 @@ const routeModule = {
13079
13079
  Component: Metadata,
13080
13080
  path: "/draft-orders/:id/metadata"
13081
13081
  },
13082
- {
13083
- Component: Promotions,
13084
- path: "/draft-orders/:id/promotions"
13085
- },
13086
13082
  {
13087
13083
  Component: SalesChannel,
13088
13084
  path: "/draft-orders/:id/sales-channel"
13089
13085
  },
13086
+ {
13087
+ Component: Shipping,
13088
+ path: "/draft-orders/:id/shipping"
13089
+ },
13090
13090
  {
13091
13091
  Component: ShippingAddress,
13092
13092
  path: "/draft-orders/:id/shipping-address"
@@ -13096,8 +13096,8 @@ const routeModule = {
13096
13096
  path: "/draft-orders/:id/transfer-ownership"
13097
13097
  },
13098
13098
  {
13099
- Component: Shipping,
13100
- path: "/draft-orders/:id/shipping"
13099
+ Component: Promotions,
13100
+ path: "/draft-orders/:id/promotions"
13101
13101
  }
13102
13102
  ]
13103
13103
  }