@medusajs/draft-order 2.11.4-preview-20251112090213 → 2.11.4-preview-20251112180139

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.
@@ -8729,7 +8729,7 @@ const Contact = ({ order }) => {
8729
8729
  children: phone
8730
8730
  }
8731
8731
  ),
8732
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Copy, { content: email, className: "text-ui-fg-muted" }) })
8732
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Copy, { content: phone, className: "text-ui-fg-muted" }) })
8733
8733
  ] })
8734
8734
  ] })
8735
8735
  ] });
@@ -9783,6 +9783,74 @@ const CustomItemsForm = () => {
9783
9783
  const schema$4 = objectType({
9784
9784
  email: stringType().email()
9785
9785
  });
9786
+ const Email = () => {
9787
+ const { id } = reactRouterDom.useParams();
9788
+ const { order, isPending, isError, error } = useOrder(id, {
9789
+ fields: "+email"
9790
+ });
9791
+ if (isError) {
9792
+ throw error;
9793
+ }
9794
+ const isReady = !isPending && !!order;
9795
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9796
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9797
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
9798
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9799
+ ] }),
9800
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
9801
+ ] });
9802
+ };
9803
+ const EmailForm = ({ order }) => {
9804
+ const form = reactHookForm.useForm({
9805
+ defaultValues: {
9806
+ email: order.email ?? ""
9807
+ },
9808
+ resolver: zod.zodResolver(schema$3)
9809
+ });
9810
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9811
+ const { handleSuccess } = useRouteModal();
9812
+ const onSubmit = form.handleSubmit(async (data) => {
9813
+ await mutateAsync(
9814
+ { email: data.email },
9815
+ {
9816
+ onSuccess: () => {
9817
+ handleSuccess();
9818
+ },
9819
+ onError: (error) => {
9820
+ ui.toast.error(error.message);
9821
+ }
9822
+ }
9823
+ );
9824
+ });
9825
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9826
+ KeyboundForm,
9827
+ {
9828
+ className: "flex flex-1 flex-col overflow-hidden",
9829
+ onSubmit,
9830
+ children: [
9831
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
9832
+ Form$2.Field,
9833
+ {
9834
+ control: form.control,
9835
+ name: "email",
9836
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9837
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
9838
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9839
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9840
+ ] })
9841
+ }
9842
+ ) }),
9843
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9844
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9845
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9846
+ ] }) })
9847
+ ]
9848
+ }
9849
+ ) });
9850
+ };
9851
+ const schema$3 = objectType({
9852
+ email: stringType().email()
9853
+ });
9786
9854
  const NumberInput = React.forwardRef(
9787
9855
  ({
9788
9856
  value,
@@ -11107,505 +11175,160 @@ function getHasUneditableRows(metadata) {
11107
11175
  (value) => !EDITABLE_TYPES.includes(typeof value)
11108
11176
  );
11109
11177
  }
11110
- const PROMOTION_QUERY_KEY = "promotions";
11111
- const promotionsQueryKeys = {
11112
- list: (query2) => [
11113
- PROMOTION_QUERY_KEY,
11114
- query2 ? query2 : void 0
11115
- ],
11116
- detail: (id, query2) => [
11117
- PROMOTION_QUERY_KEY,
11178
+ const SalesChannel = () => {
11179
+ const { id } = reactRouterDom.useParams();
11180
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11118
11181
  id,
11119
- query2 ? query2 : void 0
11120
- ]
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
+ ] });
11121
11200
  };
11122
- const usePromotions = (query2, options) => {
11123
- const { data, ...rest } = reactQuery.useQuery({
11124
- queryKey: promotionsQueryKeys.list(query2),
11125
- queryFn: async () => sdk.admin.promotion.list(query2),
11126
- ...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)
11127
11207
  });
11128
- 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
+ ) });
11129
11240
  };
11130
- 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;
11131
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
+ });
11132
11291
  const {
11133
11292
  order: preview,
11293
+ isPending: isPreviewPending,
11134
11294
  isError: isPreviewError,
11135
11295
  error: previewError
11136
- } = useOrderPreview(id, void 0);
11296
+ } = useOrderPreview(id);
11137
11297
  useInitiateOrderEdit({ preview });
11138
11298
  const { onCancel } = useCancelOrderEdit({ preview });
11299
+ if (isError) {
11300
+ throw error;
11301
+ }
11139
11302
  if (isPreviewError) {
11140
11303
  throw previewError;
11141
11304
  }
11142
- const isReady = !!preview;
11143
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11144
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11145
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11146
- ] });
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
+ ] }) });
11147
11318
  };
11148
- const PromotionForm = ({ preview }) => {
11149
- const { items, shipping_methods } = preview;
11319
+ const ShippingForm = ({ preview, order }) => {
11320
+ var _a;
11321
+ const { setIsOpen } = useStackedModal();
11150
11322
  const [isSubmitting, setIsSubmitting] = React.useState(false);
11151
- const [comboboxValue, setComboboxValue] = React.useState("");
11152
- const { handleSuccess } = useRouteModal();
11153
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11154
- const promoIds = getPromotionIds(items, shipping_methods);
11155
- 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(
11156
11326
  {
11157
- id: promoIds
11327
+ id: appliedShippingOptionIds,
11328
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11158
11329
  },
11159
11330
  {
11160
- enabled: !!promoIds.length
11161
- }
11162
- );
11163
- const comboboxData = useComboboxData({
11164
- queryKey: ["promotions", "combobox", promoIds],
11165
- queryFn: async (params) => {
11166
- return await sdk.admin.promotion.list({
11167
- ...params,
11168
- id: {
11169
- $nin: promoIds
11170
- }
11171
- });
11172
- },
11173
- getOptions: (data) => {
11174
- return data.promotions.map((promotion) => ({
11175
- label: promotion.code,
11176
- value: promotion.code
11177
- }));
11178
- }
11179
- });
11180
- const add = async (value) => {
11181
- if (!value) {
11182
- return;
11183
- }
11184
- addPromotions(
11185
- {
11186
- promo_codes: [value]
11187
- },
11188
- {
11189
- onError: (e) => {
11190
- ui.toast.error(e.message);
11191
- comboboxData.onSearchValueChange("");
11192
- setComboboxValue("");
11193
- },
11194
- onSuccess: () => {
11195
- comboboxData.onSearchValueChange("");
11196
- setComboboxValue("");
11197
- }
11198
- }
11199
- );
11200
- };
11201
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11202
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11203
- const onSubmit = async () => {
11204
- setIsSubmitting(true);
11205
- let requestSucceeded = false;
11206
- await requestOrderEdit(void 0, {
11207
- onError: (e) => {
11208
- ui.toast.error(e.message);
11209
- },
11210
- onSuccess: () => {
11211
- requestSucceeded = true;
11212
- }
11213
- });
11214
- if (!requestSucceeded) {
11215
- setIsSubmitting(false);
11216
- return;
11217
- }
11218
- await confirmOrderEdit(void 0, {
11219
- onError: (e) => {
11220
- ui.toast.error(e.message);
11221
- },
11222
- onSuccess: () => {
11223
- handleSuccess();
11224
- },
11225
- onSettled: () => {
11226
- setIsSubmitting(false);
11227
- }
11228
- });
11229
- };
11230
- if (isError) {
11231
- throw error;
11232
- }
11233
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11234
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11235
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11236
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11237
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11238
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11239
- ] }),
11240
- /* @__PURE__ */ jsxRuntime.jsx(
11241
- Combobox,
11242
- {
11243
- id: "promotion-combobox",
11244
- "aria-describedby": "promotion-combobox-hint",
11245
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11246
- fetchNextPage: comboboxData.fetchNextPage,
11247
- options: comboboxData.options,
11248
- onSearchValueChange: comboboxData.onSearchValueChange,
11249
- searchValue: comboboxData.searchValue,
11250
- disabled: comboboxData.disabled || isAddingPromotions,
11251
- onChange: add,
11252
- value: comboboxValue
11253
- }
11254
- )
11255
- ] }),
11256
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11257
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11258
- PromotionItem,
11259
- {
11260
- promotion,
11261
- orderId: preview.id,
11262
- isLoading: isPending
11263
- },
11264
- promotion.id
11265
- )) })
11266
- ] }) }),
11267
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11268
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11269
- /* @__PURE__ */ jsxRuntime.jsx(
11270
- ui.Button,
11271
- {
11272
- size: "small",
11273
- type: "submit",
11274
- isLoading: isSubmitting || isAddingPromotions,
11275
- children: "Save"
11276
- }
11277
- )
11278
- ] }) })
11279
- ] });
11280
- };
11281
- const PromotionItem = ({
11282
- promotion,
11283
- orderId,
11284
- isLoading
11285
- }) => {
11286
- var _a;
11287
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11288
- const onRemove = async () => {
11289
- removePromotions(
11290
- {
11291
- promo_codes: [promotion.code]
11292
- },
11293
- {
11294
- onError: (e) => {
11295
- ui.toast.error(e.message);
11296
- }
11297
- }
11298
- );
11299
- };
11300
- const displayValue = getDisplayValue(promotion);
11301
- return /* @__PURE__ */ jsxRuntime.jsxs(
11302
- "div",
11303
- {
11304
- className: ui.clx(
11305
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11306
- {
11307
- "animate-pulse": isLoading
11308
- }
11309
- ),
11310
- children: [
11311
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11312
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11313
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11314
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11315
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11316
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11317
- ] }),
11318
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11319
- ] })
11320
- ] }),
11321
- /* @__PURE__ */ jsxRuntime.jsx(
11322
- ui.IconButton,
11323
- {
11324
- size: "small",
11325
- type: "button",
11326
- variant: "transparent",
11327
- onClick: onRemove,
11328
- isLoading: isPending || isLoading,
11329
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11330
- }
11331
- )
11332
- ]
11333
- },
11334
- promotion.id
11335
- );
11336
- };
11337
- function getDisplayValue(promotion) {
11338
- var _a, _b, _c, _d;
11339
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11340
- if (!value) {
11341
- return null;
11342
- }
11343
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11344
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11345
- if (!currency) {
11346
- return null;
11347
- }
11348
- return getLocaleAmount(value, currency);
11349
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11350
- return formatPercentage(value);
11351
- }
11352
- return null;
11353
- }
11354
- const formatter = new Intl.NumberFormat([], {
11355
- style: "percent",
11356
- minimumFractionDigits: 2
11357
- });
11358
- const formatPercentage = (value, isPercentageValue = false) => {
11359
- let val = value || 0;
11360
- if (!isPercentageValue) {
11361
- val = val / 100;
11362
- }
11363
- return formatter.format(val);
11364
- };
11365
- function getPromotionIds(items, shippingMethods) {
11366
- const promotionIds = /* @__PURE__ */ new Set();
11367
- for (const item of items) {
11368
- if (item.adjustments) {
11369
- for (const adjustment of item.adjustments) {
11370
- if (adjustment.promotion_id) {
11371
- promotionIds.add(adjustment.promotion_id);
11372
- }
11373
- }
11374
- }
11375
- }
11376
- for (const shippingMethod of shippingMethods) {
11377
- if (shippingMethod.adjustments) {
11378
- for (const adjustment of shippingMethod.adjustments) {
11379
- if (adjustment.promotion_id) {
11380
- promotionIds.add(adjustment.promotion_id);
11381
- }
11382
- }
11383
- }
11384
- }
11385
- return Array.from(promotionIds);
11386
- }
11387
- const Email = () => {
11388
- const { id } = reactRouterDom.useParams();
11389
- const { order, isPending, isError, error } = useOrder(id, {
11390
- fields: "+email"
11391
- });
11392
- if (isError) {
11393
- throw error;
11394
- }
11395
- const isReady = !isPending && !!order;
11396
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11397
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11398
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
11399
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
11400
- ] }),
11401
- isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
11402
- ] });
11403
- };
11404
- const EmailForm = ({ order }) => {
11405
- const form = reactHookForm.useForm({
11406
- defaultValues: {
11407
- email: order.email ?? ""
11408
- },
11409
- resolver: zod.zodResolver(schema$3)
11410
- });
11411
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11412
- const { handleSuccess } = useRouteModal();
11413
- const onSubmit = form.handleSubmit(async (data) => {
11414
- await mutateAsync(
11415
- { email: data.email },
11416
- {
11417
- onSuccess: () => {
11418
- handleSuccess();
11419
- },
11420
- onError: (error) => {
11421
- ui.toast.error(error.message);
11422
- }
11423
- }
11424
- );
11425
- });
11426
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11427
- KeyboundForm,
11428
- {
11429
- className: "flex flex-1 flex-col overflow-hidden",
11430
- onSubmit,
11431
- children: [
11432
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
11433
- Form$2.Field,
11434
- {
11435
- control: form.control,
11436
- name: "email",
11437
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11438
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
11439
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11440
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11441
- ] })
11442
- }
11443
- ) }),
11444
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11445
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11446
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11447
- ] }) })
11448
- ]
11449
- }
11450
- ) });
11451
- };
11452
- const schema$3 = objectType({
11453
- email: stringType().email()
11454
- });
11455
- const SalesChannel = () => {
11456
- const { id } = reactRouterDom.useParams();
11457
- const { draft_order, isPending, isError, error } = useDraftOrder(
11458
- id,
11459
- {
11460
- fields: "+sales_channel_id"
11461
- },
11462
- {
11463
- enabled: !!id
11464
- }
11465
- );
11466
- if (isError) {
11467
- throw error;
11468
- }
11469
- const ISrEADY = !!draft_order && !isPending;
11470
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11471
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11472
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11473
- /* @__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" }) })
11474
- ] }),
11475
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11476
- ] });
11477
- };
11478
- const SalesChannelForm = ({ order }) => {
11479
- const form = reactHookForm.useForm({
11480
- defaultValues: {
11481
- sales_channel_id: order.sales_channel_id || ""
11482
- },
11483
- resolver: zod.zodResolver(schema$2)
11484
- });
11485
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11486
- const { handleSuccess } = useRouteModal();
11487
- const onSubmit = form.handleSubmit(async (data) => {
11488
- await mutateAsync(
11489
- {
11490
- sales_channel_id: data.sales_channel_id
11491
- },
11492
- {
11493
- onSuccess: () => {
11494
- ui.toast.success("Sales channel updated");
11495
- handleSuccess();
11496
- },
11497
- onError: (error) => {
11498
- ui.toast.error(error.message);
11499
- }
11500
- }
11501
- );
11502
- });
11503
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11504
- KeyboundForm,
11505
- {
11506
- className: "flex flex-1 flex-col overflow-hidden",
11507
- onSubmit,
11508
- children: [
11509
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11510
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11511
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11512
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11513
- ] }) })
11514
- ]
11515
- }
11516
- ) });
11517
- };
11518
- const SalesChannelField = ({ control, order }) => {
11519
- const salesChannels = useComboboxData({
11520
- queryFn: async (params) => {
11521
- return await sdk.admin.salesChannel.list(params);
11522
- },
11523
- queryKey: ["sales-channels"],
11524
- getOptions: (data) => {
11525
- return data.sales_channels.map((salesChannel) => ({
11526
- label: salesChannel.name,
11527
- value: salesChannel.id
11528
- }));
11529
- },
11530
- defaultValue: order.sales_channel_id || void 0
11531
- });
11532
- return /* @__PURE__ */ jsxRuntime.jsx(
11533
- Form$2.Field,
11534
- {
11535
- control,
11536
- name: "sales_channel_id",
11537
- render: ({ field }) => {
11538
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11539
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11540
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11541
- Combobox,
11542
- {
11543
- options: salesChannels.options,
11544
- fetchNextPage: salesChannels.fetchNextPage,
11545
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11546
- searchValue: salesChannels.searchValue,
11547
- onSearchValueChange: salesChannels.onSearchValueChange,
11548
- placeholder: "Select sales channel",
11549
- ...field
11550
- }
11551
- ) }),
11552
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11553
- ] });
11554
- }
11555
- }
11556
- );
11557
- };
11558
- const schema$2 = objectType({
11559
- sales_channel_id: stringType().min(1)
11560
- });
11561
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11562
- const Shipping = () => {
11563
- var _a;
11564
- const { id } = reactRouterDom.useParams();
11565
- const { order, isPending, isError, error } = useOrder(id, {
11566
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11567
- });
11568
- const {
11569
- order: preview,
11570
- isPending: isPreviewPending,
11571
- isError: isPreviewError,
11572
- error: previewError
11573
- } = useOrderPreview(id);
11574
- useInitiateOrderEdit({ preview });
11575
- const { onCancel } = useCancelOrderEdit({ preview });
11576
- if (isError) {
11577
- throw error;
11578
- }
11579
- if (isPreviewError) {
11580
- throw previewError;
11581
- }
11582
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11583
- const isReady = preview && !isPreviewPending && order && !isPending;
11584
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11585
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11586
- /* @__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: [
11587
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11588
- /* @__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." }) })
11589
- ] }) }) }),
11590
- /* @__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" }) }) })
11591
- ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11592
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11593
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11594
- ] }) });
11595
- };
11596
- const ShippingForm = ({ preview, order }) => {
11597
- var _a;
11598
- const { setIsOpen } = useStackedModal();
11599
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11600
- const [data, setData] = React.useState(null);
11601
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11602
- const { shipping_options } = useShippingOptions(
11603
- {
11604
- id: appliedShippingOptionIds,
11605
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11606
- },
11607
- {
11608
- enabled: appliedShippingOptionIds.length > 0
11331
+ enabled: appliedShippingOptionIds.length > 0
11609
11332
  }
11610
11333
  );
11611
11334
  const uniqueShippingProfiles = React.useMemo(() => {
@@ -12964,86 +12687,363 @@ const Illustration = () => {
12964
12687
  /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12965
12688
  "path",
12966
12689
  {
12967
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12968
- stroke: "#A1A1AA",
12969
- strokeWidth: "1.5",
12970
- strokeLinecap: "round",
12971
- 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
12972
12913
  }
12973
- ) }),
12974
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12975
- "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,
12976
12983
  {
12977
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12978
- stroke: "#A1A1AA",
12979
- strokeWidth: "1.5",
12980
- strokeLinecap: "round",
12981
- 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, {})
12982
12990
  }
12983
- ) }),
12984
- /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12985
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12986
- "rect",
12987
- {
12988
- width: "12",
12989
- height: "12",
12990
- fill: "white",
12991
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12992
- }
12993
- ) }),
12994
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12995
- "rect",
12996
- {
12997
- width: "12",
12998
- height: "12",
12999
- fill: "white",
13000
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
13001
- }
13002
- ) }),
13003
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13004
- "rect",
13005
- {
13006
- width: "12",
13007
- height: "12",
13008
- fill: "white",
13009
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
13010
- }
13011
- ) }),
13012
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13013
- "rect",
13014
- {
13015
- width: "12",
13016
- height: "12",
13017
- fill: "white",
13018
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
13019
- }
13020
- ) }),
13021
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13022
- "rect",
13023
- {
13024
- width: "12",
13025
- height: "12",
13026
- fill: "white",
13027
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
13028
- }
13029
- ) }),
13030
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13031
- "rect",
13032
- {
13033
- width: "12",
13034
- height: "12",
13035
- fill: "white",
13036
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
13037
- }
13038
- ) })
13039
- ] })
12991
+ )
13040
12992
  ]
13041
- }
12993
+ },
12994
+ promotion.id
13042
12995
  );
13043
12996
  };
13044
- const schema = objectType({
13045
- 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
13046
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
+ }
13047
13047
  const widgetModule = { widgets: [] };
13048
13048
  const routeModule = {
13049
13049
  routes: [
@@ -13072,6 +13072,10 @@ const routeModule = {
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,14 +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
- {
13088
- Component: Email,
13089
- path: "/draft-orders/:id/email"
13090
- },
13091
13087
  {
13092
13088
  Component: SalesChannel,
13093
13089
  path: "/draft-orders/:id/sales-channel"
@@ -13103,6 +13099,10 @@ const routeModule = {
13103
13099
  {
13104
13100
  Component: TransferOwnership,
13105
13101
  path: "/draft-orders/:id/transfer-ownership"
13102
+ },
13103
+ {
13104
+ Component: Promotions,
13105
+ path: "/draft-orders/:id/promotions"
13106
13106
  }
13107
13107
  ]
13108
13108
  }