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

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -11176,477 +11176,61 @@ function getHasUneditableRows(metadata) {
11176
11176
  (value) => !EDITABLE_TYPES.includes(typeof value)
11177
11177
  );
11178
11178
  }
11179
- const PROMOTION_QUERY_KEY = "promotions";
11180
- const promotionsQueryKeys = {
11181
- list: (query2) => [
11182
- PROMOTION_QUERY_KEY,
11183
- query2 ? query2 : void 0
11184
- ],
11185
- detail: (id, query2) => [
11186
- PROMOTION_QUERY_KEY,
11187
- id,
11188
- query2 ? query2 : void 0
11189
- ]
11190
- };
11191
- const usePromotions = (query2, options) => {
11192
- const { data, ...rest } = reactQuery.useQuery({
11193
- queryKey: promotionsQueryKeys.list(query2),
11194
- queryFn: async () => sdk.admin.promotion.list(query2),
11195
- ...options
11196
- });
11197
- return { ...data, ...rest };
11198
- };
11199
- const Promotions = () => {
11200
- const { id } = reactRouterDom.useParams();
11201
- const {
11202
- order: preview,
11203
- isError: isPreviewError,
11204
- error: previewError
11205
- } = useOrderPreview(id, void 0);
11206
- useInitiateOrderEdit({ preview });
11207
- const { onCancel } = useCancelOrderEdit({ preview });
11208
- if (isPreviewError) {
11209
- throw previewError;
11210
- }
11211
- const isReady = !!preview;
11212
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11213
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11214
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11215
- ] });
11216
- };
11217
- const PromotionForm = ({ preview }) => {
11218
- const { items, shipping_methods } = preview;
11219
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11220
- const [comboboxValue, setComboboxValue] = React.useState("");
11221
- const { handleSuccess } = useRouteModal();
11222
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11223
- const promoIds = getPromotionIds(items, shipping_methods);
11224
- const { promotions, isPending, isError, error } = usePromotions(
11225
- {
11226
- id: promoIds
11227
- },
11228
- {
11229
- enabled: !!promoIds.length
11230
- }
11231
- );
11232
- const comboboxData = useComboboxData({
11233
- queryKey: ["promotions", "combobox", promoIds],
11234
- queryFn: async (params) => {
11235
- return await sdk.admin.promotion.list({
11236
- ...params,
11237
- id: {
11238
- $nin: promoIds
11239
- }
11240
- });
11241
- },
11242
- getOptions: (data) => {
11243
- return data.promotions.map((promotion) => ({
11244
- label: promotion.code,
11245
- value: promotion.code
11246
- }));
11247
- }
11248
- });
11249
- const add = async (value) => {
11250
- if (!value) {
11251
- return;
11252
- }
11253
- addPromotions(
11254
- {
11255
- promo_codes: [value]
11256
- },
11257
- {
11258
- onError: (e) => {
11259
- ui.toast.error(e.message);
11260
- comboboxData.onSearchValueChange("");
11261
- setComboboxValue("");
11262
- },
11263
- onSuccess: () => {
11264
- comboboxData.onSearchValueChange("");
11265
- setComboboxValue("");
11266
- }
11267
- }
11268
- );
11269
- };
11270
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11271
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11272
- const onSubmit = async () => {
11273
- setIsSubmitting(true);
11274
- let requestSucceeded = false;
11275
- await requestOrderEdit(void 0, {
11276
- onError: (e) => {
11277
- ui.toast.error(e.message);
11278
- },
11279
- onSuccess: () => {
11280
- requestSucceeded = true;
11281
- }
11282
- });
11283
- if (!requestSucceeded) {
11284
- setIsSubmitting(false);
11285
- return;
11286
- }
11287
- await confirmOrderEdit(void 0, {
11288
- onError: (e) => {
11289
- ui.toast.error(e.message);
11290
- },
11291
- onSuccess: () => {
11292
- handleSuccess();
11293
- },
11294
- onSettled: () => {
11295
- setIsSubmitting(false);
11296
- }
11297
- });
11298
- };
11299
- if (isError) {
11300
- throw error;
11301
- }
11302
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11303
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11304
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11305
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11306
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11307
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11308
- ] }),
11309
- /* @__PURE__ */ jsxRuntime.jsx(
11310
- Combobox,
11311
- {
11312
- id: "promotion-combobox",
11313
- "aria-describedby": "promotion-combobox-hint",
11314
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11315
- fetchNextPage: comboboxData.fetchNextPage,
11316
- options: comboboxData.options,
11317
- onSearchValueChange: comboboxData.onSearchValueChange,
11318
- searchValue: comboboxData.searchValue,
11319
- disabled: comboboxData.disabled || isAddingPromotions,
11320
- onChange: add,
11321
- value: comboboxValue
11322
- }
11323
- )
11324
- ] }),
11325
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11326
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11327
- PromotionItem,
11328
- {
11329
- promotion,
11330
- orderId: preview.id,
11331
- isLoading: isPending
11332
- },
11333
- promotion.id
11334
- )) })
11335
- ] }) }),
11336
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11337
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11338
- /* @__PURE__ */ jsxRuntime.jsx(
11339
- ui.Button,
11340
- {
11341
- size: "small",
11342
- type: "submit",
11343
- isLoading: isSubmitting || isAddingPromotions,
11344
- children: "Save"
11345
- }
11346
- )
11347
- ] }) })
11348
- ] });
11349
- };
11350
- const PromotionItem = ({
11351
- promotion,
11352
- orderId,
11353
- isLoading
11354
- }) => {
11355
- var _a;
11356
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11357
- const onRemove = async () => {
11358
- removePromotions(
11359
- {
11360
- promo_codes: [promotion.code]
11361
- },
11362
- {
11363
- onError: (e) => {
11364
- ui.toast.error(e.message);
11365
- }
11366
- }
11367
- );
11368
- };
11369
- const displayValue = getDisplayValue(promotion);
11370
- return /* @__PURE__ */ jsxRuntime.jsxs(
11371
- "div",
11372
- {
11373
- className: ui.clx(
11374
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11375
- {
11376
- "animate-pulse": isLoading
11377
- }
11378
- ),
11379
- children: [
11380
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11381
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11382
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11383
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11384
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11385
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11386
- ] }),
11387
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11388
- ] })
11389
- ] }),
11390
- /* @__PURE__ */ jsxRuntime.jsx(
11391
- ui.IconButton,
11392
- {
11393
- size: "small",
11394
- type: "button",
11395
- variant: "transparent",
11396
- onClick: onRemove,
11397
- isLoading: isPending || isLoading,
11398
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11399
- }
11400
- )
11401
- ]
11402
- },
11403
- promotion.id
11404
- );
11405
- };
11406
- function getDisplayValue(promotion) {
11407
- var _a, _b, _c, _d;
11408
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11409
- if (!value) {
11410
- return null;
11411
- }
11412
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11413
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11414
- if (!currency) {
11415
- return null;
11416
- }
11417
- return getLocaleAmount(value, currency);
11418
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11419
- return formatPercentage(value);
11420
- }
11421
- return null;
11422
- }
11423
- const formatter = new Intl.NumberFormat([], {
11424
- style: "percent",
11425
- minimumFractionDigits: 2
11426
- });
11427
- const formatPercentage = (value, isPercentageValue = false) => {
11428
- let val = value || 0;
11429
- if (!isPercentageValue) {
11430
- val = val / 100;
11431
- }
11432
- return formatter.format(val);
11433
- };
11434
- function getPromotionIds(items, shippingMethods) {
11435
- const promotionIds = /* @__PURE__ */ new Set();
11436
- for (const item of items) {
11437
- if (item.adjustments) {
11438
- for (const adjustment of item.adjustments) {
11439
- if (adjustment.promotion_id) {
11440
- promotionIds.add(adjustment.promotion_id);
11441
- }
11442
- }
11443
- }
11444
- }
11445
- for (const shippingMethod of shippingMethods) {
11446
- if (shippingMethod.adjustments) {
11447
- for (const adjustment of shippingMethod.adjustments) {
11448
- if (adjustment.promotion_id) {
11449
- promotionIds.add(adjustment.promotion_id);
11450
- }
11451
- }
11452
- }
11453
- }
11454
- return Array.from(promotionIds);
11455
- }
11456
- const ShippingAddress = () => {
11457
- const { id } = reactRouterDom.useParams();
11458
- const { order, isPending, isError, error } = useOrder(id, {
11459
- fields: "+shipping_address"
11460
- });
11461
- if (isError) {
11462
- throw error;
11463
- }
11464
- const isReady = !isPending && !!order;
11465
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11466
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11467
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Shipping Address" }) }),
11468
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11469
- ] }),
11470
- isReady && /* @__PURE__ */ jsxRuntime.jsx(ShippingAddressForm, { order })
11471
- ] });
11472
- };
11473
- const ShippingAddressForm = ({ order }) => {
11474
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11475
- const form = reactHookForm.useForm({
11476
- defaultValues: {
11477
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11478
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11479
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11480
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11481
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11482
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11483
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11484
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11485
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11486
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11487
- },
11488
- resolver: zod.zodResolver(schema$2)
11489
- });
11490
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11491
- const { handleSuccess } = useRouteModal();
11492
- const onSubmit = form.handleSubmit(async (data) => {
11493
- await mutateAsync(
11494
- {
11495
- shipping_address: {
11496
- first_name: data.first_name,
11497
- last_name: data.last_name,
11498
- company: data.company,
11499
- address_1: data.address_1,
11500
- address_2: data.address_2,
11501
- city: data.city,
11502
- province: data.province,
11503
- country_code: data.country_code,
11504
- postal_code: data.postal_code,
11505
- phone: data.phone
11506
- }
11507
- },
11508
- {
11509
- onSuccess: () => {
11510
- handleSuccess();
11511
- },
11512
- onError: (error) => {
11513
- ui.toast.error(error.message);
11514
- }
11515
- }
11516
- );
11517
- });
11518
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11519
- KeyboundForm,
11520
- {
11521
- className: "flex flex-1 flex-col overflow-hidden",
11522
- onSubmit,
11523
- children: [
11524
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
11525
- /* @__PURE__ */ jsxRuntime.jsx(
11526
- Form$2.Field,
11527
- {
11528
- control: form.control,
11529
- name: "country_code",
11530
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11531
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
11532
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
11533
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11534
- ] })
11535
- }
11536
- ),
11537
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11538
- /* @__PURE__ */ jsxRuntime.jsx(
11539
- Form$2.Field,
11540
- {
11541
- control: form.control,
11542
- name: "first_name",
11543
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11544
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
11545
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11546
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11547
- ] })
11548
- }
11549
- ),
11550
- /* @__PURE__ */ jsxRuntime.jsx(
11551
- Form$2.Field,
11552
- {
11553
- control: form.control,
11554
- name: "last_name",
11555
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11556
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
11557
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11558
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11559
- ] })
11560
- }
11561
- )
11562
- ] }),
11563
- /* @__PURE__ */ jsxRuntime.jsx(
11564
- Form$2.Field,
11565
- {
11566
- control: form.control,
11567
- name: "company",
11568
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11569
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
11570
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11571
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11572
- ] })
11573
- }
11574
- ),
11575
- /* @__PURE__ */ jsxRuntime.jsx(
11576
- Form$2.Field,
11577
- {
11578
- control: form.control,
11579
- name: "address_1",
11580
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11581
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
11582
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11583
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11584
- ] })
11585
- }
11586
- ),
11587
- /* @__PURE__ */ jsxRuntime.jsx(
11588
- Form$2.Field,
11589
- {
11590
- control: form.control,
11591
- name: "address_2",
11592
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11593
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11594
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11595
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11596
- ] })
11597
- }
11598
- ),
11599
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11600
- /* @__PURE__ */ jsxRuntime.jsx(
11601
- Form$2.Field,
11602
- {
11603
- control: form.control,
11604
- name: "postal_code",
11605
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11606
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
11607
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11608
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11609
- ] })
11610
- }
11611
- ),
11612
- /* @__PURE__ */ jsxRuntime.jsx(
11613
- Form$2.Field,
11614
- {
11615
- control: form.control,
11616
- name: "city",
11617
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11618
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
11619
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11620
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11621
- ] })
11622
- }
11623
- )
11624
- ] }),
11625
- /* @__PURE__ */ jsxRuntime.jsx(
11626
- Form$2.Field,
11627
- {
11628
- control: form.control,
11629
- name: "province",
11630
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11631
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11632
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11633
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11634
- ] })
11635
- }
11636
- ),
11637
- /* @__PURE__ */ jsxRuntime.jsx(
11638
- Form$2.Field,
11639
- {
11640
- control: form.control,
11641
- name: "phone",
11642
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11643
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
11644
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11645
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11646
- ] })
11647
- }
11648
- )
11649
- ] }) }),
11179
+ const SalesChannel = () => {
11180
+ const { id } = reactRouterDom.useParams();
11181
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11182
+ id,
11183
+ {
11184
+ fields: "+sales_channel_id"
11185
+ },
11186
+ {
11187
+ enabled: !!id
11188
+ }
11189
+ );
11190
+ if (isError) {
11191
+ throw error;
11192
+ }
11193
+ const ISrEADY = !!draft_order && !isPending;
11194
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11195
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11196
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11197
+ /* @__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" }) })
11198
+ ] }),
11199
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11200
+ ] });
11201
+ };
11202
+ const SalesChannelForm = ({ order }) => {
11203
+ const form = reactHookForm.useForm({
11204
+ defaultValues: {
11205
+ sales_channel_id: order.sales_channel_id || ""
11206
+ },
11207
+ resolver: zod.zodResolver(schema$2)
11208
+ });
11209
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11210
+ const { handleSuccess } = useRouteModal();
11211
+ const onSubmit = form.handleSubmit(async (data) => {
11212
+ await mutateAsync(
11213
+ {
11214
+ sales_channel_id: data.sales_channel_id
11215
+ },
11216
+ {
11217
+ onSuccess: () => {
11218
+ ui.toast.success("Sales channel updated");
11219
+ handleSuccess();
11220
+ },
11221
+ onError: (error) => {
11222
+ ui.toast.error(error.message);
11223
+ }
11224
+ }
11225
+ );
11226
+ });
11227
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11228
+ KeyboundForm,
11229
+ {
11230
+ className: "flex flex-1 flex-col overflow-hidden",
11231
+ onSubmit,
11232
+ children: [
11233
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11650
11234
  /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11651
11235
  /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11652
11236
  /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
@@ -11655,7 +11239,49 @@ const ShippingAddressForm = ({ order }) => {
11655
11239
  }
11656
11240
  ) });
11657
11241
  };
11658
- const schema$2 = addressSchema;
11242
+ const SalesChannelField = ({ control, order }) => {
11243
+ const salesChannels = useComboboxData({
11244
+ queryFn: async (params) => {
11245
+ return await sdk.admin.salesChannel.list(params);
11246
+ },
11247
+ queryKey: ["sales-channels"],
11248
+ getOptions: (data) => {
11249
+ return data.sales_channels.map((salesChannel) => ({
11250
+ label: salesChannel.name,
11251
+ value: salesChannel.id
11252
+ }));
11253
+ },
11254
+ defaultValue: order.sales_channel_id || void 0
11255
+ });
11256
+ return /* @__PURE__ */ jsxRuntime.jsx(
11257
+ Form$2.Field,
11258
+ {
11259
+ control,
11260
+ name: "sales_channel_id",
11261
+ render: ({ field }) => {
11262
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11263
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11264
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11265
+ Combobox,
11266
+ {
11267
+ options: salesChannels.options,
11268
+ fetchNextPage: salesChannels.fetchNextPage,
11269
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11270
+ searchValue: salesChannels.searchValue,
11271
+ onSearchValueChange: salesChannels.onSearchValueChange,
11272
+ placeholder: "Select sales channel",
11273
+ ...field
11274
+ }
11275
+ ) }),
11276
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11277
+ ] });
11278
+ }
11279
+ }
11280
+ );
11281
+ };
11282
+ const schema$2 = objectType({
11283
+ sales_channel_id: stringType().min(1)
11284
+ });
11659
11285
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
11660
11286
  const Shipping = () => {
11661
11287
  var _a;
@@ -12410,59 +12036,262 @@ const ShippingOptionField = ({
12410
12036
  /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Choose the shipping option to use." })
12411
12037
  ] }),
12412
12038
  /* @__PURE__ */ jsxRuntime.jsx(
12413
- ConditionalTooltip,
12039
+ ConditionalTooltip,
12040
+ {
12041
+ content: tooltipContent,
12042
+ showTooltip: !locationId || !shippingProfileId,
12043
+ children: /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12044
+ Combobox,
12045
+ {
12046
+ options: shippingOptions.options,
12047
+ fetchNextPage: shippingOptions.fetchNextPage,
12048
+ isFetchingNextPage: shippingOptions.isFetchingNextPage,
12049
+ searchValue: shippingOptions.searchValue,
12050
+ onSearchValueChange: shippingOptions.onSearchValueChange,
12051
+ placeholder: "Select shipping option",
12052
+ ...field,
12053
+ disabled: !locationId || !shippingProfileId
12054
+ }
12055
+ ) }) })
12056
+ }
12057
+ )
12058
+ ] }) });
12059
+ }
12060
+ }
12061
+ );
12062
+ };
12063
+ const CustomAmountField = ({
12064
+ control,
12065
+ currencyCode
12066
+ }) => {
12067
+ return /* @__PURE__ */ jsxRuntime.jsx(
12068
+ Form$2.Field,
12069
+ {
12070
+ control,
12071
+ name: "custom_amount",
12072
+ render: ({ field: { onChange, ...field } }) => {
12073
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12074
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12075
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12076
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12077
+ ] }),
12078
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12079
+ ui.CurrencyInput,
12080
+ {
12081
+ ...field,
12082
+ onValueChange: (value) => onChange(value),
12083
+ symbol: getNativeSymbol(currencyCode),
12084
+ code: currencyCode
12085
+ }
12086
+ ) })
12087
+ ] });
12088
+ }
12089
+ }
12090
+ );
12091
+ };
12092
+ const ShippingAddress = () => {
12093
+ const { id } = reactRouterDom.useParams();
12094
+ const { order, isPending, isError, error } = useOrder(id, {
12095
+ fields: "+shipping_address"
12096
+ });
12097
+ if (isError) {
12098
+ throw error;
12099
+ }
12100
+ const isReady = !isPending && !!order;
12101
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12102
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12103
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Shipping Address" }) }),
12104
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12105
+ ] }),
12106
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(ShippingAddressForm, { order })
12107
+ ] });
12108
+ };
12109
+ const ShippingAddressForm = ({ order }) => {
12110
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12111
+ const form = reactHookForm.useForm({
12112
+ defaultValues: {
12113
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12114
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12115
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12116
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12117
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12118
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12119
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12120
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12121
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12122
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12123
+ },
12124
+ resolver: zod.zodResolver(schema$1)
12125
+ });
12126
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12127
+ const { handleSuccess } = useRouteModal();
12128
+ const onSubmit = form.handleSubmit(async (data) => {
12129
+ await mutateAsync(
12130
+ {
12131
+ shipping_address: {
12132
+ first_name: data.first_name,
12133
+ last_name: data.last_name,
12134
+ company: data.company,
12135
+ address_1: data.address_1,
12136
+ address_2: data.address_2,
12137
+ city: data.city,
12138
+ province: data.province,
12139
+ country_code: data.country_code,
12140
+ postal_code: data.postal_code,
12141
+ phone: data.phone
12142
+ }
12143
+ },
12144
+ {
12145
+ onSuccess: () => {
12146
+ handleSuccess();
12147
+ },
12148
+ onError: (error) => {
12149
+ ui.toast.error(error.message);
12150
+ }
12151
+ }
12152
+ );
12153
+ });
12154
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12155
+ KeyboundForm,
12156
+ {
12157
+ className: "flex flex-1 flex-col overflow-hidden",
12158
+ onSubmit,
12159
+ children: [
12160
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
12161
+ /* @__PURE__ */ jsxRuntime.jsx(
12162
+ Form$2.Field,
12163
+ {
12164
+ control: form.control,
12165
+ name: "country_code",
12166
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12167
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
12168
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
12169
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12170
+ ] })
12171
+ }
12172
+ ),
12173
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12174
+ /* @__PURE__ */ jsxRuntime.jsx(
12175
+ Form$2.Field,
12176
+ {
12177
+ control: form.control,
12178
+ name: "first_name",
12179
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12180
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
12181
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12182
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12183
+ ] })
12184
+ }
12185
+ ),
12186
+ /* @__PURE__ */ jsxRuntime.jsx(
12187
+ Form$2.Field,
12188
+ {
12189
+ control: form.control,
12190
+ name: "last_name",
12191
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12192
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
12193
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12194
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12195
+ ] })
12196
+ }
12197
+ )
12198
+ ] }),
12199
+ /* @__PURE__ */ jsxRuntime.jsx(
12200
+ Form$2.Field,
12414
12201
  {
12415
- content: tooltipContent,
12416
- showTooltip: !locationId || !shippingProfileId,
12417
- children: /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12418
- Combobox,
12419
- {
12420
- options: shippingOptions.options,
12421
- fetchNextPage: shippingOptions.fetchNextPage,
12422
- isFetchingNextPage: shippingOptions.isFetchingNextPage,
12423
- searchValue: shippingOptions.searchValue,
12424
- onSearchValueChange: shippingOptions.onSearchValueChange,
12425
- placeholder: "Select shipping option",
12426
- ...field,
12427
- disabled: !locationId || !shippingProfileId
12428
- }
12429
- ) }) })
12202
+ control: form.control,
12203
+ name: "company",
12204
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12205
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
12206
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12207
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12208
+ ] })
12430
12209
  }
12431
- )
12432
- ] }) });
12433
- }
12434
- }
12435
- );
12436
- };
12437
- const CustomAmountField = ({
12438
- control,
12439
- currencyCode
12440
- }) => {
12441
- return /* @__PURE__ */ jsxRuntime.jsx(
12442
- Form$2.Field,
12443
- {
12444
- control,
12445
- name: "custom_amount",
12446
- render: ({ field: { onChange, ...field } }) => {
12447
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12448
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12449
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12450
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12210
+ ),
12211
+ /* @__PURE__ */ jsxRuntime.jsx(
12212
+ Form$2.Field,
12213
+ {
12214
+ control: form.control,
12215
+ name: "address_1",
12216
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12217
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
12218
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12219
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12220
+ ] })
12221
+ }
12222
+ ),
12223
+ /* @__PURE__ */ jsxRuntime.jsx(
12224
+ Form$2.Field,
12225
+ {
12226
+ control: form.control,
12227
+ name: "address_2",
12228
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12229
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12230
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12231
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12232
+ ] })
12233
+ }
12234
+ ),
12235
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12236
+ /* @__PURE__ */ jsxRuntime.jsx(
12237
+ Form$2.Field,
12238
+ {
12239
+ control: form.control,
12240
+ name: "postal_code",
12241
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12242
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
12243
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12244
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12245
+ ] })
12246
+ }
12247
+ ),
12248
+ /* @__PURE__ */ jsxRuntime.jsx(
12249
+ Form$2.Field,
12250
+ {
12251
+ control: form.control,
12252
+ name: "city",
12253
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12254
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
12255
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12256
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12257
+ ] })
12258
+ }
12259
+ )
12451
12260
  ] }),
12452
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12453
- ui.CurrencyInput,
12261
+ /* @__PURE__ */ jsxRuntime.jsx(
12262
+ Form$2.Field,
12454
12263
  {
12455
- ...field,
12456
- onValueChange: (value) => onChange(value),
12457
- symbol: getNativeSymbol(currencyCode),
12458
- code: currencyCode
12264
+ control: form.control,
12265
+ name: "province",
12266
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12267
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12268
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12269
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12270
+ ] })
12459
12271
  }
12460
- ) })
12461
- ] });
12462
- }
12272
+ ),
12273
+ /* @__PURE__ */ jsxRuntime.jsx(
12274
+ Form$2.Field,
12275
+ {
12276
+ control: form.control,
12277
+ name: "phone",
12278
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12279
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
12280
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12281
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12282
+ ] })
12283
+ }
12284
+ )
12285
+ ] }) }),
12286
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12287
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12288
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12289
+ ] }) })
12290
+ ]
12463
12291
  }
12464
- );
12292
+ ) });
12465
12293
  };
12294
+ const schema$1 = addressSchema;
12466
12295
  const TransferOwnership = () => {
12467
12296
  const { id } = reactRouterDom.useParams();
12468
12297
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12486,7 +12315,7 @@ const TransferOwnershipForm = ({ order }) => {
12486
12315
  defaultValues: {
12487
12316
  customer_id: order.customer_id || ""
12488
12317
  },
12489
- resolver: zod.zodResolver(schema$1)
12318
+ resolver: zod.zodResolver(schema)
12490
12319
  });
12491
12320
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12492
12321
  const { handleSuccess } = useRouteModal();
@@ -12936,115 +12765,286 @@ const Illustration = () => {
12936
12765
  }
12937
12766
  );
12938
12767
  };
12939
- const schema$1 = objectType({
12768
+ const schema = objectType({
12940
12769
  customer_id: stringType().min(1)
12941
12770
  });
12942
- const SalesChannel = () => {
12943
- const { id } = reactRouterDom.useParams();
12944
- const { draft_order, isPending, isError, error } = useDraftOrder(
12771
+ const PROMOTION_QUERY_KEY = "promotions";
12772
+ const promotionsQueryKeys = {
12773
+ list: (query2) => [
12774
+ PROMOTION_QUERY_KEY,
12775
+ query2 ? query2 : void 0
12776
+ ],
12777
+ detail: (id, query2) => [
12778
+ PROMOTION_QUERY_KEY,
12945
12779
  id,
12780
+ query2 ? query2 : void 0
12781
+ ]
12782
+ };
12783
+ const usePromotions = (query2, options) => {
12784
+ const { data, ...rest } = reactQuery.useQuery({
12785
+ queryKey: promotionsQueryKeys.list(query2),
12786
+ queryFn: async () => sdk.admin.promotion.list(query2),
12787
+ ...options
12788
+ });
12789
+ return { ...data, ...rest };
12790
+ };
12791
+ const Promotions = () => {
12792
+ const { id } = reactRouterDom.useParams();
12793
+ const {
12794
+ order: preview,
12795
+ isError: isPreviewError,
12796
+ error: previewError
12797
+ } = useOrderPreview(id, void 0);
12798
+ useInitiateOrderEdit({ preview });
12799
+ const { onCancel } = useCancelOrderEdit({ preview });
12800
+ if (isPreviewError) {
12801
+ throw previewError;
12802
+ }
12803
+ const isReady = !!preview;
12804
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
12805
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
12806
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
12807
+ ] });
12808
+ };
12809
+ const PromotionForm = ({ preview }) => {
12810
+ const { items, shipping_methods } = preview;
12811
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
12812
+ const [comboboxValue, setComboboxValue] = React.useState("");
12813
+ const { handleSuccess } = useRouteModal();
12814
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
12815
+ const promoIds = getPromotionIds(items, shipping_methods);
12816
+ const { promotions, isPending, isError, error } = usePromotions(
12946
12817
  {
12947
- fields: "+sales_channel_id"
12818
+ id: promoIds
12948
12819
  },
12949
12820
  {
12950
- enabled: !!id
12821
+ enabled: !!promoIds.length
12822
+ }
12823
+ );
12824
+ const comboboxData = useComboboxData({
12825
+ queryKey: ["promotions", "combobox", promoIds],
12826
+ queryFn: async (params) => {
12827
+ return await sdk.admin.promotion.list({
12828
+ ...params,
12829
+ id: {
12830
+ $nin: promoIds
12831
+ }
12832
+ });
12833
+ },
12834
+ getOptions: (data) => {
12835
+ return data.promotions.map((promotion) => ({
12836
+ label: promotion.code,
12837
+ value: promotion.code
12838
+ }));
12839
+ }
12840
+ });
12841
+ const add = async (value) => {
12842
+ if (!value) {
12843
+ return;
12844
+ }
12845
+ addPromotions(
12846
+ {
12847
+ promo_codes: [value]
12848
+ },
12849
+ {
12850
+ onError: (e) => {
12851
+ ui.toast.error(e.message);
12852
+ comboboxData.onSearchValueChange("");
12853
+ setComboboxValue("");
12854
+ },
12855
+ onSuccess: () => {
12856
+ comboboxData.onSearchValueChange("");
12857
+ setComboboxValue("");
12858
+ }
12859
+ }
12860
+ );
12861
+ };
12862
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12863
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
12864
+ const onSubmit = async () => {
12865
+ setIsSubmitting(true);
12866
+ let requestSucceeded = false;
12867
+ await requestOrderEdit(void 0, {
12868
+ onError: (e) => {
12869
+ ui.toast.error(e.message);
12870
+ },
12871
+ onSuccess: () => {
12872
+ requestSucceeded = true;
12873
+ }
12874
+ });
12875
+ if (!requestSucceeded) {
12876
+ setIsSubmitting(false);
12877
+ return;
12951
12878
  }
12952
- );
12879
+ await confirmOrderEdit(void 0, {
12880
+ onError: (e) => {
12881
+ ui.toast.error(e.message);
12882
+ },
12883
+ onSuccess: () => {
12884
+ handleSuccess();
12885
+ },
12886
+ onSettled: () => {
12887
+ setIsSubmitting(false);
12888
+ }
12889
+ });
12890
+ };
12953
12891
  if (isError) {
12954
12892
  throw error;
12955
12893
  }
12956
- const ISrEADY = !!draft_order && !isPending;
12957
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12958
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12959
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
12960
- /* @__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" }) })
12961
- ] }),
12962
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
12894
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
12895
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
12896
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
12897
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12898
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
12899
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
12900
+ ] }),
12901
+ /* @__PURE__ */ jsxRuntime.jsx(
12902
+ Combobox,
12903
+ {
12904
+ id: "promotion-combobox",
12905
+ "aria-describedby": "promotion-combobox-hint",
12906
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
12907
+ fetchNextPage: comboboxData.fetchNextPage,
12908
+ options: comboboxData.options,
12909
+ onSearchValueChange: comboboxData.onSearchValueChange,
12910
+ searchValue: comboboxData.searchValue,
12911
+ disabled: comboboxData.disabled || isAddingPromotions,
12912
+ onChange: add,
12913
+ value: comboboxValue
12914
+ }
12915
+ )
12916
+ ] }),
12917
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12918
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
12919
+ PromotionItem,
12920
+ {
12921
+ promotion,
12922
+ orderId: preview.id,
12923
+ isLoading: isPending
12924
+ },
12925
+ promotion.id
12926
+ )) })
12927
+ ] }) }),
12928
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12929
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12930
+ /* @__PURE__ */ jsxRuntime.jsx(
12931
+ ui.Button,
12932
+ {
12933
+ size: "small",
12934
+ type: "submit",
12935
+ isLoading: isSubmitting || isAddingPromotions,
12936
+ children: "Save"
12937
+ }
12938
+ )
12939
+ ] }) })
12963
12940
  ] });
12964
12941
  };
12965
- const SalesChannelForm = ({ order }) => {
12966
- const form = reactHookForm.useForm({
12967
- defaultValues: {
12968
- sales_channel_id: order.sales_channel_id || ""
12969
- },
12970
- resolver: zod.zodResolver(schema)
12971
- });
12972
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12973
- const { handleSuccess } = useRouteModal();
12974
- const onSubmit = form.handleSubmit(async (data) => {
12975
- await mutateAsync(
12942
+ const PromotionItem = ({
12943
+ promotion,
12944
+ orderId,
12945
+ isLoading
12946
+ }) => {
12947
+ var _a;
12948
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
12949
+ const onRemove = async () => {
12950
+ removePromotions(
12976
12951
  {
12977
- sales_channel_id: data.sales_channel_id
12952
+ promo_codes: [promotion.code]
12978
12953
  },
12979
12954
  {
12980
- onSuccess: () => {
12981
- ui.toast.success("Sales channel updated");
12982
- handleSuccess();
12983
- },
12984
- onError: (error) => {
12985
- ui.toast.error(error.message);
12955
+ onError: (e) => {
12956
+ ui.toast.error(e.message);
12986
12957
  }
12987
12958
  }
12988
12959
  );
12989
- });
12990
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12991
- KeyboundForm,
12960
+ };
12961
+ const displayValue = getDisplayValue(promotion);
12962
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12963
+ "div",
12992
12964
  {
12993
- className: "flex flex-1 flex-col overflow-hidden",
12994
- onSubmit,
12965
+ className: ui.clx(
12966
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
12967
+ {
12968
+ "animate-pulse": isLoading
12969
+ }
12970
+ ),
12995
12971
  children: [
12996
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
12997
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12998
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12999
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13000
- ] }) })
12972
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12973
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
12974
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
12975
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
12976
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
12977
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
12978
+ ] }),
12979
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
12980
+ ] })
12981
+ ] }),
12982
+ /* @__PURE__ */ jsxRuntime.jsx(
12983
+ ui.IconButton,
12984
+ {
12985
+ size: "small",
12986
+ type: "button",
12987
+ variant: "transparent",
12988
+ onClick: onRemove,
12989
+ isLoading: isPending || isLoading,
12990
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
12991
+ }
12992
+ )
13001
12993
  ]
13002
- }
13003
- ) });
13004
- };
13005
- const SalesChannelField = ({ control, order }) => {
13006
- const salesChannels = useComboboxData({
13007
- queryFn: async (params) => {
13008
- return await sdk.admin.salesChannel.list(params);
13009
- },
13010
- queryKey: ["sales-channels"],
13011
- getOptions: (data) => {
13012
- return data.sales_channels.map((salesChannel) => ({
13013
- label: salesChannel.name,
13014
- value: salesChannel.id
13015
- }));
13016
12994
  },
13017
- defaultValue: order.sales_channel_id || void 0
13018
- });
13019
- return /* @__PURE__ */ jsxRuntime.jsx(
13020
- Form$2.Field,
13021
- {
13022
- control,
13023
- name: "sales_channel_id",
13024
- render: ({ field }) => {
13025
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13026
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
13027
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
13028
- Combobox,
13029
- {
13030
- options: salesChannels.options,
13031
- fetchNextPage: salesChannels.fetchNextPage,
13032
- isFetchingNextPage: salesChannels.isFetchingNextPage,
13033
- searchValue: salesChannels.searchValue,
13034
- onSearchValueChange: salesChannels.onSearchValueChange,
13035
- placeholder: "Select sales channel",
13036
- ...field
13037
- }
13038
- ) }),
13039
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13040
- ] });
13041
- }
13042
- }
12995
+ promotion.id
13043
12996
  );
13044
12997
  };
13045
- const schema = objectType({
13046
- sales_channel_id: stringType().min(1)
12998
+ function getDisplayValue(promotion) {
12999
+ var _a, _b, _c, _d;
13000
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
13001
+ if (!value) {
13002
+ return null;
13003
+ }
13004
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
13005
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
13006
+ if (!currency) {
13007
+ return null;
13008
+ }
13009
+ return getLocaleAmount(value, currency);
13010
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
13011
+ return formatPercentage(value);
13012
+ }
13013
+ return null;
13014
+ }
13015
+ const formatter = new Intl.NumberFormat([], {
13016
+ style: "percent",
13017
+ minimumFractionDigits: 2
13047
13018
  });
13019
+ const formatPercentage = (value, isPercentageValue = false) => {
13020
+ let val = value || 0;
13021
+ if (!isPercentageValue) {
13022
+ val = val / 100;
13023
+ }
13024
+ return formatter.format(val);
13025
+ };
13026
+ function getPromotionIds(items, shippingMethods) {
13027
+ const promotionIds = /* @__PURE__ */ new Set();
13028
+ for (const item of items) {
13029
+ if (item.adjustments) {
13030
+ for (const adjustment of item.adjustments) {
13031
+ if (adjustment.promotion_id) {
13032
+ promotionIds.add(adjustment.promotion_id);
13033
+ }
13034
+ }
13035
+ }
13036
+ }
13037
+ for (const shippingMethod of shippingMethods) {
13038
+ if (shippingMethod.adjustments) {
13039
+ for (const adjustment of shippingMethod.adjustments) {
13040
+ if (adjustment.promotion_id) {
13041
+ promotionIds.add(adjustment.promotion_id);
13042
+ }
13043
+ }
13044
+ }
13045
+ }
13046
+ return Array.from(promotionIds);
13047
+ }
13048
13048
  const widgetModule = { widgets: [] };
13049
13049
  const routeModule = {
13050
13050
  routes: [
@@ -13086,24 +13086,24 @@ const routeModule = {
13086
13086
  path: "/draft-orders/:id/metadata"
13087
13087
  },
13088
13088
  {
13089
- Component: Promotions,
13090
- path: "/draft-orders/:id/promotions"
13091
- },
13092
- {
13093
- Component: ShippingAddress,
13094
- path: "/draft-orders/:id/shipping-address"
13089
+ Component: SalesChannel,
13090
+ path: "/draft-orders/:id/sales-channel"
13095
13091
  },
13096
13092
  {
13097
13093
  Component: Shipping,
13098
13094
  path: "/draft-orders/:id/shipping"
13099
13095
  },
13096
+ {
13097
+ Component: ShippingAddress,
13098
+ path: "/draft-orders/:id/shipping-address"
13099
+ },
13100
13100
  {
13101
13101
  Component: TransferOwnership,
13102
13102
  path: "/draft-orders/:id/transfer-ownership"
13103
13103
  },
13104
13104
  {
13105
- Component: SalesChannel,
13106
- path: "/draft-orders/:id/sales-channel"
13105
+ Component: Promotions,
13106
+ path: "/draft-orders/:id/promotions"
13107
13107
  }
13108
13108
  ]
13109
13109
  }