@medusajs/draft-order 2.11.1-preview-20251022032035 → 2.11.1-preview-20251022090215

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.
@@ -4,7 +4,7 @@ import { Tooltip, DropdownMenu, clx, IconButton, useDataTable, DataTable as Data
4
4
  import { useQuery, useQueryClient, useMutation, keepPreviousData, useInfiniteQuery } from "@tanstack/react-query";
5
5
  import React, { useState, useCallback, useMemo, Fragment, createContext, forwardRef, useId, useContext, useTransition, useRef, useImperativeHandle, useDeferredValue, useEffect, Suspense } from "react";
6
6
  import { useSearchParams, Link, useNavigate, Outlet, useBlocker, useLocation, useParams } from "react-router-dom";
7
- import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, EllipsisVertical, ArrowUpMini, ArrowDownMini, Minus, PencilSquare } from "@medusajs/icons";
7
+ import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, Minus, PencilSquare, EllipsisVertical, ArrowUpMini, ArrowDownMini } from "@medusajs/icons";
8
8
  import Medusa from "@medusajs/js-sdk";
9
9
  import { format, formatDistance, sub, subDays, subMonths } from "date-fns";
10
10
  import { enUS } from "date-fns/locale";
@@ -9755,54 +9755,31 @@ const BillingAddressForm = ({ order }) => {
9755
9755
  ) });
9756
9756
  };
9757
9757
  const schema$5 = addressSchema;
9758
- const InlineTip = forwardRef(
9759
- ({ variant = "tip", label, className, children, ...props }, ref) => {
9760
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9761
- return /* @__PURE__ */ jsxs(
9762
- "div",
9763
- {
9764
- ref,
9765
- className: clx(
9766
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9767
- className
9768
- ),
9769
- ...props,
9770
- children: [
9771
- /* @__PURE__ */ jsx(
9772
- "div",
9773
- {
9774
- role: "presentation",
9775
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9776
- "bg-ui-tag-orange-icon": variant === "warning"
9777
- })
9778
- }
9779
- ),
9780
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9781
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9782
- labelValue,
9783
- ":"
9784
- ] }),
9785
- " ",
9786
- children
9787
- ] })
9788
- ]
9789
- }
9790
- );
9791
- }
9792
- );
9793
- InlineTip.displayName = "InlineTip";
9794
- const MetadataFieldSchema = objectType({
9795
- key: stringType(),
9796
- disabled: booleanType().optional(),
9797
- value: anyType()
9798
- });
9799
- const MetadataSchema = objectType({
9800
- metadata: arrayType(MetadataFieldSchema)
9758
+ const CustomItems = () => {
9759
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9760
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9761
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
9762
+ ] });
9763
+ };
9764
+ const CustomItemsForm = () => {
9765
+ const form = useForm({
9766
+ resolver: zodResolver(schema$4)
9767
+ });
9768
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9769
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9770
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9771
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9772
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9773
+ ] }) })
9774
+ ] }) });
9775
+ };
9776
+ const schema$4 = objectType({
9777
+ email: stringType().email()
9801
9778
  });
9802
- const Metadata = () => {
9779
+ const Email = () => {
9803
9780
  const { id } = useParams();
9804
9781
  const { order, isPending, isError, error } = useOrder(id, {
9805
- fields: "metadata"
9782
+ fields: "+email"
9806
9783
  });
9807
9784
  if (isError) {
9808
9785
  throw error;
@@ -9810,33 +9787,26 @@ const Metadata = () => {
9810
9787
  const isReady = !isPending && !!order;
9811
9788
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9812
9789
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9813
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9814
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9790
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
9791
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9815
9792
  ] }),
9816
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9793
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
9817
9794
  ] });
9818
9795
  };
9819
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9820
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9821
- const MetadataForm = ({ orderId, metadata }) => {
9822
- const { handleSuccess } = useRouteModal();
9823
- const hasUneditableRows = getHasUneditableRows(metadata);
9824
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9796
+ const EmailForm = ({ order }) => {
9825
9797
  const form = useForm({
9826
9798
  defaultValues: {
9827
- metadata: getDefaultValues(metadata)
9799
+ email: order.email ?? ""
9828
9800
  },
9829
- resolver: zodResolver(MetadataSchema)
9801
+ resolver: zodResolver(schema$3)
9830
9802
  });
9831
- const handleSubmit = form.handleSubmit(async (data) => {
9832
- const parsedData = parseValues(data);
9803
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9804
+ const { handleSuccess } = useRouteModal();
9805
+ const onSubmit = form.handleSubmit(async (data) => {
9833
9806
  await mutateAsync(
9834
- {
9835
- metadata: parsedData
9836
- },
9807
+ { email: data.email },
9837
9808
  {
9838
9809
  onSuccess: () => {
9839
- toast.success("Metadata updated");
9840
9810
  handleSuccess();
9841
9811
  },
9842
9812
  onError: (error) => {
@@ -9845,266 +9815,35 @@ const MetadataForm = ({ orderId, metadata }) => {
9845
9815
  }
9846
9816
  );
9847
9817
  });
9848
- const { fields, insert, remove } = useFieldArray({
9849
- control: form.control,
9850
- name: "metadata"
9851
- });
9852
- function deleteRow(index) {
9853
- remove(index);
9854
- if (fields.length === 1) {
9855
- insert(0, {
9856
- key: "",
9857
- value: "",
9858
- disabled: false
9859
- });
9860
- }
9861
- }
9862
- function insertRow(index, position) {
9863
- insert(index + (position === "above" ? 0 : 1), {
9864
- key: "",
9865
- value: "",
9866
- disabled: false
9867
- });
9868
- }
9869
9818
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9870
9819
  KeyboundForm,
9871
9820
  {
9872
- onSubmit: handleSubmit,
9873
9821
  className: "flex flex-1 flex-col overflow-hidden",
9822
+ onSubmit,
9874
9823
  children: [
9875
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9876
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9877
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9878
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
9879
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
9880
- ] }),
9881
- fields.map((field, index) => {
9882
- const isDisabled = field.disabled || false;
9883
- let placeholder = "-";
9884
- if (typeof field.value === "object") {
9885
- placeholder = "{ ... }";
9886
- }
9887
- if (Array.isArray(field.value)) {
9888
- placeholder = "[ ... ]";
9889
- }
9890
- return /* @__PURE__ */ jsx(
9891
- ConditionalTooltip,
9892
- {
9893
- showTooltip: isDisabled,
9894
- content: "This row is disabled because it contains non-primitive data.",
9895
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9896
- /* @__PURE__ */ jsxs(
9897
- "div",
9898
- {
9899
- className: clx("grid grid-cols-2 divide-x", {
9900
- "overflow-hidden rounded-b-lg": index === fields.length - 1
9901
- }),
9902
- children: [
9903
- /* @__PURE__ */ jsx(
9904
- Form$2.Field,
9905
- {
9906
- control: form.control,
9907
- name: `metadata.${index}.key`,
9908
- render: ({ field: field2 }) => {
9909
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9910
- GridInput,
9911
- {
9912
- "aria-labelledby": METADATA_KEY_LABEL_ID,
9913
- ...field2,
9914
- disabled: isDisabled,
9915
- placeholder: "Key"
9916
- }
9917
- ) }) });
9918
- }
9919
- }
9920
- ),
9921
- /* @__PURE__ */ jsx(
9922
- Form$2.Field,
9923
- {
9924
- control: form.control,
9925
- name: `metadata.${index}.value`,
9926
- render: ({ field: { value, ...field2 } }) => {
9927
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9928
- GridInput,
9929
- {
9930
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
9931
- ...field2,
9932
- value: isDisabled ? placeholder : value,
9933
- disabled: isDisabled,
9934
- placeholder: "Value"
9935
- }
9936
- ) }) });
9937
- }
9938
- }
9939
- )
9940
- ]
9941
- }
9942
- ),
9943
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
9944
- /* @__PURE__ */ jsx(
9945
- DropdownMenu.Trigger,
9946
- {
9947
- className: clx(
9948
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9949
- {
9950
- hidden: isDisabled
9951
- }
9952
- ),
9953
- disabled: isDisabled,
9954
- asChild: true,
9955
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
9956
- }
9957
- ),
9958
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
9959
- /* @__PURE__ */ jsxs(
9960
- DropdownMenu.Item,
9961
- {
9962
- className: "gap-x-2",
9963
- onClick: () => insertRow(index, "above"),
9964
- children: [
9965
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
9966
- "Insert row above"
9967
- ]
9968
- }
9969
- ),
9970
- /* @__PURE__ */ jsxs(
9971
- DropdownMenu.Item,
9972
- {
9973
- className: "gap-x-2",
9974
- onClick: () => insertRow(index, "below"),
9975
- children: [
9976
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
9977
- "Insert row below"
9978
- ]
9979
- }
9980
- ),
9981
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
9982
- /* @__PURE__ */ jsxs(
9983
- DropdownMenu.Item,
9984
- {
9985
- className: "gap-x-2",
9986
- onClick: () => deleteRow(index),
9987
- children: [
9988
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
9989
- "Delete row"
9990
- ]
9991
- }
9992
- )
9993
- ] })
9994
- ] })
9995
- ] })
9996
- },
9997
- field.id
9998
- );
9999
- })
10000
- ] }),
10001
- hasUneditableRows && /* @__PURE__ */ 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." })
10002
- ] }),
10003
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10004
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10005
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10006
- ] }) })
10007
- ]
10008
- }
10009
- ) });
10010
- };
10011
- const GridInput = forwardRef(({ className, ...props }, ref) => {
10012
- return /* @__PURE__ */ jsx(
10013
- "input",
10014
- {
10015
- ref,
10016
- ...props,
10017
- autoComplete: "off",
10018
- className: clx(
10019
- "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",
10020
- className
10021
- )
10022
- }
10023
- );
10024
- });
10025
- GridInput.displayName = "MetadataForm.GridInput";
10026
- const PlaceholderInner = () => {
10027
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10028
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10029
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10030
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
10031
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
10032
- ] }) })
10033
- ] });
10034
- };
10035
- const EDITABLE_TYPES = ["string", "number", "boolean"];
10036
- function getDefaultValues(metadata) {
10037
- if (!metadata || !Object.keys(metadata).length) {
10038
- return [
10039
- {
10040
- key: "",
10041
- value: "",
10042
- disabled: false
10043
- }
10044
- ];
10045
- }
10046
- return Object.entries(metadata).map(([key, value]) => {
10047
- if (!EDITABLE_TYPES.includes(typeof value)) {
10048
- return {
10049
- key,
10050
- value,
10051
- disabled: true
10052
- };
10053
- }
10054
- let stringValue = value;
10055
- if (typeof value !== "string") {
10056
- stringValue = JSON.stringify(value);
10057
- }
10058
- return {
10059
- key,
10060
- value: stringValue,
10061
- original_key: key
10062
- };
10063
- });
10064
- }
10065
- function parseValues(values) {
10066
- const metadata = values.metadata;
10067
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10068
- if (isEmpty) {
10069
- return null;
10070
- }
10071
- const update = {};
10072
- metadata.forEach((field) => {
10073
- let key = field.key;
10074
- let value = field.value;
10075
- const disabled = field.disabled;
10076
- if (!key || !value) {
10077
- return;
10078
- }
10079
- if (disabled) {
10080
- update[key] = value;
10081
- return;
10082
- }
10083
- key = key.trim();
10084
- value = value.trim();
10085
- if (value === "true") {
10086
- update[key] = true;
10087
- } else if (value === "false") {
10088
- update[key] = false;
10089
- } else {
10090
- const parsedNumber = parseFloat(value);
10091
- if (!isNaN(parsedNumber)) {
10092
- update[key] = parsedNumber;
10093
- } else {
10094
- update[key] = value;
10095
- }
9824
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
9825
+ Form$2.Field,
9826
+ {
9827
+ control: form.control,
9828
+ name: "email",
9829
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9830
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
9831
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9832
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9833
+ ] })
9834
+ }
9835
+ ) }),
9836
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9837
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9838
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9839
+ ] }) })
9840
+ ]
10096
9841
  }
10097
- });
10098
- return update;
10099
- }
10100
- function getHasUneditableRows(metadata) {
10101
- if (!metadata) {
10102
- return false;
10103
- }
10104
- return Object.values(metadata).some(
10105
- (value) => !EDITABLE_TYPES.includes(typeof value)
10106
- );
10107
- }
9842
+ ) });
9843
+ };
9844
+ const schema$3 = objectType({
9845
+ email: stringType().email()
9846
+ });
10108
9847
  const NumberInput = forwardRef(
10109
9848
  ({
10110
9849
  value,
@@ -11067,18 +10806,368 @@ const CustomItemForm = ({ orderId, currencyCode }) => {
11067
10806
  ] }) })
11068
10807
  }
11069
10808
  )
11070
- ] }) }) }),
11071
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11072
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11073
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10809
+ ] }) }) }),
10810
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10811
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10812
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10813
+ ] }) })
10814
+ ] }) }) });
10815
+ };
10816
+ const customItemSchema = objectType({
10817
+ title: stringType().min(1),
10818
+ quantity: numberType(),
10819
+ unit_price: unionType([numberType(), stringType()])
10820
+ });
10821
+ const InlineTip = forwardRef(
10822
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
10823
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10824
+ return /* @__PURE__ */ jsxs(
10825
+ "div",
10826
+ {
10827
+ ref,
10828
+ className: clx(
10829
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10830
+ className
10831
+ ),
10832
+ ...props,
10833
+ children: [
10834
+ /* @__PURE__ */ jsx(
10835
+ "div",
10836
+ {
10837
+ role: "presentation",
10838
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10839
+ "bg-ui-tag-orange-icon": variant === "warning"
10840
+ })
10841
+ }
10842
+ ),
10843
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
10844
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10845
+ labelValue,
10846
+ ":"
10847
+ ] }),
10848
+ " ",
10849
+ children
10850
+ ] })
10851
+ ]
10852
+ }
10853
+ );
10854
+ }
10855
+ );
10856
+ InlineTip.displayName = "InlineTip";
10857
+ const MetadataFieldSchema = objectType({
10858
+ key: stringType(),
10859
+ disabled: booleanType().optional(),
10860
+ value: anyType()
10861
+ });
10862
+ const MetadataSchema = objectType({
10863
+ metadata: arrayType(MetadataFieldSchema)
10864
+ });
10865
+ const Metadata = () => {
10866
+ const { id } = useParams();
10867
+ const { order, isPending, isError, error } = useOrder(id, {
10868
+ fields: "metadata"
10869
+ });
10870
+ if (isError) {
10871
+ throw error;
10872
+ }
10873
+ const isReady = !isPending && !!order;
10874
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10875
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10876
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
10877
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10878
+ ] }),
10879
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10880
+ ] });
10881
+ };
10882
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10883
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10884
+ const MetadataForm = ({ orderId, metadata }) => {
10885
+ const { handleSuccess } = useRouteModal();
10886
+ const hasUneditableRows = getHasUneditableRows(metadata);
10887
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10888
+ const form = useForm({
10889
+ defaultValues: {
10890
+ metadata: getDefaultValues(metadata)
10891
+ },
10892
+ resolver: zodResolver(MetadataSchema)
10893
+ });
10894
+ const handleSubmit = form.handleSubmit(async (data) => {
10895
+ const parsedData = parseValues(data);
10896
+ await mutateAsync(
10897
+ {
10898
+ metadata: parsedData
10899
+ },
10900
+ {
10901
+ onSuccess: () => {
10902
+ toast.success("Metadata updated");
10903
+ handleSuccess();
10904
+ },
10905
+ onError: (error) => {
10906
+ toast.error(error.message);
10907
+ }
10908
+ }
10909
+ );
10910
+ });
10911
+ const { fields, insert, remove } = useFieldArray({
10912
+ control: form.control,
10913
+ name: "metadata"
10914
+ });
10915
+ function deleteRow(index) {
10916
+ remove(index);
10917
+ if (fields.length === 1) {
10918
+ insert(0, {
10919
+ key: "",
10920
+ value: "",
10921
+ disabled: false
10922
+ });
10923
+ }
10924
+ }
10925
+ function insertRow(index, position) {
10926
+ insert(index + (position === "above" ? 0 : 1), {
10927
+ key: "",
10928
+ value: "",
10929
+ disabled: false
10930
+ });
10931
+ }
10932
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10933
+ KeyboundForm,
10934
+ {
10935
+ onSubmit: handleSubmit,
10936
+ className: "flex flex-1 flex-col overflow-hidden",
10937
+ children: [
10938
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10939
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10940
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10941
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
10942
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
10943
+ ] }),
10944
+ fields.map((field, index) => {
10945
+ const isDisabled = field.disabled || false;
10946
+ let placeholder = "-";
10947
+ if (typeof field.value === "object") {
10948
+ placeholder = "{ ... }";
10949
+ }
10950
+ if (Array.isArray(field.value)) {
10951
+ placeholder = "[ ... ]";
10952
+ }
10953
+ return /* @__PURE__ */ jsx(
10954
+ ConditionalTooltip,
10955
+ {
10956
+ showTooltip: isDisabled,
10957
+ content: "This row is disabled because it contains non-primitive data.",
10958
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
10959
+ /* @__PURE__ */ jsxs(
10960
+ "div",
10961
+ {
10962
+ className: clx("grid grid-cols-2 divide-x", {
10963
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
10964
+ }),
10965
+ children: [
10966
+ /* @__PURE__ */ jsx(
10967
+ Form$2.Field,
10968
+ {
10969
+ control: form.control,
10970
+ name: `metadata.${index}.key`,
10971
+ render: ({ field: field2 }) => {
10972
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10973
+ GridInput,
10974
+ {
10975
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
10976
+ ...field2,
10977
+ disabled: isDisabled,
10978
+ placeholder: "Key"
10979
+ }
10980
+ ) }) });
10981
+ }
10982
+ }
10983
+ ),
10984
+ /* @__PURE__ */ jsx(
10985
+ Form$2.Field,
10986
+ {
10987
+ control: form.control,
10988
+ name: `metadata.${index}.value`,
10989
+ render: ({ field: { value, ...field2 } }) => {
10990
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10991
+ GridInput,
10992
+ {
10993
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
10994
+ ...field2,
10995
+ value: isDisabled ? placeholder : value,
10996
+ disabled: isDisabled,
10997
+ placeholder: "Value"
10998
+ }
10999
+ ) }) });
11000
+ }
11001
+ }
11002
+ )
11003
+ ]
11004
+ }
11005
+ ),
11006
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
11007
+ /* @__PURE__ */ jsx(
11008
+ DropdownMenu.Trigger,
11009
+ {
11010
+ className: clx(
11011
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11012
+ {
11013
+ hidden: isDisabled
11014
+ }
11015
+ ),
11016
+ disabled: isDisabled,
11017
+ asChild: true,
11018
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
11019
+ }
11020
+ ),
11021
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
11022
+ /* @__PURE__ */ jsxs(
11023
+ DropdownMenu.Item,
11024
+ {
11025
+ className: "gap-x-2",
11026
+ onClick: () => insertRow(index, "above"),
11027
+ children: [
11028
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
11029
+ "Insert row above"
11030
+ ]
11031
+ }
11032
+ ),
11033
+ /* @__PURE__ */ jsxs(
11034
+ DropdownMenu.Item,
11035
+ {
11036
+ className: "gap-x-2",
11037
+ onClick: () => insertRow(index, "below"),
11038
+ children: [
11039
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
11040
+ "Insert row below"
11041
+ ]
11042
+ }
11043
+ ),
11044
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
11045
+ /* @__PURE__ */ jsxs(
11046
+ DropdownMenu.Item,
11047
+ {
11048
+ className: "gap-x-2",
11049
+ onClick: () => deleteRow(index),
11050
+ children: [
11051
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
11052
+ "Delete row"
11053
+ ]
11054
+ }
11055
+ )
11056
+ ] })
11057
+ ] })
11058
+ ] })
11059
+ },
11060
+ field.id
11061
+ );
11062
+ })
11063
+ ] }),
11064
+ hasUneditableRows && /* @__PURE__ */ 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." })
11065
+ ] }),
11066
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11067
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11068
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11069
+ ] }) })
11070
+ ]
11071
+ }
11072
+ ) });
11073
+ };
11074
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
11075
+ return /* @__PURE__ */ jsx(
11076
+ "input",
11077
+ {
11078
+ ref,
11079
+ ...props,
11080
+ autoComplete: "off",
11081
+ className: clx(
11082
+ "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",
11083
+ className
11084
+ )
11085
+ }
11086
+ );
11087
+ });
11088
+ GridInput.displayName = "MetadataForm.GridInput";
11089
+ const PlaceholderInner = () => {
11090
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11091
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11092
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11093
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11094
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11074
11095
  ] }) })
11075
- ] }) }) });
11096
+ ] });
11076
11097
  };
11077
- const customItemSchema = objectType({
11078
- title: stringType().min(1),
11079
- quantity: numberType(),
11080
- unit_price: unionType([numberType(), stringType()])
11081
- });
11098
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
11099
+ function getDefaultValues(metadata) {
11100
+ if (!metadata || !Object.keys(metadata).length) {
11101
+ return [
11102
+ {
11103
+ key: "",
11104
+ value: "",
11105
+ disabled: false
11106
+ }
11107
+ ];
11108
+ }
11109
+ return Object.entries(metadata).map(([key, value]) => {
11110
+ if (!EDITABLE_TYPES.includes(typeof value)) {
11111
+ return {
11112
+ key,
11113
+ value,
11114
+ disabled: true
11115
+ };
11116
+ }
11117
+ let stringValue = value;
11118
+ if (typeof value !== "string") {
11119
+ stringValue = JSON.stringify(value);
11120
+ }
11121
+ return {
11122
+ key,
11123
+ value: stringValue,
11124
+ original_key: key
11125
+ };
11126
+ });
11127
+ }
11128
+ function parseValues(values) {
11129
+ const metadata = values.metadata;
11130
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11131
+ if (isEmpty) {
11132
+ return null;
11133
+ }
11134
+ const update = {};
11135
+ metadata.forEach((field) => {
11136
+ let key = field.key;
11137
+ let value = field.value;
11138
+ const disabled = field.disabled;
11139
+ if (!key || !value) {
11140
+ return;
11141
+ }
11142
+ if (disabled) {
11143
+ update[key] = value;
11144
+ return;
11145
+ }
11146
+ key = key.trim();
11147
+ value = value.trim();
11148
+ if (value === "true") {
11149
+ update[key] = true;
11150
+ } else if (value === "false") {
11151
+ update[key] = false;
11152
+ } else {
11153
+ const parsedNumber = parseFloat(value);
11154
+ if (!isNaN(parsedNumber)) {
11155
+ update[key] = parsedNumber;
11156
+ } else {
11157
+ update[key] = value;
11158
+ }
11159
+ }
11160
+ });
11161
+ return update;
11162
+ }
11163
+ function getHasUneditableRows(metadata) {
11164
+ if (!metadata) {
11165
+ return false;
11166
+ }
11167
+ return Object.values(metadata).some(
11168
+ (value) => !EDITABLE_TYPES.includes(typeof value)
11169
+ );
11170
+ }
11082
11171
  const PROMOTION_QUERY_KEY = "promotions";
11083
11172
  const promotionsQueryKeys = {
11084
11173
  list: (query2) => [
@@ -11384,7 +11473,7 @@ const SalesChannelForm = ({ order }) => {
11384
11473
  defaultValues: {
11385
11474
  sales_channel_id: order.sales_channel_id || ""
11386
11475
  },
11387
- resolver: zodResolver(schema$4)
11476
+ resolver: zodResolver(schema$2)
11388
11477
  });
11389
11478
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11390
11479
  const { handleSuccess } = useRouteModal();
@@ -11459,7 +11548,7 @@ const SalesChannelField = ({ control, order }) => {
11459
11548
  }
11460
11549
  );
11461
11550
  };
11462
- const schema$4 = objectType({
11551
+ const schema$2 = objectType({
11463
11552
  sales_channel_id: stringType().min(1)
11464
11553
  });
11465
11554
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
@@ -12301,7 +12390,7 @@ const ShippingAddressForm = ({ order }) => {
12301
12390
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12302
12391
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12303
12392
  },
12304
- resolver: zodResolver(schema$3)
12393
+ resolver: zodResolver(schema$1)
12305
12394
  });
12306
12395
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12307
12396
  const { handleSuccess } = useRouteModal();
@@ -12471,7 +12560,7 @@ const ShippingAddressForm = ({ order }) => {
12471
12560
  }
12472
12561
  ) });
12473
12562
  };
12474
- const schema$3 = addressSchema;
12563
+ const schema$1 = addressSchema;
12475
12564
  const TransferOwnership = () => {
12476
12565
  const { id } = useParams();
12477
12566
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12495,7 +12584,7 @@ const TransferOwnershipForm = ({ order }) => {
12495
12584
  defaultValues: {
12496
12585
  customer_id: order.customer_id || ""
12497
12586
  },
12498
- resolver: zodResolver(schema$2)
12587
+ resolver: zodResolver(schema)
12499
12588
  });
12500
12589
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12501
12590
  const { handleSuccess } = useRouteModal();
@@ -12945,97 +13034,8 @@ const Illustration = () => {
12945
13034
  }
12946
13035
  );
12947
13036
  };
12948
- const schema$2 = objectType({
12949
- customer_id: stringType().min(1)
12950
- });
12951
- const CustomItems = () => {
12952
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12953
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
12954
- /* @__PURE__ */ jsx(CustomItemsForm, {})
12955
- ] });
12956
- };
12957
- const CustomItemsForm = () => {
12958
- const form = useForm({
12959
- resolver: zodResolver(schema$1)
12960
- });
12961
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
12962
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
12963
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12964
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12965
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
12966
- ] }) })
12967
- ] }) });
12968
- };
12969
- const schema$1 = objectType({
12970
- email: stringType().email()
12971
- });
12972
- const Email = () => {
12973
- const { id } = useParams();
12974
- const { order, isPending, isError, error } = useOrder(id, {
12975
- fields: "+email"
12976
- });
12977
- if (isError) {
12978
- throw error;
12979
- }
12980
- const isReady = !isPending && !!order;
12981
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12982
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12983
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
12984
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
12985
- ] }),
12986
- isReady && /* @__PURE__ */ jsx(EmailForm, { order })
12987
- ] });
12988
- };
12989
- const EmailForm = ({ order }) => {
12990
- const form = useForm({
12991
- defaultValues: {
12992
- email: order.email ?? ""
12993
- },
12994
- resolver: zodResolver(schema)
12995
- });
12996
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12997
- const { handleSuccess } = useRouteModal();
12998
- const onSubmit = form.handleSubmit(async (data) => {
12999
- await mutateAsync(
13000
- { email: data.email },
13001
- {
13002
- onSuccess: () => {
13003
- handleSuccess();
13004
- },
13005
- onError: (error) => {
13006
- toast.error(error.message);
13007
- }
13008
- }
13009
- );
13010
- });
13011
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
13012
- KeyboundForm,
13013
- {
13014
- className: "flex flex-1 flex-col overflow-hidden",
13015
- onSubmit,
13016
- children: [
13017
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
13018
- Form$2.Field,
13019
- {
13020
- control: form.control,
13021
- name: "email",
13022
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13023
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
13024
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13025
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13026
- ] })
13027
- }
13028
- ) }),
13029
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13030
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13031
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13032
- ] }) })
13033
- ]
13034
- }
13035
- ) });
13036
- };
13037
13037
  const schema = objectType({
13038
- email: stringType().email()
13038
+ customer_id: stringType().min(1)
13039
13039
  });
13040
13040
  const widgetModule = { widgets: [] };
13041
13041
  const routeModule = {
@@ -13062,13 +13062,21 @@ const routeModule = {
13062
13062
  path: "/draft-orders/:id/billing-address"
13063
13063
  },
13064
13064
  {
13065
- Component: Metadata,
13066
- path: "/draft-orders/:id/metadata"
13065
+ Component: CustomItems,
13066
+ path: "/draft-orders/:id/custom-items"
13067
+ },
13068
+ {
13069
+ Component: Email,
13070
+ path: "/draft-orders/:id/email"
13067
13071
  },
13068
13072
  {
13069
13073
  Component: Items,
13070
13074
  path: "/draft-orders/:id/items"
13071
13075
  },
13076
+ {
13077
+ Component: Metadata,
13078
+ path: "/draft-orders/:id/metadata"
13079
+ },
13072
13080
  {
13073
13081
  Component: Promotions,
13074
13082
  path: "/draft-orders/:id/promotions"
@@ -13088,14 +13096,6 @@ const routeModule = {
13088
13096
  {
13089
13097
  Component: TransferOwnership,
13090
13098
  path: "/draft-orders/:id/transfer-ownership"
13091
- },
13092
- {
13093
- Component: CustomItems,
13094
- path: "/draft-orders/:id/custom-items"
13095
- },
13096
- {
13097
- Component: Email,
13098
- path: "/draft-orders/:id/email"
13099
13099
  }
13100
13100
  ]
13101
13101
  }