@medusajs/draft-order 2.11.0-preview-20251021000332 → 2.11.0-preview-20251021060207

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.
@@ -9565,27 +9565,196 @@ const ID = () => {
9565
9565
  /* @__PURE__ */ jsx(Outlet, {})
9566
9566
  ] });
9567
9567
  };
9568
- const CustomItems = () => {
9568
+ const BillingAddress = () => {
9569
+ const { id } = useParams();
9570
+ const { order, isPending, isError, error } = useOrder(id, {
9571
+ fields: "+billing_address"
9572
+ });
9573
+ if (isError) {
9574
+ throw error;
9575
+ }
9576
+ const isReady = !isPending && !!order;
9569
9577
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9570
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9571
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9578
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9579
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Billing Address" }) }),
9580
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
9581
+ ] }),
9582
+ isReady && /* @__PURE__ */ jsx(BillingAddressForm, { order })
9572
9583
  ] });
9573
9584
  };
9574
- const CustomItemsForm = () => {
9585
+ const BillingAddressForm = ({ order }) => {
9586
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
9575
9587
  const form = useForm({
9588
+ defaultValues: {
9589
+ first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
9590
+ last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
9591
+ company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
9592
+ address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
9593
+ address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
9594
+ city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
9595
+ province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
9596
+ country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
9597
+ postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9598
+ phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9599
+ },
9576
9600
  resolver: zodResolver(schema$5)
9577
9601
  });
9578
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9579
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9580
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9581
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9582
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9583
- ] }) })
9584
- ] }) });
9602
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9603
+ const { handleSuccess } = useRouteModal();
9604
+ const onSubmit = form.handleSubmit(async (data) => {
9605
+ await mutateAsync(
9606
+ { billing_address: data },
9607
+ {
9608
+ onSuccess: () => {
9609
+ handleSuccess();
9610
+ },
9611
+ onError: (error) => {
9612
+ toast.error(error.message);
9613
+ }
9614
+ }
9615
+ );
9616
+ });
9617
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9618
+ KeyboundForm,
9619
+ {
9620
+ className: "flex flex-1 flex-col overflow-hidden",
9621
+ onSubmit,
9622
+ children: [
9623
+ /* @__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: [
9624
+ /* @__PURE__ */ jsx(
9625
+ Form$2.Field,
9626
+ {
9627
+ control: form.control,
9628
+ name: "country_code",
9629
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9630
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
9631
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
9632
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9633
+ ] })
9634
+ }
9635
+ ),
9636
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9637
+ /* @__PURE__ */ jsx(
9638
+ Form$2.Field,
9639
+ {
9640
+ control: form.control,
9641
+ name: "first_name",
9642
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9643
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
9644
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9645
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9646
+ ] })
9647
+ }
9648
+ ),
9649
+ /* @__PURE__ */ jsx(
9650
+ Form$2.Field,
9651
+ {
9652
+ control: form.control,
9653
+ name: "last_name",
9654
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9655
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
9656
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9657
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9658
+ ] })
9659
+ }
9660
+ )
9661
+ ] }),
9662
+ /* @__PURE__ */ jsx(
9663
+ Form$2.Field,
9664
+ {
9665
+ control: form.control,
9666
+ name: "company",
9667
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9668
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
9669
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9670
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9671
+ ] })
9672
+ }
9673
+ ),
9674
+ /* @__PURE__ */ jsx(
9675
+ Form$2.Field,
9676
+ {
9677
+ control: form.control,
9678
+ name: "address_1",
9679
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9680
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
9681
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9682
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9683
+ ] })
9684
+ }
9685
+ ),
9686
+ /* @__PURE__ */ jsx(
9687
+ Form$2.Field,
9688
+ {
9689
+ control: form.control,
9690
+ name: "address_2",
9691
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9692
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
9693
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9694
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9695
+ ] })
9696
+ }
9697
+ ),
9698
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9699
+ /* @__PURE__ */ jsx(
9700
+ Form$2.Field,
9701
+ {
9702
+ control: form.control,
9703
+ name: "postal_code",
9704
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9705
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
9706
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9707
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9708
+ ] })
9709
+ }
9710
+ ),
9711
+ /* @__PURE__ */ jsx(
9712
+ Form$2.Field,
9713
+ {
9714
+ control: form.control,
9715
+ name: "city",
9716
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9717
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
9718
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9719
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9720
+ ] })
9721
+ }
9722
+ )
9723
+ ] }),
9724
+ /* @__PURE__ */ jsx(
9725
+ Form$2.Field,
9726
+ {
9727
+ control: form.control,
9728
+ name: "province",
9729
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9730
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
9731
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9732
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9733
+ ] })
9734
+ }
9735
+ ),
9736
+ /* @__PURE__ */ jsx(
9737
+ Form$2.Field,
9738
+ {
9739
+ control: form.control,
9740
+ name: "phone",
9741
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9742
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
9743
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9744
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9745
+ ] })
9746
+ }
9747
+ )
9748
+ ] }) }),
9749
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9750
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9751
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9752
+ ] }) })
9753
+ ]
9754
+ }
9755
+ ) });
9585
9756
  };
9586
- const schema$5 = objectType({
9587
- email: stringType().email()
9588
- });
9757
+ const schema$5 = addressSchema;
9589
9758
  const Email = () => {
9590
9759
  const { id } = useParams();
9591
9760
  const { order, isPending, isError, error } = useOrder(id, {
@@ -10978,292 +11147,15 @@ function getHasUneditableRows(metadata) {
10978
11147
  (value) => !EDITABLE_TYPES.includes(typeof value)
10979
11148
  );
10980
11149
  }
10981
- const PROMOTION_QUERY_KEY = "promotions";
10982
- const promotionsQueryKeys = {
10983
- list: (query2) => [
10984
- PROMOTION_QUERY_KEY,
10985
- query2 ? query2 : void 0
10986
- ],
10987
- detail: (id, query2) => [
10988
- PROMOTION_QUERY_KEY,
10989
- id,
10990
- query2 ? query2 : void 0
10991
- ]
10992
- };
10993
- const usePromotions = (query2, options) => {
10994
- const { data, ...rest } = useQuery({
10995
- queryKey: promotionsQueryKeys.list(query2),
10996
- queryFn: async () => sdk.admin.promotion.list(query2),
10997
- ...options
10998
- });
10999
- return { ...data, ...rest };
11000
- };
11001
- const Promotions = () => {
11150
+ const SalesChannel = () => {
11002
11151
  const { id } = useParams();
11003
- const {
11004
- order: preview,
11005
- isError: isPreviewError,
11006
- error: previewError
11007
- } = useOrderPreview(id, void 0);
11008
- useInitiateOrderEdit({ preview });
11009
- const { onCancel } = useCancelOrderEdit({ preview });
11010
- if (isPreviewError) {
11011
- throw previewError;
11012
- }
11013
- const isReady = !!preview;
11014
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11015
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11016
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11017
- ] });
11018
- };
11019
- const PromotionForm = ({ preview }) => {
11020
- const { items, shipping_methods } = preview;
11021
- const [isSubmitting, setIsSubmitting] = useState(false);
11022
- const [comboboxValue, setComboboxValue] = useState("");
11023
- const { handleSuccess } = useRouteModal();
11024
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11025
- const promoIds = getPromotionIds(items, shipping_methods);
11026
- const { promotions, isPending, isError, error } = usePromotions(
11152
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11153
+ id,
11027
11154
  {
11028
- id: promoIds
11155
+ fields: "+sales_channel_id"
11029
11156
  },
11030
11157
  {
11031
- enabled: !!promoIds.length
11032
- }
11033
- );
11034
- const comboboxData = useComboboxData({
11035
- queryKey: ["promotions", "combobox", promoIds],
11036
- queryFn: async (params) => {
11037
- return await sdk.admin.promotion.list({
11038
- ...params,
11039
- id: {
11040
- $nin: promoIds
11041
- }
11042
- });
11043
- },
11044
- getOptions: (data) => {
11045
- return data.promotions.map((promotion) => ({
11046
- label: promotion.code,
11047
- value: promotion.code
11048
- }));
11049
- }
11050
- });
11051
- const add = async (value) => {
11052
- if (!value) {
11053
- return;
11054
- }
11055
- addPromotions(
11056
- {
11057
- promo_codes: [value]
11058
- },
11059
- {
11060
- onError: (e) => {
11061
- toast.error(e.message);
11062
- comboboxData.onSearchValueChange("");
11063
- setComboboxValue("");
11064
- },
11065
- onSuccess: () => {
11066
- comboboxData.onSearchValueChange("");
11067
- setComboboxValue("");
11068
- }
11069
- }
11070
- );
11071
- };
11072
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11073
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11074
- const onSubmit = async () => {
11075
- setIsSubmitting(true);
11076
- let requestSucceeded = false;
11077
- await requestOrderEdit(void 0, {
11078
- onError: (e) => {
11079
- toast.error(e.message);
11080
- },
11081
- onSuccess: () => {
11082
- requestSucceeded = true;
11083
- }
11084
- });
11085
- if (!requestSucceeded) {
11086
- setIsSubmitting(false);
11087
- return;
11088
- }
11089
- await confirmOrderEdit(void 0, {
11090
- onError: (e) => {
11091
- toast.error(e.message);
11092
- },
11093
- onSuccess: () => {
11094
- handleSuccess();
11095
- },
11096
- onSettled: () => {
11097
- setIsSubmitting(false);
11098
- }
11099
- });
11100
- };
11101
- if (isError) {
11102
- throw error;
11103
- }
11104
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11105
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11106
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11107
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11108
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11109
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11110
- ] }),
11111
- /* @__PURE__ */ jsx(
11112
- Combobox,
11113
- {
11114
- id: "promotion-combobox",
11115
- "aria-describedby": "promotion-combobox-hint",
11116
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11117
- fetchNextPage: comboboxData.fetchNextPage,
11118
- options: comboboxData.options,
11119
- onSearchValueChange: comboboxData.onSearchValueChange,
11120
- searchValue: comboboxData.searchValue,
11121
- disabled: comboboxData.disabled || isAddingPromotions,
11122
- onChange: add,
11123
- value: comboboxValue
11124
- }
11125
- )
11126
- ] }),
11127
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11128
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11129
- PromotionItem,
11130
- {
11131
- promotion,
11132
- orderId: preview.id,
11133
- isLoading: isPending
11134
- },
11135
- promotion.id
11136
- )) })
11137
- ] }) }),
11138
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11139
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11140
- /* @__PURE__ */ jsx(
11141
- Button,
11142
- {
11143
- size: "small",
11144
- type: "submit",
11145
- isLoading: isSubmitting || isAddingPromotions,
11146
- children: "Save"
11147
- }
11148
- )
11149
- ] }) })
11150
- ] });
11151
- };
11152
- const PromotionItem = ({
11153
- promotion,
11154
- orderId,
11155
- isLoading
11156
- }) => {
11157
- var _a;
11158
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11159
- const onRemove = async () => {
11160
- removePromotions(
11161
- {
11162
- promo_codes: [promotion.code]
11163
- },
11164
- {
11165
- onError: (e) => {
11166
- toast.error(e.message);
11167
- }
11168
- }
11169
- );
11170
- };
11171
- const displayValue = getDisplayValue(promotion);
11172
- return /* @__PURE__ */ jsxs(
11173
- "div",
11174
- {
11175
- className: clx(
11176
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11177
- {
11178
- "animate-pulse": isLoading
11179
- }
11180
- ),
11181
- children: [
11182
- /* @__PURE__ */ jsxs("div", { children: [
11183
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11184
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11185
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11186
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11187
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11188
- ] }),
11189
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11190
- ] })
11191
- ] }),
11192
- /* @__PURE__ */ jsx(
11193
- IconButton,
11194
- {
11195
- size: "small",
11196
- type: "button",
11197
- variant: "transparent",
11198
- onClick: onRemove,
11199
- isLoading: isPending || isLoading,
11200
- children: /* @__PURE__ */ jsx(XMark, {})
11201
- }
11202
- )
11203
- ]
11204
- },
11205
- promotion.id
11206
- );
11207
- };
11208
- function getDisplayValue(promotion) {
11209
- var _a, _b, _c, _d;
11210
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11211
- if (!value) {
11212
- return null;
11213
- }
11214
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11215
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11216
- if (!currency) {
11217
- return null;
11218
- }
11219
- return getLocaleAmount(value, currency);
11220
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11221
- return formatPercentage(value);
11222
- }
11223
- return null;
11224
- }
11225
- const formatter = new Intl.NumberFormat([], {
11226
- style: "percent",
11227
- minimumFractionDigits: 2
11228
- });
11229
- const formatPercentage = (value, isPercentageValue = false) => {
11230
- let val = value || 0;
11231
- if (!isPercentageValue) {
11232
- val = val / 100;
11233
- }
11234
- return formatter.format(val);
11235
- };
11236
- function getPromotionIds(items, shippingMethods) {
11237
- const promotionIds = /* @__PURE__ */ new Set();
11238
- for (const item of items) {
11239
- if (item.adjustments) {
11240
- for (const adjustment of item.adjustments) {
11241
- if (adjustment.promotion_id) {
11242
- promotionIds.add(adjustment.promotion_id);
11243
- }
11244
- }
11245
- }
11246
- }
11247
- for (const shippingMethod of shippingMethods) {
11248
- if (shippingMethod.adjustments) {
11249
- for (const adjustment of shippingMethod.adjustments) {
11250
- if (adjustment.promotion_id) {
11251
- promotionIds.add(adjustment.promotion_id);
11252
- }
11253
- }
11254
- }
11255
- }
11256
- return Array.from(promotionIds);
11257
- }
11258
- const SalesChannel = () => {
11259
- const { id } = useParams();
11260
- const { draft_order, isPending, isError, error } = useDraftOrder(
11261
- id,
11262
- {
11263
- fields: "+sales_channel_id"
11264
- },
11265
- {
11266
- enabled: !!id
11158
+ enabled: !!id
11267
11159
  }
11268
11160
  );
11269
11161
  if (isError) {
@@ -12168,236 +12060,33 @@ const CustomAmountField = ({
12168
12060
  }
12169
12061
  );
12170
12062
  };
12171
- const ShippingAddress = () => {
12063
+ const TransferOwnership = () => {
12172
12064
  const { id } = useParams();
12173
- const { order, isPending, isError, error } = useOrder(id, {
12174
- fields: "+shipping_address"
12065
+ const { draft_order, isPending, isError, error } = useDraftOrder(id, {
12066
+ fields: "id,customer_id,customer.*"
12175
12067
  });
12176
12068
  if (isError) {
12177
12069
  throw error;
12178
12070
  }
12179
- const isReady = !isPending && !!order;
12071
+ const isReady = !isPending && !!draft_order;
12180
12072
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12181
12073
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12182
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12183
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12074
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
12075
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
12184
12076
  ] }),
12185
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12077
+ isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
12186
12078
  ] });
12187
12079
  };
12188
- const ShippingAddressForm = ({ order }) => {
12189
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12080
+ const TransferOwnershipForm = ({ order }) => {
12081
+ var _a, _b;
12190
12082
  const form = useForm({
12191
12083
  defaultValues: {
12192
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12193
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12194
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12195
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12196
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12197
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12198
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12199
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12200
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12201
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12084
+ customer_id: order.customer_id || ""
12202
12085
  },
12203
12086
  resolver: zodResolver(schema$2)
12204
12087
  });
12205
12088
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12206
12089
  const { handleSuccess } = useRouteModal();
12207
- const onSubmit = form.handleSubmit(async (data) => {
12208
- await mutateAsync(
12209
- {
12210
- shipping_address: {
12211
- first_name: data.first_name,
12212
- last_name: data.last_name,
12213
- company: data.company,
12214
- address_1: data.address_1,
12215
- address_2: data.address_2,
12216
- city: data.city,
12217
- province: data.province,
12218
- country_code: data.country_code,
12219
- postal_code: data.postal_code,
12220
- phone: data.phone
12221
- }
12222
- },
12223
- {
12224
- onSuccess: () => {
12225
- handleSuccess();
12226
- },
12227
- onError: (error) => {
12228
- toast.error(error.message);
12229
- }
12230
- }
12231
- );
12232
- });
12233
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12234
- KeyboundForm,
12235
- {
12236
- className: "flex flex-1 flex-col overflow-hidden",
12237
- onSubmit,
12238
- children: [
12239
- /* @__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: [
12240
- /* @__PURE__ */ jsx(
12241
- Form$2.Field,
12242
- {
12243
- control: form.control,
12244
- name: "country_code",
12245
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12246
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12247
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12248
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12249
- ] })
12250
- }
12251
- ),
12252
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12253
- /* @__PURE__ */ jsx(
12254
- Form$2.Field,
12255
- {
12256
- control: form.control,
12257
- name: "first_name",
12258
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12259
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12260
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12261
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12262
- ] })
12263
- }
12264
- ),
12265
- /* @__PURE__ */ jsx(
12266
- Form$2.Field,
12267
- {
12268
- control: form.control,
12269
- name: "last_name",
12270
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12271
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12272
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12273
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12274
- ] })
12275
- }
12276
- )
12277
- ] }),
12278
- /* @__PURE__ */ jsx(
12279
- Form$2.Field,
12280
- {
12281
- control: form.control,
12282
- name: "company",
12283
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12284
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12285
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12286
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12287
- ] })
12288
- }
12289
- ),
12290
- /* @__PURE__ */ jsx(
12291
- Form$2.Field,
12292
- {
12293
- control: form.control,
12294
- name: "address_1",
12295
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12296
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12297
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12298
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12299
- ] })
12300
- }
12301
- ),
12302
- /* @__PURE__ */ jsx(
12303
- Form$2.Field,
12304
- {
12305
- control: form.control,
12306
- name: "address_2",
12307
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12308
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12309
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12310
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12311
- ] })
12312
- }
12313
- ),
12314
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12315
- /* @__PURE__ */ jsx(
12316
- Form$2.Field,
12317
- {
12318
- control: form.control,
12319
- name: "postal_code",
12320
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12321
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12322
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12323
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12324
- ] })
12325
- }
12326
- ),
12327
- /* @__PURE__ */ jsx(
12328
- Form$2.Field,
12329
- {
12330
- control: form.control,
12331
- name: "city",
12332
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12333
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12334
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12335
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12336
- ] })
12337
- }
12338
- )
12339
- ] }),
12340
- /* @__PURE__ */ jsx(
12341
- Form$2.Field,
12342
- {
12343
- control: form.control,
12344
- name: "province",
12345
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12346
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12347
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12348
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12349
- ] })
12350
- }
12351
- ),
12352
- /* @__PURE__ */ jsx(
12353
- Form$2.Field,
12354
- {
12355
- control: form.control,
12356
- name: "phone",
12357
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12358
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12359
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12360
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12361
- ] })
12362
- }
12363
- )
12364
- ] }) }),
12365
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12366
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12367
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12368
- ] }) })
12369
- ]
12370
- }
12371
- ) });
12372
- };
12373
- const schema$2 = addressSchema;
12374
- const TransferOwnership = () => {
12375
- const { id } = useParams();
12376
- const { draft_order, isPending, isError, error } = useDraftOrder(id, {
12377
- fields: "id,customer_id,customer.*"
12378
- });
12379
- if (isError) {
12380
- throw error;
12381
- }
12382
- const isReady = !isPending && !!draft_order;
12383
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12384
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12385
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
12386
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
12387
- ] }),
12388
- isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
12389
- ] });
12390
- };
12391
- const TransferOwnershipForm = ({ order }) => {
12392
- var _a, _b;
12393
- const form = useForm({
12394
- defaultValues: {
12395
- customer_id: order.customer_id || ""
12396
- },
12397
- resolver: zodResolver(schema$1)
12398
- });
12399
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12400
- const { handleSuccess } = useRouteModal();
12401
12090
  const name = [(_a = order.customer) == null ? void 0 : _a.first_name, (_b = order.customer) == null ? void 0 : _b.last_name].filter(Boolean).join(" ");
12402
12091
  const currentCustomer = order.customer ? {
12403
12092
  label: name ? `${name} (${order.customer.email})` : order.customer.email,
@@ -12844,13 +12533,13 @@ const Illustration = () => {
12844
12533
  }
12845
12534
  );
12846
12535
  };
12847
- const schema$1 = objectType({
12536
+ const schema$2 = objectType({
12848
12537
  customer_id: stringType().min(1)
12849
12538
  });
12850
- const BillingAddress = () => {
12539
+ const ShippingAddress = () => {
12851
12540
  const { id } = useParams();
12852
12541
  const { order, isPending, isError, error } = useOrder(id, {
12853
- fields: "+billing_address"
12542
+ fields: "+shipping_address"
12854
12543
  });
12855
12544
  if (isError) {
12856
12545
  throw error;
@@ -12858,34 +12547,47 @@ const BillingAddress = () => {
12858
12547
  const isReady = !isPending && !!order;
12859
12548
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12860
12549
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12861
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Billing Address" }) }),
12862
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
12550
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12551
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12863
12552
  ] }),
12864
- isReady && /* @__PURE__ */ jsx(BillingAddressForm, { order })
12553
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12865
12554
  ] });
12866
12555
  };
12867
- const BillingAddressForm = ({ order }) => {
12556
+ const ShippingAddressForm = ({ order }) => {
12868
12557
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12869
12558
  const form = useForm({
12870
12559
  defaultValues: {
12871
- first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
12872
- last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
12873
- company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
12874
- address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
12875
- address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
12876
- city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
12877
- province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
12878
- country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
12879
- postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
12880
- phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
12560
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12561
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12562
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12563
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12564
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12565
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12566
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12567
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12568
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12569
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12881
12570
  },
12882
- resolver: zodResolver(schema)
12571
+ resolver: zodResolver(schema$1)
12883
12572
  });
12884
12573
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12885
12574
  const { handleSuccess } = useRouteModal();
12886
12575
  const onSubmit = form.handleSubmit(async (data) => {
12887
12576
  await mutateAsync(
12888
- { billing_address: data },
12577
+ {
12578
+ shipping_address: {
12579
+ first_name: data.first_name,
12580
+ last_name: data.last_name,
12581
+ company: data.company,
12582
+ address_1: data.address_1,
12583
+ address_2: data.address_2,
12584
+ city: data.city,
12585
+ province: data.province,
12586
+ country_code: data.country_code,
12587
+ postal_code: data.postal_code,
12588
+ phone: data.phone
12589
+ }
12590
+ },
12889
12591
  {
12890
12592
  onSuccess: () => {
12891
12593
  handleSuccess();
@@ -13036,30 +12738,328 @@ const BillingAddressForm = ({ order }) => {
13036
12738
  }
13037
12739
  ) });
13038
12740
  };
13039
- const schema = addressSchema;
13040
- const widgetModule = { widgets: [] };
13041
- const routeModule = {
13042
- routes: [
13043
- {
13044
- Component: List,
13045
- path: "/draft-orders",
13046
- handle: handle$1,
13047
- children: [
13048
- {
13049
- Component: Create,
13050
- path: "/draft-orders/create"
13051
- }
13052
- ]
13053
- },
13054
- {
12741
+ const schema$1 = addressSchema;
12742
+ const PROMOTION_QUERY_KEY = "promotions";
12743
+ const promotionsQueryKeys = {
12744
+ list: (query2) => [
12745
+ PROMOTION_QUERY_KEY,
12746
+ query2 ? query2 : void 0
12747
+ ],
12748
+ detail: (id, query2) => [
12749
+ PROMOTION_QUERY_KEY,
12750
+ id,
12751
+ query2 ? query2 : void 0
12752
+ ]
12753
+ };
12754
+ const usePromotions = (query2, options) => {
12755
+ const { data, ...rest } = useQuery({
12756
+ queryKey: promotionsQueryKeys.list(query2),
12757
+ queryFn: async () => sdk.admin.promotion.list(query2),
12758
+ ...options
12759
+ });
12760
+ return { ...data, ...rest };
12761
+ };
12762
+ const Promotions = () => {
12763
+ const { id } = useParams();
12764
+ const {
12765
+ order: preview,
12766
+ isError: isPreviewError,
12767
+ error: previewError
12768
+ } = useOrderPreview(id, void 0);
12769
+ useInitiateOrderEdit({ preview });
12770
+ const { onCancel } = useCancelOrderEdit({ preview });
12771
+ if (isPreviewError) {
12772
+ throw previewError;
12773
+ }
12774
+ const isReady = !!preview;
12775
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
12776
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
12777
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
12778
+ ] });
12779
+ };
12780
+ const PromotionForm = ({ preview }) => {
12781
+ const { items, shipping_methods } = preview;
12782
+ const [isSubmitting, setIsSubmitting] = useState(false);
12783
+ const [comboboxValue, setComboboxValue] = useState("");
12784
+ const { handleSuccess } = useRouteModal();
12785
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
12786
+ const promoIds = getPromotionIds(items, shipping_methods);
12787
+ const { promotions, isPending, isError, error } = usePromotions(
12788
+ {
12789
+ id: promoIds
12790
+ },
12791
+ {
12792
+ enabled: !!promoIds.length
12793
+ }
12794
+ );
12795
+ const comboboxData = useComboboxData({
12796
+ queryKey: ["promotions", "combobox", promoIds],
12797
+ queryFn: async (params) => {
12798
+ return await sdk.admin.promotion.list({
12799
+ ...params,
12800
+ id: {
12801
+ $nin: promoIds
12802
+ }
12803
+ });
12804
+ },
12805
+ getOptions: (data) => {
12806
+ return data.promotions.map((promotion) => ({
12807
+ label: promotion.code,
12808
+ value: promotion.code
12809
+ }));
12810
+ }
12811
+ });
12812
+ const add = async (value) => {
12813
+ if (!value) {
12814
+ return;
12815
+ }
12816
+ addPromotions(
12817
+ {
12818
+ promo_codes: [value]
12819
+ },
12820
+ {
12821
+ onError: (e) => {
12822
+ toast.error(e.message);
12823
+ comboboxData.onSearchValueChange("");
12824
+ setComboboxValue("");
12825
+ },
12826
+ onSuccess: () => {
12827
+ comboboxData.onSearchValueChange("");
12828
+ setComboboxValue("");
12829
+ }
12830
+ }
12831
+ );
12832
+ };
12833
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12834
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
12835
+ const onSubmit = async () => {
12836
+ setIsSubmitting(true);
12837
+ let requestSucceeded = false;
12838
+ await requestOrderEdit(void 0, {
12839
+ onError: (e) => {
12840
+ toast.error(e.message);
12841
+ },
12842
+ onSuccess: () => {
12843
+ requestSucceeded = true;
12844
+ }
12845
+ });
12846
+ if (!requestSucceeded) {
12847
+ setIsSubmitting(false);
12848
+ return;
12849
+ }
12850
+ await confirmOrderEdit(void 0, {
12851
+ onError: (e) => {
12852
+ toast.error(e.message);
12853
+ },
12854
+ onSuccess: () => {
12855
+ handleSuccess();
12856
+ },
12857
+ onSettled: () => {
12858
+ setIsSubmitting(false);
12859
+ }
12860
+ });
12861
+ };
12862
+ if (isError) {
12863
+ throw error;
12864
+ }
12865
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
12866
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
12867
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
12868
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12869
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
12870
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
12871
+ ] }),
12872
+ /* @__PURE__ */ jsx(
12873
+ Combobox,
12874
+ {
12875
+ id: "promotion-combobox",
12876
+ "aria-describedby": "promotion-combobox-hint",
12877
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
12878
+ fetchNextPage: comboboxData.fetchNextPage,
12879
+ options: comboboxData.options,
12880
+ onSearchValueChange: comboboxData.onSearchValueChange,
12881
+ searchValue: comboboxData.searchValue,
12882
+ disabled: comboboxData.disabled || isAddingPromotions,
12883
+ onChange: add,
12884
+ value: comboboxValue
12885
+ }
12886
+ )
12887
+ ] }),
12888
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12889
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
12890
+ PromotionItem,
12891
+ {
12892
+ promotion,
12893
+ orderId: preview.id,
12894
+ isLoading: isPending
12895
+ },
12896
+ promotion.id
12897
+ )) })
12898
+ ] }) }),
12899
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12900
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12901
+ /* @__PURE__ */ jsx(
12902
+ Button,
12903
+ {
12904
+ size: "small",
12905
+ type: "submit",
12906
+ isLoading: isSubmitting || isAddingPromotions,
12907
+ children: "Save"
12908
+ }
12909
+ )
12910
+ ] }) })
12911
+ ] });
12912
+ };
12913
+ const PromotionItem = ({
12914
+ promotion,
12915
+ orderId,
12916
+ isLoading
12917
+ }) => {
12918
+ var _a;
12919
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
12920
+ const onRemove = async () => {
12921
+ removePromotions(
12922
+ {
12923
+ promo_codes: [promotion.code]
12924
+ },
12925
+ {
12926
+ onError: (e) => {
12927
+ toast.error(e.message);
12928
+ }
12929
+ }
12930
+ );
12931
+ };
12932
+ const displayValue = getDisplayValue(promotion);
12933
+ return /* @__PURE__ */ jsxs(
12934
+ "div",
12935
+ {
12936
+ className: clx(
12937
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
12938
+ {
12939
+ "animate-pulse": isLoading
12940
+ }
12941
+ ),
12942
+ children: [
12943
+ /* @__PURE__ */ jsxs("div", { children: [
12944
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
12945
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
12946
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
12947
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
12948
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
12949
+ ] }),
12950
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
12951
+ ] })
12952
+ ] }),
12953
+ /* @__PURE__ */ jsx(
12954
+ IconButton,
12955
+ {
12956
+ size: "small",
12957
+ type: "button",
12958
+ variant: "transparent",
12959
+ onClick: onRemove,
12960
+ isLoading: isPending || isLoading,
12961
+ children: /* @__PURE__ */ jsx(XMark, {})
12962
+ }
12963
+ )
12964
+ ]
12965
+ },
12966
+ promotion.id
12967
+ );
12968
+ };
12969
+ function getDisplayValue(promotion) {
12970
+ var _a, _b, _c, _d;
12971
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
12972
+ if (!value) {
12973
+ return null;
12974
+ }
12975
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
12976
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
12977
+ if (!currency) {
12978
+ return null;
12979
+ }
12980
+ return getLocaleAmount(value, currency);
12981
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
12982
+ return formatPercentage(value);
12983
+ }
12984
+ return null;
12985
+ }
12986
+ const formatter = new Intl.NumberFormat([], {
12987
+ style: "percent",
12988
+ minimumFractionDigits: 2
12989
+ });
12990
+ const formatPercentage = (value, isPercentageValue = false) => {
12991
+ let val = value || 0;
12992
+ if (!isPercentageValue) {
12993
+ val = val / 100;
12994
+ }
12995
+ return formatter.format(val);
12996
+ };
12997
+ function getPromotionIds(items, shippingMethods) {
12998
+ const promotionIds = /* @__PURE__ */ new Set();
12999
+ for (const item of items) {
13000
+ if (item.adjustments) {
13001
+ for (const adjustment of item.adjustments) {
13002
+ if (adjustment.promotion_id) {
13003
+ promotionIds.add(adjustment.promotion_id);
13004
+ }
13005
+ }
13006
+ }
13007
+ }
13008
+ for (const shippingMethod of shippingMethods) {
13009
+ if (shippingMethod.adjustments) {
13010
+ for (const adjustment of shippingMethod.adjustments) {
13011
+ if (adjustment.promotion_id) {
13012
+ promotionIds.add(adjustment.promotion_id);
13013
+ }
13014
+ }
13015
+ }
13016
+ }
13017
+ return Array.from(promotionIds);
13018
+ }
13019
+ const CustomItems = () => {
13020
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
13021
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
13022
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
13023
+ ] });
13024
+ };
13025
+ const CustomItemsForm = () => {
13026
+ const form = useForm({
13027
+ resolver: zodResolver(schema)
13028
+ });
13029
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
13030
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
13031
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13032
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13033
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
13034
+ ] }) })
13035
+ ] }) });
13036
+ };
13037
+ const schema = objectType({
13038
+ email: stringType().email()
13039
+ });
13040
+ const widgetModule = { widgets: [] };
13041
+ const routeModule = {
13042
+ routes: [
13043
+ {
13044
+ Component: List,
13045
+ path: "/draft-orders",
13046
+ handle: handle$1,
13047
+ children: [
13048
+ {
13049
+ Component: Create,
13050
+ path: "/draft-orders/create"
13051
+ }
13052
+ ]
13053
+ },
13054
+ {
13055
13055
  Component: ID,
13056
13056
  path: "/draft-orders/:id",
13057
13057
  handle,
13058
13058
  loader,
13059
13059
  children: [
13060
13060
  {
13061
- Component: CustomItems,
13062
- path: "/draft-orders/:id/custom-items"
13061
+ Component: BillingAddress,
13062
+ path: "/draft-orders/:id/billing-address"
13063
13063
  },
13064
13064
  {
13065
13065
  Component: Email,
@@ -13073,10 +13073,6 @@ const routeModule = {
13073
13073
  Component: Metadata,
13074
13074
  path: "/draft-orders/:id/metadata"
13075
13075
  },
13076
- {
13077
- Component: Promotions,
13078
- path: "/draft-orders/:id/promotions"
13079
- },
13080
13076
  {
13081
13077
  Component: SalesChannel,
13082
13078
  path: "/draft-orders/:id/sales-channel"
@@ -13085,17 +13081,21 @@ const routeModule = {
13085
13081
  Component: Shipping,
13086
13082
  path: "/draft-orders/:id/shipping"
13087
13083
  },
13084
+ {
13085
+ Component: TransferOwnership,
13086
+ path: "/draft-orders/:id/transfer-ownership"
13087
+ },
13088
13088
  {
13089
13089
  Component: ShippingAddress,
13090
13090
  path: "/draft-orders/:id/shipping-address"
13091
13091
  },
13092
13092
  {
13093
- Component: TransferOwnership,
13094
- path: "/draft-orders/:id/transfer-ownership"
13093
+ Component: Promotions,
13094
+ path: "/draft-orders/:id/promotions"
13095
13095
  },
13096
13096
  {
13097
- Component: BillingAddress,
13098
- path: "/draft-orders/:id/billing-address"
13097
+ Component: CustomItems,
13098
+ path: "/draft-orders/:id/custom-items"
13099
13099
  }
13100
13100
  ]
13101
13101
  }