@medusajs/draft-order 2.14.0-preview-20260423092943 → 2.14.0

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.
@@ -9899,196 +9899,6 @@ const ID = () => {
9899
9899
  /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Outlet, {})
9900
9900
  ] });
9901
9901
  };
9902
- const BillingAddress = () => {
9903
- const { id } = reactRouterDom.useParams();
9904
- const { order, isPending, isError, error } = useOrder(id, {
9905
- fields: "+billing_address"
9906
- });
9907
- if (isError) {
9908
- throw error;
9909
- }
9910
- const isReady = !isPending && !!order;
9911
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9912
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9913
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Billing Address" }) }),
9914
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
9915
- ] }),
9916
- isReady && /* @__PURE__ */ jsxRuntime.jsx(BillingAddressForm, { order })
9917
- ] });
9918
- };
9919
- const BillingAddressForm = ({ order }) => {
9920
- var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
9921
- const form = reactHookForm.useForm({
9922
- defaultValues: {
9923
- first_name: ((_a2 = order.billing_address) == null ? void 0 : _a2.first_name) ?? "",
9924
- last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
9925
- company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
9926
- address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
9927
- address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
9928
- city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
9929
- province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
9930
- country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
9931
- postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9932
- phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9933
- },
9934
- resolver: zod.zodResolver(schema$5)
9935
- });
9936
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9937
- const { handleSuccess } = useRouteModal();
9938
- const onSubmit = form.handleSubmit(async (data) => {
9939
- await mutateAsync(
9940
- { billing_address: data },
9941
- {
9942
- onSuccess: () => {
9943
- handleSuccess();
9944
- },
9945
- onError: (error) => {
9946
- ui.toast.error(error.message);
9947
- }
9948
- }
9949
- );
9950
- });
9951
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9952
- KeyboundForm,
9953
- {
9954
- className: "flex flex-1 flex-col overflow-hidden",
9955
- onSubmit,
9956
- children: [
9957
- /* @__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: [
9958
- /* @__PURE__ */ jsxRuntime.jsx(
9959
- Form$2.Field,
9960
- {
9961
- control: form.control,
9962
- name: "country_code",
9963
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9964
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
9965
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
9966
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9967
- ] })
9968
- }
9969
- ),
9970
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9971
- /* @__PURE__ */ jsxRuntime.jsx(
9972
- Form$2.Field,
9973
- {
9974
- control: form.control,
9975
- name: "first_name",
9976
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9977
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
9978
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9979
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9980
- ] })
9981
- }
9982
- ),
9983
- /* @__PURE__ */ jsxRuntime.jsx(
9984
- Form$2.Field,
9985
- {
9986
- control: form.control,
9987
- name: "last_name",
9988
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9989
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
9990
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9991
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9992
- ] })
9993
- }
9994
- )
9995
- ] }),
9996
- /* @__PURE__ */ jsxRuntime.jsx(
9997
- Form$2.Field,
9998
- {
9999
- control: form.control,
10000
- name: "company",
10001
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10002
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
10003
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10004
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10005
- ] })
10006
- }
10007
- ),
10008
- /* @__PURE__ */ jsxRuntime.jsx(
10009
- Form$2.Field,
10010
- {
10011
- control: form.control,
10012
- name: "address_1",
10013
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10014
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
10015
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10016
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10017
- ] })
10018
- }
10019
- ),
10020
- /* @__PURE__ */ jsxRuntime.jsx(
10021
- Form$2.Field,
10022
- {
10023
- control: form.control,
10024
- name: "address_2",
10025
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10026
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
10027
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10028
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10029
- ] })
10030
- }
10031
- ),
10032
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
10033
- /* @__PURE__ */ jsxRuntime.jsx(
10034
- Form$2.Field,
10035
- {
10036
- control: form.control,
10037
- name: "postal_code",
10038
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10039
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
10040
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10041
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10042
- ] })
10043
- }
10044
- ),
10045
- /* @__PURE__ */ jsxRuntime.jsx(
10046
- Form$2.Field,
10047
- {
10048
- control: form.control,
10049
- name: "city",
10050
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10051
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
10052
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10053
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10054
- ] })
10055
- }
10056
- )
10057
- ] }),
10058
- /* @__PURE__ */ jsxRuntime.jsx(
10059
- Form$2.Field,
10060
- {
10061
- control: form.control,
10062
- name: "province",
10063
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10064
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
10065
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10066
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10067
- ] })
10068
- }
10069
- ),
10070
- /* @__PURE__ */ jsxRuntime.jsx(
10071
- Form$2.Field,
10072
- {
10073
- control: form.control,
10074
- name: "phone",
10075
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10076
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
10077
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10078
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10079
- ] })
10080
- }
10081
- )
10082
- ] }) }),
10083
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10084
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10085
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10086
- ] }) })
10087
- ]
10088
- }
10089
- ) });
10090
- };
10091
- const schema$5 = addressSchema;
10092
9902
  const CustomItems = () => {
10093
9903
  return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10094
9904
  /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
@@ -10097,7 +9907,7 @@ const CustomItems = () => {
10097
9907
  };
10098
9908
  const CustomItemsForm = () => {
10099
9909
  const form = reactHookForm.useForm({
10100
- resolver: zod.zodResolver(schema$4)
9910
+ resolver: zod.zodResolver(schema$5)
10101
9911
  });
10102
9912
  return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
10103
9913
  /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
@@ -10107,93 +9917,25 @@ const CustomItemsForm = () => {
10107
9917
  ] }) })
10108
9918
  ] }) });
10109
9919
  };
10110
- const schema$4 = object({
9920
+ const schema$5 = object({
10111
9921
  email: string().email()
10112
9922
  });
10113
- const Email = () => {
10114
- const { id } = reactRouterDom.useParams();
10115
- const { order, isPending, isError, error } = useOrder(id, {
10116
- fields: "+email"
10117
- });
10118
- if (isError) {
10119
- throw error;
10120
- }
10121
- const isReady = !isPending && !!order;
10122
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10123
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10124
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
10125
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
10126
- ] }),
10127
- isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
10128
- ] });
10129
- };
10130
- const EmailForm = ({ order }) => {
10131
- const form = reactHookForm.useForm({
10132
- defaultValues: {
10133
- email: order.email ?? ""
10134
- },
10135
- resolver: zod.zodResolver(schema$3)
10136
- });
10137
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10138
- const { handleSuccess } = useRouteModal();
10139
- const onSubmit = form.handleSubmit(async (data) => {
10140
- await mutateAsync(
10141
- { email: data.email },
10142
- {
10143
- onSuccess: () => {
10144
- handleSuccess();
10145
- },
10146
- onError: (error) => {
10147
- ui.toast.error(error.message);
10148
- }
10149
- }
10150
- );
10151
- });
10152
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10153
- KeyboundForm,
10154
- {
10155
- className: "flex flex-1 flex-col overflow-hidden",
10156
- onSubmit,
10157
- children: [
10158
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
10159
- Form$2.Field,
10160
- {
10161
- control: form.control,
10162
- name: "email",
10163
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10164
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
10165
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10166
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10167
- ] })
10168
- }
10169
- ) }),
10170
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10171
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10172
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10173
- ] }) })
10174
- ]
10175
- }
10176
- ) });
10177
- };
10178
- const schema$3 = object({
10179
- email: string().email()
10180
- });
10181
- const NumberInput = React.forwardRef(
10182
- ({
10183
- value,
10184
- onChange,
10185
- size = "base",
10186
- min = 0,
10187
- max = 100,
10188
- step = 1,
10189
- className,
10190
- disabled,
10191
- ...props
10192
- }, ref) => {
10193
- const handleChange = (event) => {
10194
- const newValue = event.target.value === "" ? min : Number(event.target.value);
10195
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
10196
- onChange(newValue);
9923
+ const NumberInput = React.forwardRef(
9924
+ ({
9925
+ value,
9926
+ onChange,
9927
+ size = "base",
9928
+ min = 0,
9929
+ max = 100,
9930
+ step = 1,
9931
+ className,
9932
+ disabled,
9933
+ ...props
9934
+ }, ref) => {
9935
+ const handleChange = (event) => {
9936
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
9937
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9938
+ onChange(newValue);
10197
9939
  }
10198
9940
  };
10199
9941
  const handleIncrement = () => {
@@ -11152,201 +10894,809 @@ const customItemSchema = object({
11152
10894
  quantity: number(),
11153
10895
  unit_price: union([number(), string()])
11154
10896
  });
11155
- const PROMOTION_QUERY_KEY = "promotions";
11156
- const promotionsQueryKeys = {
11157
- list: (query2) => [
11158
- PROMOTION_QUERY_KEY,
11159
- query2 ? query2 : void 0
11160
- ],
11161
- detail: (id, query2) => [
11162
- PROMOTION_QUERY_KEY,
11163
- id,
11164
- query2 ? query2 : void 0
11165
- ]
11166
- };
11167
- const usePromotions = (query2, options) => {
11168
- const { data, ...rest } = reactQuery.useQuery({
11169
- queryKey: promotionsQueryKeys.list(query2),
11170
- queryFn: async () => sdk.admin.promotion.list(query2),
11171
- ...options
11172
- });
11173
- return { ...data, ...rest };
11174
- };
11175
- const Promotions = () => {
10897
+ const BillingAddress = () => {
11176
10898
  const { id } = reactRouterDom.useParams();
11177
- const {
11178
- order: preview,
11179
- isError: isPreviewError,
11180
- error: previewError
11181
- } = useOrderPreview(id, void 0);
11182
- useInitiateOrderEdit({ preview });
11183
- const { onCancel } = useCancelOrderEdit({ preview });
11184
- if (isPreviewError) {
11185
- throw previewError;
10899
+ const { order, isPending, isError, error } = useOrder(id, {
10900
+ fields: "+billing_address"
10901
+ });
10902
+ if (isError) {
10903
+ throw error;
11186
10904
  }
11187
- const isReady = !!preview;
11188
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11189
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11190
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
10905
+ const isReady = !isPending && !!order;
10906
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10907
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10908
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Billing Address" }) }),
10909
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
10910
+ ] }),
10911
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(BillingAddressForm, { order })
11191
10912
  ] });
11192
10913
  };
11193
- const PromotionForm = ({ preview }) => {
11194
- const { items, shipping_methods } = preview;
11195
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11196
- const [comboboxValue, setComboboxValue] = React.useState("");
11197
- const { handleSuccess } = useRouteModal();
11198
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11199
- const promoIds = getPromotionIds(items, shipping_methods);
11200
- const { promotions, isPending, isError, error } = usePromotions(
11201
- {
11202
- id: promoIds
11203
- },
11204
- {
11205
- enabled: !!promoIds.length
11206
- }
11207
- );
11208
- const comboboxData = useComboboxData({
11209
- queryKey: ["promotions", "combobox", promoIds],
11210
- queryFn: async (params) => {
11211
- return await sdk.admin.promotion.list({
11212
- ...params,
11213
- id: {
11214
- $nin: promoIds
11215
- }
11216
- });
10914
+ const BillingAddressForm = ({ order }) => {
10915
+ var _a2, _b, _c, _d, _e, _f, _g, _h, _i, _j;
10916
+ const form = reactHookForm.useForm({
10917
+ defaultValues: {
10918
+ first_name: ((_a2 = order.billing_address) == null ? void 0 : _a2.first_name) ?? "",
10919
+ last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
10920
+ company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
10921
+ address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
10922
+ address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
10923
+ city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
10924
+ province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
10925
+ country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
10926
+ postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
10927
+ phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
11217
10928
  },
11218
- getOptions: (data) => {
11219
- return data.promotions.map((promotion) => ({
11220
- label: promotion.code,
11221
- value: promotion.code
11222
- }));
11223
- }
10929
+ resolver: zod.zodResolver(schema$4)
11224
10930
  });
11225
- const add = async (value) => {
11226
- if (!value) {
11227
- return;
11228
- }
11229
- addPromotions(
11230
- {
11231
- promo_codes: [value]
11232
- },
10931
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10932
+ const { handleSuccess } = useRouteModal();
10933
+ const onSubmit = form.handleSubmit(async (data) => {
10934
+ await mutateAsync(
10935
+ { billing_address: data },
11233
10936
  {
11234
- onError: (e) => {
11235
- ui.toast.error(e.message);
11236
- comboboxData.onSearchValueChange("");
11237
- setComboboxValue("");
11238
- },
11239
10937
  onSuccess: () => {
11240
- comboboxData.onSearchValueChange("");
11241
- setComboboxValue("");
11242
- }
11243
- }
11244
- );
11245
- };
11246
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11247
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11248
- const onSubmit = async () => {
11249
- setIsSubmitting(true);
11250
- let requestSucceeded = false;
11251
- await requestOrderEdit(void 0, {
11252
- onError: (e) => {
11253
- ui.toast.error(e.message);
11254
- },
11255
- onSuccess: () => {
11256
- requestSucceeded = true;
11257
- }
11258
- });
11259
- if (!requestSucceeded) {
11260
- setIsSubmitting(false);
11261
- return;
11262
- }
11263
- await confirmOrderEdit(void 0, {
11264
- onError: (e) => {
11265
- ui.toast.error(e.message);
11266
- },
11267
- onSuccess: () => {
11268
- handleSuccess();
11269
- },
11270
- onSettled: () => {
11271
- setIsSubmitting(false);
11272
- }
11273
- });
11274
- };
11275
- if (isError) {
11276
- throw error;
11277
- }
11278
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11279
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11280
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11281
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11282
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11283
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11284
- ] }),
11285
- /* @__PURE__ */ jsxRuntime.jsx(
11286
- Combobox,
11287
- {
11288
- id: "promotion-combobox",
11289
- "aria-describedby": "promotion-combobox-hint",
11290
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11291
- fetchNextPage: comboboxData.fetchNextPage,
11292
- options: comboboxData.options,
11293
- onSearchValueChange: comboboxData.onSearchValueChange,
11294
- searchValue: comboboxData.searchValue,
11295
- disabled: comboboxData.disabled || isAddingPromotions,
11296
- onChange: add,
11297
- value: comboboxValue
11298
- }
11299
- )
11300
- ] }),
11301
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11302
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11303
- PromotionItem,
11304
- {
11305
- promotion,
11306
- orderId: preview.id,
11307
- isLoading: isPending
10938
+ handleSuccess();
11308
10939
  },
11309
- promotion.id
11310
- )) })
11311
- ] }) }),
11312
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11313
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11314
- /* @__PURE__ */ jsxRuntime.jsx(
11315
- ui.Button,
11316
- {
11317
- size: "small",
11318
- type: "submit",
11319
- isLoading: isSubmitting || isAddingPromotions,
11320
- children: "Save"
11321
- }
11322
- )
11323
- ] }) })
11324
- ] });
11325
- };
11326
- const PromotionItem = ({
11327
- promotion,
11328
- orderId,
11329
- isLoading
11330
- }) => {
11331
- var _a2;
11332
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11333
- const onRemove = async () => {
11334
- removePromotions(
11335
- {
11336
- promo_codes: [promotion.code]
11337
- },
11338
- {
11339
- onError: (e) => {
11340
- ui.toast.error(e.message);
10940
+ onError: (error) => {
10941
+ ui.toast.error(error.message);
11341
10942
  }
11342
10943
  }
11343
10944
  );
11344
- };
11345
- const displayValue = getDisplayValue(promotion);
11346
- return /* @__PURE__ */ jsxRuntime.jsxs(
11347
- "div",
10945
+ });
10946
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10947
+ KeyboundForm,
11348
10948
  {
11349
- className: ui.clx(
10949
+ className: "flex flex-1 flex-col overflow-hidden",
10950
+ onSubmit,
10951
+ children: [
10952
+ /* @__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: [
10953
+ /* @__PURE__ */ jsxRuntime.jsx(
10954
+ Form$2.Field,
10955
+ {
10956
+ control: form.control,
10957
+ name: "country_code",
10958
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10959
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
10960
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
10961
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10962
+ ] })
10963
+ }
10964
+ ),
10965
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
10966
+ /* @__PURE__ */ jsxRuntime.jsx(
10967
+ Form$2.Field,
10968
+ {
10969
+ control: form.control,
10970
+ name: "first_name",
10971
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10972
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
10973
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10974
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10975
+ ] })
10976
+ }
10977
+ ),
10978
+ /* @__PURE__ */ jsxRuntime.jsx(
10979
+ Form$2.Field,
10980
+ {
10981
+ control: form.control,
10982
+ name: "last_name",
10983
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10984
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
10985
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10986
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10987
+ ] })
10988
+ }
10989
+ )
10990
+ ] }),
10991
+ /* @__PURE__ */ jsxRuntime.jsx(
10992
+ Form$2.Field,
10993
+ {
10994
+ control: form.control,
10995
+ name: "company",
10996
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10997
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
10998
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10999
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11000
+ ] })
11001
+ }
11002
+ ),
11003
+ /* @__PURE__ */ jsxRuntime.jsx(
11004
+ Form$2.Field,
11005
+ {
11006
+ control: form.control,
11007
+ name: "address_1",
11008
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11009
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
11010
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11011
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11012
+ ] })
11013
+ }
11014
+ ),
11015
+ /* @__PURE__ */ jsxRuntime.jsx(
11016
+ Form$2.Field,
11017
+ {
11018
+ control: form.control,
11019
+ name: "address_2",
11020
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11021
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11022
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11023
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11024
+ ] })
11025
+ }
11026
+ ),
11027
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11028
+ /* @__PURE__ */ jsxRuntime.jsx(
11029
+ Form$2.Field,
11030
+ {
11031
+ control: form.control,
11032
+ name: "postal_code",
11033
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11034
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
11035
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11036
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11037
+ ] })
11038
+ }
11039
+ ),
11040
+ /* @__PURE__ */ jsxRuntime.jsx(
11041
+ Form$2.Field,
11042
+ {
11043
+ control: form.control,
11044
+ name: "city",
11045
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11046
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
11047
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11048
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11049
+ ] })
11050
+ }
11051
+ )
11052
+ ] }),
11053
+ /* @__PURE__ */ jsxRuntime.jsx(
11054
+ Form$2.Field,
11055
+ {
11056
+ control: form.control,
11057
+ name: "province",
11058
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11059
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11060
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11061
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11062
+ ] })
11063
+ }
11064
+ ),
11065
+ /* @__PURE__ */ jsxRuntime.jsx(
11066
+ Form$2.Field,
11067
+ {
11068
+ control: form.control,
11069
+ name: "phone",
11070
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11071
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
11072
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11073
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11074
+ ] })
11075
+ }
11076
+ )
11077
+ ] }) }),
11078
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11079
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11080
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11081
+ ] }) })
11082
+ ]
11083
+ }
11084
+ ) });
11085
+ };
11086
+ const schema$4 = addressSchema;
11087
+ const Email = () => {
11088
+ const { id } = reactRouterDom.useParams();
11089
+ const { order, isPending, isError, error } = useOrder(id, {
11090
+ fields: "+email"
11091
+ });
11092
+ if (isError) {
11093
+ throw error;
11094
+ }
11095
+ const isReady = !isPending && !!order;
11096
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11097
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11098
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
11099
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
11100
+ ] }),
11101
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
11102
+ ] });
11103
+ };
11104
+ const EmailForm = ({ order }) => {
11105
+ const form = reactHookForm.useForm({
11106
+ defaultValues: {
11107
+ email: order.email ?? ""
11108
+ },
11109
+ resolver: zod.zodResolver(schema$3)
11110
+ });
11111
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11112
+ const { handleSuccess } = useRouteModal();
11113
+ const onSubmit = form.handleSubmit(async (data) => {
11114
+ await mutateAsync(
11115
+ { email: data.email },
11116
+ {
11117
+ onSuccess: () => {
11118
+ handleSuccess();
11119
+ },
11120
+ onError: (error) => {
11121
+ ui.toast.error(error.message);
11122
+ }
11123
+ }
11124
+ );
11125
+ });
11126
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11127
+ KeyboundForm,
11128
+ {
11129
+ className: "flex flex-1 flex-col overflow-hidden",
11130
+ onSubmit,
11131
+ children: [
11132
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
11133
+ Form$2.Field,
11134
+ {
11135
+ control: form.control,
11136
+ name: "email",
11137
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11138
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
11139
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11140
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11141
+ ] })
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(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11147
+ ] }) })
11148
+ ]
11149
+ }
11150
+ ) });
11151
+ };
11152
+ const schema$3 = object({
11153
+ email: string().email()
11154
+ });
11155
+ const InlineTip = React.forwardRef(
11156
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
11157
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
11158
+ return /* @__PURE__ */ jsxRuntime.jsxs(
11159
+ "div",
11160
+ {
11161
+ ref,
11162
+ className: ui.clx(
11163
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
11164
+ className
11165
+ ),
11166
+ ...props,
11167
+ children: [
11168
+ /* @__PURE__ */ jsxRuntime.jsx(
11169
+ "div",
11170
+ {
11171
+ role: "presentation",
11172
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
11173
+ "bg-ui-tag-orange-icon": variant === "warning"
11174
+ })
11175
+ }
11176
+ ),
11177
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
11178
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
11179
+ labelValue,
11180
+ ":"
11181
+ ] }),
11182
+ " ",
11183
+ children
11184
+ ] })
11185
+ ]
11186
+ }
11187
+ );
11188
+ }
11189
+ );
11190
+ InlineTip.displayName = "InlineTip";
11191
+ const MetadataFieldSchema = object({
11192
+ key: string(),
11193
+ disabled: boolean().optional(),
11194
+ value: any()
11195
+ });
11196
+ const MetadataSchema = object({
11197
+ metadata: array(MetadataFieldSchema)
11198
+ });
11199
+ const Metadata = () => {
11200
+ const { id } = reactRouterDom.useParams();
11201
+ const { order, isPending, isError, error } = useOrder(id, {
11202
+ fields: "metadata"
11203
+ });
11204
+ if (isError) {
11205
+ throw error;
11206
+ }
11207
+ const isReady = !isPending && !!order;
11208
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11209
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11210
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
11211
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
11212
+ ] }),
11213
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
11214
+ ] });
11215
+ };
11216
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
11217
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
11218
+ const MetadataForm = ({ orderId, metadata }) => {
11219
+ const { handleSuccess } = useRouteModal();
11220
+ const hasUneditableRows = getHasUneditableRows(metadata);
11221
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
11222
+ const form = reactHookForm.useForm({
11223
+ defaultValues: {
11224
+ metadata: getDefaultValues(metadata)
11225
+ },
11226
+ resolver: zod.zodResolver(MetadataSchema)
11227
+ });
11228
+ const handleSubmit = form.handleSubmit(async (data) => {
11229
+ const parsedData = parseValues(data);
11230
+ await mutateAsync(
11231
+ {
11232
+ metadata: parsedData
11233
+ },
11234
+ {
11235
+ onSuccess: () => {
11236
+ ui.toast.success("Metadata updated");
11237
+ handleSuccess();
11238
+ },
11239
+ onError: (error) => {
11240
+ ui.toast.error(error.message);
11241
+ }
11242
+ }
11243
+ );
11244
+ });
11245
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
11246
+ control: form.control,
11247
+ name: "metadata"
11248
+ });
11249
+ function deleteRow(index) {
11250
+ remove(index);
11251
+ if (fields.length === 1) {
11252
+ insert(0, {
11253
+ key: "",
11254
+ value: "",
11255
+ disabled: false
11256
+ });
11257
+ }
11258
+ }
11259
+ function insertRow(index, position) {
11260
+ insert(index + (position === "above" ? 0 : 1), {
11261
+ key: "",
11262
+ value: "",
11263
+ disabled: false
11264
+ });
11265
+ }
11266
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11267
+ KeyboundForm,
11268
+ {
11269
+ onSubmit: handleSubmit,
11270
+ className: "flex flex-1 flex-col overflow-hidden",
11271
+ children: [
11272
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
11273
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
11274
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
11275
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
11276
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
11277
+ ] }),
11278
+ fields.map((field, index) => {
11279
+ const isDisabled = field.disabled || false;
11280
+ let placeholder = "-";
11281
+ if (typeof field.value === "object") {
11282
+ placeholder = "{ ... }";
11283
+ }
11284
+ if (Array.isArray(field.value)) {
11285
+ placeholder = "[ ... ]";
11286
+ }
11287
+ return /* @__PURE__ */ jsxRuntime.jsx(
11288
+ ConditionalTooltip,
11289
+ {
11290
+ showTooltip: isDisabled,
11291
+ content: "This row is disabled because it contains non-primitive data.",
11292
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
11293
+ /* @__PURE__ */ jsxRuntime.jsxs(
11294
+ "div",
11295
+ {
11296
+ className: ui.clx("grid grid-cols-2 divide-x", {
11297
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
11298
+ }),
11299
+ children: [
11300
+ /* @__PURE__ */ jsxRuntime.jsx(
11301
+ Form$2.Field,
11302
+ {
11303
+ control: form.control,
11304
+ name: `metadata.${index}.key`,
11305
+ render: ({ field: field2 }) => {
11306
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11307
+ GridInput,
11308
+ {
11309
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
11310
+ ...field2,
11311
+ disabled: isDisabled,
11312
+ placeholder: "Key"
11313
+ }
11314
+ ) }) });
11315
+ }
11316
+ }
11317
+ ),
11318
+ /* @__PURE__ */ jsxRuntime.jsx(
11319
+ Form$2.Field,
11320
+ {
11321
+ control: form.control,
11322
+ name: `metadata.${index}.value`,
11323
+ render: ({ field: { value, ...field2 } }) => {
11324
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11325
+ GridInput,
11326
+ {
11327
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
11328
+ ...field2,
11329
+ value: isDisabled ? placeholder : value,
11330
+ disabled: isDisabled,
11331
+ placeholder: "Value"
11332
+ }
11333
+ ) }) });
11334
+ }
11335
+ }
11336
+ )
11337
+ ]
11338
+ }
11339
+ ),
11340
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
11341
+ /* @__PURE__ */ jsxRuntime.jsx(
11342
+ ui.DropdownMenu.Trigger,
11343
+ {
11344
+ className: ui.clx(
11345
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11346
+ {
11347
+ hidden: isDisabled
11348
+ }
11349
+ ),
11350
+ disabled: isDisabled,
11351
+ asChild: true,
11352
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
11353
+ }
11354
+ ),
11355
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11356
+ /* @__PURE__ */ jsxRuntime.jsxs(
11357
+ ui.DropdownMenu.Item,
11358
+ {
11359
+ className: "gap-x-2",
11360
+ onClick: () => insertRow(index, "above"),
11361
+ children: [
11362
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11363
+ "Insert row above"
11364
+ ]
11365
+ }
11366
+ ),
11367
+ /* @__PURE__ */ jsxRuntime.jsxs(
11368
+ ui.DropdownMenu.Item,
11369
+ {
11370
+ className: "gap-x-2",
11371
+ onClick: () => insertRow(index, "below"),
11372
+ children: [
11373
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11374
+ "Insert row below"
11375
+ ]
11376
+ }
11377
+ ),
11378
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11379
+ /* @__PURE__ */ jsxRuntime.jsxs(
11380
+ ui.DropdownMenu.Item,
11381
+ {
11382
+ className: "gap-x-2",
11383
+ onClick: () => deleteRow(index),
11384
+ children: [
11385
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11386
+ "Delete row"
11387
+ ]
11388
+ }
11389
+ )
11390
+ ] })
11391
+ ] })
11392
+ ] })
11393
+ },
11394
+ field.id
11395
+ );
11396
+ })
11397
+ ] }),
11398
+ hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
11399
+ ] }),
11400
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11401
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11402
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11403
+ ] }) })
11404
+ ]
11405
+ }
11406
+ ) });
11407
+ };
11408
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11409
+ return /* @__PURE__ */ jsxRuntime.jsx(
11410
+ "input",
11411
+ {
11412
+ ref,
11413
+ ...props,
11414
+ autoComplete: "off",
11415
+ className: ui.clx(
11416
+ "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
11417
+ className
11418
+ )
11419
+ }
11420
+ );
11421
+ });
11422
+ GridInput.displayName = "MetadataForm.GridInput";
11423
+ const PlaceholderInner = () => {
11424
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11425
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11426
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11427
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11428
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11429
+ ] }) })
11430
+ ] });
11431
+ };
11432
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
11433
+ function getDefaultValues(metadata) {
11434
+ if (!metadata || !Object.keys(metadata).length) {
11435
+ return [
11436
+ {
11437
+ key: "",
11438
+ value: "",
11439
+ disabled: false
11440
+ }
11441
+ ];
11442
+ }
11443
+ return Object.entries(metadata).map(([key, value]) => {
11444
+ if (!EDITABLE_TYPES.includes(typeof value)) {
11445
+ return {
11446
+ key,
11447
+ value,
11448
+ disabled: true
11449
+ };
11450
+ }
11451
+ let stringValue = value;
11452
+ if (typeof value !== "string") {
11453
+ stringValue = JSON.stringify(value);
11454
+ }
11455
+ return {
11456
+ key,
11457
+ value: stringValue,
11458
+ original_key: key
11459
+ };
11460
+ });
11461
+ }
11462
+ function parseValues(values) {
11463
+ const metadata = values.metadata;
11464
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11465
+ if (isEmpty) {
11466
+ return null;
11467
+ }
11468
+ const update = {};
11469
+ metadata.forEach((field) => {
11470
+ let key = field.key;
11471
+ let value = field.value;
11472
+ const disabled = field.disabled;
11473
+ if (!key || !value) {
11474
+ return;
11475
+ }
11476
+ if (disabled) {
11477
+ update[key] = value;
11478
+ return;
11479
+ }
11480
+ key = key.trim();
11481
+ value = value.trim();
11482
+ if (value === "true") {
11483
+ update[key] = true;
11484
+ } else if (value === "false") {
11485
+ update[key] = false;
11486
+ } else {
11487
+ const parsedNumber = parseFloat(value);
11488
+ if (!isNaN(parsedNumber)) {
11489
+ update[key] = parsedNumber;
11490
+ } else {
11491
+ update[key] = value;
11492
+ }
11493
+ }
11494
+ });
11495
+ return update;
11496
+ }
11497
+ function getHasUneditableRows(metadata) {
11498
+ if (!metadata) {
11499
+ return false;
11500
+ }
11501
+ return Object.values(metadata).some(
11502
+ (value) => !EDITABLE_TYPES.includes(typeof value)
11503
+ );
11504
+ }
11505
+ const PROMOTION_QUERY_KEY = "promotions";
11506
+ const promotionsQueryKeys = {
11507
+ list: (query2) => [
11508
+ PROMOTION_QUERY_KEY,
11509
+ query2 ? query2 : void 0
11510
+ ],
11511
+ detail: (id, query2) => [
11512
+ PROMOTION_QUERY_KEY,
11513
+ id,
11514
+ query2 ? query2 : void 0
11515
+ ]
11516
+ };
11517
+ const usePromotions = (query2, options) => {
11518
+ const { data, ...rest } = reactQuery.useQuery({
11519
+ queryKey: promotionsQueryKeys.list(query2),
11520
+ queryFn: async () => sdk.admin.promotion.list(query2),
11521
+ ...options
11522
+ });
11523
+ return { ...data, ...rest };
11524
+ };
11525
+ const Promotions = () => {
11526
+ const { id } = reactRouterDom.useParams();
11527
+ const {
11528
+ order: preview,
11529
+ isError: isPreviewError,
11530
+ error: previewError
11531
+ } = useOrderPreview(id, void 0);
11532
+ useInitiateOrderEdit({ preview });
11533
+ const { onCancel } = useCancelOrderEdit({ preview });
11534
+ if (isPreviewError) {
11535
+ throw previewError;
11536
+ }
11537
+ const isReady = !!preview;
11538
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11539
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11540
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11541
+ ] });
11542
+ };
11543
+ const PromotionForm = ({ preview }) => {
11544
+ const { items, shipping_methods } = preview;
11545
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
11546
+ const [comboboxValue, setComboboxValue] = React.useState("");
11547
+ const { handleSuccess } = useRouteModal();
11548
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11549
+ const promoIds = getPromotionIds(items, shipping_methods);
11550
+ const { promotions, isPending, isError, error } = usePromotions(
11551
+ {
11552
+ id: promoIds
11553
+ },
11554
+ {
11555
+ enabled: !!promoIds.length
11556
+ }
11557
+ );
11558
+ const comboboxData = useComboboxData({
11559
+ queryKey: ["promotions", "combobox", promoIds],
11560
+ queryFn: async (params) => {
11561
+ return await sdk.admin.promotion.list({
11562
+ ...params,
11563
+ id: {
11564
+ $nin: promoIds
11565
+ }
11566
+ });
11567
+ },
11568
+ getOptions: (data) => {
11569
+ return data.promotions.map((promotion) => ({
11570
+ label: promotion.code,
11571
+ value: promotion.code
11572
+ }));
11573
+ }
11574
+ });
11575
+ const add = async (value) => {
11576
+ if (!value) {
11577
+ return;
11578
+ }
11579
+ addPromotions(
11580
+ {
11581
+ promo_codes: [value]
11582
+ },
11583
+ {
11584
+ onError: (e) => {
11585
+ ui.toast.error(e.message);
11586
+ comboboxData.onSearchValueChange("");
11587
+ setComboboxValue("");
11588
+ },
11589
+ onSuccess: () => {
11590
+ comboboxData.onSearchValueChange("");
11591
+ setComboboxValue("");
11592
+ }
11593
+ }
11594
+ );
11595
+ };
11596
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11597
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11598
+ const onSubmit = async () => {
11599
+ setIsSubmitting(true);
11600
+ let requestSucceeded = false;
11601
+ await requestOrderEdit(void 0, {
11602
+ onError: (e) => {
11603
+ ui.toast.error(e.message);
11604
+ },
11605
+ onSuccess: () => {
11606
+ requestSucceeded = true;
11607
+ }
11608
+ });
11609
+ if (!requestSucceeded) {
11610
+ setIsSubmitting(false);
11611
+ return;
11612
+ }
11613
+ await confirmOrderEdit(void 0, {
11614
+ onError: (e) => {
11615
+ ui.toast.error(e.message);
11616
+ },
11617
+ onSuccess: () => {
11618
+ handleSuccess();
11619
+ },
11620
+ onSettled: () => {
11621
+ setIsSubmitting(false);
11622
+ }
11623
+ });
11624
+ };
11625
+ if (isError) {
11626
+ throw error;
11627
+ }
11628
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11629
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11630
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11631
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11632
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11633
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11634
+ ] }),
11635
+ /* @__PURE__ */ jsxRuntime.jsx(
11636
+ Combobox,
11637
+ {
11638
+ id: "promotion-combobox",
11639
+ "aria-describedby": "promotion-combobox-hint",
11640
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
11641
+ fetchNextPage: comboboxData.fetchNextPage,
11642
+ options: comboboxData.options,
11643
+ onSearchValueChange: comboboxData.onSearchValueChange,
11644
+ searchValue: comboboxData.searchValue,
11645
+ disabled: comboboxData.disabled || isAddingPromotions,
11646
+ onChange: add,
11647
+ value: comboboxValue
11648
+ }
11649
+ )
11650
+ ] }),
11651
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11652
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11653
+ PromotionItem,
11654
+ {
11655
+ promotion,
11656
+ orderId: preview.id,
11657
+ isLoading: isPending
11658
+ },
11659
+ promotion.id
11660
+ )) })
11661
+ ] }) }),
11662
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11663
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11664
+ /* @__PURE__ */ jsxRuntime.jsx(
11665
+ ui.Button,
11666
+ {
11667
+ size: "small",
11668
+ type: "submit",
11669
+ isLoading: isSubmitting || isAddingPromotions,
11670
+ children: "Save"
11671
+ }
11672
+ )
11673
+ ] }) })
11674
+ ] });
11675
+ };
11676
+ const PromotionItem = ({
11677
+ promotion,
11678
+ orderId,
11679
+ isLoading
11680
+ }) => {
11681
+ var _a2;
11682
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11683
+ const onRemove = async () => {
11684
+ removePromotions(
11685
+ {
11686
+ promo_codes: [promotion.code]
11687
+ },
11688
+ {
11689
+ onError: (e) => {
11690
+ ui.toast.error(e.message);
11691
+ }
11692
+ }
11693
+ );
11694
+ };
11695
+ const displayValue = getDisplayValue(promotion);
11696
+ return /* @__PURE__ */ jsxRuntime.jsxs(
11697
+ "div",
11698
+ {
11699
+ className: ui.clx(
11350
11700
  "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11351
11701
  {
11352
11702
  "animate-pulse": isLoading
@@ -11425,360 +11775,116 @@ function getPromotionIds(items, shippingMethods) {
11425
11775
  promotionIds.add(adjustment.promotion_id);
11426
11776
  }
11427
11777
  }
11428
- }
11429
- }
11430
- return Array.from(promotionIds);
11431
- }
11432
- const InlineTip = React.forwardRef(
11433
- ({ variant = "tip", label, className, children, ...props }, ref) => {
11434
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
11435
- return /* @__PURE__ */ jsxRuntime.jsxs(
11436
- "div",
11437
- {
11438
- ref,
11439
- className: ui.clx(
11440
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
11441
- className
11442
- ),
11443
- ...props,
11444
- children: [
11445
- /* @__PURE__ */ jsxRuntime.jsx(
11446
- "div",
11447
- {
11448
- role: "presentation",
11449
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
11450
- "bg-ui-tag-orange-icon": variant === "warning"
11451
- })
11452
- }
11453
- ),
11454
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
11455
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
11456
- labelValue,
11457
- ":"
11458
- ] }),
11459
- " ",
11460
- children
11461
- ] })
11462
- ]
11463
- }
11464
- );
11778
+ }
11465
11779
  }
11466
- );
11467
- InlineTip.displayName = "InlineTip";
11468
- const MetadataFieldSchema = object({
11469
- key: string(),
11470
- disabled: boolean().optional(),
11471
- value: any()
11472
- });
11473
- const MetadataSchema = object({
11474
- metadata: array(MetadataFieldSchema)
11475
- });
11476
- const Metadata = () => {
11780
+ return Array.from(promotionIds);
11781
+ }
11782
+ const SalesChannel = () => {
11477
11783
  const { id } = reactRouterDom.useParams();
11478
- const { order, isPending, isError, error } = useOrder(id, {
11479
- fields: "metadata"
11480
- });
11784
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11785
+ id,
11786
+ {
11787
+ fields: "+sales_channel_id"
11788
+ },
11789
+ {
11790
+ enabled: !!id
11791
+ }
11792
+ );
11481
11793
  if (isError) {
11482
11794
  throw error;
11483
11795
  }
11484
- const isReady = !isPending && !!order;
11796
+ const ISrEADY = !!draft_order && !isPending;
11485
11797
  return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11486
11798
  /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11487
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
11488
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
11799
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11800
+ /* @__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" }) })
11489
11801
  ] }),
11490
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
11802
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11491
11803
  ] });
11492
11804
  };
11493
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
11494
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
11495
- const MetadataForm = ({ orderId, metadata }) => {
11496
- const { handleSuccess } = useRouteModal();
11497
- const hasUneditableRows = getHasUneditableRows(metadata);
11498
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
11805
+ const SalesChannelForm = ({ order }) => {
11499
11806
  const form = reactHookForm.useForm({
11500
11807
  defaultValues: {
11501
- metadata: getDefaultValues(metadata)
11808
+ sales_channel_id: order.sales_channel_id || ""
11502
11809
  },
11503
- resolver: zod.zodResolver(MetadataSchema)
11810
+ resolver: zod.zodResolver(schema$2)
11504
11811
  });
11505
- const handleSubmit = form.handleSubmit(async (data) => {
11506
- const parsedData = parseValues(data);
11812
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11813
+ const { handleSuccess } = useRouteModal();
11814
+ const onSubmit = form.handleSubmit(async (data) => {
11507
11815
  await mutateAsync(
11508
11816
  {
11509
- metadata: parsedData
11510
- },
11511
- {
11512
- onSuccess: () => {
11513
- ui.toast.success("Metadata updated");
11514
- handleSuccess();
11515
- },
11516
- onError: (error) => {
11517
- ui.toast.error(error.message);
11518
- }
11519
- }
11520
- );
11521
- });
11522
- const { fields, insert, remove } = reactHookForm.useFieldArray({
11523
- control: form.control,
11524
- name: "metadata"
11525
- });
11526
- function deleteRow(index) {
11527
- remove(index);
11528
- if (fields.length === 1) {
11529
- insert(0, {
11530
- key: "",
11531
- value: "",
11532
- disabled: false
11533
- });
11534
- }
11535
- }
11536
- function insertRow(index, position) {
11537
- insert(index + (position === "above" ? 0 : 1), {
11538
- key: "",
11539
- value: "",
11540
- disabled: false
11541
- });
11542
- }
11543
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11544
- KeyboundForm,
11545
- {
11546
- onSubmit: handleSubmit,
11547
- className: "flex flex-1 flex-col overflow-hidden",
11548
- children: [
11549
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
11550
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
11551
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
11552
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
11553
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
11554
- ] }),
11555
- fields.map((field, index) => {
11556
- const isDisabled = field.disabled || false;
11557
- let placeholder = "-";
11558
- if (typeof field.value === "object") {
11559
- placeholder = "{ ... }";
11560
- }
11561
- if (Array.isArray(field.value)) {
11562
- placeholder = "[ ... ]";
11563
- }
11564
- return /* @__PURE__ */ jsxRuntime.jsx(
11565
- ConditionalTooltip,
11566
- {
11567
- showTooltip: isDisabled,
11568
- content: "This row is disabled because it contains non-primitive data.",
11569
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
11570
- /* @__PURE__ */ jsxRuntime.jsxs(
11571
- "div",
11572
- {
11573
- className: ui.clx("grid grid-cols-2 divide-x", {
11574
- "overflow-hidden rounded-b-lg": index === fields.length - 1
11575
- }),
11576
- children: [
11577
- /* @__PURE__ */ jsxRuntime.jsx(
11578
- Form$2.Field,
11579
- {
11580
- control: form.control,
11581
- name: `metadata.${index}.key`,
11582
- render: ({ field: field2 }) => {
11583
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11584
- GridInput,
11585
- {
11586
- "aria-labelledby": METADATA_KEY_LABEL_ID,
11587
- ...field2,
11588
- disabled: isDisabled,
11589
- placeholder: "Key"
11590
- }
11591
- ) }) });
11592
- }
11593
- }
11594
- ),
11595
- /* @__PURE__ */ jsxRuntime.jsx(
11596
- Form$2.Field,
11597
- {
11598
- control: form.control,
11599
- name: `metadata.${index}.value`,
11600
- render: ({ field: { value, ...field2 } }) => {
11601
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11602
- GridInput,
11603
- {
11604
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
11605
- ...field2,
11606
- value: isDisabled ? placeholder : value,
11607
- disabled: isDisabled,
11608
- placeholder: "Value"
11609
- }
11610
- ) }) });
11611
- }
11612
- }
11613
- )
11614
- ]
11615
- }
11616
- ),
11617
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
11618
- /* @__PURE__ */ jsxRuntime.jsx(
11619
- ui.DropdownMenu.Trigger,
11620
- {
11621
- className: ui.clx(
11622
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11623
- {
11624
- hidden: isDisabled
11625
- }
11626
- ),
11627
- disabled: isDisabled,
11628
- asChild: true,
11629
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
11630
- }
11631
- ),
11632
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11633
- /* @__PURE__ */ jsxRuntime.jsxs(
11634
- ui.DropdownMenu.Item,
11635
- {
11636
- className: "gap-x-2",
11637
- onClick: () => insertRow(index, "above"),
11638
- children: [
11639
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11640
- "Insert row above"
11641
- ]
11642
- }
11643
- ),
11644
- /* @__PURE__ */ jsxRuntime.jsxs(
11645
- ui.DropdownMenu.Item,
11646
- {
11647
- className: "gap-x-2",
11648
- onClick: () => insertRow(index, "below"),
11649
- children: [
11650
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11651
- "Insert row below"
11652
- ]
11653
- }
11654
- ),
11655
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11656
- /* @__PURE__ */ jsxRuntime.jsxs(
11657
- ui.DropdownMenu.Item,
11658
- {
11659
- className: "gap-x-2",
11660
- onClick: () => deleteRow(index),
11661
- children: [
11662
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11663
- "Delete row"
11664
- ]
11665
- }
11666
- )
11667
- ] })
11668
- ] })
11669
- ] })
11670
- },
11671
- field.id
11672
- );
11673
- })
11674
- ] }),
11675
- hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
11676
- ] }),
11677
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11678
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11679
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11680
- ] }) })
11681
- ]
11682
- }
11683
- ) });
11684
- };
11685
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11686
- return /* @__PURE__ */ jsxRuntime.jsx(
11687
- "input",
11688
- {
11689
- ref,
11690
- ...props,
11691
- autoComplete: "off",
11692
- className: ui.clx(
11693
- "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
11694
- className
11695
- )
11696
- }
11697
- );
11698
- });
11699
- GridInput.displayName = "MetadataForm.GridInput";
11700
- const PlaceholderInner = () => {
11701
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11702
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11703
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11704
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11705
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11706
- ] }) })
11707
- ] });
11708
- };
11709
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11710
- function getDefaultValues(metadata) {
11711
- if (!metadata || !Object.keys(metadata).length) {
11712
- return [
11817
+ sales_channel_id: data.sales_channel_id
11818
+ },
11713
11819
  {
11714
- key: "",
11715
- value: "",
11716
- disabled: false
11820
+ onSuccess: () => {
11821
+ ui.toast.success("Sales channel updated");
11822
+ handleSuccess();
11823
+ },
11824
+ onError: (error) => {
11825
+ ui.toast.error(error.message);
11826
+ }
11717
11827
  }
11718
- ];
11719
- }
11720
- return Object.entries(metadata).map(([key, value]) => {
11721
- if (!EDITABLE_TYPES.includes(typeof value)) {
11722
- return {
11723
- key,
11724
- value,
11725
- disabled: true
11726
- };
11727
- }
11728
- let stringValue = value;
11729
- if (typeof value !== "string") {
11730
- stringValue = JSON.stringify(value);
11731
- }
11732
- return {
11733
- key,
11734
- value: stringValue,
11735
- original_key: key
11736
- };
11828
+ );
11737
11829
  });
11738
- }
11739
- function parseValues(values) {
11740
- const metadata = values.metadata;
11741
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11742
- if (isEmpty) {
11743
- return null;
11744
- }
11745
- const update = {};
11746
- metadata.forEach((field) => {
11747
- let key = field.key;
11748
- let value = field.value;
11749
- const disabled = field.disabled;
11750
- if (!key || !value) {
11751
- return;
11752
- }
11753
- if (disabled) {
11754
- update[key] = value;
11755
- return;
11830
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11831
+ KeyboundForm,
11832
+ {
11833
+ className: "flex flex-1 flex-col overflow-hidden",
11834
+ onSubmit,
11835
+ children: [
11836
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11837
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11838
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11839
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11840
+ ] }) })
11841
+ ]
11756
11842
  }
11757
- key = key.trim();
11758
- value = value.trim();
11759
- if (value === "true") {
11760
- update[key] = true;
11761
- } else if (value === "false") {
11762
- update[key] = false;
11763
- } else {
11764
- const parsedNumber = parseFloat(value);
11765
- if (!isNaN(parsedNumber)) {
11766
- update[key] = parsedNumber;
11767
- } else {
11768
- update[key] = value;
11843
+ ) });
11844
+ };
11845
+ const SalesChannelField = ({ control, order }) => {
11846
+ const salesChannels = useComboboxData({
11847
+ queryFn: async (params) => {
11848
+ return await sdk.admin.salesChannel.list(params);
11849
+ },
11850
+ queryKey: ["sales-channels"],
11851
+ getOptions: (data) => {
11852
+ return data.sales_channels.map((salesChannel) => ({
11853
+ label: salesChannel.name,
11854
+ value: salesChannel.id
11855
+ }));
11856
+ },
11857
+ defaultValue: order.sales_channel_id || void 0
11858
+ });
11859
+ return /* @__PURE__ */ jsxRuntime.jsx(
11860
+ Form$2.Field,
11861
+ {
11862
+ control,
11863
+ name: "sales_channel_id",
11864
+ render: ({ field }) => {
11865
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11866
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11867
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11868
+ Combobox,
11869
+ {
11870
+ options: salesChannels.options,
11871
+ fetchNextPage: salesChannels.fetchNextPage,
11872
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11873
+ searchValue: salesChannels.searchValue,
11874
+ onSearchValueChange: salesChannels.onSearchValueChange,
11875
+ placeholder: "Select sales channel",
11876
+ ...field
11877
+ }
11878
+ ) }),
11879
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11880
+ ] });
11769
11881
  }
11770
11882
  }
11771
- });
11772
- return update;
11773
- }
11774
- function getHasUneditableRows(metadata) {
11775
- if (!metadata) {
11776
- return false;
11777
- }
11778
- return Object.values(metadata).some(
11779
- (value) => !EDITABLE_TYPES.includes(typeof value)
11780
11883
  );
11781
- }
11884
+ };
11885
+ const schema$2 = object({
11886
+ sales_channel_id: string().min(1)
11887
+ });
11782
11888
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
11783
11889
  const Shipping = () => {
11784
11890
  var _a2;
@@ -12586,112 +12692,6 @@ const CustomAmountField = ({
12586
12692
  }
12587
12693
  );
12588
12694
  };
12589
- const SalesChannel = () => {
12590
- const { id } = reactRouterDom.useParams();
12591
- const { draft_order, isPending, isError, error } = useDraftOrder(
12592
- id,
12593
- {
12594
- fields: "+sales_channel_id"
12595
- },
12596
- {
12597
- enabled: !!id
12598
- }
12599
- );
12600
- if (isError) {
12601
- throw error;
12602
- }
12603
- const ISrEADY = !!draft_order && !isPending;
12604
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12605
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12606
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
12607
- /* @__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" }) })
12608
- ] }),
12609
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
12610
- ] });
12611
- };
12612
- const SalesChannelForm = ({ order }) => {
12613
- const form = reactHookForm.useForm({
12614
- defaultValues: {
12615
- sales_channel_id: order.sales_channel_id || ""
12616
- },
12617
- resolver: zod.zodResolver(schema$2)
12618
- });
12619
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12620
- const { handleSuccess } = useRouteModal();
12621
- const onSubmit = form.handleSubmit(async (data) => {
12622
- await mutateAsync(
12623
- {
12624
- sales_channel_id: data.sales_channel_id
12625
- },
12626
- {
12627
- onSuccess: () => {
12628
- ui.toast.success("Sales channel updated");
12629
- handleSuccess();
12630
- },
12631
- onError: (error) => {
12632
- ui.toast.error(error.message);
12633
- }
12634
- }
12635
- );
12636
- });
12637
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12638
- KeyboundForm,
12639
- {
12640
- className: "flex flex-1 flex-col overflow-hidden",
12641
- onSubmit,
12642
- children: [
12643
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
12644
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12645
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12646
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12647
- ] }) })
12648
- ]
12649
- }
12650
- ) });
12651
- };
12652
- const SalesChannelField = ({ control, order }) => {
12653
- const salesChannels = useComboboxData({
12654
- queryFn: async (params) => {
12655
- return await sdk.admin.salesChannel.list(params);
12656
- },
12657
- queryKey: ["sales-channels"],
12658
- getOptions: (data) => {
12659
- return data.sales_channels.map((salesChannel) => ({
12660
- label: salesChannel.name,
12661
- value: salesChannel.id
12662
- }));
12663
- },
12664
- defaultValue: order.sales_channel_id || void 0
12665
- });
12666
- return /* @__PURE__ */ jsxRuntime.jsx(
12667
- Form$2.Field,
12668
- {
12669
- control,
12670
- name: "sales_channel_id",
12671
- render: ({ field }) => {
12672
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12673
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
12674
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12675
- Combobox,
12676
- {
12677
- options: salesChannels.options,
12678
- fetchNextPage: salesChannels.fetchNextPage,
12679
- isFetchingNextPage: salesChannels.isFetchingNextPage,
12680
- searchValue: salesChannels.searchValue,
12681
- onSearchValueChange: salesChannels.onSearchValueChange,
12682
- placeholder: "Select sales channel",
12683
- ...field
12684
- }
12685
- ) }),
12686
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12687
- ] });
12688
- }
12689
- }
12690
- );
12691
- };
12692
- const schema$2 = object({
12693
- sales_channel_id: string().min(1)
12694
- });
12695
12695
  const ShippingAddress = () => {
12696
12696
  const { id } = reactRouterDom.useParams();
12697
12697
  const { order, isPending, isError, error } = useOrder(id, {
@@ -13391,38 +13391,38 @@ const routeModule = {
13391
13391
  handle,
13392
13392
  loader,
13393
13393
  children: [
13394
- {
13395
- Component: BillingAddress,
13396
- path: "/draft-orders/:id/billing-address"
13397
- },
13398
13394
  {
13399
13395
  Component: CustomItems,
13400
13396
  path: "/draft-orders/:id/custom-items"
13401
13397
  },
13402
- {
13403
- Component: Email,
13404
- path: "/draft-orders/:id/email"
13405
- },
13406
13398
  {
13407
13399
  Component: Items,
13408
13400
  path: "/draft-orders/:id/items"
13409
13401
  },
13410
13402
  {
13411
- Component: Promotions,
13412
- path: "/draft-orders/:id/promotions"
13403
+ Component: BillingAddress,
13404
+ path: "/draft-orders/:id/billing-address"
13405
+ },
13406
+ {
13407
+ Component: Email,
13408
+ path: "/draft-orders/:id/email"
13413
13409
  },
13414
13410
  {
13415
13411
  Component: Metadata,
13416
13412
  path: "/draft-orders/:id/metadata"
13417
13413
  },
13418
13414
  {
13419
- Component: Shipping,
13420
- path: "/draft-orders/:id/shipping"
13415
+ Component: Promotions,
13416
+ path: "/draft-orders/:id/promotions"
13421
13417
  },
13422
13418
  {
13423
13419
  Component: SalesChannel,
13424
13420
  path: "/draft-orders/:id/sales-channel"
13425
13421
  },
13422
+ {
13423
+ Component: Shipping,
13424
+ path: "/draft-orders/:id/shipping"
13425
+ },
13426
13426
  {
13427
13427
  Component: ShippingAddress,
13428
13428
  path: "/draft-orders/:id/shipping-address"