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