@medusajs/draft-order 2.11.0-preview-20251021031736 → 2.11.0-preview-20251021060207

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.
@@ -9755,27 +9755,6 @@ const BillingAddressForm = ({ order }) => {
9755
9755
  ) });
9756
9756
  };
9757
9757
  const schema$5 = addressSchema;
9758
- const CustomItems = () => {
9759
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9760
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9761
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9762
- ] });
9763
- };
9764
- const CustomItemsForm = () => {
9765
- const form = useForm({
9766
- resolver: zodResolver(schema$4)
9767
- });
9768
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9769
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9770
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9771
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9772
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9773
- ] }) })
9774
- ] }) });
9775
- };
9776
- const schema$4 = objectType({
9777
- email: stringType().email()
9778
- });
9779
9758
  const Email = () => {
9780
9759
  const { id } = useParams();
9781
9760
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9798,7 +9777,7 @@ const EmailForm = ({ order }) => {
9798
9777
  defaultValues: {
9799
9778
  email: order.email ?? ""
9800
9779
  },
9801
- resolver: zodResolver(schema$3)
9780
+ resolver: zodResolver(schema$4)
9802
9781
  });
9803
9782
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9804
9783
  const { handleSuccess } = useRouteModal();
@@ -9841,7 +9820,7 @@ const EmailForm = ({ order }) => {
9841
9820
  }
9842
9821
  ) });
9843
9822
  };
9844
- const schema$3 = objectType({
9823
+ const schema$4 = objectType({
9845
9824
  email: stringType().email()
9846
9825
  });
9847
9826
  const NumberInput = forwardRef(
@@ -11168,105 +11147,183 @@ function getHasUneditableRows(metadata) {
11168
11147
  (value) => !EDITABLE_TYPES.includes(typeof value)
11169
11148
  );
11170
11149
  }
11171
- const PROMOTION_QUERY_KEY = "promotions";
11172
- const promotionsQueryKeys = {
11173
- list: (query2) => [
11174
- PROMOTION_QUERY_KEY,
11175
- query2 ? query2 : void 0
11176
- ],
11177
- detail: (id, query2) => [
11178
- PROMOTION_QUERY_KEY,
11150
+ const SalesChannel = () => {
11151
+ const { id } = useParams();
11152
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11179
11153
  id,
11180
- query2 ? query2 : void 0
11181
- ]
11154
+ {
11155
+ fields: "+sales_channel_id"
11156
+ },
11157
+ {
11158
+ enabled: !!id
11159
+ }
11160
+ );
11161
+ if (isError) {
11162
+ throw error;
11163
+ }
11164
+ const ISrEADY = !!draft_order && !isPending;
11165
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11166
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11167
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11168
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11169
+ ] }),
11170
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11171
+ ] });
11182
11172
  };
11183
- const usePromotions = (query2, options) => {
11184
- const { data, ...rest } = useQuery({
11185
- queryKey: promotionsQueryKeys.list(query2),
11186
- queryFn: async () => sdk.admin.promotion.list(query2),
11187
- ...options
11173
+ const SalesChannelForm = ({ order }) => {
11174
+ const form = useForm({
11175
+ defaultValues: {
11176
+ sales_channel_id: order.sales_channel_id || ""
11177
+ },
11178
+ resolver: zodResolver(schema$3)
11188
11179
  });
11189
- return { ...data, ...rest };
11180
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11181
+ const { handleSuccess } = useRouteModal();
11182
+ const onSubmit = form.handleSubmit(async (data) => {
11183
+ await mutateAsync(
11184
+ {
11185
+ sales_channel_id: data.sales_channel_id
11186
+ },
11187
+ {
11188
+ onSuccess: () => {
11189
+ toast.success("Sales channel updated");
11190
+ handleSuccess();
11191
+ },
11192
+ onError: (error) => {
11193
+ toast.error(error.message);
11194
+ }
11195
+ }
11196
+ );
11197
+ });
11198
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11199
+ KeyboundForm,
11200
+ {
11201
+ className: "flex flex-1 flex-col overflow-hidden",
11202
+ onSubmit,
11203
+ children: [
11204
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11205
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11206
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11207
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11208
+ ] }) })
11209
+ ]
11210
+ }
11211
+ ) });
11190
11212
  };
11191
- const Promotions = () => {
11213
+ const SalesChannelField = ({ control, order }) => {
11214
+ const salesChannels = useComboboxData({
11215
+ queryFn: async (params) => {
11216
+ return await sdk.admin.salesChannel.list(params);
11217
+ },
11218
+ queryKey: ["sales-channels"],
11219
+ getOptions: (data) => {
11220
+ return data.sales_channels.map((salesChannel) => ({
11221
+ label: salesChannel.name,
11222
+ value: salesChannel.id
11223
+ }));
11224
+ },
11225
+ defaultValue: order.sales_channel_id || void 0
11226
+ });
11227
+ return /* @__PURE__ */ jsx(
11228
+ Form$2.Field,
11229
+ {
11230
+ control,
11231
+ name: "sales_channel_id",
11232
+ render: ({ field }) => {
11233
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11234
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11235
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11236
+ Combobox,
11237
+ {
11238
+ options: salesChannels.options,
11239
+ fetchNextPage: salesChannels.fetchNextPage,
11240
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11241
+ searchValue: salesChannels.searchValue,
11242
+ onSearchValueChange: salesChannels.onSearchValueChange,
11243
+ placeholder: "Select sales channel",
11244
+ ...field
11245
+ }
11246
+ ) }),
11247
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11248
+ ] });
11249
+ }
11250
+ }
11251
+ );
11252
+ };
11253
+ const schema$3 = objectType({
11254
+ sales_channel_id: stringType().min(1)
11255
+ });
11256
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
11257
+ const Shipping = () => {
11258
+ var _a;
11192
11259
  const { id } = useParams();
11260
+ const { order, isPending, isError, error } = useOrder(id, {
11261
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11262
+ });
11193
11263
  const {
11194
11264
  order: preview,
11265
+ isPending: isPreviewPending,
11195
11266
  isError: isPreviewError,
11196
11267
  error: previewError
11197
- } = useOrderPreview(id, void 0);
11268
+ } = useOrderPreview(id);
11198
11269
  useInitiateOrderEdit({ preview });
11199
11270
  const { onCancel } = useCancelOrderEdit({ preview });
11271
+ if (isError) {
11272
+ throw error;
11273
+ }
11200
11274
  if (isPreviewError) {
11201
11275
  throw previewError;
11202
11276
  }
11203
- const isReady = !!preview;
11204
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11205
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11206
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11207
- ] });
11277
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11278
+ const isReady = preview && !isPreviewPending && order && !isPending;
11279
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11280
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11281
+ /* @__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: [
11282
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11283
+ /* @__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." }) })
11284
+ ] }) }) }),
11285
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11286
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11287
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11288
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11289
+ ] }) });
11208
11290
  };
11209
- const PromotionForm = ({ preview }) => {
11210
- const { items, shipping_methods } = preview;
11291
+ const ShippingForm = ({ preview, order }) => {
11292
+ var _a;
11293
+ const { setIsOpen } = useStackedModal();
11211
11294
  const [isSubmitting, setIsSubmitting] = useState(false);
11212
- const [comboboxValue, setComboboxValue] = useState("");
11213
- const { handleSuccess } = useRouteModal();
11214
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11215
- const promoIds = getPromotionIds(items, shipping_methods);
11216
- const { promotions, isPending, isError, error } = usePromotions(
11295
+ const [data, setData] = useState(null);
11296
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11297
+ const { shipping_options } = useShippingOptions(
11217
11298
  {
11218
- id: promoIds
11299
+ id: appliedShippingOptionIds,
11300
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11219
11301
  },
11220
11302
  {
11221
- enabled: !!promoIds.length
11303
+ enabled: appliedShippingOptionIds.length > 0
11222
11304
  }
11223
11305
  );
11224
- const comboboxData = useComboboxData({
11225
- queryKey: ["promotions", "combobox", promoIds],
11226
- queryFn: async (params) => {
11227
- return await sdk.admin.promotion.list({
11228
- ...params,
11229
- id: {
11230
- $nin: promoIds
11231
- }
11232
- });
11233
- },
11234
- getOptions: (data) => {
11235
- return data.promotions.map((promotion) => ({
11236
- label: promotion.code,
11237
- value: promotion.code
11238
- }));
11239
- }
11240
- });
11241
- const add = async (value) => {
11242
- if (!value) {
11243
- return;
11244
- }
11245
- addPromotions(
11246
- {
11247
- promo_codes: [value]
11248
- },
11249
- {
11250
- onError: (e) => {
11251
- toast.error(e.message);
11252
- comboboxData.onSearchValueChange("");
11253
- setComboboxValue("");
11254
- },
11255
- onSuccess: () => {
11256
- comboboxData.onSearchValueChange("");
11257
- setComboboxValue("");
11258
- }
11259
- }
11260
- );
11261
- };
11306
+ const uniqueShippingProfiles = useMemo(() => {
11307
+ const profiles = /* @__PURE__ */ new Map();
11308
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
11309
+ profiles.set(profile.id, profile);
11310
+ });
11311
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11312
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
11313
+ });
11314
+ return Array.from(profiles.values());
11315
+ }, [order.items, shipping_options]);
11316
+ const { handleSuccess } = useRouteModal();
11262
11317
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11263
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11318
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11319
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11320
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11264
11321
  const onSubmit = async () => {
11265
11322
  setIsSubmitting(true);
11266
11323
  let requestSucceeded = false;
11267
11324
  await requestOrderEdit(void 0, {
11268
11325
  onError: (e) => {
11269
- toast.error(e.message);
11326
+ toast.error(`Failed to request order edit: ${e.message}`);
11270
11327
  },
11271
11328
  onSuccess: () => {
11272
11329
  requestSucceeded = true;
@@ -11278,256 +11335,7 @@ const PromotionForm = ({ preview }) => {
11278
11335
  }
11279
11336
  await confirmOrderEdit(void 0, {
11280
11337
  onError: (e) => {
11281
- toast.error(e.message);
11282
- },
11283
- onSuccess: () => {
11284
- handleSuccess();
11285
- },
11286
- onSettled: () => {
11287
- setIsSubmitting(false);
11288
- }
11289
- });
11290
- };
11291
- if (isError) {
11292
- throw error;
11293
- }
11294
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11295
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11296
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11297
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11298
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11299
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11300
- ] }),
11301
- /* @__PURE__ */ jsx(
11302
- Combobox,
11303
- {
11304
- id: "promotion-combobox",
11305
- "aria-describedby": "promotion-combobox-hint",
11306
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11307
- fetchNextPage: comboboxData.fetchNextPage,
11308
- options: comboboxData.options,
11309
- onSearchValueChange: comboboxData.onSearchValueChange,
11310
- searchValue: comboboxData.searchValue,
11311
- disabled: comboboxData.disabled || isAddingPromotions,
11312
- onChange: add,
11313
- value: comboboxValue
11314
- }
11315
- )
11316
- ] }),
11317
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11318
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11319
- PromotionItem,
11320
- {
11321
- promotion,
11322
- orderId: preview.id,
11323
- isLoading: isPending
11324
- },
11325
- promotion.id
11326
- )) })
11327
- ] }) }),
11328
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11329
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11330
- /* @__PURE__ */ jsx(
11331
- Button,
11332
- {
11333
- size: "small",
11334
- type: "submit",
11335
- isLoading: isSubmitting || isAddingPromotions,
11336
- children: "Save"
11337
- }
11338
- )
11339
- ] }) })
11340
- ] });
11341
- };
11342
- const PromotionItem = ({
11343
- promotion,
11344
- orderId,
11345
- isLoading
11346
- }) => {
11347
- var _a;
11348
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11349
- const onRemove = async () => {
11350
- removePromotions(
11351
- {
11352
- promo_codes: [promotion.code]
11353
- },
11354
- {
11355
- onError: (e) => {
11356
- toast.error(e.message);
11357
- }
11358
- }
11359
- );
11360
- };
11361
- const displayValue = getDisplayValue(promotion);
11362
- return /* @__PURE__ */ jsxs(
11363
- "div",
11364
- {
11365
- className: clx(
11366
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11367
- {
11368
- "animate-pulse": isLoading
11369
- }
11370
- ),
11371
- children: [
11372
- /* @__PURE__ */ jsxs("div", { children: [
11373
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11374
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11375
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11376
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11377
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11378
- ] }),
11379
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11380
- ] })
11381
- ] }),
11382
- /* @__PURE__ */ jsx(
11383
- IconButton,
11384
- {
11385
- size: "small",
11386
- type: "button",
11387
- variant: "transparent",
11388
- onClick: onRemove,
11389
- isLoading: isPending || isLoading,
11390
- children: /* @__PURE__ */ jsx(XMark, {})
11391
- }
11392
- )
11393
- ]
11394
- },
11395
- promotion.id
11396
- );
11397
- };
11398
- function getDisplayValue(promotion) {
11399
- var _a, _b, _c, _d;
11400
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11401
- if (!value) {
11402
- return null;
11403
- }
11404
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11405
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11406
- if (!currency) {
11407
- return null;
11408
- }
11409
- return getLocaleAmount(value, currency);
11410
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11411
- return formatPercentage(value);
11412
- }
11413
- return null;
11414
- }
11415
- const formatter = new Intl.NumberFormat([], {
11416
- style: "percent",
11417
- minimumFractionDigits: 2
11418
- });
11419
- const formatPercentage = (value, isPercentageValue = false) => {
11420
- let val = value || 0;
11421
- if (!isPercentageValue) {
11422
- val = val / 100;
11423
- }
11424
- return formatter.format(val);
11425
- };
11426
- function getPromotionIds(items, shippingMethods) {
11427
- const promotionIds = /* @__PURE__ */ new Set();
11428
- for (const item of items) {
11429
- if (item.adjustments) {
11430
- for (const adjustment of item.adjustments) {
11431
- if (adjustment.promotion_id) {
11432
- promotionIds.add(adjustment.promotion_id);
11433
- }
11434
- }
11435
- }
11436
- }
11437
- for (const shippingMethod of shippingMethods) {
11438
- if (shippingMethod.adjustments) {
11439
- for (const adjustment of shippingMethod.adjustments) {
11440
- if (adjustment.promotion_id) {
11441
- promotionIds.add(adjustment.promotion_id);
11442
- }
11443
- }
11444
- }
11445
- }
11446
- return Array.from(promotionIds);
11447
- }
11448
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11449
- const Shipping = () => {
11450
- var _a;
11451
- const { id } = useParams();
11452
- const { order, isPending, isError, error } = useOrder(id, {
11453
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11454
- });
11455
- const {
11456
- order: preview,
11457
- isPending: isPreviewPending,
11458
- isError: isPreviewError,
11459
- error: previewError
11460
- } = useOrderPreview(id);
11461
- useInitiateOrderEdit({ preview });
11462
- const { onCancel } = useCancelOrderEdit({ preview });
11463
- if (isError) {
11464
- throw error;
11465
- }
11466
- if (isPreviewError) {
11467
- throw previewError;
11468
- }
11469
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11470
- const isReady = preview && !isPreviewPending && order && !isPending;
11471
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11472
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11473
- /* @__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: [
11474
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11475
- /* @__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." }) })
11476
- ] }) }) }),
11477
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11478
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11479
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11480
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11481
- ] }) });
11482
- };
11483
- const ShippingForm = ({ preview, order }) => {
11484
- var _a;
11485
- const { setIsOpen } = useStackedModal();
11486
- const [isSubmitting, setIsSubmitting] = useState(false);
11487
- const [data, setData] = useState(null);
11488
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11489
- const { shipping_options } = useShippingOptions(
11490
- {
11491
- id: appliedShippingOptionIds,
11492
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11493
- },
11494
- {
11495
- enabled: appliedShippingOptionIds.length > 0
11496
- }
11497
- );
11498
- const uniqueShippingProfiles = useMemo(() => {
11499
- const profiles = /* @__PURE__ */ new Map();
11500
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11501
- profiles.set(profile.id, profile);
11502
- });
11503
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11504
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11505
- });
11506
- return Array.from(profiles.values());
11507
- }, [order.items, shipping_options]);
11508
- const { handleSuccess } = useRouteModal();
11509
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11510
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11511
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11512
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11513
- const onSubmit = async () => {
11514
- setIsSubmitting(true);
11515
- let requestSucceeded = false;
11516
- await requestOrderEdit(void 0, {
11517
- onError: (e) => {
11518
- toast.error(`Failed to request order edit: ${e.message}`);
11519
- },
11520
- onSuccess: () => {
11521
- requestSucceeded = true;
11522
- }
11523
- });
11524
- if (!requestSucceeded) {
11525
- setIsSubmitting(false);
11526
- return;
11527
- }
11528
- await confirmOrderEdit(void 0, {
11529
- onError: (e) => {
11530
- toast.error(`Failed to confirm order edit: ${e.message}`);
11338
+ toast.error(`Failed to confirm order edit: ${e.message}`);
11531
11339
  },
11532
11340
  onSuccess: () => {
11533
11341
  handleSuccess();
@@ -12252,224 +12060,21 @@ const CustomAmountField = ({
12252
12060
  }
12253
12061
  );
12254
12062
  };
12255
- const ShippingAddress = () => {
12063
+ const TransferOwnership = () => {
12256
12064
  const { id } = useParams();
12257
- const { order, isPending, isError, error } = useOrder(id, {
12258
- fields: "+shipping_address"
12065
+ const { draft_order, isPending, isError, error } = useDraftOrder(id, {
12066
+ fields: "id,customer_id,customer.*"
12259
12067
  });
12260
12068
  if (isError) {
12261
12069
  throw error;
12262
12070
  }
12263
- const isReady = !isPending && !!order;
12071
+ const isReady = !isPending && !!draft_order;
12264
12072
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12265
12073
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12266
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12267
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12074
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
12075
+ /* @__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" }) })
12268
12076
  ] }),
12269
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12270
- ] });
12271
- };
12272
- const ShippingAddressForm = ({ order }) => {
12273
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12274
- const form = useForm({
12275
- defaultValues: {
12276
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12277
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12278
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12279
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12280
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12281
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12282
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12283
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12284
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12285
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12286
- },
12287
- resolver: zodResolver(schema$2)
12288
- });
12289
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12290
- const { handleSuccess } = useRouteModal();
12291
- const onSubmit = form.handleSubmit(async (data) => {
12292
- await mutateAsync(
12293
- {
12294
- shipping_address: {
12295
- first_name: data.first_name,
12296
- last_name: data.last_name,
12297
- company: data.company,
12298
- address_1: data.address_1,
12299
- address_2: data.address_2,
12300
- city: data.city,
12301
- province: data.province,
12302
- country_code: data.country_code,
12303
- postal_code: data.postal_code,
12304
- phone: data.phone
12305
- }
12306
- },
12307
- {
12308
- onSuccess: () => {
12309
- handleSuccess();
12310
- },
12311
- onError: (error) => {
12312
- toast.error(error.message);
12313
- }
12314
- }
12315
- );
12316
- });
12317
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12318
- KeyboundForm,
12319
- {
12320
- className: "flex flex-1 flex-col overflow-hidden",
12321
- onSubmit,
12322
- children: [
12323
- /* @__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: [
12324
- /* @__PURE__ */ jsx(
12325
- Form$2.Field,
12326
- {
12327
- control: form.control,
12328
- name: "country_code",
12329
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12330
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12331
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12332
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12333
- ] })
12334
- }
12335
- ),
12336
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12337
- /* @__PURE__ */ jsx(
12338
- Form$2.Field,
12339
- {
12340
- control: form.control,
12341
- name: "first_name",
12342
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12343
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12344
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12345
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12346
- ] })
12347
- }
12348
- ),
12349
- /* @__PURE__ */ jsx(
12350
- Form$2.Field,
12351
- {
12352
- control: form.control,
12353
- name: "last_name",
12354
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12355
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12356
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12357
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12358
- ] })
12359
- }
12360
- )
12361
- ] }),
12362
- /* @__PURE__ */ jsx(
12363
- Form$2.Field,
12364
- {
12365
- control: form.control,
12366
- name: "company",
12367
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12368
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12369
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12370
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12371
- ] })
12372
- }
12373
- ),
12374
- /* @__PURE__ */ jsx(
12375
- Form$2.Field,
12376
- {
12377
- control: form.control,
12378
- name: "address_1",
12379
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12380
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12381
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12382
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12383
- ] })
12384
- }
12385
- ),
12386
- /* @__PURE__ */ jsx(
12387
- Form$2.Field,
12388
- {
12389
- control: form.control,
12390
- name: "address_2",
12391
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12392
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12393
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12394
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12395
- ] })
12396
- }
12397
- ),
12398
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12399
- /* @__PURE__ */ jsx(
12400
- Form$2.Field,
12401
- {
12402
- control: form.control,
12403
- name: "postal_code",
12404
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12405
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12406
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12407
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12408
- ] })
12409
- }
12410
- ),
12411
- /* @__PURE__ */ jsx(
12412
- Form$2.Field,
12413
- {
12414
- control: form.control,
12415
- name: "city",
12416
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12417
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12418
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12419
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12420
- ] })
12421
- }
12422
- )
12423
- ] }),
12424
- /* @__PURE__ */ jsx(
12425
- Form$2.Field,
12426
- {
12427
- control: form.control,
12428
- name: "province",
12429
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12430
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12431
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12432
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12433
- ] })
12434
- }
12435
- ),
12436
- /* @__PURE__ */ jsx(
12437
- Form$2.Field,
12438
- {
12439
- control: form.control,
12440
- name: "phone",
12441
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12442
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12443
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12444
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12445
- ] })
12446
- }
12447
- )
12448
- ] }) }),
12449
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12450
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12451
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12452
- ] }) })
12453
- ]
12454
- }
12455
- ) });
12456
- };
12457
- const schema$2 = addressSchema;
12458
- const TransferOwnership = () => {
12459
- const { id } = useParams();
12460
- const { draft_order, isPending, isError, error } = useDraftOrder(id, {
12461
- fields: "id,customer_id,customer.*"
12462
- });
12463
- if (isError) {
12464
- throw error;
12465
- }
12466
- const isReady = !isPending && !!draft_order;
12467
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12468
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12469
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
12470
- /* @__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" }) })
12471
- ] }),
12472
- isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
12077
+ isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
12473
12078
  ] });
12474
12079
  };
12475
12080
  const TransferOwnershipForm = ({ order }) => {
@@ -12478,7 +12083,7 @@ const TransferOwnershipForm = ({ order }) => {
12478
12083
  defaultValues: {
12479
12084
  customer_id: order.customer_id || ""
12480
12085
  },
12481
- resolver: zodResolver(schema$1)
12086
+ resolver: zodResolver(schema$2)
12482
12087
  });
12483
12088
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12484
12089
  const { handleSuccess } = useRouteModal();
@@ -12928,49 +12533,63 @@ const Illustration = () => {
12928
12533
  }
12929
12534
  );
12930
12535
  };
12931
- const schema$1 = objectType({
12536
+ const schema$2 = objectType({
12932
12537
  customer_id: stringType().min(1)
12933
12538
  });
12934
- const SalesChannel = () => {
12539
+ const ShippingAddress = () => {
12935
12540
  const { id } = useParams();
12936
- const { draft_order, isPending, isError, error } = useDraftOrder(
12937
- id,
12938
- {
12939
- fields: "+sales_channel_id"
12940
- },
12941
- {
12942
- enabled: !!id
12943
- }
12944
- );
12541
+ const { order, isPending, isError, error } = useOrder(id, {
12542
+ fields: "+shipping_address"
12543
+ });
12945
12544
  if (isError) {
12946
12545
  throw error;
12947
12546
  }
12948
- const ISrEADY = !!draft_order && !isPending;
12547
+ const isReady = !isPending && !!order;
12949
12548
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12950
12549
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12951
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
12952
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12550
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12551
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12953
12552
  ] }),
12954
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
12553
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12955
12554
  ] });
12956
12555
  };
12957
- const SalesChannelForm = ({ order }) => {
12556
+ const ShippingAddressForm = ({ order }) => {
12557
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12958
12558
  const form = useForm({
12959
12559
  defaultValues: {
12960
- sales_channel_id: order.sales_channel_id || ""
12560
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12561
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12562
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12563
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12564
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12565
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12566
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12567
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12568
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12569
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12961
12570
  },
12962
- resolver: zodResolver(schema)
12571
+ resolver: zodResolver(schema$1)
12963
12572
  });
12964
12573
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12965
12574
  const { handleSuccess } = useRouteModal();
12966
12575
  const onSubmit = form.handleSubmit(async (data) => {
12967
12576
  await mutateAsync(
12968
12577
  {
12969
- sales_channel_id: data.sales_channel_id
12578
+ shipping_address: {
12579
+ first_name: data.first_name,
12580
+ last_name: data.last_name,
12581
+ company: data.company,
12582
+ address_1: data.address_1,
12583
+ address_2: data.address_2,
12584
+ city: data.city,
12585
+ province: data.province,
12586
+ country_code: data.country_code,
12587
+ postal_code: data.postal_code,
12588
+ phone: data.phone
12589
+ }
12970
12590
  },
12971
12591
  {
12972
12592
  onSuccess: () => {
12973
- toast.success("Sales channel updated");
12974
12593
  handleSuccess();
12975
12594
  },
12976
12595
  onError: (error) => {
@@ -12985,7 +12604,132 @@ const SalesChannelForm = ({ order }) => {
12985
12604
  className: "flex flex-1 flex-col overflow-hidden",
12986
12605
  onSubmit,
12987
12606
  children: [
12988
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
12607
+ /* @__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: [
12608
+ /* @__PURE__ */ jsx(
12609
+ Form$2.Field,
12610
+ {
12611
+ control: form.control,
12612
+ name: "country_code",
12613
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12614
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12615
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12616
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12617
+ ] })
12618
+ }
12619
+ ),
12620
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12621
+ /* @__PURE__ */ jsx(
12622
+ Form$2.Field,
12623
+ {
12624
+ control: form.control,
12625
+ name: "first_name",
12626
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12627
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12628
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12629
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12630
+ ] })
12631
+ }
12632
+ ),
12633
+ /* @__PURE__ */ jsx(
12634
+ Form$2.Field,
12635
+ {
12636
+ control: form.control,
12637
+ name: "last_name",
12638
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12639
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12640
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12641
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12642
+ ] })
12643
+ }
12644
+ )
12645
+ ] }),
12646
+ /* @__PURE__ */ jsx(
12647
+ Form$2.Field,
12648
+ {
12649
+ control: form.control,
12650
+ name: "company",
12651
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12652
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12653
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12654
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12655
+ ] })
12656
+ }
12657
+ ),
12658
+ /* @__PURE__ */ jsx(
12659
+ Form$2.Field,
12660
+ {
12661
+ control: form.control,
12662
+ name: "address_1",
12663
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12664
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12665
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12666
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12667
+ ] })
12668
+ }
12669
+ ),
12670
+ /* @__PURE__ */ jsx(
12671
+ Form$2.Field,
12672
+ {
12673
+ control: form.control,
12674
+ name: "address_2",
12675
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12676
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12677
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12678
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12679
+ ] })
12680
+ }
12681
+ ),
12682
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12683
+ /* @__PURE__ */ jsx(
12684
+ Form$2.Field,
12685
+ {
12686
+ control: form.control,
12687
+ name: "postal_code",
12688
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12689
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12690
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12691
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12692
+ ] })
12693
+ }
12694
+ ),
12695
+ /* @__PURE__ */ jsx(
12696
+ Form$2.Field,
12697
+ {
12698
+ control: form.control,
12699
+ name: "city",
12700
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12701
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12702
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12703
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12704
+ ] })
12705
+ }
12706
+ )
12707
+ ] }),
12708
+ /* @__PURE__ */ jsx(
12709
+ Form$2.Field,
12710
+ {
12711
+ control: form.control,
12712
+ name: "province",
12713
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12714
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12715
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12716
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12717
+ ] })
12718
+ }
12719
+ ),
12720
+ /* @__PURE__ */ jsx(
12721
+ Form$2.Field,
12722
+ {
12723
+ control: form.control,
12724
+ name: "phone",
12725
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12726
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12727
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12728
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12729
+ ] })
12730
+ }
12731
+ )
12732
+ ] }) }),
12989
12733
  /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12990
12734
  /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12991
12735
  /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
@@ -12994,48 +12738,304 @@ const SalesChannelForm = ({ order }) => {
12994
12738
  }
12995
12739
  ) });
12996
12740
  };
12997
- const SalesChannelField = ({ control, order }) => {
12998
- const salesChannels = useComboboxData({
12741
+ const schema$1 = addressSchema;
12742
+ const PROMOTION_QUERY_KEY = "promotions";
12743
+ const promotionsQueryKeys = {
12744
+ list: (query2) => [
12745
+ PROMOTION_QUERY_KEY,
12746
+ query2 ? query2 : void 0
12747
+ ],
12748
+ detail: (id, query2) => [
12749
+ PROMOTION_QUERY_KEY,
12750
+ id,
12751
+ query2 ? query2 : void 0
12752
+ ]
12753
+ };
12754
+ const usePromotions = (query2, options) => {
12755
+ const { data, ...rest } = useQuery({
12756
+ queryKey: promotionsQueryKeys.list(query2),
12757
+ queryFn: async () => sdk.admin.promotion.list(query2),
12758
+ ...options
12759
+ });
12760
+ return { ...data, ...rest };
12761
+ };
12762
+ const Promotions = () => {
12763
+ const { id } = useParams();
12764
+ const {
12765
+ order: preview,
12766
+ isError: isPreviewError,
12767
+ error: previewError
12768
+ } = useOrderPreview(id, void 0);
12769
+ useInitiateOrderEdit({ preview });
12770
+ const { onCancel } = useCancelOrderEdit({ preview });
12771
+ if (isPreviewError) {
12772
+ throw previewError;
12773
+ }
12774
+ const isReady = !!preview;
12775
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
12776
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
12777
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
12778
+ ] });
12779
+ };
12780
+ const PromotionForm = ({ preview }) => {
12781
+ const { items, shipping_methods } = preview;
12782
+ const [isSubmitting, setIsSubmitting] = useState(false);
12783
+ const [comboboxValue, setComboboxValue] = useState("");
12784
+ const { handleSuccess } = useRouteModal();
12785
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
12786
+ const promoIds = getPromotionIds(items, shipping_methods);
12787
+ const { promotions, isPending, isError, error } = usePromotions(
12788
+ {
12789
+ id: promoIds
12790
+ },
12791
+ {
12792
+ enabled: !!promoIds.length
12793
+ }
12794
+ );
12795
+ const comboboxData = useComboboxData({
12796
+ queryKey: ["promotions", "combobox", promoIds],
12999
12797
  queryFn: async (params) => {
13000
- return await sdk.admin.salesChannel.list(params);
12798
+ return await sdk.admin.promotion.list({
12799
+ ...params,
12800
+ id: {
12801
+ $nin: promoIds
12802
+ }
12803
+ });
13001
12804
  },
13002
- queryKey: ["sales-channels"],
13003
12805
  getOptions: (data) => {
13004
- return data.sales_channels.map((salesChannel) => ({
13005
- label: salesChannel.name,
13006
- value: salesChannel.id
12806
+ return data.promotions.map((promotion) => ({
12807
+ label: promotion.code,
12808
+ value: promotion.code
13007
12809
  }));
13008
- },
13009
- defaultValue: order.sales_channel_id || void 0
12810
+ }
13010
12811
  });
13011
- return /* @__PURE__ */ jsx(
13012
- Form$2.Field,
13013
- {
13014
- control,
13015
- name: "sales_channel_id",
13016
- render: ({ field }) => {
13017
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13018
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
13019
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
13020
- Combobox,
13021
- {
13022
- options: salesChannels.options,
13023
- fetchNextPage: salesChannels.fetchNextPage,
13024
- isFetchingNextPage: salesChannels.isFetchingNextPage,
13025
- searchValue: salesChannels.searchValue,
13026
- onSearchValueChange: salesChannels.onSearchValueChange,
13027
- placeholder: "Select sales channel",
13028
- ...field
13029
- }
13030
- ) }),
13031
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13032
- ] });
12812
+ const add = async (value) => {
12813
+ if (!value) {
12814
+ return;
12815
+ }
12816
+ addPromotions(
12817
+ {
12818
+ promo_codes: [value]
12819
+ },
12820
+ {
12821
+ onError: (e) => {
12822
+ toast.error(e.message);
12823
+ comboboxData.onSearchValueChange("");
12824
+ setComboboxValue("");
12825
+ },
12826
+ onSuccess: () => {
12827
+ comboboxData.onSearchValueChange("");
12828
+ setComboboxValue("");
12829
+ }
12830
+ }
12831
+ );
12832
+ };
12833
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12834
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
12835
+ const onSubmit = async () => {
12836
+ setIsSubmitting(true);
12837
+ let requestSucceeded = false;
12838
+ await requestOrderEdit(void 0, {
12839
+ onError: (e) => {
12840
+ toast.error(e.message);
12841
+ },
12842
+ onSuccess: () => {
12843
+ requestSucceeded = true;
13033
12844
  }
12845
+ });
12846
+ if (!requestSucceeded) {
12847
+ setIsSubmitting(false);
12848
+ return;
13034
12849
  }
12850
+ await confirmOrderEdit(void 0, {
12851
+ onError: (e) => {
12852
+ toast.error(e.message);
12853
+ },
12854
+ onSuccess: () => {
12855
+ handleSuccess();
12856
+ },
12857
+ onSettled: () => {
12858
+ setIsSubmitting(false);
12859
+ }
12860
+ });
12861
+ };
12862
+ if (isError) {
12863
+ throw error;
12864
+ }
12865
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
12866
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
12867
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
12868
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12869
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
12870
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
12871
+ ] }),
12872
+ /* @__PURE__ */ jsx(
12873
+ Combobox,
12874
+ {
12875
+ id: "promotion-combobox",
12876
+ "aria-describedby": "promotion-combobox-hint",
12877
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
12878
+ fetchNextPage: comboboxData.fetchNextPage,
12879
+ options: comboboxData.options,
12880
+ onSearchValueChange: comboboxData.onSearchValueChange,
12881
+ searchValue: comboboxData.searchValue,
12882
+ disabled: comboboxData.disabled || isAddingPromotions,
12883
+ onChange: add,
12884
+ value: comboboxValue
12885
+ }
12886
+ )
12887
+ ] }),
12888
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12889
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
12890
+ PromotionItem,
12891
+ {
12892
+ promotion,
12893
+ orderId: preview.id,
12894
+ isLoading: isPending
12895
+ },
12896
+ promotion.id
12897
+ )) })
12898
+ ] }) }),
12899
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12900
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12901
+ /* @__PURE__ */ jsx(
12902
+ Button,
12903
+ {
12904
+ size: "small",
12905
+ type: "submit",
12906
+ isLoading: isSubmitting || isAddingPromotions,
12907
+ children: "Save"
12908
+ }
12909
+ )
12910
+ ] }) })
12911
+ ] });
12912
+ };
12913
+ const PromotionItem = ({
12914
+ promotion,
12915
+ orderId,
12916
+ isLoading
12917
+ }) => {
12918
+ var _a;
12919
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
12920
+ const onRemove = async () => {
12921
+ removePromotions(
12922
+ {
12923
+ promo_codes: [promotion.code]
12924
+ },
12925
+ {
12926
+ onError: (e) => {
12927
+ toast.error(e.message);
12928
+ }
12929
+ }
12930
+ );
12931
+ };
12932
+ const displayValue = getDisplayValue(promotion);
12933
+ return /* @__PURE__ */ jsxs(
12934
+ "div",
12935
+ {
12936
+ className: clx(
12937
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
12938
+ {
12939
+ "animate-pulse": isLoading
12940
+ }
12941
+ ),
12942
+ children: [
12943
+ /* @__PURE__ */ jsxs("div", { children: [
12944
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
12945
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
12946
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
12947
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
12948
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
12949
+ ] }),
12950
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
12951
+ ] })
12952
+ ] }),
12953
+ /* @__PURE__ */ jsx(
12954
+ IconButton,
12955
+ {
12956
+ size: "small",
12957
+ type: "button",
12958
+ variant: "transparent",
12959
+ onClick: onRemove,
12960
+ isLoading: isPending || isLoading,
12961
+ children: /* @__PURE__ */ jsx(XMark, {})
12962
+ }
12963
+ )
12964
+ ]
12965
+ },
12966
+ promotion.id
13035
12967
  );
13036
12968
  };
12969
+ function getDisplayValue(promotion) {
12970
+ var _a, _b, _c, _d;
12971
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
12972
+ if (!value) {
12973
+ return null;
12974
+ }
12975
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
12976
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
12977
+ if (!currency) {
12978
+ return null;
12979
+ }
12980
+ return getLocaleAmount(value, currency);
12981
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
12982
+ return formatPercentage(value);
12983
+ }
12984
+ return null;
12985
+ }
12986
+ const formatter = new Intl.NumberFormat([], {
12987
+ style: "percent",
12988
+ minimumFractionDigits: 2
12989
+ });
12990
+ const formatPercentage = (value, isPercentageValue = false) => {
12991
+ let val = value || 0;
12992
+ if (!isPercentageValue) {
12993
+ val = val / 100;
12994
+ }
12995
+ return formatter.format(val);
12996
+ };
12997
+ function getPromotionIds(items, shippingMethods) {
12998
+ const promotionIds = /* @__PURE__ */ new Set();
12999
+ for (const item of items) {
13000
+ if (item.adjustments) {
13001
+ for (const adjustment of item.adjustments) {
13002
+ if (adjustment.promotion_id) {
13003
+ promotionIds.add(adjustment.promotion_id);
13004
+ }
13005
+ }
13006
+ }
13007
+ }
13008
+ for (const shippingMethod of shippingMethods) {
13009
+ if (shippingMethod.adjustments) {
13010
+ for (const adjustment of shippingMethod.adjustments) {
13011
+ if (adjustment.promotion_id) {
13012
+ promotionIds.add(adjustment.promotion_id);
13013
+ }
13014
+ }
13015
+ }
13016
+ }
13017
+ return Array.from(promotionIds);
13018
+ }
13019
+ const CustomItems = () => {
13020
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
13021
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
13022
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
13023
+ ] });
13024
+ };
13025
+ const CustomItemsForm = () => {
13026
+ const form = useForm({
13027
+ resolver: zodResolver(schema)
13028
+ });
13029
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
13030
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
13031
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13032
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13033
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
13034
+ ] }) })
13035
+ ] }) });
13036
+ };
13037
13037
  const schema = objectType({
13038
- sales_channel_id: stringType().min(1)
13038
+ email: stringType().email()
13039
13039
  });
13040
13040
  const widgetModule = { widgets: [] };
13041
13041
  const routeModule = {
@@ -13061,10 +13061,6 @@ const routeModule = {
13061
13061
  Component: BillingAddress,
13062
13062
  path: "/draft-orders/:id/billing-address"
13063
13063
  },
13064
- {
13065
- Component: CustomItems,
13066
- path: "/draft-orders/:id/custom-items"
13067
- },
13068
13064
  {
13069
13065
  Component: Email,
13070
13066
  path: "/draft-orders/:id/email"
@@ -13078,24 +13074,28 @@ const routeModule = {
13078
13074
  path: "/draft-orders/:id/metadata"
13079
13075
  },
13080
13076
  {
13081
- Component: Promotions,
13082
- path: "/draft-orders/:id/promotions"
13077
+ Component: SalesChannel,
13078
+ path: "/draft-orders/:id/sales-channel"
13083
13079
  },
13084
13080
  {
13085
13081
  Component: Shipping,
13086
13082
  path: "/draft-orders/:id/shipping"
13087
13083
  },
13084
+ {
13085
+ Component: TransferOwnership,
13086
+ path: "/draft-orders/:id/transfer-ownership"
13087
+ },
13088
13088
  {
13089
13089
  Component: ShippingAddress,
13090
13090
  path: "/draft-orders/:id/shipping-address"
13091
13091
  },
13092
13092
  {
13093
- Component: TransferOwnership,
13094
- path: "/draft-orders/:id/transfer-ownership"
13093
+ Component: Promotions,
13094
+ path: "/draft-orders/:id/promotions"
13095
13095
  },
13096
13096
  {
13097
- Component: SalesChannel,
13098
- path: "/draft-orders/:id/sales-channel"
13097
+ Component: CustomItems,
13098
+ path: "/draft-orders/:id/custom-items"
13099
13099
  }
13100
13100
  ]
13101
13101
  }