@medusajs/draft-order 2.11.1-preview-20251023031637 → 2.11.1-preview-20251023060204

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