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