@medusajs/draft-order 2.11.4-snapshot-20251107133446 → 2.11.4-snapshot-20251107205323

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.
@@ -352,10 +352,14 @@ const DataTableAction = ({
352
352
  }
353
353
  return /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { ...buttonProps, onClick: props.onClick, children: label });
354
354
  };
355
+ const backendUrl = __BACKEND_URL__ ?? "/";
356
+ const authType = __AUTH_TYPE__ ?? "session";
357
+ const jwtTokenStorageKey = __JWT_TOKEN_STORAGE_KEY__ || void 0;
355
358
  const sdk = new Medusa__default.default({
356
- baseUrl: __BACKEND_URL__ || "/",
359
+ baseUrl: backendUrl,
357
360
  auth: {
358
- type: "session"
361
+ type: authType,
362
+ jwtTokenStorageKey
359
363
  }
360
364
  });
361
365
  const CUSTOMER_QUERY_KEY = "customers";
@@ -9758,6 +9762,27 @@ const BillingAddressForm = ({ order }) => {
9758
9762
  ) });
9759
9763
  };
9760
9764
  const schema$5 = addressSchema;
9765
+ const CustomItems = () => {
9766
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9767
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9768
+ /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9769
+ ] });
9770
+ };
9771
+ const CustomItemsForm = () => {
9772
+ const form = reactHookForm.useForm({
9773
+ resolver: zod.zodResolver(schema$4)
9774
+ });
9775
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9776
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9777
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9778
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9779
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9780
+ ] }) })
9781
+ ] }) });
9782
+ };
9783
+ const schema$4 = objectType({
9784
+ email: stringType().email()
9785
+ });
9761
9786
  const Email = () => {
9762
9787
  const { id } = reactRouterDom.useParams();
9763
9788
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9780,7 +9805,7 @@ const EmailForm = ({ order }) => {
9780
9805
  defaultValues: {
9781
9806
  email: order.email ?? ""
9782
9807
  },
9783
- resolver: zod.zodResolver(schema$4)
9808
+ resolver: zod.zodResolver(schema$3)
9784
9809
  });
9785
9810
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9786
9811
  const { handleSuccess } = useRouteModal();
@@ -9823,27 +9848,6 @@ const EmailForm = ({ order }) => {
9823
9848
  }
9824
9849
  ) });
9825
9850
  };
9826
- const schema$4 = objectType({
9827
- email: stringType().email()
9828
- });
9829
- const CustomItems = () => {
9830
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9831
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9832
- /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9833
- ] });
9834
- };
9835
- const CustomItemsForm = () => {
9836
- const form = reactHookForm.useForm({
9837
- resolver: zod.zodResolver(schema$3)
9838
- });
9839
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9840
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9841
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9842
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9843
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9844
- ] }) })
9845
- ] }) });
9846
- };
9847
9851
  const schema$3 = objectType({
9848
9852
  email: stringType().email()
9849
9853
  });
@@ -11171,460 +11175,183 @@ function getHasUneditableRows(metadata) {
11171
11175
  (value) => !EDITABLE_TYPES.includes(typeof value)
11172
11176
  );
11173
11177
  }
11174
- const PROMOTION_QUERY_KEY = "promotions";
11175
- const promotionsQueryKeys = {
11176
- list: (query2) => [
11177
- PROMOTION_QUERY_KEY,
11178
- query2 ? query2 : void 0
11179
- ],
11180
- detail: (id, query2) => [
11181
- PROMOTION_QUERY_KEY,
11178
+ const SalesChannel = () => {
11179
+ const { id } = reactRouterDom.useParams();
11180
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11182
11181
  id,
11183
- query2 ? query2 : void 0
11184
- ]
11182
+ {
11183
+ fields: "+sales_channel_id"
11184
+ },
11185
+ {
11186
+ enabled: !!id
11187
+ }
11188
+ );
11189
+ if (isError) {
11190
+ throw error;
11191
+ }
11192
+ const ISrEADY = !!draft_order && !isPending;
11193
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11194
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11195
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11196
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11197
+ ] }),
11198
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11199
+ ] });
11185
11200
  };
11186
- const usePromotions = (query2, options) => {
11187
- const { data, ...rest } = reactQuery.useQuery({
11188
- queryKey: promotionsQueryKeys.list(query2),
11189
- queryFn: async () => sdk.admin.promotion.list(query2),
11190
- ...options
11201
+ const SalesChannelForm = ({ order }) => {
11202
+ const form = reactHookForm.useForm({
11203
+ defaultValues: {
11204
+ sales_channel_id: order.sales_channel_id || ""
11205
+ },
11206
+ resolver: zod.zodResolver(schema$2)
11191
11207
  });
11192
- return { ...data, ...rest };
11208
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11209
+ const { handleSuccess } = useRouteModal();
11210
+ const onSubmit = form.handleSubmit(async (data) => {
11211
+ await mutateAsync(
11212
+ {
11213
+ sales_channel_id: data.sales_channel_id
11214
+ },
11215
+ {
11216
+ onSuccess: () => {
11217
+ ui.toast.success("Sales channel updated");
11218
+ handleSuccess();
11219
+ },
11220
+ onError: (error) => {
11221
+ ui.toast.error(error.message);
11222
+ }
11223
+ }
11224
+ );
11225
+ });
11226
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11227
+ KeyboundForm,
11228
+ {
11229
+ className: "flex flex-1 flex-col overflow-hidden",
11230
+ onSubmit,
11231
+ children: [
11232
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11233
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11234
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11235
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11236
+ ] }) })
11237
+ ]
11238
+ }
11239
+ ) });
11193
11240
  };
11194
- const Promotions = () => {
11241
+ const SalesChannelField = ({ control, order }) => {
11242
+ const salesChannels = useComboboxData({
11243
+ queryFn: async (params) => {
11244
+ return await sdk.admin.salesChannel.list(params);
11245
+ },
11246
+ queryKey: ["sales-channels"],
11247
+ getOptions: (data) => {
11248
+ return data.sales_channels.map((salesChannel) => ({
11249
+ label: salesChannel.name,
11250
+ value: salesChannel.id
11251
+ }));
11252
+ },
11253
+ defaultValue: order.sales_channel_id || void 0
11254
+ });
11255
+ return /* @__PURE__ */ jsxRuntime.jsx(
11256
+ Form$2.Field,
11257
+ {
11258
+ control,
11259
+ name: "sales_channel_id",
11260
+ render: ({ field }) => {
11261
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11262
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11263
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11264
+ Combobox,
11265
+ {
11266
+ options: salesChannels.options,
11267
+ fetchNextPage: salesChannels.fetchNextPage,
11268
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11269
+ searchValue: salesChannels.searchValue,
11270
+ onSearchValueChange: salesChannels.onSearchValueChange,
11271
+ placeholder: "Select sales channel",
11272
+ ...field
11273
+ }
11274
+ ) }),
11275
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11276
+ ] });
11277
+ }
11278
+ }
11279
+ );
11280
+ };
11281
+ const schema$2 = objectType({
11282
+ sales_channel_id: stringType().min(1)
11283
+ });
11284
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
11285
+ const Shipping = () => {
11286
+ var _a;
11195
11287
  const { id } = reactRouterDom.useParams();
11288
+ const { order, isPending, isError, error } = useOrder(id, {
11289
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11290
+ });
11196
11291
  const {
11197
11292
  order: preview,
11293
+ isPending: isPreviewPending,
11198
11294
  isError: isPreviewError,
11199
11295
  error: previewError
11200
- } = useOrderPreview(id, void 0);
11296
+ } = useOrderPreview(id);
11201
11297
  useInitiateOrderEdit({ preview });
11202
11298
  const { onCancel } = useCancelOrderEdit({ preview });
11299
+ if (isError) {
11300
+ throw error;
11301
+ }
11203
11302
  if (isPreviewError) {
11204
11303
  throw previewError;
11205
11304
  }
11206
- const isReady = !!preview;
11207
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11208
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11209
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11210
- ] });
11305
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11306
+ const isReady = preview && !isPreviewPending && order && !isPending;
11307
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11308
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11309
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
11310
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11311
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11312
+ ] }) }) }),
11313
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11314
+ ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11315
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11316
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11317
+ ] }) });
11211
11318
  };
11212
- const PromotionForm = ({ preview }) => {
11213
- const { items, shipping_methods } = preview;
11319
+ const ShippingForm = ({ preview, order }) => {
11320
+ var _a;
11321
+ const { setIsOpen } = useStackedModal();
11214
11322
  const [isSubmitting, setIsSubmitting] = React.useState(false);
11215
- const [comboboxValue, setComboboxValue] = React.useState("");
11216
- const { handleSuccess } = useRouteModal();
11217
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11218
- const promoIds = getPromotionIds(items, shipping_methods);
11219
- const { promotions, isPending, isError, error } = usePromotions(
11323
+ const [data, setData] = React.useState(null);
11324
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11325
+ const { shipping_options } = useShippingOptions(
11220
11326
  {
11221
- id: promoIds
11327
+ id: appliedShippingOptionIds,
11328
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11222
11329
  },
11223
11330
  {
11224
- enabled: !!promoIds.length
11331
+ enabled: appliedShippingOptionIds.length > 0
11225
11332
  }
11226
11333
  );
11227
- const comboboxData = useComboboxData({
11228
- queryKey: ["promotions", "combobox", promoIds],
11229
- queryFn: async (params) => {
11230
- return await sdk.admin.promotion.list({
11231
- ...params,
11232
- id: {
11233
- $nin: promoIds
11234
- }
11235
- });
11236
- },
11237
- getOptions: (data) => {
11238
- return data.promotions.map((promotion) => ({
11239
- label: promotion.code,
11240
- value: promotion.code
11241
- }));
11242
- }
11243
- });
11244
- const add = async (value) => {
11245
- if (!value) {
11246
- return;
11247
- }
11248
- addPromotions(
11249
- {
11250
- promo_codes: [value]
11251
- },
11252
- {
11253
- onError: (e) => {
11254
- ui.toast.error(e.message);
11255
- comboboxData.onSearchValueChange("");
11256
- setComboboxValue("");
11257
- },
11258
- onSuccess: () => {
11259
- comboboxData.onSearchValueChange("");
11260
- setComboboxValue("");
11261
- }
11262
- }
11263
- );
11264
- };
11265
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11266
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11267
- const onSubmit = async () => {
11268
- setIsSubmitting(true);
11269
- let requestSucceeded = false;
11270
- await requestOrderEdit(void 0, {
11271
- onError: (e) => {
11272
- ui.toast.error(e.message);
11273
- },
11274
- onSuccess: () => {
11275
- requestSucceeded = true;
11276
- }
11277
- });
11278
- if (!requestSucceeded) {
11279
- setIsSubmitting(false);
11280
- return;
11281
- }
11282
- await confirmOrderEdit(void 0, {
11283
- onError: (e) => {
11284
- ui.toast.error(e.message);
11285
- },
11286
- onSuccess: () => {
11287
- handleSuccess();
11288
- },
11289
- onSettled: () => {
11290
- setIsSubmitting(false);
11291
- }
11292
- });
11293
- };
11294
- if (isError) {
11295
- throw error;
11296
- }
11297
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11298
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11299
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11300
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11301
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11302
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11303
- ] }),
11304
- /* @__PURE__ */ jsxRuntime.jsx(
11305
- Combobox,
11306
- {
11307
- id: "promotion-combobox",
11308
- "aria-describedby": "promotion-combobox-hint",
11309
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11310
- fetchNextPage: comboboxData.fetchNextPage,
11311
- options: comboboxData.options,
11312
- onSearchValueChange: comboboxData.onSearchValueChange,
11313
- searchValue: comboboxData.searchValue,
11314
- disabled: comboboxData.disabled || isAddingPromotions,
11315
- onChange: add,
11316
- value: comboboxValue
11317
- }
11318
- )
11319
- ] }),
11320
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11321
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11322
- PromotionItem,
11323
- {
11324
- promotion,
11325
- orderId: preview.id,
11326
- isLoading: isPending
11327
- },
11328
- promotion.id
11329
- )) })
11330
- ] }) }),
11331
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11332
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11333
- /* @__PURE__ */ jsxRuntime.jsx(
11334
- ui.Button,
11335
- {
11336
- size: "small",
11337
- type: "submit",
11338
- isLoading: isSubmitting || isAddingPromotions,
11339
- children: "Save"
11340
- }
11341
- )
11342
- ] }) })
11343
- ] });
11344
- };
11345
- const PromotionItem = ({
11346
- promotion,
11347
- orderId,
11348
- isLoading
11349
- }) => {
11350
- var _a;
11351
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11352
- const onRemove = async () => {
11353
- removePromotions(
11354
- {
11355
- promo_codes: [promotion.code]
11356
- },
11357
- {
11358
- onError: (e) => {
11359
- ui.toast.error(e.message);
11360
- }
11361
- }
11362
- );
11363
- };
11364
- const displayValue = getDisplayValue(promotion);
11365
- return /* @__PURE__ */ jsxRuntime.jsxs(
11366
- "div",
11367
- {
11368
- className: ui.clx(
11369
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11370
- {
11371
- "animate-pulse": isLoading
11372
- }
11373
- ),
11374
- children: [
11375
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11376
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11377
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11378
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11379
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11380
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11381
- ] }),
11382
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11383
- ] })
11384
- ] }),
11385
- /* @__PURE__ */ jsxRuntime.jsx(
11386
- ui.IconButton,
11387
- {
11388
- size: "small",
11389
- type: "button",
11390
- variant: "transparent",
11391
- onClick: onRemove,
11392
- isLoading: isPending || isLoading,
11393
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11394
- }
11395
- )
11396
- ]
11397
- },
11398
- promotion.id
11399
- );
11400
- };
11401
- function getDisplayValue(promotion) {
11402
- var _a, _b, _c, _d;
11403
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11404
- if (!value) {
11405
- return null;
11406
- }
11407
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11408
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11409
- if (!currency) {
11410
- return null;
11411
- }
11412
- return getLocaleAmount(value, currency);
11413
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11414
- return formatPercentage(value);
11415
- }
11416
- return null;
11417
- }
11418
- const formatter = new Intl.NumberFormat([], {
11419
- style: "percent",
11420
- minimumFractionDigits: 2
11421
- });
11422
- const formatPercentage = (value, isPercentageValue = false) => {
11423
- let val = value || 0;
11424
- if (!isPercentageValue) {
11425
- val = val / 100;
11426
- }
11427
- return formatter.format(val);
11428
- };
11429
- function getPromotionIds(items, shippingMethods) {
11430
- const promotionIds = /* @__PURE__ */ new Set();
11431
- for (const item of items) {
11432
- if (item.adjustments) {
11433
- for (const adjustment of item.adjustments) {
11434
- if (adjustment.promotion_id) {
11435
- promotionIds.add(adjustment.promotion_id);
11436
- }
11437
- }
11438
- }
11439
- }
11440
- for (const shippingMethod of shippingMethods) {
11441
- if (shippingMethod.adjustments) {
11442
- for (const adjustment of shippingMethod.adjustments) {
11443
- if (adjustment.promotion_id) {
11444
- promotionIds.add(adjustment.promotion_id);
11445
- }
11446
- }
11447
- }
11448
- }
11449
- return Array.from(promotionIds);
11450
- }
11451
- const SalesChannel = () => {
11452
- const { id } = reactRouterDom.useParams();
11453
- const { draft_order, isPending, isError, error } = useDraftOrder(
11454
- id,
11455
- {
11456
- fields: "+sales_channel_id"
11457
- },
11458
- {
11459
- enabled: !!id
11460
- }
11461
- );
11462
- if (isError) {
11463
- throw error;
11464
- }
11465
- const ISrEADY = !!draft_order && !isPending;
11466
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11467
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11468
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11469
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11470
- ] }),
11471
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11472
- ] });
11473
- };
11474
- const SalesChannelForm = ({ order }) => {
11475
- const form = reactHookForm.useForm({
11476
- defaultValues: {
11477
- sales_channel_id: order.sales_channel_id || ""
11478
- },
11479
- resolver: zod.zodResolver(schema$2)
11480
- });
11481
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11482
- const { handleSuccess } = useRouteModal();
11483
- const onSubmit = form.handleSubmit(async (data) => {
11484
- await mutateAsync(
11485
- {
11486
- sales_channel_id: data.sales_channel_id
11487
- },
11488
- {
11489
- onSuccess: () => {
11490
- ui.toast.success("Sales channel updated");
11491
- handleSuccess();
11492
- },
11493
- onError: (error) => {
11494
- ui.toast.error(error.message);
11495
- }
11496
- }
11497
- );
11498
- });
11499
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11500
- KeyboundForm,
11501
- {
11502
- className: "flex flex-1 flex-col overflow-hidden",
11503
- onSubmit,
11504
- children: [
11505
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11506
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11507
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11508
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11509
- ] }) })
11510
- ]
11511
- }
11512
- ) });
11513
- };
11514
- const SalesChannelField = ({ control, order }) => {
11515
- const salesChannels = useComboboxData({
11516
- queryFn: async (params) => {
11517
- return await sdk.admin.salesChannel.list(params);
11518
- },
11519
- queryKey: ["sales-channels"],
11520
- getOptions: (data) => {
11521
- return data.sales_channels.map((salesChannel) => ({
11522
- label: salesChannel.name,
11523
- value: salesChannel.id
11524
- }));
11525
- },
11526
- defaultValue: order.sales_channel_id || void 0
11527
- });
11528
- return /* @__PURE__ */ jsxRuntime.jsx(
11529
- Form$2.Field,
11530
- {
11531
- control,
11532
- name: "sales_channel_id",
11533
- render: ({ field }) => {
11534
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11535
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11536
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11537
- Combobox,
11538
- {
11539
- options: salesChannels.options,
11540
- fetchNextPage: salesChannels.fetchNextPage,
11541
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11542
- searchValue: salesChannels.searchValue,
11543
- onSearchValueChange: salesChannels.onSearchValueChange,
11544
- placeholder: "Select sales channel",
11545
- ...field
11546
- }
11547
- ) }),
11548
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11549
- ] });
11550
- }
11551
- }
11552
- );
11553
- };
11554
- const schema$2 = objectType({
11555
- sales_channel_id: stringType().min(1)
11556
- });
11557
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11558
- const Shipping = () => {
11559
- var _a;
11560
- const { id } = reactRouterDom.useParams();
11561
- const { order, isPending, isError, error } = useOrder(id, {
11562
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11563
- });
11564
- const {
11565
- order: preview,
11566
- isPending: isPreviewPending,
11567
- isError: isPreviewError,
11568
- error: previewError
11569
- } = useOrderPreview(id);
11570
- useInitiateOrderEdit({ preview });
11571
- const { onCancel } = useCancelOrderEdit({ preview });
11572
- if (isError) {
11573
- throw error;
11574
- }
11575
- if (isPreviewError) {
11576
- throw previewError;
11577
- }
11578
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11579
- const isReady = preview && !isPreviewPending && order && !isPending;
11580
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11581
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11582
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
11583
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11584
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11585
- ] }) }) }),
11586
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11587
- ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11588
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11589
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11590
- ] }) });
11591
- };
11592
- const ShippingForm = ({ preview, order }) => {
11593
- var _a;
11594
- const { setIsOpen } = useStackedModal();
11595
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11596
- const [data, setData] = React.useState(null);
11597
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11598
- const { shipping_options } = useShippingOptions(
11599
- {
11600
- id: appliedShippingOptionIds,
11601
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11602
- },
11603
- {
11604
- enabled: appliedShippingOptionIds.length > 0
11605
- }
11606
- );
11607
- const uniqueShippingProfiles = React.useMemo(() => {
11608
- const profiles = /* @__PURE__ */ new Map();
11609
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11610
- profiles.set(profile.id, profile);
11611
- });
11612
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11613
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11614
- });
11615
- return Array.from(profiles.values());
11616
- }, [order.items, shipping_options]);
11617
- const { handleSuccess } = useRouteModal();
11618
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11619
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11620
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11621
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11622
- const onSubmit = async () => {
11623
- setIsSubmitting(true);
11624
- let requestSucceeded = false;
11625
- await requestOrderEdit(void 0, {
11626
- onError: (e) => {
11627
- ui.toast.error(`Failed to request order edit: ${e.message}`);
11334
+ const uniqueShippingProfiles = React.useMemo(() => {
11335
+ const profiles = /* @__PURE__ */ new Map();
11336
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
11337
+ profiles.set(profile.id, profile);
11338
+ });
11339
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11340
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
11341
+ });
11342
+ return Array.from(profiles.values());
11343
+ }, [order.items, shipping_options]);
11344
+ const { handleSuccess } = useRouteModal();
11345
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11346
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11347
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11348
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11349
+ const onSubmit = async () => {
11350
+ setIsSubmitting(true);
11351
+ let requestSucceeded = false;
11352
+ await requestOrderEdit(void 0, {
11353
+ onError: (e) => {
11354
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
11628
11355
  },
11629
11356
  onSuccess: () => {
11630
11357
  requestSucceeded = true;
@@ -12960,86 +12687,363 @@ const Illustration = () => {
12960
12687
  /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12961
12688
  "path",
12962
12689
  {
12963
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12964
- stroke: "#A1A1AA",
12965
- strokeWidth: "1.5",
12966
- strokeLinecap: "round",
12967
- strokeLinejoin: "round"
12690
+ d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12691
+ stroke: "#A1A1AA",
12692
+ strokeWidth: "1.5",
12693
+ strokeLinecap: "round",
12694
+ strokeLinejoin: "round"
12695
+ }
12696
+ ) }),
12697
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12698
+ "path",
12699
+ {
12700
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12701
+ stroke: "#A1A1AA",
12702
+ strokeWidth: "1.5",
12703
+ strokeLinecap: "round",
12704
+ strokeLinejoin: "round"
12705
+ }
12706
+ ) }),
12707
+ /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12708
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12709
+ "rect",
12710
+ {
12711
+ width: "12",
12712
+ height: "12",
12713
+ fill: "white",
12714
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12715
+ }
12716
+ ) }),
12717
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12718
+ "rect",
12719
+ {
12720
+ width: "12",
12721
+ height: "12",
12722
+ fill: "white",
12723
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12724
+ }
12725
+ ) }),
12726
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12727
+ "rect",
12728
+ {
12729
+ width: "12",
12730
+ height: "12",
12731
+ fill: "white",
12732
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12733
+ }
12734
+ ) }),
12735
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12736
+ "rect",
12737
+ {
12738
+ width: "12",
12739
+ height: "12",
12740
+ fill: "white",
12741
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12742
+ }
12743
+ ) }),
12744
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12745
+ "rect",
12746
+ {
12747
+ width: "12",
12748
+ height: "12",
12749
+ fill: "white",
12750
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12751
+ }
12752
+ ) }),
12753
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12754
+ "rect",
12755
+ {
12756
+ width: "12",
12757
+ height: "12",
12758
+ fill: "white",
12759
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12760
+ }
12761
+ ) })
12762
+ ] })
12763
+ ]
12764
+ }
12765
+ );
12766
+ };
12767
+ const schema = objectType({
12768
+ customer_id: stringType().min(1)
12769
+ });
12770
+ const PROMOTION_QUERY_KEY = "promotions";
12771
+ const promotionsQueryKeys = {
12772
+ list: (query2) => [
12773
+ PROMOTION_QUERY_KEY,
12774
+ query2 ? query2 : void 0
12775
+ ],
12776
+ detail: (id, query2) => [
12777
+ PROMOTION_QUERY_KEY,
12778
+ id,
12779
+ query2 ? query2 : void 0
12780
+ ]
12781
+ };
12782
+ const usePromotions = (query2, options) => {
12783
+ const { data, ...rest } = reactQuery.useQuery({
12784
+ queryKey: promotionsQueryKeys.list(query2),
12785
+ queryFn: async () => sdk.admin.promotion.list(query2),
12786
+ ...options
12787
+ });
12788
+ return { ...data, ...rest };
12789
+ };
12790
+ const Promotions = () => {
12791
+ const { id } = reactRouterDom.useParams();
12792
+ const {
12793
+ order: preview,
12794
+ isError: isPreviewError,
12795
+ error: previewError
12796
+ } = useOrderPreview(id, void 0);
12797
+ useInitiateOrderEdit({ preview });
12798
+ const { onCancel } = useCancelOrderEdit({ preview });
12799
+ if (isPreviewError) {
12800
+ throw previewError;
12801
+ }
12802
+ const isReady = !!preview;
12803
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
12804
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
12805
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
12806
+ ] });
12807
+ };
12808
+ const PromotionForm = ({ preview }) => {
12809
+ const { items, shipping_methods } = preview;
12810
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
12811
+ const [comboboxValue, setComboboxValue] = React.useState("");
12812
+ const { handleSuccess } = useRouteModal();
12813
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
12814
+ const promoIds = getPromotionIds(items, shipping_methods);
12815
+ const { promotions, isPending, isError, error } = usePromotions(
12816
+ {
12817
+ id: promoIds
12818
+ },
12819
+ {
12820
+ enabled: !!promoIds.length
12821
+ }
12822
+ );
12823
+ const comboboxData = useComboboxData({
12824
+ queryKey: ["promotions", "combobox", promoIds],
12825
+ queryFn: async (params) => {
12826
+ return await sdk.admin.promotion.list({
12827
+ ...params,
12828
+ id: {
12829
+ $nin: promoIds
12830
+ }
12831
+ });
12832
+ },
12833
+ getOptions: (data) => {
12834
+ return data.promotions.map((promotion) => ({
12835
+ label: promotion.code,
12836
+ value: promotion.code
12837
+ }));
12838
+ }
12839
+ });
12840
+ const add = async (value) => {
12841
+ if (!value) {
12842
+ return;
12843
+ }
12844
+ addPromotions(
12845
+ {
12846
+ promo_codes: [value]
12847
+ },
12848
+ {
12849
+ onError: (e) => {
12850
+ ui.toast.error(e.message);
12851
+ comboboxData.onSearchValueChange("");
12852
+ setComboboxValue("");
12853
+ },
12854
+ onSuccess: () => {
12855
+ comboboxData.onSearchValueChange("");
12856
+ setComboboxValue("");
12857
+ }
12858
+ }
12859
+ );
12860
+ };
12861
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12862
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
12863
+ const onSubmit = async () => {
12864
+ setIsSubmitting(true);
12865
+ let requestSucceeded = false;
12866
+ await requestOrderEdit(void 0, {
12867
+ onError: (e) => {
12868
+ ui.toast.error(e.message);
12869
+ },
12870
+ onSuccess: () => {
12871
+ requestSucceeded = true;
12872
+ }
12873
+ });
12874
+ if (!requestSucceeded) {
12875
+ setIsSubmitting(false);
12876
+ return;
12877
+ }
12878
+ await confirmOrderEdit(void 0, {
12879
+ onError: (e) => {
12880
+ ui.toast.error(e.message);
12881
+ },
12882
+ onSuccess: () => {
12883
+ handleSuccess();
12884
+ },
12885
+ onSettled: () => {
12886
+ setIsSubmitting(false);
12887
+ }
12888
+ });
12889
+ };
12890
+ if (isError) {
12891
+ throw error;
12892
+ }
12893
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
12894
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
12895
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
12896
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12897
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
12898
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
12899
+ ] }),
12900
+ /* @__PURE__ */ jsxRuntime.jsx(
12901
+ Combobox,
12902
+ {
12903
+ id: "promotion-combobox",
12904
+ "aria-describedby": "promotion-combobox-hint",
12905
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
12906
+ fetchNextPage: comboboxData.fetchNextPage,
12907
+ options: comboboxData.options,
12908
+ onSearchValueChange: comboboxData.onSearchValueChange,
12909
+ searchValue: comboboxData.searchValue,
12910
+ disabled: comboboxData.disabled || isAddingPromotions,
12911
+ onChange: add,
12912
+ value: comboboxValue
12968
12913
  }
12969
- ) }),
12970
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12971
- "path",
12914
+ )
12915
+ ] }),
12916
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12917
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
12918
+ PromotionItem,
12919
+ {
12920
+ promotion,
12921
+ orderId: preview.id,
12922
+ isLoading: isPending
12923
+ },
12924
+ promotion.id
12925
+ )) })
12926
+ ] }) }),
12927
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12928
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12929
+ /* @__PURE__ */ jsxRuntime.jsx(
12930
+ ui.Button,
12931
+ {
12932
+ size: "small",
12933
+ type: "submit",
12934
+ isLoading: isSubmitting || isAddingPromotions,
12935
+ children: "Save"
12936
+ }
12937
+ )
12938
+ ] }) })
12939
+ ] });
12940
+ };
12941
+ const PromotionItem = ({
12942
+ promotion,
12943
+ orderId,
12944
+ isLoading
12945
+ }) => {
12946
+ var _a;
12947
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
12948
+ const onRemove = async () => {
12949
+ removePromotions(
12950
+ {
12951
+ promo_codes: [promotion.code]
12952
+ },
12953
+ {
12954
+ onError: (e) => {
12955
+ ui.toast.error(e.message);
12956
+ }
12957
+ }
12958
+ );
12959
+ };
12960
+ const displayValue = getDisplayValue(promotion);
12961
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12962
+ "div",
12963
+ {
12964
+ className: ui.clx(
12965
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
12966
+ {
12967
+ "animate-pulse": isLoading
12968
+ }
12969
+ ),
12970
+ children: [
12971
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12972
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
12973
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
12974
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
12975
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
12976
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
12977
+ ] }),
12978
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
12979
+ ] })
12980
+ ] }),
12981
+ /* @__PURE__ */ jsxRuntime.jsx(
12982
+ ui.IconButton,
12972
12983
  {
12973
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12974
- stroke: "#A1A1AA",
12975
- strokeWidth: "1.5",
12976
- strokeLinecap: "round",
12977
- strokeLinejoin: "round"
12984
+ size: "small",
12985
+ type: "button",
12986
+ variant: "transparent",
12987
+ onClick: onRemove,
12988
+ isLoading: isPending || isLoading,
12989
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
12978
12990
  }
12979
- ) }),
12980
- /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12981
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12982
- "rect",
12983
- {
12984
- width: "12",
12985
- height: "12",
12986
- fill: "white",
12987
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12988
- }
12989
- ) }),
12990
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12991
- "rect",
12992
- {
12993
- width: "12",
12994
- height: "12",
12995
- fill: "white",
12996
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12997
- }
12998
- ) }),
12999
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13000
- "rect",
13001
- {
13002
- width: "12",
13003
- height: "12",
13004
- fill: "white",
13005
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
13006
- }
13007
- ) }),
13008
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13009
- "rect",
13010
- {
13011
- width: "12",
13012
- height: "12",
13013
- fill: "white",
13014
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
13015
- }
13016
- ) }),
13017
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13018
- "rect",
13019
- {
13020
- width: "12",
13021
- height: "12",
13022
- fill: "white",
13023
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
13024
- }
13025
- ) }),
13026
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13027
- "rect",
13028
- {
13029
- width: "12",
13030
- height: "12",
13031
- fill: "white",
13032
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
13033
- }
13034
- ) })
13035
- ] })
12991
+ )
13036
12992
  ]
13037
- }
12993
+ },
12994
+ promotion.id
13038
12995
  );
13039
12996
  };
13040
- const schema = objectType({
13041
- customer_id: stringType().min(1)
12997
+ function getDisplayValue(promotion) {
12998
+ var _a, _b, _c, _d;
12999
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
13000
+ if (!value) {
13001
+ return null;
13002
+ }
13003
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
13004
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
13005
+ if (!currency) {
13006
+ return null;
13007
+ }
13008
+ return getLocaleAmount(value, currency);
13009
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
13010
+ return formatPercentage(value);
13011
+ }
13012
+ return null;
13013
+ }
13014
+ const formatter = new Intl.NumberFormat([], {
13015
+ style: "percent",
13016
+ minimumFractionDigits: 2
13042
13017
  });
13018
+ const formatPercentage = (value, isPercentageValue = false) => {
13019
+ let val = value || 0;
13020
+ if (!isPercentageValue) {
13021
+ val = val / 100;
13022
+ }
13023
+ return formatter.format(val);
13024
+ };
13025
+ function getPromotionIds(items, shippingMethods) {
13026
+ const promotionIds = /* @__PURE__ */ new Set();
13027
+ for (const item of items) {
13028
+ if (item.adjustments) {
13029
+ for (const adjustment of item.adjustments) {
13030
+ if (adjustment.promotion_id) {
13031
+ promotionIds.add(adjustment.promotion_id);
13032
+ }
13033
+ }
13034
+ }
13035
+ }
13036
+ for (const shippingMethod of shippingMethods) {
13037
+ if (shippingMethod.adjustments) {
13038
+ for (const adjustment of shippingMethod.adjustments) {
13039
+ if (adjustment.promotion_id) {
13040
+ promotionIds.add(adjustment.promotion_id);
13041
+ }
13042
+ }
13043
+ }
13044
+ }
13045
+ return Array.from(promotionIds);
13046
+ }
13043
13047
  const widgetModule = { widgets: [] };
13044
13048
  const routeModule = {
13045
13049
  routes: [
@@ -13064,14 +13068,14 @@ const routeModule = {
13064
13068
  Component: BillingAddress,
13065
13069
  path: "/draft-orders/:id/billing-address"
13066
13070
  },
13067
- {
13068
- Component: Email,
13069
- path: "/draft-orders/:id/email"
13070
- },
13071
13071
  {
13072
13072
  Component: CustomItems,
13073
13073
  path: "/draft-orders/:id/custom-items"
13074
13074
  },
13075
+ {
13076
+ Component: Email,
13077
+ path: "/draft-orders/:id/email"
13078
+ },
13075
13079
  {
13076
13080
  Component: Items,
13077
13081
  path: "/draft-orders/:id/items"
@@ -13080,10 +13084,6 @@ const routeModule = {
13080
13084
  Component: Metadata,
13081
13085
  path: "/draft-orders/:id/metadata"
13082
13086
  },
13083
- {
13084
- Component: Promotions,
13085
- path: "/draft-orders/:id/promotions"
13086
- },
13087
13087
  {
13088
13088
  Component: SalesChannel,
13089
13089
  path: "/draft-orders/:id/sales-channel"
@@ -13099,6 +13099,10 @@ const routeModule = {
13099
13099
  {
13100
13100
  Component: TransferOwnership,
13101
13101
  path: "/draft-orders/:id/transfer-ownership"
13102
+ },
13103
+ {
13104
+ Component: Promotions,
13105
+ path: "/draft-orders/:id/promotions"
13102
13106
  }
13103
13107
  ]
13104
13108
  }