@medusajs/draft-order 2.10.2-preview-20250904031023 → 2.10.2-preview-20250904090151

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.
@@ -9771,74 +9771,6 @@ const CustomItemsForm = () => {
9771
9771
  const schema$4 = objectType({
9772
9772
  email: stringType().email()
9773
9773
  });
9774
- const Email = () => {
9775
- const { id } = reactRouterDom.useParams();
9776
- const { order, isPending, isError, error } = useOrder(id, {
9777
- fields: "+email"
9778
- });
9779
- if (isError) {
9780
- throw error;
9781
- }
9782
- const isReady = !isPending && !!order;
9783
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9784
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9785
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
9786
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9787
- ] }),
9788
- isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
9789
- ] });
9790
- };
9791
- const EmailForm = ({ order }) => {
9792
- const form = reactHookForm.useForm({
9793
- defaultValues: {
9794
- email: order.email ?? ""
9795
- },
9796
- resolver: zod.zodResolver(schema$3)
9797
- });
9798
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9799
- const { handleSuccess } = useRouteModal();
9800
- const onSubmit = form.handleSubmit(async (data) => {
9801
- await mutateAsync(
9802
- { email: data.email },
9803
- {
9804
- onSuccess: () => {
9805
- handleSuccess();
9806
- },
9807
- onError: (error) => {
9808
- ui.toast.error(error.message);
9809
- }
9810
- }
9811
- );
9812
- });
9813
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9814
- KeyboundForm,
9815
- {
9816
- className: "flex flex-1 flex-col overflow-hidden",
9817
- onSubmit,
9818
- children: [
9819
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
9820
- Form$2.Field,
9821
- {
9822
- control: form.control,
9823
- name: "email",
9824
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9825
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
9826
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9827
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9828
- ] })
9829
- }
9830
- ) }),
9831
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9832
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9833
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9834
- ] }) })
9835
- ]
9836
- }
9837
- ) });
9838
- };
9839
- const schema$3 = objectType({
9840
- email: stringType().email()
9841
- });
9842
9774
  const NumberInput = React.forwardRef(
9843
9775
  ({
9844
9776
  value,
@@ -10813,385 +10745,108 @@ const customItemSchema = objectType({
10813
10745
  quantity: numberType(),
10814
10746
  unit_price: unionType([numberType(), stringType()])
10815
10747
  });
10816
- const PROMOTION_QUERY_KEY = "promotions";
10817
- const promotionsQueryKeys = {
10818
- list: (query2) => [
10819
- PROMOTION_QUERY_KEY,
10820
- query2 ? query2 : void 0
10821
- ],
10822
- detail: (id, query2) => [
10823
- PROMOTION_QUERY_KEY,
10824
- id,
10825
- query2 ? query2 : void 0
10826
- ]
10827
- };
10828
- const usePromotions = (query2, options) => {
10829
- const { data, ...rest } = reactQuery.useQuery({
10830
- queryKey: promotionsQueryKeys.list(query2),
10831
- queryFn: async () => sdk.admin.promotion.list(query2),
10832
- ...options
10833
- });
10834
- return { ...data, ...rest };
10835
- };
10836
- const Promotions = () => {
10748
+ const InlineTip = React.forwardRef(
10749
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
10750
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10751
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10752
+ "div",
10753
+ {
10754
+ ref,
10755
+ className: ui.clx(
10756
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10757
+ className
10758
+ ),
10759
+ ...props,
10760
+ children: [
10761
+ /* @__PURE__ */ jsxRuntime.jsx(
10762
+ "div",
10763
+ {
10764
+ role: "presentation",
10765
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10766
+ "bg-ui-tag-orange-icon": variant === "warning"
10767
+ })
10768
+ }
10769
+ ),
10770
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10771
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10772
+ labelValue,
10773
+ ":"
10774
+ ] }),
10775
+ " ",
10776
+ children
10777
+ ] })
10778
+ ]
10779
+ }
10780
+ );
10781
+ }
10782
+ );
10783
+ InlineTip.displayName = "InlineTip";
10784
+ const MetadataFieldSchema = objectType({
10785
+ key: stringType(),
10786
+ disabled: booleanType().optional(),
10787
+ value: anyType()
10788
+ });
10789
+ const MetadataSchema = objectType({
10790
+ metadata: arrayType(MetadataFieldSchema)
10791
+ });
10792
+ const Metadata = () => {
10837
10793
  const { id } = reactRouterDom.useParams();
10838
- const {
10839
- order: preview,
10840
- isError: isPreviewError,
10841
- error: previewError
10842
- } = useOrderPreview(id, void 0);
10843
- useInitiateOrderEdit({ preview });
10844
- const { onCancel } = useCancelOrderEdit({ preview });
10845
- if (isPreviewError) {
10846
- throw previewError;
10794
+ const { order, isPending, isError, error } = useOrder(id, {
10795
+ fields: "metadata"
10796
+ });
10797
+ if (isError) {
10798
+ throw error;
10847
10799
  }
10848
- const isReady = !!preview;
10849
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
10850
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
10851
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
10800
+ const isReady = !isPending && !!order;
10801
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10802
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10803
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10804
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10805
+ ] }),
10806
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10852
10807
  ] });
10853
10808
  };
10854
- const PromotionForm = ({ preview }) => {
10855
- const { items, shipping_methods } = preview;
10856
- const [isSubmitting, setIsSubmitting] = React.useState(false);
10857
- const [comboboxValue, setComboboxValue] = React.useState("");
10809
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10810
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10811
+ const MetadataForm = ({ orderId, metadata }) => {
10858
10812
  const { handleSuccess } = useRouteModal();
10859
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10860
- const promoCodes = getPromotionCodes(items, shipping_methods);
10861
- const { promotions, isPending, isError, error } = usePromotions(
10862
- {
10863
- code: promoCodes
10864
- },
10865
- {
10866
- enabled: !!promoCodes.length
10867
- }
10868
- );
10869
- const comboboxData = useComboboxData({
10870
- queryKey: ["promotions", "combobox", promoCodes],
10871
- queryFn: async (params) => {
10872
- return await sdk.admin.promotion.list({
10873
- ...params,
10874
- code: {
10875
- $nin: promoCodes
10876
- }
10877
- });
10813
+ const hasUneditableRows = getHasUneditableRows(metadata);
10814
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10815
+ const form = reactHookForm.useForm({
10816
+ defaultValues: {
10817
+ metadata: getDefaultValues(metadata)
10878
10818
  },
10879
- getOptions: (data) => {
10880
- return data.promotions.map((promotion) => ({
10881
- label: promotion.code,
10882
- value: promotion.code
10883
- }));
10884
- }
10819
+ resolver: zod.zodResolver(MetadataSchema)
10885
10820
  });
10886
- const add = async (value) => {
10887
- if (!value) {
10888
- return;
10889
- }
10890
- addPromotions(
10821
+ const handleSubmit = form.handleSubmit(async (data) => {
10822
+ const parsedData = parseValues(data);
10823
+ await mutateAsync(
10891
10824
  {
10892
- promo_codes: [value]
10825
+ metadata: parsedData
10893
10826
  },
10894
10827
  {
10895
- onError: (e) => {
10896
- ui.toast.error(e.message);
10897
- comboboxData.onSearchValueChange("");
10898
- setComboboxValue("");
10899
- },
10900
10828
  onSuccess: () => {
10901
- comboboxData.onSearchValueChange("");
10902
- setComboboxValue("");
10829
+ ui.toast.success("Metadata updated");
10830
+ handleSuccess();
10831
+ },
10832
+ onError: (error) => {
10833
+ ui.toast.error(error.message);
10903
10834
  }
10904
10835
  }
10905
10836
  );
10906
- };
10907
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10908
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10909
- const onSubmit = async () => {
10910
- setIsSubmitting(true);
10911
- let requestSucceeded = false;
10912
- await requestOrderEdit(void 0, {
10913
- onError: (e) => {
10914
- ui.toast.error(e.message);
10915
- },
10916
- onSuccess: () => {
10917
- requestSucceeded = true;
10918
- }
10919
- });
10920
- if (!requestSucceeded) {
10921
- setIsSubmitting(false);
10922
- return;
10923
- }
10924
- await confirmOrderEdit(void 0, {
10925
- onError: (e) => {
10926
- ui.toast.error(e.message);
10927
- },
10928
- onSuccess: () => {
10929
- handleSuccess();
10930
- },
10931
- onSettled: () => {
10932
- setIsSubmitting(false);
10933
- }
10934
- });
10935
- };
10936
- if (isError) {
10937
- throw error;
10938
- }
10939
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10940
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
10941
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
10942
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10943
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10944
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10945
- ] }),
10946
- /* @__PURE__ */ jsxRuntime.jsx(
10947
- Combobox,
10948
- {
10949
- id: "promotion-combobox",
10950
- "aria-describedby": "promotion-combobox-hint",
10951
- isFetchingNextPage: comboboxData.isFetchingNextPage,
10952
- fetchNextPage: comboboxData.fetchNextPage,
10953
- options: comboboxData.options,
10954
- onSearchValueChange: comboboxData.onSearchValueChange,
10955
- searchValue: comboboxData.searchValue,
10956
- disabled: comboboxData.disabled || isAddingPromotions,
10957
- onChange: add,
10958
- value: comboboxValue
10959
- }
10960
- )
10961
- ] }),
10962
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10963
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
10964
- PromotionItem,
10965
- {
10966
- promotion,
10967
- orderId: preview.id,
10968
- isLoading: isPending
10969
- },
10970
- promotion.id
10971
- )) })
10972
- ] }) }),
10973
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10974
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10975
- /* @__PURE__ */ jsxRuntime.jsx(
10976
- ui.Button,
10977
- {
10978
- size: "small",
10979
- type: "submit",
10980
- isLoading: isSubmitting || isAddingPromotions,
10981
- children: "Save"
10982
- }
10983
- )
10984
- ] }) })
10985
- ] });
10986
- };
10987
- const PromotionItem = ({
10988
- promotion,
10989
- orderId,
10990
- isLoading
10991
- }) => {
10992
- var _a;
10993
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10994
- const onRemove = async () => {
10995
- removePromotions(
10996
- {
10997
- promo_codes: [promotion.code]
10998
- },
10999
- {
11000
- onError: (e) => {
11001
- ui.toast.error(e.message);
11002
- }
11003
- }
11004
- );
11005
- };
11006
- const displayValue = getDisplayValue(promotion);
11007
- return /* @__PURE__ */ jsxRuntime.jsxs(
11008
- "div",
11009
- {
11010
- className: ui.clx(
11011
- "px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between",
11012
- {
11013
- "animate-pulse": isLoading
11014
- }
11015
- ),
11016
- children: [
11017
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11018
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11019
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5 text-ui-fg-subtle", children: [
11020
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11021
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11022
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11023
- ] }),
11024
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11025
- ] })
11026
- ] }),
11027
- /* @__PURE__ */ jsxRuntime.jsx(
11028
- ui.IconButton,
11029
- {
11030
- size: "small",
11031
- type: "button",
11032
- variant: "transparent",
11033
- onClick: onRemove,
11034
- isLoading: isPending || isLoading,
11035
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11036
- }
11037
- )
11038
- ]
11039
- },
11040
- promotion.id
11041
- );
11042
- };
11043
- function getDisplayValue(promotion) {
11044
- var _a, _b, _c, _d;
11045
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11046
- if (!value) {
11047
- return null;
11048
- }
11049
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11050
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11051
- if (!currency) {
11052
- return null;
11053
- }
11054
- return getLocaleAmount(value, currency);
11055
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11056
- return formatPercentage(value);
11057
- }
11058
- return null;
11059
- }
11060
- const formatter = new Intl.NumberFormat([], {
11061
- style: "percent",
11062
- minimumFractionDigits: 2
11063
- });
11064
- const formatPercentage = (value, isPercentageValue = false) => {
11065
- let val = value || 0;
11066
- if (!isPercentageValue) {
11067
- val = val / 100;
11068
- }
11069
- return formatter.format(val);
11070
- };
11071
- function getPromotionCodes(items, shippingMethods) {
11072
- const codes = /* @__PURE__ */ new Set();
11073
- for (const item of items) {
11074
- if (item.adjustments) {
11075
- for (const adjustment of item.adjustments) {
11076
- if (adjustment.code) {
11077
- codes.add(adjustment.code);
11078
- }
11079
- }
11080
- }
11081
- }
11082
- for (const shippingMethod of shippingMethods) {
11083
- if (shippingMethod.adjustments) {
11084
- for (const adjustment of shippingMethod.adjustments) {
11085
- if (adjustment.code) {
11086
- codes.add(adjustment.code);
11087
- }
11088
- }
11089
- }
11090
- }
11091
- return Array.from(codes);
11092
- }
11093
- const InlineTip = React.forwardRef(
11094
- ({ variant = "tip", label, className, children, ...props }, ref) => {
11095
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
11096
- return /* @__PURE__ */ jsxRuntime.jsxs(
11097
- "div",
11098
- {
11099
- ref,
11100
- className: ui.clx(
11101
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
11102
- className
11103
- ),
11104
- ...props,
11105
- children: [
11106
- /* @__PURE__ */ jsxRuntime.jsx(
11107
- "div",
11108
- {
11109
- role: "presentation",
11110
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
11111
- "bg-ui-tag-orange-icon": variant === "warning"
11112
- })
11113
- }
11114
- ),
11115
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
11116
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
11117
- labelValue,
11118
- ":"
11119
- ] }),
11120
- " ",
11121
- children
11122
- ] })
11123
- ]
11124
- }
11125
- );
11126
- }
11127
- );
11128
- InlineTip.displayName = "InlineTip";
11129
- const MetadataFieldSchema = objectType({
11130
- key: stringType(),
11131
- disabled: booleanType().optional(),
11132
- value: anyType()
11133
- });
11134
- const MetadataSchema = objectType({
11135
- metadata: arrayType(MetadataFieldSchema)
11136
- });
11137
- const Metadata = () => {
11138
- const { id } = reactRouterDom.useParams();
11139
- const { order, isPending, isError, error } = useOrder(id, {
11140
- fields: "metadata"
11141
- });
11142
- if (isError) {
11143
- throw error;
11144
- }
11145
- const isReady = !isPending && !!order;
11146
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11147
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11148
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
11149
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
11150
- ] }),
11151
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
11152
- ] });
11153
- };
11154
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
11155
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
11156
- const MetadataForm = ({ orderId, metadata }) => {
11157
- const { handleSuccess } = useRouteModal();
11158
- const hasUneditableRows = getHasUneditableRows(metadata);
11159
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
11160
- const form = reactHookForm.useForm({
11161
- defaultValues: {
11162
- metadata: getDefaultValues(metadata)
11163
- },
11164
- resolver: zod.zodResolver(MetadataSchema)
11165
- });
11166
- const handleSubmit = form.handleSubmit(async (data) => {
11167
- const parsedData = parseValues(data);
11168
- await mutateAsync(
11169
- {
11170
- metadata: parsedData
11171
- },
11172
- {
11173
- onSuccess: () => {
11174
- ui.toast.success("Metadata updated");
11175
- handleSuccess();
11176
- },
11177
- onError: (error) => {
11178
- ui.toast.error(error.message);
11179
- }
11180
- }
11181
- );
11182
- });
11183
- const { fields, insert, remove } = reactHookForm.useFieldArray({
11184
- control: form.control,
11185
- name: "metadata"
11186
- });
11187
- function deleteRow(index) {
11188
- remove(index);
11189
- if (fields.length === 1) {
11190
- insert(0, {
11191
- key: "",
11192
- value: "",
11193
- disabled: false
11194
- });
10837
+ });
10838
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
10839
+ control: form.control,
10840
+ name: "metadata"
10841
+ });
10842
+ function deleteRow(index) {
10843
+ remove(index);
10844
+ if (fields.length === 1) {
10845
+ insert(0, {
10846
+ key: "",
10847
+ value: "",
10848
+ disabled: false
10849
+ });
11195
10850
  }
11196
10851
  }
11197
10852
  function insertRow(index, position) {
@@ -11436,116 +11091,287 @@ function getHasUneditableRows(metadata) {
11436
11091
  if (!metadata) {
11437
11092
  return false;
11438
11093
  }
11439
- return Object.values(metadata).some(
11440
- (value) => !EDITABLE_TYPES.includes(typeof value)
11441
- );
11442
- }
11443
- const SalesChannel = () => {
11444
- const { id } = reactRouterDom.useParams();
11445
- const { draft_order, isPending, isError, error } = useDraftOrder(
11446
- id,
11094
+ return Object.values(metadata).some(
11095
+ (value) => !EDITABLE_TYPES.includes(typeof value)
11096
+ );
11097
+ }
11098
+ const PROMOTION_QUERY_KEY = "promotions";
11099
+ const promotionsQueryKeys = {
11100
+ list: (query2) => [
11101
+ PROMOTION_QUERY_KEY,
11102
+ query2 ? query2 : void 0
11103
+ ],
11104
+ detail: (id, query2) => [
11105
+ PROMOTION_QUERY_KEY,
11106
+ id,
11107
+ query2 ? query2 : void 0
11108
+ ]
11109
+ };
11110
+ const usePromotions = (query2, options) => {
11111
+ const { data, ...rest } = reactQuery.useQuery({
11112
+ queryKey: promotionsQueryKeys.list(query2),
11113
+ queryFn: async () => sdk.admin.promotion.list(query2),
11114
+ ...options
11115
+ });
11116
+ return { ...data, ...rest };
11117
+ };
11118
+ const Promotions = () => {
11119
+ const { id } = reactRouterDom.useParams();
11120
+ const {
11121
+ order: preview,
11122
+ isError: isPreviewError,
11123
+ error: previewError
11124
+ } = useOrderPreview(id, void 0);
11125
+ useInitiateOrderEdit({ preview });
11126
+ const { onCancel } = useCancelOrderEdit({ preview });
11127
+ if (isPreviewError) {
11128
+ throw previewError;
11129
+ }
11130
+ const isReady = !!preview;
11131
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11132
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11133
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11134
+ ] });
11135
+ };
11136
+ const PromotionForm = ({ preview }) => {
11137
+ const { items, shipping_methods } = preview;
11138
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
11139
+ const [comboboxValue, setComboboxValue] = React.useState("");
11140
+ const { handleSuccess } = useRouteModal();
11141
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11142
+ const promoCodes = getPromotionCodes(items, shipping_methods);
11143
+ const { promotions, isPending, isError, error } = usePromotions(
11447
11144
  {
11448
- fields: "+sales_channel_id"
11145
+ code: promoCodes
11449
11146
  },
11450
11147
  {
11451
- enabled: !!id
11148
+ enabled: !!promoCodes.length
11452
11149
  }
11453
11150
  );
11151
+ const comboboxData = useComboboxData({
11152
+ queryKey: ["promotions", "combobox", promoCodes],
11153
+ queryFn: async (params) => {
11154
+ return await sdk.admin.promotion.list({
11155
+ ...params,
11156
+ code: {
11157
+ $nin: promoCodes
11158
+ }
11159
+ });
11160
+ },
11161
+ getOptions: (data) => {
11162
+ return data.promotions.map((promotion) => ({
11163
+ label: promotion.code,
11164
+ value: promotion.code
11165
+ }));
11166
+ }
11167
+ });
11168
+ const add = async (value) => {
11169
+ if (!value) {
11170
+ return;
11171
+ }
11172
+ addPromotions(
11173
+ {
11174
+ promo_codes: [value]
11175
+ },
11176
+ {
11177
+ onError: (e) => {
11178
+ ui.toast.error(e.message);
11179
+ comboboxData.onSearchValueChange("");
11180
+ setComboboxValue("");
11181
+ },
11182
+ onSuccess: () => {
11183
+ comboboxData.onSearchValueChange("");
11184
+ setComboboxValue("");
11185
+ }
11186
+ }
11187
+ );
11188
+ };
11189
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11190
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11191
+ const onSubmit = async () => {
11192
+ setIsSubmitting(true);
11193
+ let requestSucceeded = false;
11194
+ await requestOrderEdit(void 0, {
11195
+ onError: (e) => {
11196
+ ui.toast.error(e.message);
11197
+ },
11198
+ onSuccess: () => {
11199
+ requestSucceeded = true;
11200
+ }
11201
+ });
11202
+ if (!requestSucceeded) {
11203
+ setIsSubmitting(false);
11204
+ return;
11205
+ }
11206
+ await confirmOrderEdit(void 0, {
11207
+ onError: (e) => {
11208
+ ui.toast.error(e.message);
11209
+ },
11210
+ onSuccess: () => {
11211
+ handleSuccess();
11212
+ },
11213
+ onSettled: () => {
11214
+ setIsSubmitting(false);
11215
+ }
11216
+ });
11217
+ };
11454
11218
  if (isError) {
11455
11219
  throw error;
11456
11220
  }
11457
- const ISrEADY = !!draft_order && !isPending;
11458
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11459
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11460
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11461
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11462
- ] }),
11463
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11221
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11222
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11223
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11224
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11225
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11226
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11227
+ ] }),
11228
+ /* @__PURE__ */ jsxRuntime.jsx(
11229
+ Combobox,
11230
+ {
11231
+ id: "promotion-combobox",
11232
+ "aria-describedby": "promotion-combobox-hint",
11233
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
11234
+ fetchNextPage: comboboxData.fetchNextPage,
11235
+ options: comboboxData.options,
11236
+ onSearchValueChange: comboboxData.onSearchValueChange,
11237
+ searchValue: comboboxData.searchValue,
11238
+ disabled: comboboxData.disabled || isAddingPromotions,
11239
+ onChange: add,
11240
+ value: comboboxValue
11241
+ }
11242
+ )
11243
+ ] }),
11244
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11245
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11246
+ PromotionItem,
11247
+ {
11248
+ promotion,
11249
+ orderId: preview.id,
11250
+ isLoading: isPending
11251
+ },
11252
+ promotion.id
11253
+ )) })
11254
+ ] }) }),
11255
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11256
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11257
+ /* @__PURE__ */ jsxRuntime.jsx(
11258
+ ui.Button,
11259
+ {
11260
+ size: "small",
11261
+ type: "submit",
11262
+ isLoading: isSubmitting || isAddingPromotions,
11263
+ children: "Save"
11264
+ }
11265
+ )
11266
+ ] }) })
11464
11267
  ] });
11465
11268
  };
11466
- const SalesChannelForm = ({ order }) => {
11467
- const form = reactHookForm.useForm({
11468
- defaultValues: {
11469
- sales_channel_id: order.sales_channel_id || ""
11470
- },
11471
- resolver: zod.zodResolver(schema$2)
11472
- });
11473
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11474
- const { handleSuccess } = useRouteModal();
11475
- const onSubmit = form.handleSubmit(async (data) => {
11476
- await mutateAsync(
11269
+ const PromotionItem = ({
11270
+ promotion,
11271
+ orderId,
11272
+ isLoading
11273
+ }) => {
11274
+ var _a;
11275
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11276
+ const onRemove = async () => {
11277
+ removePromotions(
11477
11278
  {
11478
- sales_channel_id: data.sales_channel_id
11279
+ promo_codes: [promotion.code]
11479
11280
  },
11480
11281
  {
11481
- onSuccess: () => {
11482
- ui.toast.success("Sales channel updated");
11483
- handleSuccess();
11484
- },
11485
- onError: (error) => {
11486
- ui.toast.error(error.message);
11282
+ onError: (e) => {
11283
+ ui.toast.error(e.message);
11487
11284
  }
11488
11285
  }
11489
11286
  );
11490
- });
11491
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11492
- KeyboundForm,
11287
+ };
11288
+ const displayValue = getDisplayValue(promotion);
11289
+ return /* @__PURE__ */ jsxRuntime.jsxs(
11290
+ "div",
11493
11291
  {
11494
- className: "flex flex-1 flex-col overflow-hidden",
11495
- onSubmit,
11292
+ className: ui.clx(
11293
+ "px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between",
11294
+ {
11295
+ "animate-pulse": isLoading
11296
+ }
11297
+ ),
11496
11298
  children: [
11497
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11498
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11499
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11500
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11501
- ] }) })
11502
- ]
11503
- }
11504
- ) });
11505
- };
11506
- const SalesChannelField = ({ control, order }) => {
11507
- const salesChannels = useComboboxData({
11508
- queryFn: async (params) => {
11509
- return await sdk.admin.salesChannel.list(params);
11510
- },
11511
- queryKey: ["sales-channels"],
11512
- getOptions: (data) => {
11513
- return data.sales_channels.map((salesChannel) => ({
11514
- label: salesChannel.name,
11515
- value: salesChannel.id
11516
- }));
11517
- },
11518
- defaultValue: order.sales_channel_id || void 0
11519
- });
11520
- return /* @__PURE__ */ jsxRuntime.jsx(
11521
- Form$2.Field,
11522
- {
11523
- control,
11524
- name: "sales_channel_id",
11525
- render: ({ field }) => {
11526
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11527
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11528
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11529
- Combobox,
11530
- {
11531
- options: salesChannels.options,
11532
- fetchNextPage: salesChannels.fetchNextPage,
11533
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11534
- searchValue: salesChannels.searchValue,
11535
- onSearchValueChange: salesChannels.onSearchValueChange,
11536
- placeholder: "Select sales channel",
11537
- ...field
11538
- }
11539
- ) }),
11540
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11541
- ] });
11542
- }
11543
- }
11299
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11300
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11301
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5 text-ui-fg-subtle", children: [
11302
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11303
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11304
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11305
+ ] }),
11306
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11307
+ ] })
11308
+ ] }),
11309
+ /* @__PURE__ */ jsxRuntime.jsx(
11310
+ ui.IconButton,
11311
+ {
11312
+ size: "small",
11313
+ type: "button",
11314
+ variant: "transparent",
11315
+ onClick: onRemove,
11316
+ isLoading: isPending || isLoading,
11317
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11318
+ }
11319
+ )
11320
+ ]
11321
+ },
11322
+ promotion.id
11544
11323
  );
11545
11324
  };
11546
- const schema$2 = objectType({
11547
- sales_channel_id: stringType().min(1)
11325
+ function getDisplayValue(promotion) {
11326
+ var _a, _b, _c, _d;
11327
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11328
+ if (!value) {
11329
+ return null;
11330
+ }
11331
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11332
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11333
+ if (!currency) {
11334
+ return null;
11335
+ }
11336
+ return getLocaleAmount(value, currency);
11337
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11338
+ return formatPercentage(value);
11339
+ }
11340
+ return null;
11341
+ }
11342
+ const formatter = new Intl.NumberFormat([], {
11343
+ style: "percent",
11344
+ minimumFractionDigits: 2
11548
11345
  });
11346
+ const formatPercentage = (value, isPercentageValue = false) => {
11347
+ let val = value || 0;
11348
+ if (!isPercentageValue) {
11349
+ val = val / 100;
11350
+ }
11351
+ return formatter.format(val);
11352
+ };
11353
+ function getPromotionCodes(items, shippingMethods) {
11354
+ const codes = /* @__PURE__ */ new Set();
11355
+ for (const item of items) {
11356
+ if (item.adjustments) {
11357
+ for (const adjustment of item.adjustments) {
11358
+ if (adjustment.code) {
11359
+ codes.add(adjustment.code);
11360
+ }
11361
+ }
11362
+ }
11363
+ }
11364
+ for (const shippingMethod of shippingMethods) {
11365
+ if (shippingMethod.adjustments) {
11366
+ for (const adjustment of shippingMethod.adjustments) {
11367
+ if (adjustment.code) {
11368
+ codes.add(adjustment.code);
11369
+ }
11370
+ }
11371
+ }
11372
+ }
11373
+ return Array.from(codes);
11374
+ }
11549
11375
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
11550
11376
  const Shipping = () => {
11551
11377
  var _a;
@@ -12353,6 +12179,112 @@ const CustomAmountField = ({
12353
12179
  }
12354
12180
  );
12355
12181
  };
12182
+ const SalesChannel = () => {
12183
+ const { id } = reactRouterDom.useParams();
12184
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12185
+ id,
12186
+ {
12187
+ fields: "+sales_channel_id"
12188
+ },
12189
+ {
12190
+ enabled: !!id
12191
+ }
12192
+ );
12193
+ if (isError) {
12194
+ throw error;
12195
+ }
12196
+ const ISrEADY = !!draft_order && !isPending;
12197
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12198
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12199
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
12200
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12201
+ ] }),
12202
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
12203
+ ] });
12204
+ };
12205
+ const SalesChannelForm = ({ order }) => {
12206
+ const form = reactHookForm.useForm({
12207
+ defaultValues: {
12208
+ sales_channel_id: order.sales_channel_id || ""
12209
+ },
12210
+ resolver: zod.zodResolver(schema$3)
12211
+ });
12212
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12213
+ const { handleSuccess } = useRouteModal();
12214
+ const onSubmit = form.handleSubmit(async (data) => {
12215
+ await mutateAsync(
12216
+ {
12217
+ sales_channel_id: data.sales_channel_id
12218
+ },
12219
+ {
12220
+ onSuccess: () => {
12221
+ ui.toast.success("Sales channel updated");
12222
+ handleSuccess();
12223
+ },
12224
+ onError: (error) => {
12225
+ ui.toast.error(error.message);
12226
+ }
12227
+ }
12228
+ );
12229
+ });
12230
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12231
+ KeyboundForm,
12232
+ {
12233
+ className: "flex flex-1 flex-col overflow-hidden",
12234
+ onSubmit,
12235
+ children: [
12236
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
12237
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12238
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12239
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12240
+ ] }) })
12241
+ ]
12242
+ }
12243
+ ) });
12244
+ };
12245
+ const SalesChannelField = ({ control, order }) => {
12246
+ const salesChannels = useComboboxData({
12247
+ queryFn: async (params) => {
12248
+ return await sdk.admin.salesChannel.list(params);
12249
+ },
12250
+ queryKey: ["sales-channels"],
12251
+ getOptions: (data) => {
12252
+ return data.sales_channels.map((salesChannel) => ({
12253
+ label: salesChannel.name,
12254
+ value: salesChannel.id
12255
+ }));
12256
+ },
12257
+ defaultValue: order.sales_channel_id || void 0
12258
+ });
12259
+ return /* @__PURE__ */ jsxRuntime.jsx(
12260
+ Form$2.Field,
12261
+ {
12262
+ control,
12263
+ name: "sales_channel_id",
12264
+ render: ({ field }) => {
12265
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12266
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
12267
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12268
+ Combobox,
12269
+ {
12270
+ options: salesChannels.options,
12271
+ fetchNextPage: salesChannels.fetchNextPage,
12272
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
12273
+ searchValue: salesChannels.searchValue,
12274
+ onSearchValueChange: salesChannels.onSearchValueChange,
12275
+ placeholder: "Select sales channel",
12276
+ ...field
12277
+ }
12278
+ ) }),
12279
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12280
+ ] });
12281
+ }
12282
+ }
12283
+ );
12284
+ };
12285
+ const schema$3 = objectType({
12286
+ sales_channel_id: stringType().min(1)
12287
+ });
12356
12288
  const ShippingAddress = () => {
12357
12289
  const { id } = reactRouterDom.useParams();
12358
12290
  const { order, isPending, isError, error } = useOrder(id, {
@@ -12385,7 +12317,7 @@ const ShippingAddressForm = ({ order }) => {
12385
12317
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12386
12318
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12387
12319
  },
12388
- resolver: zod.zodResolver(schema$1)
12320
+ resolver: zod.zodResolver(schema$2)
12389
12321
  });
12390
12322
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12391
12323
  const { handleSuccess } = useRouteModal();
@@ -12555,7 +12487,7 @@ const ShippingAddressForm = ({ order }) => {
12555
12487
  }
12556
12488
  ) });
12557
12489
  };
12558
- const schema$1 = addressSchema;
12490
+ const schema$2 = addressSchema;
12559
12491
  const TransferOwnership = () => {
12560
12492
  const { id } = reactRouterDom.useParams();
12561
12493
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12579,7 +12511,7 @@ const TransferOwnershipForm = ({ order }) => {
12579
12511
  defaultValues: {
12580
12512
  customer_id: order.customer_id || ""
12581
12513
  },
12582
- resolver: zod.zodResolver(schema)
12514
+ resolver: zod.zodResolver(schema$1)
12583
12515
  });
12584
12516
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12585
12517
  const { handleSuccess } = useRouteModal();
@@ -13029,9 +12961,77 @@ const Illustration = () => {
13029
12961
  }
13030
12962
  );
13031
12963
  };
13032
- const schema = objectType({
12964
+ const schema$1 = objectType({
13033
12965
  customer_id: stringType().min(1)
13034
12966
  });
12967
+ const Email = () => {
12968
+ const { id } = reactRouterDom.useParams();
12969
+ const { order, isPending, isError, error } = useOrder(id, {
12970
+ fields: "+email"
12971
+ });
12972
+ if (isError) {
12973
+ throw error;
12974
+ }
12975
+ const isReady = !isPending && !!order;
12976
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12977
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12978
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
12979
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
12980
+ ] }),
12981
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
12982
+ ] });
12983
+ };
12984
+ const EmailForm = ({ order }) => {
12985
+ const form = reactHookForm.useForm({
12986
+ defaultValues: {
12987
+ email: order.email ?? ""
12988
+ },
12989
+ resolver: zod.zodResolver(schema)
12990
+ });
12991
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12992
+ const { handleSuccess } = useRouteModal();
12993
+ const onSubmit = form.handleSubmit(async (data) => {
12994
+ await mutateAsync(
12995
+ { email: data.email },
12996
+ {
12997
+ onSuccess: () => {
12998
+ handleSuccess();
12999
+ },
13000
+ onError: (error) => {
13001
+ ui.toast.error(error.message);
13002
+ }
13003
+ }
13004
+ );
13005
+ });
13006
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
13007
+ KeyboundForm,
13008
+ {
13009
+ className: "flex flex-1 flex-col overflow-hidden",
13010
+ onSubmit,
13011
+ children: [
13012
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
13013
+ Form$2.Field,
13014
+ {
13015
+ control: form.control,
13016
+ name: "email",
13017
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13018
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
13019
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13020
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13021
+ ] })
13022
+ }
13023
+ ) }),
13024
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
13025
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13026
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13027
+ ] }) })
13028
+ ]
13029
+ }
13030
+ ) });
13031
+ };
13032
+ const schema = objectType({
13033
+ email: stringType().email()
13034
+ });
13035
13035
  const widgetModule = { widgets: [] };
13036
13036
  const routeModule = {
13037
13037
  routes: [
@@ -13060,30 +13060,26 @@ const routeModule = {
13060
13060
  Component: CustomItems,
13061
13061
  path: "/draft-orders/:id/custom-items"
13062
13062
  },
13063
- {
13064
- Component: Email,
13065
- path: "/draft-orders/:id/email"
13066
- },
13067
13063
  {
13068
13064
  Component: Items,
13069
13065
  path: "/draft-orders/:id/items"
13070
13066
  },
13071
- {
13072
- Component: Promotions,
13073
- path: "/draft-orders/:id/promotions"
13074
- },
13075
13067
  {
13076
13068
  Component: Metadata,
13077
13069
  path: "/draft-orders/:id/metadata"
13078
13070
  },
13079
13071
  {
13080
- Component: SalesChannel,
13081
- path: "/draft-orders/:id/sales-channel"
13072
+ Component: Promotions,
13073
+ path: "/draft-orders/:id/promotions"
13082
13074
  },
13083
13075
  {
13084
13076
  Component: Shipping,
13085
13077
  path: "/draft-orders/:id/shipping"
13086
13078
  },
13079
+ {
13080
+ Component: SalesChannel,
13081
+ path: "/draft-orders/:id/sales-channel"
13082
+ },
13087
13083
  {
13088
13084
  Component: ShippingAddress,
13089
13085
  path: "/draft-orders/:id/shipping-address"
@@ -13091,6 +13087,10 @@ const routeModule = {
13091
13087
  {
13092
13088
  Component: TransferOwnership,
13093
13089
  path: "/draft-orders/:id/transfer-ownership"
13090
+ },
13091
+ {
13092
+ Component: Email,
13093
+ path: "/draft-orders/:id/email"
13094
13094
  }
13095
13095
  ]
13096
13096
  }