@medusajs/draft-order 2.22.1 → 2.22.2-preview-20261028132907

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.
@@ -5983,95 +5983,6 @@ const ID = () => {
5983
5983
  }
5984
5984
  );
5985
5985
  };
5986
- const CustomItems = () => {
5987
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
5988
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
5989
- /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
5990
- ] });
5991
- };
5992
- const CustomItemsForm = () => {
5993
- const form = reactHookForm.useForm({
5994
- resolver: zod$1.zodResolver(schema$5)
5995
- });
5996
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
5997
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
5998
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
5999
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
6000
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
6001
- ] }) })
6002
- ] }) });
6003
- };
6004
- const schema$5 = zod.z.object({
6005
- email: zod.z.string().email()
6006
- });
6007
- const Email = () => {
6008
- const { id } = reactRouterDom.useParams();
6009
- const { order, isPending, isError, error } = useOrder(id, {
6010
- fields: "+email"
6011
- });
6012
- if (isError) {
6013
- throw error;
6014
- }
6015
- const isReady = !isPending && !!order;
6016
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
6017
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
6018
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
6019
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
6020
- ] }),
6021
- isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
6022
- ] });
6023
- };
6024
- const EmailForm = ({ order }) => {
6025
- const form = reactHookForm.useForm({
6026
- defaultValues: {
6027
- email: order.email ?? ""
6028
- },
6029
- resolver: zod$1.zodResolver(schema$4)
6030
- });
6031
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
6032
- const { handleSuccess } = useRouteModal();
6033
- const onSubmit = form.handleSubmit(async (data) => {
6034
- await mutateAsync(
6035
- { email: data.email },
6036
- {
6037
- onSuccess: () => {
6038
- handleSuccess();
6039
- },
6040
- onError: (error) => {
6041
- ui.toast.error(error.message);
6042
- }
6043
- }
6044
- );
6045
- });
6046
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
6047
- KeyboundForm,
6048
- {
6049
- className: "flex flex-1 flex-col overflow-hidden",
6050
- onSubmit,
6051
- children: [
6052
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
6053
- Form$2.Field,
6054
- {
6055
- control: form.control,
6056
- name: "email",
6057
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
6058
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
6059
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
6060
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
6061
- ] })
6062
- }
6063
- ) }),
6064
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
6065
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
6066
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
6067
- ] }) })
6068
- ]
6069
- }
6070
- ) });
6071
- };
6072
- const schema$4 = zod.z.object({
6073
- email: zod.z.string().email()
6074
- });
6075
5986
  const BillingAddress = () => {
6076
5987
  const { id } = reactRouterDom.useParams();
6077
5988
  const { order, isPending, isError, error } = useOrder(id, {
@@ -6103,7 +6014,7 @@ const BillingAddressForm = ({ order }) => {
6103
6014
  postal_code: order.billing_address?.postal_code ?? "",
6104
6015
  phone: order.billing_address?.phone ?? ""
6105
6016
  },
6106
- resolver: zod$1.zodResolver(schema$3)
6017
+ resolver: zod$1.zodResolver(schema$5)
6107
6018
  });
6108
6019
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
6109
6020
  const { handleSuccess } = useRouteModal();
@@ -6260,7 +6171,96 @@ const BillingAddressForm = ({ order }) => {
6260
6171
  }
6261
6172
  ) });
6262
6173
  };
6263
- const schema$3 = addressSchema;
6174
+ const schema$5 = addressSchema;
6175
+ const CustomItems = () => {
6176
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
6177
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
6178
+ /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
6179
+ ] });
6180
+ };
6181
+ const CustomItemsForm = () => {
6182
+ const form = reactHookForm.useForm({
6183
+ resolver: zod$1.zodResolver(schema$4)
6184
+ });
6185
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
6186
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
6187
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
6188
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
6189
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
6190
+ ] }) })
6191
+ ] }) });
6192
+ };
6193
+ const schema$4 = zod.z.object({
6194
+ email: zod.z.string().email()
6195
+ });
6196
+ const Email = () => {
6197
+ const { id } = reactRouterDom.useParams();
6198
+ const { order, isPending, isError, error } = useOrder(id, {
6199
+ fields: "+email"
6200
+ });
6201
+ if (isError) {
6202
+ throw error;
6203
+ }
6204
+ const isReady = !isPending && !!order;
6205
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
6206
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
6207
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
6208
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
6209
+ ] }),
6210
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
6211
+ ] });
6212
+ };
6213
+ const EmailForm = ({ order }) => {
6214
+ const form = reactHookForm.useForm({
6215
+ defaultValues: {
6216
+ email: order.email ?? ""
6217
+ },
6218
+ resolver: zod$1.zodResolver(schema$3)
6219
+ });
6220
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
6221
+ const { handleSuccess } = useRouteModal();
6222
+ const onSubmit = form.handleSubmit(async (data) => {
6223
+ await mutateAsync(
6224
+ { email: data.email },
6225
+ {
6226
+ onSuccess: () => {
6227
+ handleSuccess();
6228
+ },
6229
+ onError: (error) => {
6230
+ ui.toast.error(error.message);
6231
+ }
6232
+ }
6233
+ );
6234
+ });
6235
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
6236
+ KeyboundForm,
6237
+ {
6238
+ className: "flex flex-1 flex-col overflow-hidden",
6239
+ onSubmit,
6240
+ children: [
6241
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
6242
+ Form$2.Field,
6243
+ {
6244
+ control: form.control,
6245
+ name: "email",
6246
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
6247
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
6248
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
6249
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
6250
+ ] })
6251
+ }
6252
+ ) }),
6253
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
6254
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
6255
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
6256
+ ] }) })
6257
+ ]
6258
+ }
6259
+ ) });
6260
+ };
6261
+ const schema$3 = zod.z.object({
6262
+ email: zod.z.string().email()
6263
+ });
6264
6264
  const NumberInput = React.forwardRef(
6265
6265
  ({
6266
6266
  value,
@@ -7280,88 +7280,46 @@ const customItemSchema = zod.z.object({
7280
7280
  quantity: zod.z.number(),
7281
7281
  unit_price: zod.z.union([zod.z.number(), zod.z.string()])
7282
7282
  });
7283
- const InlineTip = React.forwardRef(
7284
- ({ variant = "tip", label, className, children, ...props }, ref) => {
7285
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
7286
- return /* @__PURE__ */ jsxRuntime.jsxs(
7287
- "div",
7288
- {
7289
- ref,
7290
- className: ui.clx(
7291
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
7292
- className
7293
- ),
7294
- ...props,
7295
- children: [
7296
- /* @__PURE__ */ jsxRuntime.jsx(
7297
- "div",
7298
- {
7299
- role: "presentation",
7300
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
7301
- "bg-ui-tag-orange-icon": variant === "warning"
7302
- })
7303
- }
7304
- ),
7305
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
7306
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
7307
- labelValue,
7308
- ":"
7309
- ] }),
7310
- " ",
7311
- children
7312
- ] })
7313
- ]
7314
- }
7315
- );
7316
- }
7317
- );
7318
- InlineTip.displayName = "InlineTip";
7319
- const MetadataFieldSchema = zod.z.object({
7320
- key: zod.z.string(),
7321
- disabled: zod.z.boolean().optional(),
7322
- value: zod.z.any()
7323
- });
7324
- const MetadataSchema = zod.z.object({
7325
- metadata: zod.z.array(MetadataFieldSchema)
7326
- });
7327
- const Metadata = () => {
7283
+ const SalesChannel = () => {
7328
7284
  const { id } = reactRouterDom.useParams();
7329
- const { order, isPending, isError, error } = useOrder(id, {
7330
- fields: "metadata"
7331
- });
7285
+ const { draft_order, isPending, isError, error } = useDraftOrder(
7286
+ id,
7287
+ {
7288
+ fields: "+sales_channel_id"
7289
+ },
7290
+ {
7291
+ enabled: !!id
7292
+ }
7293
+ );
7332
7294
  if (isError) {
7333
7295
  throw error;
7334
7296
  }
7335
- const isReady = !isPending && !!order;
7297
+ const ISrEADY = !!draft_order && !isPending;
7336
7298
  return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
7337
7299
  /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
7338
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
7339
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
7300
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
7301
+ /* @__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" }) })
7340
7302
  ] }),
7341
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order?.metadata })
7303
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
7342
7304
  ] });
7343
7305
  };
7344
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
7345
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
7346
- const MetadataForm = ({ orderId, metadata }) => {
7347
- const { handleSuccess } = useRouteModal();
7348
- const hasUneditableRows = getHasUneditableRows(metadata);
7349
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
7306
+ const SalesChannelForm = ({ order }) => {
7350
7307
  const form = reactHookForm.useForm({
7351
7308
  defaultValues: {
7352
- metadata: getDefaultValues(metadata)
7309
+ sales_channel_id: order.sales_channel_id || ""
7353
7310
  },
7354
- resolver: zod$1.zodResolver(MetadataSchema)
7311
+ resolver: zod$1.zodResolver(schema$2)
7355
7312
  });
7356
- const handleSubmit = form.handleSubmit(async (data) => {
7357
- const parsedData = parseValues(data);
7313
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
7314
+ const { handleSuccess } = useRouteModal();
7315
+ const onSubmit = form.handleSubmit(async (data) => {
7358
7316
  await mutateAsync(
7359
7317
  {
7360
- metadata: parsedData
7318
+ sales_channel_id: data.sales_channel_id
7361
7319
  },
7362
7320
  {
7363
7321
  onSuccess: () => {
7364
- ui.toast.success("Metadata updated");
7322
+ ui.toast.success("Sales channel updated");
7365
7323
  handleSuccess();
7366
7324
  },
7367
7325
  onError: (error) => {
@@ -7370,365 +7328,133 @@ const MetadataForm = ({ orderId, metadata }) => {
7370
7328
  }
7371
7329
  );
7372
7330
  });
7373
- const { fields, insert, remove } = reactHookForm.useFieldArray({
7374
- control: form.control,
7375
- name: "metadata"
7376
- });
7377
- function deleteRow(index) {
7378
- remove(index);
7379
- if (fields.length === 1) {
7380
- insert(0, {
7381
- key: "",
7382
- value: "",
7383
- disabled: false
7384
- });
7385
- }
7386
- }
7387
- function insertRow(index, position) {
7388
- insert(index + (position === "above" ? 0 : 1), {
7389
- key: "",
7390
- value: "",
7391
- disabled: false
7392
- });
7393
- }
7394
7331
  return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
7395
7332
  KeyboundForm,
7396
7333
  {
7397
- onSubmit: handleSubmit,
7398
7334
  className: "flex flex-1 flex-col overflow-hidden",
7335
+ onSubmit,
7399
7336
  children: [
7400
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
7401
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
7402
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
7403
- /* @__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" }) }),
7404
- /* @__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" }) })
7405
- ] }),
7406
- fields.map((field, index) => {
7407
- const isDisabled = field.disabled || false;
7408
- let placeholder = "-";
7409
- if (typeof field.value === "object") {
7410
- placeholder = "{ ... }";
7411
- }
7412
- if (Array.isArray(field.value)) {
7413
- placeholder = "[ ... ]";
7414
- }
7415
- return /* @__PURE__ */ jsxRuntime.jsx(
7416
- ConditionalTooltip,
7417
- {
7418
- showTooltip: isDisabled,
7419
- content: "This row is disabled because it contains non-primitive data.",
7420
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
7421
- /* @__PURE__ */ jsxRuntime.jsxs(
7422
- "div",
7423
- {
7424
- className: ui.clx("grid grid-cols-2 divide-x", {
7425
- "overflow-hidden rounded-b-lg": index === fields.length - 1
7426
- }),
7427
- children: [
7428
- /* @__PURE__ */ jsxRuntime.jsx(
7429
- Form$2.Field,
7430
- {
7431
- control: form.control,
7432
- name: `metadata.${index}.key`,
7433
- render: ({ field: field2 }) => {
7434
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
7435
- GridInput,
7436
- {
7437
- "aria-labelledby": METADATA_KEY_LABEL_ID,
7438
- ...field2,
7439
- disabled: isDisabled,
7440
- placeholder: "Key"
7441
- }
7442
- ) }) });
7443
- }
7444
- }
7445
- ),
7446
- /* @__PURE__ */ jsxRuntime.jsx(
7447
- Form$2.Field,
7448
- {
7449
- control: form.control,
7450
- name: `metadata.${index}.value`,
7451
- render: ({ field: { value, ...field2 } }) => {
7452
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
7453
- GridInput,
7454
- {
7455
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
7456
- ...field2,
7457
- value: isDisabled ? placeholder : value,
7458
- disabled: isDisabled,
7459
- placeholder: "Value"
7460
- }
7461
- ) }) });
7462
- }
7463
- }
7464
- )
7465
- ]
7466
- }
7467
- ),
7468
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
7469
- /* @__PURE__ */ jsxRuntime.jsx(
7470
- ui.DropdownMenu.Trigger,
7471
- {
7472
- className: ui.clx(
7473
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
7474
- {
7475
- hidden: isDisabled
7476
- }
7477
- ),
7478
- disabled: isDisabled,
7479
- asChild: true,
7480
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
7481
- }
7482
- ),
7483
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
7484
- /* @__PURE__ */ jsxRuntime.jsxs(
7485
- ui.DropdownMenu.Item,
7486
- {
7487
- className: "gap-x-2",
7488
- onClick: () => insertRow(index, "above"),
7489
- children: [
7490
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
7491
- "Insert row above"
7492
- ]
7493
- }
7494
- ),
7495
- /* @__PURE__ */ jsxRuntime.jsxs(
7496
- ui.DropdownMenu.Item,
7497
- {
7498
- className: "gap-x-2",
7499
- onClick: () => insertRow(index, "below"),
7500
- children: [
7501
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
7502
- "Insert row below"
7503
- ]
7504
- }
7505
- ),
7506
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
7507
- /* @__PURE__ */ jsxRuntime.jsxs(
7508
- ui.DropdownMenu.Item,
7509
- {
7510
- className: "gap-x-2",
7511
- onClick: () => deleteRow(index),
7512
- children: [
7513
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
7514
- "Delete row"
7515
- ]
7516
- }
7517
- )
7518
- ] })
7519
- ] })
7520
- ] })
7521
- },
7522
- field.id
7523
- );
7524
- })
7525
- ] }),
7526
- 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." })
7527
- ] }),
7528
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
7529
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
7337
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
7338
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
7339
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7530
7340
  /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
7531
7341
  ] }) })
7532
7342
  ]
7533
7343
  }
7534
7344
  ) });
7535
7345
  };
7536
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
7346
+ const SalesChannelField = ({ control, order }) => {
7347
+ const salesChannels = useComboboxData({
7348
+ queryFn: async (params) => {
7349
+ return await sdk.admin.salesChannel.list(params);
7350
+ },
7351
+ queryKey: ["sales-channels"],
7352
+ getOptions: (data) => {
7353
+ return data.sales_channels.map((salesChannel) => ({
7354
+ label: salesChannel.name,
7355
+ value: salesChannel.id
7356
+ }));
7357
+ },
7358
+ defaultValue: order.sales_channel_id || void 0
7359
+ });
7537
7360
  return /* @__PURE__ */ jsxRuntime.jsx(
7538
- "input",
7361
+ Form$2.Field,
7539
7362
  {
7540
- ref,
7541
- ...props,
7542
- autoComplete: "off",
7543
- className: ui.clx(
7544
- "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",
7545
- className
7546
- )
7363
+ control,
7364
+ name: "sales_channel_id",
7365
+ render: ({ field }) => {
7366
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
7367
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
7368
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
7369
+ Combobox,
7370
+ {
7371
+ options: salesChannels.options,
7372
+ fetchNextPage: salesChannels.fetchNextPage,
7373
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
7374
+ searchValue: salesChannels.searchValue,
7375
+ onSearchValueChange: salesChannels.onSearchValueChange,
7376
+ placeholder: "Select sales channel",
7377
+ ...field
7378
+ }
7379
+ ) }),
7380
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
7381
+ ] });
7382
+ }
7547
7383
  }
7548
7384
  );
7549
- });
7550
- GridInput.displayName = "MetadataForm.GridInput";
7551
- const PlaceholderInner = () => {
7552
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
7553
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
7554
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
7555
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
7556
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
7557
- ] }) })
7558
- ] });
7559
7385
  };
7560
- const EDITABLE_TYPES = ["string", "number", "boolean"];
7561
- function getDefaultValues(metadata) {
7562
- if (!metadata || !Object.keys(metadata).length) {
7563
- return [
7564
- {
7565
- key: "",
7566
- value: "",
7567
- disabled: false
7568
- }
7569
- ];
7570
- }
7571
- return Object.entries(metadata).map(([key, value]) => {
7572
- if (!EDITABLE_TYPES.includes(typeof value)) {
7573
- return {
7574
- key,
7575
- value,
7576
- disabled: true
7577
- };
7578
- }
7579
- let stringValue = value;
7580
- if (typeof value !== "string") {
7581
- stringValue = JSON.stringify(value);
7582
- }
7583
- return {
7584
- key,
7585
- value: stringValue,
7586
- original_key: key
7587
- };
7386
+ const schema$2 = zod.z.object({
7387
+ sales_channel_id: zod.z.string().min(1)
7388
+ });
7389
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
7390
+ const Shipping = () => {
7391
+ const { id } = reactRouterDom.useParams();
7392
+ const { order, isPending, isError, error } = useOrder(id, {
7393
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
7588
7394
  });
7589
- }
7590
- function parseValues(values) {
7591
- const metadata = values.metadata;
7592
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
7593
- if (isEmpty) {
7594
- return null;
7595
- }
7596
- const update = {};
7597
- metadata.forEach((field) => {
7598
- let key = field.key;
7599
- let value = field.value;
7600
- const disabled = field.disabled;
7601
- if (!key || !value) {
7602
- return;
7603
- }
7604
- if (disabled) {
7605
- update[key] = value;
7606
- return;
7607
- }
7608
- key = key.trim();
7609
- value = value.trim();
7610
- if (value === "true") {
7611
- update[key] = true;
7612
- } else if (value === "false") {
7613
- update[key] = false;
7614
- } else {
7615
- const parsedNumber = parseFloat(value);
7616
- if (!isNaN(parsedNumber)) {
7617
- update[key] = parsedNumber;
7618
- } else {
7619
- update[key] = value;
7620
- }
7621
- }
7622
- });
7623
- return update;
7624
- }
7625
- function getHasUneditableRows(metadata) {
7626
- if (!metadata) {
7627
- return false;
7628
- }
7629
- return Object.values(metadata).some(
7630
- (value) => !EDITABLE_TYPES.includes(typeof value)
7631
- );
7632
- }
7633
- const PROMOTION_QUERY_KEY = "promotions";
7634
- const promotionsQueryKeys = {
7635
- list: (query2) => [
7636
- PROMOTION_QUERY_KEY,
7637
- query2 ? query2 : void 0
7638
- ],
7639
- detail: (id, query2) => [
7640
- PROMOTION_QUERY_KEY,
7641
- id,
7642
- query2 ? query2 : void 0
7643
- ]
7644
- };
7645
- const usePromotions = (query2, options) => {
7646
- const { data, ...rest } = reactQuery.useQuery({
7647
- queryKey: promotionsQueryKeys.list(query2),
7648
- queryFn: async () => sdk.admin.promotion.list(query2),
7649
- ...options
7650
- });
7651
- return { ...data, ...rest };
7652
- };
7653
- const Promotions = () => {
7654
- const { id } = reactRouterDom.useParams();
7655
7395
  const {
7656
7396
  order: preview,
7397
+ isPending: isPreviewPending,
7657
7398
  isError: isPreviewError,
7658
7399
  error: previewError
7659
- } = useOrderPreview(id, void 0);
7400
+ } = useOrderPreview(id);
7660
7401
  useInitiateOrderEdit({ preview });
7661
7402
  const { onCancel } = useCancelOrderEdit({ preview });
7403
+ if (isError) {
7404
+ throw error;
7405
+ }
7662
7406
  if (isPreviewError) {
7663
7407
  throw previewError;
7664
7408
  }
7665
- const isReady = !!preview;
7666
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
7667
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
7668
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
7669
- ] });
7409
+ const orderHasItems = (order?.items?.length || 0) > 0;
7410
+ const isReady = preview && !isPreviewPending && order && !isPending;
7411
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
7412
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
7413
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
7414
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
7415
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
7416
+ ] }) }) }),
7417
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
7418
+ ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
7419
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
7420
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
7421
+ ] }) });
7670
7422
  };
7671
- const PromotionForm = ({ preview }) => {
7672
- const { items, shipping_methods } = preview;
7423
+ const ShippingForm = ({ preview, order }) => {
7424
+ const { setIsOpen } = useStackedModal();
7673
7425
  const [isSubmitting, setIsSubmitting] = React.useState(false);
7674
- const [comboboxValue, setComboboxValue] = React.useState("");
7675
- const { handleSuccess } = useRouteModal();
7676
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
7677
- const promoIds = getPromotionIds(items, shipping_methods);
7678
- const { promotions, isPending, isError, error } = usePromotions(
7426
+ const [data, setData] = React.useState(null);
7427
+ const appliedShippingOptionIds = preview.shipping_methods?.map((method) => method.shipping_option_id).filter(Boolean);
7428
+ const { shipping_options } = useShippingOptions(
7679
7429
  {
7680
- id: promoIds
7430
+ id: appliedShippingOptionIds,
7431
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
7681
7432
  },
7682
7433
  {
7683
- enabled: !!promoIds.length
7434
+ enabled: appliedShippingOptionIds.length > 0
7684
7435
  }
7685
7436
  );
7686
- const comboboxData = useComboboxData({
7687
- queryKey: ["promotions", "combobox", promoIds],
7688
- queryFn: async (params) => {
7689
- return await sdk.admin.promotion.list({
7690
- ...params,
7691
- id: {
7692
- $nin: promoIds
7693
- }
7694
- });
7695
- },
7696
- getOptions: (data) => {
7697
- return data.promotions.map((promotion) => ({
7698
- label: promotion.code,
7699
- value: promotion.code
7700
- }));
7701
- }
7702
- });
7703
- const add = async (value) => {
7704
- if (!value) {
7705
- return;
7706
- }
7707
- addPromotions(
7708
- {
7709
- promo_codes: [value]
7710
- },
7711
- {
7712
- onError: (e) => {
7713
- ui.toast.error(e.message);
7714
- comboboxData.onSearchValueChange("");
7715
- setComboboxValue("");
7716
- },
7717
- onSuccess: () => {
7718
- comboboxData.onSearchValueChange("");
7719
- setComboboxValue("");
7720
- }
7721
- }
7722
- );
7723
- };
7437
+ const uniqueShippingProfiles = React.useMemo(() => {
7438
+ const profiles = /* @__PURE__ */ new Map();
7439
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
7440
+ profiles.set(profile.id, profile);
7441
+ });
7442
+ shipping_options?.forEach((option) => {
7443
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
7444
+ });
7445
+ return Array.from(profiles.values());
7446
+ }, [order.items, shipping_options]);
7447
+ const { handleSuccess } = useRouteModal();
7724
7448
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
7725
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
7449
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
7450
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
7451
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
7726
7452
  const onSubmit = async () => {
7727
7453
  setIsSubmitting(true);
7728
7454
  let requestSucceeded = false;
7729
7455
  await requestOrderEdit(void 0, {
7730
7456
  onError: (e) => {
7731
- ui.toast.error(e.message);
7457
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
7732
7458
  },
7733
7459
  onSuccess: () => {
7734
7460
  requestSucceeded = true;
@@ -7740,7 +7466,7 @@ const PromotionForm = ({ preview }) => {
7740
7466
  }
7741
7467
  await confirmOrderEdit(void 0, {
7742
7468
  onError: (e) => {
7743
- ui.toast.error(e.message);
7469
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
7744
7470
  },
7745
7471
  onSuccess: () => {
7746
7472
  handleSuccess();
@@ -7750,258 +7476,13 @@ const PromotionForm = ({ preview }) => {
7750
7476
  }
7751
7477
  });
7752
7478
  };
7753
- if (isError) {
7754
- throw error;
7755
- }
7756
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
7757
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
7758
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
7759
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
7760
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
7761
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
7762
- ] }),
7763
- /* @__PURE__ */ jsxRuntime.jsx(
7764
- Combobox,
7765
- {
7766
- id: "promotion-combobox",
7767
- "aria-describedby": "promotion-combobox-hint",
7768
- isFetchingNextPage: comboboxData.isFetchingNextPage,
7769
- fetchNextPage: comboboxData.fetchNextPage,
7770
- options: comboboxData.options,
7771
- onSearchValueChange: comboboxData.onSearchValueChange,
7772
- searchValue: comboboxData.searchValue,
7773
- disabled: comboboxData.disabled || isAddingPromotions,
7774
- onChange: add,
7775
- value: comboboxValue
7776
- }
7777
- )
7778
- ] }),
7779
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
7780
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions?.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
7781
- PromotionItem,
7782
- {
7783
- promotion,
7784
- orderId: preview.id,
7785
- isLoading: isPending
7786
- },
7787
- promotion.id
7788
- )) })
7789
- ] }) }),
7790
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
7791
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
7792
- /* @__PURE__ */ jsxRuntime.jsx(
7793
- ui.Button,
7794
- {
7795
- size: "small",
7796
- type: "submit",
7797
- isLoading: isSubmitting || isAddingPromotions,
7798
- children: "Save"
7799
- }
7800
- )
7801
- ] }) })
7802
- ] });
7803
- };
7804
- const PromotionItem = ({
7805
- promotion,
7806
- orderId,
7807
- isLoading
7808
- }) => {
7809
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
7810
- const onRemove = async () => {
7811
- removePromotions(
7812
- {
7813
- promo_codes: [promotion.code]
7814
- },
7815
- {
7816
- onError: (e) => {
7817
- ui.toast.error(e.message);
7479
+ const onKeydown = React.useCallback(
7480
+ (e) => {
7481
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
7482
+ if (data || isSubmitting) {
7483
+ return;
7818
7484
  }
7819
- }
7820
- );
7821
- };
7822
- const displayValue = getDisplayValue(promotion);
7823
- return /* @__PURE__ */ jsxRuntime.jsxs(
7824
- "div",
7825
- {
7826
- className: ui.clx(
7827
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
7828
- {
7829
- "animate-pulse": isLoading
7830
- }
7831
- ),
7832
- children: [
7833
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
7834
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
7835
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
7836
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
7837
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
7838
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
7839
- ] }),
7840
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: promotion.application_method?.allocation })
7841
- ] })
7842
- ] }),
7843
- /* @__PURE__ */ jsxRuntime.jsx(
7844
- ui.IconButton,
7845
- {
7846
- size: "small",
7847
- type: "button",
7848
- variant: "transparent",
7849
- onClick: onRemove,
7850
- isLoading: isPending || isLoading,
7851
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
7852
- }
7853
- )
7854
- ]
7855
- },
7856
- promotion.id
7857
- );
7858
- };
7859
- function getDisplayValue(promotion) {
7860
- const value = promotion.application_method?.value;
7861
- if (!value) {
7862
- return null;
7863
- }
7864
- if (promotion.application_method?.type === "fixed") {
7865
- const currency = promotion.application_method?.currency_code;
7866
- if (!currency) {
7867
- return null;
7868
- }
7869
- return getLocaleAmount(value, currency);
7870
- } else if (promotion.application_method?.type === "percentage") {
7871
- return formatPercentage(value);
7872
- }
7873
- return null;
7874
- }
7875
- const formatter = new Intl.NumberFormat([], {
7876
- style: "percent",
7877
- minimumFractionDigits: 2
7878
- });
7879
- const formatPercentage = (value, isPercentageValue = false) => {
7880
- let val = value || 0;
7881
- if (!isPercentageValue) {
7882
- val = val / 100;
7883
- }
7884
- return formatter.format(val);
7885
- };
7886
- function getPromotionIds(items, shippingMethods) {
7887
- const promotionIds = /* @__PURE__ */ new Set();
7888
- for (const item of items) {
7889
- if (item.adjustments) {
7890
- for (const adjustment of item.adjustments) {
7891
- if (adjustment.promotion_id) {
7892
- promotionIds.add(adjustment.promotion_id);
7893
- }
7894
- }
7895
- }
7896
- }
7897
- for (const shippingMethod of shippingMethods) {
7898
- if (shippingMethod.adjustments) {
7899
- for (const adjustment of shippingMethod.adjustments) {
7900
- if (adjustment.promotion_id) {
7901
- promotionIds.add(adjustment.promotion_id);
7902
- }
7903
- }
7904
- }
7905
- }
7906
- return Array.from(promotionIds);
7907
- }
7908
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
7909
- const Shipping = () => {
7910
- const { id } = reactRouterDom.useParams();
7911
- const { order, isPending, isError, error } = useOrder(id, {
7912
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
7913
- });
7914
- const {
7915
- order: preview,
7916
- isPending: isPreviewPending,
7917
- isError: isPreviewError,
7918
- error: previewError
7919
- } = useOrderPreview(id);
7920
- useInitiateOrderEdit({ preview });
7921
- const { onCancel } = useCancelOrderEdit({ preview });
7922
- if (isError) {
7923
- throw error;
7924
- }
7925
- if (isPreviewError) {
7926
- throw previewError;
7927
- }
7928
- const orderHasItems = (order?.items?.length || 0) > 0;
7929
- const isReady = preview && !isPreviewPending && order && !isPending;
7930
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
7931
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
7932
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
7933
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
7934
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
7935
- ] }) }) }),
7936
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
7937
- ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
7938
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
7939
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
7940
- ] }) });
7941
- };
7942
- const ShippingForm = ({ preview, order }) => {
7943
- const { setIsOpen } = useStackedModal();
7944
- const [isSubmitting, setIsSubmitting] = React.useState(false);
7945
- const [data, setData] = React.useState(null);
7946
- const appliedShippingOptionIds = preview.shipping_methods?.map((method) => method.shipping_option_id).filter(Boolean);
7947
- const { shipping_options } = useShippingOptions(
7948
- {
7949
- id: appliedShippingOptionIds,
7950
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
7951
- },
7952
- {
7953
- enabled: appliedShippingOptionIds.length > 0
7954
- }
7955
- );
7956
- const uniqueShippingProfiles = React.useMemo(() => {
7957
- const profiles = /* @__PURE__ */ new Map();
7958
- getUniqueShippingProfiles(order.items).forEach((profile) => {
7959
- profiles.set(profile.id, profile);
7960
- });
7961
- shipping_options?.forEach((option) => {
7962
- profiles.set(option.shipping_profile_id, option.shipping_profile);
7963
- });
7964
- return Array.from(profiles.values());
7965
- }, [order.items, shipping_options]);
7966
- const { handleSuccess } = useRouteModal();
7967
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
7968
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
7969
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
7970
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
7971
- const onSubmit = async () => {
7972
- setIsSubmitting(true);
7973
- let requestSucceeded = false;
7974
- await requestOrderEdit(void 0, {
7975
- onError: (e) => {
7976
- ui.toast.error(`Failed to request order edit: ${e.message}`);
7977
- },
7978
- onSuccess: () => {
7979
- requestSucceeded = true;
7980
- }
7981
- });
7982
- if (!requestSucceeded) {
7983
- setIsSubmitting(false);
7984
- return;
7985
- }
7986
- await confirmOrderEdit(void 0, {
7987
- onError: (e) => {
7988
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
7989
- },
7990
- onSuccess: () => {
7991
- handleSuccess();
7992
- },
7993
- onSettled: () => {
7994
- setIsSubmitting(false);
7995
- }
7996
- });
7997
- };
7998
- const onKeydown = React.useCallback(
7999
- (e) => {
8000
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
8001
- if (data || isSubmitting) {
8002
- return;
8003
- }
8004
- onSubmit();
7485
+ onSubmit();
8005
7486
  }
8006
7487
  },
8007
7488
  [data, isSubmitting, onSubmit]
@@ -8730,7 +8211,7 @@ const ShippingAddressForm = ({ order }) => {
8730
8211
  postal_code: order.shipping_address?.postal_code ?? "",
8731
8212
  phone: order.shipping_address?.phone ?? ""
8732
8213
  },
8733
- resolver: zod$1.zodResolver(schema$2)
8214
+ resolver: zod$1.zodResolver(schema$1)
8734
8215
  });
8735
8216
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
8736
8217
  const { handleSuccess } = useRouteModal();
@@ -8900,7 +8381,7 @@ const ShippingAddressForm = ({ order }) => {
8900
8381
  }
8901
8382
  ) });
8902
8383
  };
8903
- const schema$2 = addressSchema;
8384
+ const schema$1 = addressSchema;
8904
8385
  const TransferOwnership = () => {
8905
8386
  const { id } = reactRouterDom.useParams();
8906
8387
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -8923,7 +8404,7 @@ const TransferOwnershipForm = ({ order }) => {
8923
8404
  defaultValues: {
8924
8405
  customer_id: order.customer_id || ""
8925
8406
  },
8926
- resolver: zod$1.zodResolver(schema$1)
8407
+ resolver: zod$1.zodResolver(schema)
8927
8408
  });
8928
8409
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
8929
8410
  const { handleSuccess } = useRouteModal();
@@ -9373,117 +8854,636 @@ const Illustration = () => {
9373
8854
  ] })
9374
8855
  ]
9375
8856
  }
9376
- );
8857
+ );
8858
+ };
8859
+ const schema = zod.z.object({
8860
+ customer_id: zod.z.string().min(1)
8861
+ });
8862
+ const PROMOTION_QUERY_KEY = "promotions";
8863
+ const promotionsQueryKeys = {
8864
+ list: (query2) => [
8865
+ PROMOTION_QUERY_KEY,
8866
+ query2 ? query2 : void 0
8867
+ ],
8868
+ detail: (id, query2) => [
8869
+ PROMOTION_QUERY_KEY,
8870
+ id,
8871
+ query2 ? query2 : void 0
8872
+ ]
8873
+ };
8874
+ const usePromotions = (query2, options) => {
8875
+ const { data, ...rest } = reactQuery.useQuery({
8876
+ queryKey: promotionsQueryKeys.list(query2),
8877
+ queryFn: async () => sdk.admin.promotion.list(query2),
8878
+ ...options
8879
+ });
8880
+ return { ...data, ...rest };
8881
+ };
8882
+ const Promotions = () => {
8883
+ const { id } = reactRouterDom.useParams();
8884
+ const {
8885
+ order: preview,
8886
+ isError: isPreviewError,
8887
+ error: previewError
8888
+ } = useOrderPreview(id, void 0);
8889
+ useInitiateOrderEdit({ preview });
8890
+ const { onCancel } = useCancelOrderEdit({ preview });
8891
+ if (isPreviewError) {
8892
+ throw previewError;
8893
+ }
8894
+ const isReady = !!preview;
8895
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
8896
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
8897
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
8898
+ ] });
8899
+ };
8900
+ const PromotionForm = ({ preview }) => {
8901
+ const { items, shipping_methods } = preview;
8902
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
8903
+ const [comboboxValue, setComboboxValue] = React.useState("");
8904
+ const { handleSuccess } = useRouteModal();
8905
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
8906
+ const promoIds = getPromotionIds(items, shipping_methods);
8907
+ const { promotions, isPending, isError, error } = usePromotions(
8908
+ {
8909
+ id: promoIds
8910
+ },
8911
+ {
8912
+ enabled: !!promoIds.length
8913
+ }
8914
+ );
8915
+ const comboboxData = useComboboxData({
8916
+ queryKey: ["promotions", "combobox", promoIds],
8917
+ queryFn: async (params) => {
8918
+ return await sdk.admin.promotion.list({
8919
+ ...params,
8920
+ id: {
8921
+ $nin: promoIds
8922
+ }
8923
+ });
8924
+ },
8925
+ getOptions: (data) => {
8926
+ return data.promotions.map((promotion) => ({
8927
+ label: promotion.code,
8928
+ value: promotion.code
8929
+ }));
8930
+ }
8931
+ });
8932
+ const add = async (value) => {
8933
+ if (!value) {
8934
+ return;
8935
+ }
8936
+ addPromotions(
8937
+ {
8938
+ promo_codes: [value]
8939
+ },
8940
+ {
8941
+ onError: (e) => {
8942
+ ui.toast.error(e.message);
8943
+ comboboxData.onSearchValueChange("");
8944
+ setComboboxValue("");
8945
+ },
8946
+ onSuccess: () => {
8947
+ comboboxData.onSearchValueChange("");
8948
+ setComboboxValue("");
8949
+ }
8950
+ }
8951
+ );
8952
+ };
8953
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
8954
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
8955
+ const onSubmit = async () => {
8956
+ setIsSubmitting(true);
8957
+ let requestSucceeded = false;
8958
+ await requestOrderEdit(void 0, {
8959
+ onError: (e) => {
8960
+ ui.toast.error(e.message);
8961
+ },
8962
+ onSuccess: () => {
8963
+ requestSucceeded = true;
8964
+ }
8965
+ });
8966
+ if (!requestSucceeded) {
8967
+ setIsSubmitting(false);
8968
+ return;
8969
+ }
8970
+ await confirmOrderEdit(void 0, {
8971
+ onError: (e) => {
8972
+ ui.toast.error(e.message);
8973
+ },
8974
+ onSuccess: () => {
8975
+ handleSuccess();
8976
+ },
8977
+ onSettled: () => {
8978
+ setIsSubmitting(false);
8979
+ }
8980
+ });
8981
+ };
8982
+ if (isError) {
8983
+ throw error;
8984
+ }
8985
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
8986
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
8987
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
8988
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
8989
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
8990
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
8991
+ ] }),
8992
+ /* @__PURE__ */ jsxRuntime.jsx(
8993
+ Combobox,
8994
+ {
8995
+ id: "promotion-combobox",
8996
+ "aria-describedby": "promotion-combobox-hint",
8997
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
8998
+ fetchNextPage: comboboxData.fetchNextPage,
8999
+ options: comboboxData.options,
9000
+ onSearchValueChange: comboboxData.onSearchValueChange,
9001
+ searchValue: comboboxData.searchValue,
9002
+ disabled: comboboxData.disabled || isAddingPromotions,
9003
+ onChange: add,
9004
+ value: comboboxValue
9005
+ }
9006
+ )
9007
+ ] }),
9008
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
9009
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions?.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
9010
+ PromotionItem,
9011
+ {
9012
+ promotion,
9013
+ orderId: preview.id,
9014
+ isLoading: isPending
9015
+ },
9016
+ promotion.id
9017
+ )) })
9018
+ ] }) }),
9019
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9020
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9021
+ /* @__PURE__ */ jsxRuntime.jsx(
9022
+ ui.Button,
9023
+ {
9024
+ size: "small",
9025
+ type: "submit",
9026
+ isLoading: isSubmitting || isAddingPromotions,
9027
+ children: "Save"
9028
+ }
9029
+ )
9030
+ ] }) })
9031
+ ] });
9032
+ };
9033
+ const PromotionItem = ({
9034
+ promotion,
9035
+ orderId,
9036
+ isLoading
9037
+ }) => {
9038
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
9039
+ const onRemove = async () => {
9040
+ removePromotions(
9041
+ {
9042
+ promo_codes: [promotion.code]
9043
+ },
9044
+ {
9045
+ onError: (e) => {
9046
+ ui.toast.error(e.message);
9047
+ }
9048
+ }
9049
+ );
9050
+ };
9051
+ const displayValue = getDisplayValue(promotion);
9052
+ return /* @__PURE__ */ jsxRuntime.jsxs(
9053
+ "div",
9054
+ {
9055
+ className: ui.clx(
9056
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
9057
+ {
9058
+ "animate-pulse": isLoading
9059
+ }
9060
+ ),
9061
+ children: [
9062
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
9063
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
9064
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
9065
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
9066
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
9067
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
9068
+ ] }),
9069
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: promotion.application_method?.allocation })
9070
+ ] })
9071
+ ] }),
9072
+ /* @__PURE__ */ jsxRuntime.jsx(
9073
+ ui.IconButton,
9074
+ {
9075
+ size: "small",
9076
+ type: "button",
9077
+ variant: "transparent",
9078
+ onClick: onRemove,
9079
+ isLoading: isPending || isLoading,
9080
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
9081
+ }
9082
+ )
9083
+ ]
9084
+ },
9085
+ promotion.id
9086
+ );
9087
+ };
9088
+ function getDisplayValue(promotion) {
9089
+ const value = promotion.application_method?.value;
9090
+ if (!value) {
9091
+ return null;
9092
+ }
9093
+ if (promotion.application_method?.type === "fixed") {
9094
+ const currency = promotion.application_method?.currency_code;
9095
+ if (!currency) {
9096
+ return null;
9097
+ }
9098
+ return getLocaleAmount(value, currency);
9099
+ } else if (promotion.application_method?.type === "percentage") {
9100
+ return formatPercentage(value);
9101
+ }
9102
+ return null;
9103
+ }
9104
+ const formatter = new Intl.NumberFormat([], {
9105
+ style: "percent",
9106
+ minimumFractionDigits: 2
9107
+ });
9108
+ const formatPercentage = (value, isPercentageValue = false) => {
9109
+ let val = value || 0;
9110
+ if (!isPercentageValue) {
9111
+ val = val / 100;
9112
+ }
9113
+ return formatter.format(val);
9114
+ };
9115
+ function getPromotionIds(items, shippingMethods) {
9116
+ const promotionIds = /* @__PURE__ */ new Set();
9117
+ for (const item of items) {
9118
+ if (item.adjustments) {
9119
+ for (const adjustment of item.adjustments) {
9120
+ if (adjustment.promotion_id) {
9121
+ promotionIds.add(adjustment.promotion_id);
9122
+ }
9123
+ }
9124
+ }
9125
+ }
9126
+ for (const shippingMethod of shippingMethods) {
9127
+ if (shippingMethod.adjustments) {
9128
+ for (const adjustment of shippingMethod.adjustments) {
9129
+ if (adjustment.promotion_id) {
9130
+ promotionIds.add(adjustment.promotion_id);
9131
+ }
9132
+ }
9133
+ }
9134
+ }
9135
+ return Array.from(promotionIds);
9136
+ }
9137
+ const InlineTip = React.forwardRef(
9138
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9139
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9140
+ return /* @__PURE__ */ jsxRuntime.jsxs(
9141
+ "div",
9142
+ {
9143
+ ref,
9144
+ className: ui.clx(
9145
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9146
+ className
9147
+ ),
9148
+ ...props,
9149
+ children: [
9150
+ /* @__PURE__ */ jsxRuntime.jsx(
9151
+ "div",
9152
+ {
9153
+ role: "presentation",
9154
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9155
+ "bg-ui-tag-orange-icon": variant === "warning"
9156
+ })
9157
+ }
9158
+ ),
9159
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
9160
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9161
+ labelValue,
9162
+ ":"
9163
+ ] }),
9164
+ " ",
9165
+ children
9166
+ ] })
9167
+ ]
9168
+ }
9169
+ );
9170
+ }
9171
+ );
9172
+ InlineTip.displayName = "InlineTip";
9173
+ const MetadataFieldSchema = zod.z.object({
9174
+ key: zod.z.string(),
9175
+ disabled: zod.z.boolean().optional(),
9176
+ value: zod.z.any()
9177
+ });
9178
+ const MetadataSchema = zod.z.object({
9179
+ metadata: zod.z.array(MetadataFieldSchema)
9180
+ });
9181
+ const Metadata = () => {
9182
+ const { id } = reactRouterDom.useParams();
9183
+ const { order, isPending, isError, error } = useOrder(id, {
9184
+ fields: "metadata"
9185
+ });
9186
+ if (isError) {
9187
+ throw error;
9188
+ }
9189
+ const isReady = !isPending && !!order;
9190
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9191
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9192
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
9193
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9194
+ ] }),
9195
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order?.metadata })
9196
+ ] });
9197
+ };
9198
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9199
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9200
+ const MetadataForm = ({ orderId, metadata }) => {
9201
+ const { handleSuccess } = useRouteModal();
9202
+ const hasUneditableRows = getHasUneditableRows(metadata);
9203
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9204
+ const form = reactHookForm.useForm({
9205
+ defaultValues: {
9206
+ metadata: getDefaultValues(metadata)
9207
+ },
9208
+ resolver: zod$1.zodResolver(MetadataSchema)
9209
+ });
9210
+ const handleSubmit = form.handleSubmit(async (data) => {
9211
+ const parsedData = parseValues(data);
9212
+ await mutateAsync(
9213
+ {
9214
+ metadata: parsedData
9215
+ },
9216
+ {
9217
+ onSuccess: () => {
9218
+ ui.toast.success("Metadata updated");
9219
+ handleSuccess();
9220
+ },
9221
+ onError: (error) => {
9222
+ ui.toast.error(error.message);
9223
+ }
9224
+ }
9225
+ );
9226
+ });
9227
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
9228
+ control: form.control,
9229
+ name: "metadata"
9230
+ });
9231
+ function deleteRow(index) {
9232
+ remove(index);
9233
+ if (fields.length === 1) {
9234
+ insert(0, {
9235
+ key: "",
9236
+ value: "",
9237
+ disabled: false
9238
+ });
9239
+ }
9240
+ }
9241
+ function insertRow(index, position) {
9242
+ insert(index + (position === "above" ? 0 : 1), {
9243
+ key: "",
9244
+ value: "",
9245
+ disabled: false
9246
+ });
9247
+ }
9248
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9249
+ KeyboundForm,
9250
+ {
9251
+ onSubmit: handleSubmit,
9252
+ className: "flex flex-1 flex-col overflow-hidden",
9253
+ children: [
9254
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9255
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9256
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9257
+ /* @__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" }) }),
9258
+ /* @__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" }) })
9259
+ ] }),
9260
+ fields.map((field, index) => {
9261
+ const isDisabled = field.disabled || false;
9262
+ let placeholder = "-";
9263
+ if (typeof field.value === "object") {
9264
+ placeholder = "{ ... }";
9265
+ }
9266
+ if (Array.isArray(field.value)) {
9267
+ placeholder = "[ ... ]";
9268
+ }
9269
+ return /* @__PURE__ */ jsxRuntime.jsx(
9270
+ ConditionalTooltip,
9271
+ {
9272
+ showTooltip: isDisabled,
9273
+ content: "This row is disabled because it contains non-primitive data.",
9274
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
9275
+ /* @__PURE__ */ jsxRuntime.jsxs(
9276
+ "div",
9277
+ {
9278
+ className: ui.clx("grid grid-cols-2 divide-x", {
9279
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9280
+ }),
9281
+ children: [
9282
+ /* @__PURE__ */ jsxRuntime.jsx(
9283
+ Form$2.Field,
9284
+ {
9285
+ control: form.control,
9286
+ name: `metadata.${index}.key`,
9287
+ render: ({ field: field2 }) => {
9288
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
9289
+ GridInput,
9290
+ {
9291
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
9292
+ ...field2,
9293
+ disabled: isDisabled,
9294
+ placeholder: "Key"
9295
+ }
9296
+ ) }) });
9297
+ }
9298
+ }
9299
+ ),
9300
+ /* @__PURE__ */ jsxRuntime.jsx(
9301
+ Form$2.Field,
9302
+ {
9303
+ control: form.control,
9304
+ name: `metadata.${index}.value`,
9305
+ render: ({ field: { value, ...field2 } }) => {
9306
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
9307
+ GridInput,
9308
+ {
9309
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
9310
+ ...field2,
9311
+ value: isDisabled ? placeholder : value,
9312
+ disabled: isDisabled,
9313
+ placeholder: "Value"
9314
+ }
9315
+ ) }) });
9316
+ }
9317
+ }
9318
+ )
9319
+ ]
9320
+ }
9321
+ ),
9322
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
9323
+ /* @__PURE__ */ jsxRuntime.jsx(
9324
+ ui.DropdownMenu.Trigger,
9325
+ {
9326
+ className: ui.clx(
9327
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9328
+ {
9329
+ hidden: isDisabled
9330
+ }
9331
+ ),
9332
+ disabled: isDisabled,
9333
+ asChild: true,
9334
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
9335
+ }
9336
+ ),
9337
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
9338
+ /* @__PURE__ */ jsxRuntime.jsxs(
9339
+ ui.DropdownMenu.Item,
9340
+ {
9341
+ className: "gap-x-2",
9342
+ onClick: () => insertRow(index, "above"),
9343
+ children: [
9344
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
9345
+ "Insert row above"
9346
+ ]
9347
+ }
9348
+ ),
9349
+ /* @__PURE__ */ jsxRuntime.jsxs(
9350
+ ui.DropdownMenu.Item,
9351
+ {
9352
+ className: "gap-x-2",
9353
+ onClick: () => insertRow(index, "below"),
9354
+ children: [
9355
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
9356
+ "Insert row below"
9357
+ ]
9358
+ }
9359
+ ),
9360
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
9361
+ /* @__PURE__ */ jsxRuntime.jsxs(
9362
+ ui.DropdownMenu.Item,
9363
+ {
9364
+ className: "gap-x-2",
9365
+ onClick: () => deleteRow(index),
9366
+ children: [
9367
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
9368
+ "Delete row"
9369
+ ]
9370
+ }
9371
+ )
9372
+ ] })
9373
+ ] })
9374
+ ] })
9375
+ },
9376
+ field.id
9377
+ );
9378
+ })
9379
+ ] }),
9380
+ 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." })
9381
+ ] }),
9382
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9383
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
9384
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9385
+ ] }) })
9386
+ ]
9387
+ }
9388
+ ) });
9377
9389
  };
9378
- const schema$1 = zod.z.object({
9379
- customer_id: zod.z.string().min(1)
9380
- });
9381
- const SalesChannel = () => {
9382
- const { id } = reactRouterDom.useParams();
9383
- const { draft_order, isPending, isError, error } = useDraftOrder(
9384
- id,
9385
- {
9386
- fields: "+sales_channel_id"
9387
- },
9390
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
9391
+ return /* @__PURE__ */ jsxRuntime.jsx(
9392
+ "input",
9388
9393
  {
9389
- enabled: !!id
9394
+ ref,
9395
+ ...props,
9396
+ autoComplete: "off",
9397
+ className: ui.clx(
9398
+ "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",
9399
+ className
9400
+ )
9390
9401
  }
9391
9402
  );
9392
- if (isError) {
9393
- throw error;
9394
- }
9395
- const ISrEADY = !!draft_order && !isPending;
9396
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9397
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9398
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
9399
- /* @__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" }) })
9400
- ] }),
9401
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
9403
+ });
9404
+ GridInput.displayName = "MetadataForm.GridInput";
9405
+ const PlaceholderInner = () => {
9406
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
9407
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
9408
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9409
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
9410
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
9411
+ ] }) })
9402
9412
  ] });
9403
9413
  };
9404
- const SalesChannelForm = ({ order }) => {
9405
- const form = reactHookForm.useForm({
9406
- defaultValues: {
9407
- sales_channel_id: order.sales_channel_id || ""
9408
- },
9409
- resolver: zod$1.zodResolver(schema)
9410
- });
9411
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9412
- const { handleSuccess } = useRouteModal();
9413
- const onSubmit = form.handleSubmit(async (data) => {
9414
- await mutateAsync(
9415
- {
9416
- sales_channel_id: data.sales_channel_id
9417
- },
9414
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
9415
+ function getDefaultValues(metadata) {
9416
+ if (!metadata || !Object.keys(metadata).length) {
9417
+ return [
9418
9418
  {
9419
- onSuccess: () => {
9420
- ui.toast.success("Sales channel updated");
9421
- handleSuccess();
9422
- },
9423
- onError: (error) => {
9424
- ui.toast.error(error.message);
9425
- }
9419
+ key: "",
9420
+ value: "",
9421
+ disabled: false
9426
9422
  }
9427
- );
9428
- });
9429
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9430
- KeyboundForm,
9431
- {
9432
- className: "flex flex-1 flex-col overflow-hidden",
9433
- onSubmit,
9434
- children: [
9435
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
9436
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9437
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9438
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9439
- ] }) })
9440
- ]
9423
+ ];
9424
+ }
9425
+ return Object.entries(metadata).map(([key, value]) => {
9426
+ if (!EDITABLE_TYPES.includes(typeof value)) {
9427
+ return {
9428
+ key,
9429
+ value,
9430
+ disabled: true
9431
+ };
9441
9432
  }
9442
- ) });
9443
- };
9444
- const SalesChannelField = ({ control, order }) => {
9445
- const salesChannels = useComboboxData({
9446
- queryFn: async (params) => {
9447
- return await sdk.admin.salesChannel.list(params);
9448
- },
9449
- queryKey: ["sales-channels"],
9450
- getOptions: (data) => {
9451
- return data.sales_channels.map((salesChannel) => ({
9452
- label: salesChannel.name,
9453
- value: salesChannel.id
9454
- }));
9455
- },
9456
- defaultValue: order.sales_channel_id || void 0
9433
+ let stringValue = value;
9434
+ if (typeof value !== "string") {
9435
+ stringValue = JSON.stringify(value);
9436
+ }
9437
+ return {
9438
+ key,
9439
+ value: stringValue,
9440
+ original_key: key
9441
+ };
9457
9442
  });
9458
- return /* @__PURE__ */ jsxRuntime.jsx(
9459
- Form$2.Field,
9460
- {
9461
- control,
9462
- name: "sales_channel_id",
9463
- render: ({ field }) => {
9464
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9465
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
9466
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
9467
- Combobox,
9468
- {
9469
- options: salesChannels.options,
9470
- fetchNextPage: salesChannels.fetchNextPage,
9471
- isFetchingNextPage: salesChannels.isFetchingNextPage,
9472
- searchValue: salesChannels.searchValue,
9473
- onSearchValueChange: salesChannels.onSearchValueChange,
9474
- placeholder: "Select sales channel",
9475
- ...field
9476
- }
9477
- ) }),
9478
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9479
- ] });
9443
+ }
9444
+ function parseValues(values) {
9445
+ const metadata = values.metadata;
9446
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
9447
+ if (isEmpty) {
9448
+ return null;
9449
+ }
9450
+ const update = {};
9451
+ metadata.forEach((field) => {
9452
+ let key = field.key;
9453
+ let value = field.value;
9454
+ const disabled = field.disabled;
9455
+ if (!key || !value) {
9456
+ return;
9457
+ }
9458
+ if (disabled) {
9459
+ update[key] = value;
9460
+ return;
9461
+ }
9462
+ key = key.trim();
9463
+ value = value.trim();
9464
+ if (value === "true") {
9465
+ update[key] = true;
9466
+ } else if (value === "false") {
9467
+ update[key] = false;
9468
+ } else {
9469
+ const parsedNumber = parseFloat(value);
9470
+ if (!isNaN(parsedNumber)) {
9471
+ update[key] = parsedNumber;
9472
+ } else {
9473
+ update[key] = value;
9480
9474
  }
9481
9475
  }
9476
+ });
9477
+ return update;
9478
+ }
9479
+ function getHasUneditableRows(metadata) {
9480
+ if (!metadata) {
9481
+ return false;
9482
+ }
9483
+ return Object.values(metadata).some(
9484
+ (value) => !EDITABLE_TYPES.includes(typeof value)
9482
9485
  );
9483
- };
9484
- const schema = zod.z.object({
9485
- sales_channel_id: zod.z.string().min(1)
9486
- });
9486
+ }
9487
9487
  const widgetModule = { widgets: [] };
9488
9488
  const routeModule = {
9489
9489
  routes: [
@@ -9504,6 +9504,10 @@ const routeModule = {
9504
9504
  handle,
9505
9505
  loader,
9506
9506
  children: [
9507
+ {
9508
+ Component: BillingAddress,
9509
+ path: "/draft-orders/:id/billing-address"
9510
+ },
9507
9511
  {
9508
9512
  Component: CustomItems,
9509
9513
  path: "/draft-orders/:id/custom-items"
@@ -9512,21 +9516,13 @@ const routeModule = {
9512
9516
  Component: Email,
9513
9517
  path: "/draft-orders/:id/email"
9514
9518
  },
9515
- {
9516
- Component: BillingAddress,
9517
- path: "/draft-orders/:id/billing-address"
9518
- },
9519
9519
  {
9520
9520
  Component: Items,
9521
9521
  path: "/draft-orders/:id/items"
9522
9522
  },
9523
9523
  {
9524
- Component: Metadata,
9525
- path: "/draft-orders/:id/metadata"
9526
- },
9527
- {
9528
- Component: Promotions,
9529
- path: "/draft-orders/:id/promotions"
9524
+ Component: SalesChannel,
9525
+ path: "/draft-orders/:id/sales-channel"
9530
9526
  },
9531
9527
  {
9532
9528
  Component: Shipping,
@@ -9541,8 +9537,12 @@ const routeModule = {
9541
9537
  path: "/draft-orders/:id/transfer-ownership"
9542
9538
  },
9543
9539
  {
9544
- Component: SalesChannel,
9545
- path: "/draft-orders/:id/sales-channel"
9540
+ Component: Promotions,
9541
+ path: "/draft-orders/:id/promotions"
9542
+ },
9543
+ {
9544
+ Component: Metadata,
9545
+ path: "/draft-orders/:id/metadata"
9546
9546
  }
9547
9547
  ]
9548
9548
  }