@medusajs/draft-order 2.10.2-preview-20250902210158 → 2.10.2-preview-20250903000308

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";
@@ -9554,27 +9554,196 @@ const ID = () => {
9554
9554
  /* @__PURE__ */ jsx(Outlet, {})
9555
9555
  ] });
9556
9556
  };
9557
- const CustomItems = () => {
9557
+ const BillingAddress = () => {
9558
+ const { id } = useParams();
9559
+ const { order, isPending, isError, error } = useOrder(id, {
9560
+ fields: "+billing_address"
9561
+ });
9562
+ if (isError) {
9563
+ throw error;
9564
+ }
9565
+ const isReady = !isPending && !!order;
9558
9566
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9559
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9560
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9567
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9568
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Billing Address" }) }),
9569
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
9570
+ ] }),
9571
+ isReady && /* @__PURE__ */ jsx(BillingAddressForm, { order })
9561
9572
  ] });
9562
9573
  };
9563
- const CustomItemsForm = () => {
9574
+ const BillingAddressForm = ({ order }) => {
9575
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
9564
9576
  const form = useForm({
9577
+ defaultValues: {
9578
+ first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
9579
+ last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
9580
+ company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
9581
+ address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
9582
+ address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
9583
+ city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
9584
+ province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
9585
+ country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
9586
+ postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9587
+ phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9588
+ },
9565
9589
  resolver: zodResolver(schema$5)
9566
9590
  });
9567
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9568
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9569
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9570
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9571
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9572
- ] }) })
9573
- ] }) });
9591
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9592
+ const { handleSuccess } = useRouteModal();
9593
+ const onSubmit = form.handleSubmit(async (data) => {
9594
+ await mutateAsync(
9595
+ { billing_address: data },
9596
+ {
9597
+ onSuccess: () => {
9598
+ handleSuccess();
9599
+ },
9600
+ onError: (error) => {
9601
+ toast.error(error.message);
9602
+ }
9603
+ }
9604
+ );
9605
+ });
9606
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9607
+ KeyboundForm,
9608
+ {
9609
+ className: "flex flex-1 flex-col overflow-hidden",
9610
+ onSubmit,
9611
+ children: [
9612
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
9613
+ /* @__PURE__ */ jsx(
9614
+ Form$2.Field,
9615
+ {
9616
+ control: form.control,
9617
+ name: "country_code",
9618
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9619
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
9620
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
9621
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9622
+ ] })
9623
+ }
9624
+ ),
9625
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9626
+ /* @__PURE__ */ jsx(
9627
+ Form$2.Field,
9628
+ {
9629
+ control: form.control,
9630
+ name: "first_name",
9631
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9632
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
9633
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9634
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9635
+ ] })
9636
+ }
9637
+ ),
9638
+ /* @__PURE__ */ jsx(
9639
+ Form$2.Field,
9640
+ {
9641
+ control: form.control,
9642
+ name: "last_name",
9643
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9644
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
9645
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9646
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9647
+ ] })
9648
+ }
9649
+ )
9650
+ ] }),
9651
+ /* @__PURE__ */ jsx(
9652
+ Form$2.Field,
9653
+ {
9654
+ control: form.control,
9655
+ name: "company",
9656
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9657
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
9658
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9659
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9660
+ ] })
9661
+ }
9662
+ ),
9663
+ /* @__PURE__ */ jsx(
9664
+ Form$2.Field,
9665
+ {
9666
+ control: form.control,
9667
+ name: "address_1",
9668
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9669
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
9670
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9671
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9672
+ ] })
9673
+ }
9674
+ ),
9675
+ /* @__PURE__ */ jsx(
9676
+ Form$2.Field,
9677
+ {
9678
+ control: form.control,
9679
+ name: "address_2",
9680
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9681
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
9682
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9683
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9684
+ ] })
9685
+ }
9686
+ ),
9687
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9688
+ /* @__PURE__ */ jsx(
9689
+ Form$2.Field,
9690
+ {
9691
+ control: form.control,
9692
+ name: "postal_code",
9693
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9694
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
9695
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9696
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9697
+ ] })
9698
+ }
9699
+ ),
9700
+ /* @__PURE__ */ jsx(
9701
+ Form$2.Field,
9702
+ {
9703
+ control: form.control,
9704
+ name: "city",
9705
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9706
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
9707
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9708
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9709
+ ] })
9710
+ }
9711
+ )
9712
+ ] }),
9713
+ /* @__PURE__ */ jsx(
9714
+ Form$2.Field,
9715
+ {
9716
+ control: form.control,
9717
+ name: "province",
9718
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9719
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
9720
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9721
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9722
+ ] })
9723
+ }
9724
+ ),
9725
+ /* @__PURE__ */ jsx(
9726
+ Form$2.Field,
9727
+ {
9728
+ control: form.control,
9729
+ name: "phone",
9730
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9731
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
9732
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9733
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9734
+ ] })
9735
+ }
9736
+ )
9737
+ ] }) }),
9738
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9739
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9740
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9741
+ ] }) })
9742
+ ]
9743
+ }
9744
+ ) });
9574
9745
  };
9575
- const schema$5 = objectType({
9576
- email: stringType().email()
9577
- });
9746
+ const schema$5 = addressSchema;
9578
9747
  const Email = () => {
9579
9748
  const { id } = useParams();
9580
9749
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9643,415 +9812,65 @@ const EmailForm = ({ order }) => {
9643
9812
  const schema$4 = objectType({
9644
9813
  email: stringType().email()
9645
9814
  });
9646
- const InlineTip = forwardRef(
9647
- ({ variant = "tip", label, className, children, ...props }, ref) => {
9648
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9815
+ const NumberInput = forwardRef(
9816
+ ({
9817
+ value,
9818
+ onChange,
9819
+ size = "base",
9820
+ min = 0,
9821
+ max = 100,
9822
+ step = 1,
9823
+ className,
9824
+ disabled,
9825
+ ...props
9826
+ }, ref) => {
9827
+ const handleChange = (event) => {
9828
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
9829
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9830
+ onChange(newValue);
9831
+ }
9832
+ };
9833
+ const handleIncrement = () => {
9834
+ const newValue = value + step;
9835
+ if (max === void 0 || newValue <= max) {
9836
+ onChange(newValue);
9837
+ }
9838
+ };
9839
+ const handleDecrement = () => {
9840
+ const newValue = value - step;
9841
+ if (min === void 0 || newValue >= min) {
9842
+ onChange(newValue);
9843
+ }
9844
+ };
9649
9845
  return /* @__PURE__ */ jsxs(
9650
9846
  "div",
9651
9847
  {
9652
- ref,
9653
9848
  className: clx(
9654
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9849
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9850
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9851
+ {
9852
+ "h-7": size === "small",
9853
+ "h-8": size === "base"
9854
+ },
9655
9855
  className
9656
9856
  ),
9657
- ...props,
9658
9857
  children: [
9659
9858
  /* @__PURE__ */ jsx(
9660
- "div",
9859
+ "input",
9661
9860
  {
9662
- role: "presentation",
9663
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9664
- "bg-ui-tag-orange-icon": variant === "warning"
9665
- })
9666
- }
9667
- ),
9668
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9669
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9670
- labelValue,
9671
- ":"
9672
- ] }),
9673
- " ",
9674
- children
9675
- ] })
9676
- ]
9677
- }
9678
- );
9679
- }
9680
- );
9681
- InlineTip.displayName = "InlineTip";
9682
- const MetadataFieldSchema = objectType({
9683
- key: stringType(),
9684
- disabled: booleanType().optional(),
9685
- value: anyType()
9686
- });
9687
- const MetadataSchema = objectType({
9688
- metadata: arrayType(MetadataFieldSchema)
9689
- });
9690
- const Metadata = () => {
9691
- const { id } = useParams();
9692
- const { order, isPending, isError, error } = useOrder(id, {
9693
- fields: "metadata"
9694
- });
9695
- if (isError) {
9696
- throw error;
9697
- }
9698
- const isReady = !isPending && !!order;
9699
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9700
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9701
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9702
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9703
- ] }),
9704
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9705
- ] });
9706
- };
9707
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9708
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9709
- const MetadataForm = ({ orderId, metadata }) => {
9710
- const { handleSuccess } = useRouteModal();
9711
- const hasUneditableRows = getHasUneditableRows(metadata);
9712
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9713
- const form = useForm({
9714
- defaultValues: {
9715
- metadata: getDefaultValues(metadata)
9716
- },
9717
- resolver: zodResolver(MetadataSchema)
9718
- });
9719
- const handleSubmit = form.handleSubmit(async (data) => {
9720
- const parsedData = parseValues(data);
9721
- await mutateAsync(
9722
- {
9723
- metadata: parsedData
9724
- },
9725
- {
9726
- onSuccess: () => {
9727
- toast.success("Metadata updated");
9728
- handleSuccess();
9729
- },
9730
- onError: (error) => {
9731
- toast.error(error.message);
9732
- }
9733
- }
9734
- );
9735
- });
9736
- const { fields, insert, remove } = useFieldArray({
9737
- control: form.control,
9738
- name: "metadata"
9739
- });
9740
- function deleteRow(index) {
9741
- remove(index);
9742
- if (fields.length === 1) {
9743
- insert(0, {
9744
- key: "",
9745
- value: "",
9746
- disabled: false
9747
- });
9748
- }
9749
- }
9750
- function insertRow(index, position) {
9751
- insert(index + (position === "above" ? 0 : 1), {
9752
- key: "",
9753
- value: "",
9754
- disabled: false
9755
- });
9756
- }
9757
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9758
- KeyboundForm,
9759
- {
9760
- onSubmit: handleSubmit,
9761
- className: "flex flex-1 flex-col overflow-hidden",
9762
- children: [
9763
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9764
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9765
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9766
- /* @__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" }) }),
9767
- /* @__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" }) })
9768
- ] }),
9769
- fields.map((field, index) => {
9770
- const isDisabled = field.disabled || false;
9771
- let placeholder = "-";
9772
- if (typeof field.value === "object") {
9773
- placeholder = "{ ... }";
9774
- }
9775
- if (Array.isArray(field.value)) {
9776
- placeholder = "[ ... ]";
9777
- }
9778
- return /* @__PURE__ */ jsx(
9779
- ConditionalTooltip,
9780
- {
9781
- showTooltip: isDisabled,
9782
- content: "This row is disabled because it contains non-primitive data.",
9783
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9784
- /* @__PURE__ */ jsxs(
9785
- "div",
9786
- {
9787
- className: clx("grid grid-cols-2 divide-x", {
9788
- "overflow-hidden rounded-b-lg": index === fields.length - 1
9789
- }),
9790
- children: [
9791
- /* @__PURE__ */ jsx(
9792
- Form$2.Field,
9793
- {
9794
- control: form.control,
9795
- name: `metadata.${index}.key`,
9796
- render: ({ field: field2 }) => {
9797
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9798
- GridInput,
9799
- {
9800
- "aria-labelledby": METADATA_KEY_LABEL_ID,
9801
- ...field2,
9802
- disabled: isDisabled,
9803
- placeholder: "Key"
9804
- }
9805
- ) }) });
9806
- }
9807
- }
9808
- ),
9809
- /* @__PURE__ */ jsx(
9810
- Form$2.Field,
9811
- {
9812
- control: form.control,
9813
- name: `metadata.${index}.value`,
9814
- render: ({ field: { value, ...field2 } }) => {
9815
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9816
- GridInput,
9817
- {
9818
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
9819
- ...field2,
9820
- value: isDisabled ? placeholder : value,
9821
- disabled: isDisabled,
9822
- placeholder: "Value"
9823
- }
9824
- ) }) });
9825
- }
9826
- }
9827
- )
9828
- ]
9829
- }
9830
- ),
9831
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
9832
- /* @__PURE__ */ jsx(
9833
- DropdownMenu.Trigger,
9834
- {
9835
- className: clx(
9836
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9837
- {
9838
- hidden: isDisabled
9839
- }
9840
- ),
9841
- disabled: isDisabled,
9842
- asChild: true,
9843
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
9844
- }
9845
- ),
9846
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
9847
- /* @__PURE__ */ jsxs(
9848
- DropdownMenu.Item,
9849
- {
9850
- className: "gap-x-2",
9851
- onClick: () => insertRow(index, "above"),
9852
- children: [
9853
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
9854
- "Insert row above"
9855
- ]
9856
- }
9857
- ),
9858
- /* @__PURE__ */ jsxs(
9859
- DropdownMenu.Item,
9860
- {
9861
- className: "gap-x-2",
9862
- onClick: () => insertRow(index, "below"),
9863
- children: [
9864
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
9865
- "Insert row below"
9866
- ]
9867
- }
9868
- ),
9869
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
9870
- /* @__PURE__ */ jsxs(
9871
- DropdownMenu.Item,
9872
- {
9873
- className: "gap-x-2",
9874
- onClick: () => deleteRow(index),
9875
- children: [
9876
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
9877
- "Delete row"
9878
- ]
9879
- }
9880
- )
9881
- ] })
9882
- ] })
9883
- ] })
9884
- },
9885
- field.id
9886
- );
9887
- })
9888
- ] }),
9889
- 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." })
9890
- ] }),
9891
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9892
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
9893
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9894
- ] }) })
9895
- ]
9896
- }
9897
- ) });
9898
- };
9899
- const GridInput = forwardRef(({ className, ...props }, ref) => {
9900
- return /* @__PURE__ */ jsx(
9901
- "input",
9902
- {
9903
- ref,
9904
- ...props,
9905
- autoComplete: "off",
9906
- className: clx(
9907
- "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",
9908
- className
9909
- )
9910
- }
9911
- );
9912
- });
9913
- GridInput.displayName = "MetadataForm.GridInput";
9914
- const PlaceholderInner = () => {
9915
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
9916
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
9917
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9918
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
9919
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
9920
- ] }) })
9921
- ] });
9922
- };
9923
- const EDITABLE_TYPES = ["string", "number", "boolean"];
9924
- function getDefaultValues(metadata) {
9925
- if (!metadata || !Object.keys(metadata).length) {
9926
- return [
9927
- {
9928
- key: "",
9929
- value: "",
9930
- disabled: false
9931
- }
9932
- ];
9933
- }
9934
- return Object.entries(metadata).map(([key, value]) => {
9935
- if (!EDITABLE_TYPES.includes(typeof value)) {
9936
- return {
9937
- key,
9938
- value,
9939
- disabled: true
9940
- };
9941
- }
9942
- let stringValue = value;
9943
- if (typeof value !== "string") {
9944
- stringValue = JSON.stringify(value);
9945
- }
9946
- return {
9947
- key,
9948
- value: stringValue,
9949
- original_key: key
9950
- };
9951
- });
9952
- }
9953
- function parseValues(values) {
9954
- const metadata = values.metadata;
9955
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
9956
- if (isEmpty) {
9957
- return null;
9958
- }
9959
- const update = {};
9960
- metadata.forEach((field) => {
9961
- let key = field.key;
9962
- let value = field.value;
9963
- const disabled = field.disabled;
9964
- if (!key || !value) {
9965
- return;
9966
- }
9967
- if (disabled) {
9968
- update[key] = value;
9969
- return;
9970
- }
9971
- key = key.trim();
9972
- value = value.trim();
9973
- if (value === "true") {
9974
- update[key] = true;
9975
- } else if (value === "false") {
9976
- update[key] = false;
9977
- } else {
9978
- const parsedNumber = parseFloat(value);
9979
- if (!isNaN(parsedNumber)) {
9980
- update[key] = parsedNumber;
9981
- } else {
9982
- update[key] = value;
9983
- }
9984
- }
9985
- });
9986
- return update;
9987
- }
9988
- function getHasUneditableRows(metadata) {
9989
- if (!metadata) {
9990
- return false;
9991
- }
9992
- return Object.values(metadata).some(
9993
- (value) => !EDITABLE_TYPES.includes(typeof value)
9994
- );
9995
- }
9996
- const NumberInput = forwardRef(
9997
- ({
9998
- value,
9999
- onChange,
10000
- size = "base",
10001
- min = 0,
10002
- max = 100,
10003
- step = 1,
10004
- className,
10005
- disabled,
10006
- ...props
10007
- }, ref) => {
10008
- const handleChange = (event) => {
10009
- const newValue = event.target.value === "" ? min : Number(event.target.value);
10010
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
10011
- onChange(newValue);
10012
- }
10013
- };
10014
- const handleIncrement = () => {
10015
- const newValue = value + step;
10016
- if (max === void 0 || newValue <= max) {
10017
- onChange(newValue);
10018
- }
10019
- };
10020
- const handleDecrement = () => {
10021
- const newValue = value - step;
10022
- if (min === void 0 || newValue >= min) {
10023
- onChange(newValue);
10024
- }
10025
- };
10026
- return /* @__PURE__ */ jsxs(
10027
- "div",
10028
- {
10029
- className: clx(
10030
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
10031
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
10032
- {
10033
- "h-7": size === "small",
10034
- "h-8": size === "base"
10035
- },
10036
- className
10037
- ),
10038
- children: [
10039
- /* @__PURE__ */ jsx(
10040
- "input",
10041
- {
10042
- ref,
10043
- type: "number",
10044
- value,
10045
- onChange: handleChange,
10046
- min,
10047
- max,
10048
- step,
10049
- className: clx(
10050
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
10051
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
10052
- "placeholder:text-ui-fg-muted"
10053
- ),
10054
- ...props
9861
+ ref,
9862
+ type: "number",
9863
+ value,
9864
+ onChange: handleChange,
9865
+ min,
9866
+ max,
9867
+ step,
9868
+ className: clx(
9869
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9870
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9871
+ "placeholder:text-ui-fg-muted"
9872
+ ),
9873
+ ...props
10055
9874
  }
10056
9875
  ),
10057
9876
  /* @__PURE__ */ jsxs(
@@ -10967,6 +10786,27 @@ const customItemSchema = objectType({
10967
10786
  quantity: numberType(),
10968
10787
  unit_price: unionType([numberType(), stringType()])
10969
10788
  });
10789
+ const CustomItems = () => {
10790
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10791
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
10792
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
10793
+ ] });
10794
+ };
10795
+ const CustomItemsForm = () => {
10796
+ const form = useForm({
10797
+ resolver: zodResolver(schema$3)
10798
+ });
10799
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
10800
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
10801
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10802
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10803
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
10804
+ ] }) })
10805
+ ] }) });
10806
+ };
10807
+ const schema$3 = objectType({
10808
+ email: stringType().email()
10809
+ });
10970
10810
  const PROMOTION_QUERY_KEY = "promotions";
10971
10811
  const promotionsQueryKeys = {
10972
10812
  list: (query2) => [
@@ -11272,7 +11112,7 @@ const SalesChannelForm = ({ order }) => {
11272
11112
  defaultValues: {
11273
11113
  sales_channel_id: order.sales_channel_id || ""
11274
11114
  },
11275
- resolver: zodResolver(schema$3)
11115
+ resolver: zodResolver(schema$2)
11276
11116
  });
11277
11117
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11278
11118
  const { handleSuccess } = useRouteModal();
@@ -11347,7 +11187,7 @@ const SalesChannelField = ({ control, order }) => {
11347
11187
  }
11348
11188
  );
11349
11189
  };
11350
- const schema$3 = objectType({
11190
+ const schema$2 = objectType({
11351
11191
  sales_channel_id: stringType().min(1)
11352
11192
  });
11353
11193
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
@@ -12189,7 +12029,7 @@ const ShippingAddressForm = ({ order }) => {
12189
12029
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12190
12030
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12191
12031
  },
12192
- resolver: zodResolver(schema$2)
12032
+ resolver: zodResolver(schema$1)
12193
12033
  });
12194
12034
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12195
12035
  const { handleSuccess } = useRouteModal();
@@ -12359,7 +12199,7 @@ const ShippingAddressForm = ({ order }) => {
12359
12199
  }
12360
12200
  ) });
12361
12201
  };
12362
- const schema$2 = addressSchema;
12202
+ const schema$1 = addressSchema;
12363
12203
  const TransferOwnership = () => {
12364
12204
  const { id } = useParams();
12365
12205
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12383,7 +12223,7 @@ const TransferOwnershipForm = ({ order }) => {
12383
12223
  defaultValues: {
12384
12224
  customer_id: order.customer_id || ""
12385
12225
  },
12386
- resolver: zodResolver(schema$1)
12226
+ resolver: zodResolver(schema)
12387
12227
  });
12388
12228
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12389
12229
  const { handleSuccess } = useRouteModal();
@@ -12833,13 +12673,57 @@ const Illustration = () => {
12833
12673
  }
12834
12674
  );
12835
12675
  };
12836
- const schema$1 = objectType({
12676
+ const schema = objectType({
12837
12677
  customer_id: stringType().min(1)
12838
12678
  });
12839
- const BillingAddress = () => {
12679
+ const InlineTip = forwardRef(
12680
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
12681
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
12682
+ return /* @__PURE__ */ jsxs(
12683
+ "div",
12684
+ {
12685
+ ref,
12686
+ className: clx(
12687
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
12688
+ className
12689
+ ),
12690
+ ...props,
12691
+ children: [
12692
+ /* @__PURE__ */ jsx(
12693
+ "div",
12694
+ {
12695
+ role: "presentation",
12696
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
12697
+ "bg-ui-tag-orange-icon": variant === "warning"
12698
+ })
12699
+ }
12700
+ ),
12701
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
12702
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
12703
+ labelValue,
12704
+ ":"
12705
+ ] }),
12706
+ " ",
12707
+ children
12708
+ ] })
12709
+ ]
12710
+ }
12711
+ );
12712
+ }
12713
+ );
12714
+ InlineTip.displayName = "InlineTip";
12715
+ const MetadataFieldSchema = objectType({
12716
+ key: stringType(),
12717
+ disabled: booleanType().optional(),
12718
+ value: anyType()
12719
+ });
12720
+ const MetadataSchema = objectType({
12721
+ metadata: arrayType(MetadataFieldSchema)
12722
+ });
12723
+ const Metadata = () => {
12840
12724
  const { id } = useParams();
12841
12725
  const { order, isPending, isError, error } = useOrder(id, {
12842
- fields: "+billing_address"
12726
+ fields: "metadata"
12843
12727
  });
12844
12728
  if (isError) {
12845
12729
  throw error;
@@ -12847,36 +12731,33 @@ const BillingAddress = () => {
12847
12731
  const isReady = !isPending && !!order;
12848
12732
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12849
12733
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12850
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Billing Address" }) }),
12851
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
12734
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
12735
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
12852
12736
  ] }),
12853
- isReady && /* @__PURE__ */ jsx(BillingAddressForm, { order })
12737
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
12854
12738
  ] });
12855
12739
  };
12856
- const BillingAddressForm = ({ order }) => {
12857
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12740
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
12741
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
12742
+ const MetadataForm = ({ orderId, metadata }) => {
12743
+ const { handleSuccess } = useRouteModal();
12744
+ const hasUneditableRows = getHasUneditableRows(metadata);
12745
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
12858
12746
  const form = useForm({
12859
12747
  defaultValues: {
12860
- first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
12861
- last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
12862
- company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
12863
- address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
12864
- address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
12865
- city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
12866
- province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
12867
- country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
12868
- postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
12869
- phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
12748
+ metadata: getDefaultValues(metadata)
12870
12749
  },
12871
- resolver: zodResolver(schema)
12750
+ resolver: zodResolver(MetadataSchema)
12872
12751
  });
12873
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12874
- const { handleSuccess } = useRouteModal();
12875
- const onSubmit = form.handleSubmit(async (data) => {
12752
+ const handleSubmit = form.handleSubmit(async (data) => {
12753
+ const parsedData = parseValues(data);
12876
12754
  await mutateAsync(
12877
- { billing_address: data },
12755
+ {
12756
+ metadata: parsedData
12757
+ },
12878
12758
  {
12879
12759
  onSuccess: () => {
12760
+ toast.success("Metadata updated");
12880
12761
  handleSuccess();
12881
12762
  },
12882
12763
  onError: (error) => {
@@ -12885,147 +12766,266 @@ const BillingAddressForm = ({ order }) => {
12885
12766
  }
12886
12767
  );
12887
12768
  });
12769
+ const { fields, insert, remove } = useFieldArray({
12770
+ control: form.control,
12771
+ name: "metadata"
12772
+ });
12773
+ function deleteRow(index) {
12774
+ remove(index);
12775
+ if (fields.length === 1) {
12776
+ insert(0, {
12777
+ key: "",
12778
+ value: "",
12779
+ disabled: false
12780
+ });
12781
+ }
12782
+ }
12783
+ function insertRow(index, position) {
12784
+ insert(index + (position === "above" ? 0 : 1), {
12785
+ key: "",
12786
+ value: "",
12787
+ disabled: false
12788
+ });
12789
+ }
12888
12790
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12889
12791
  KeyboundForm,
12890
12792
  {
12793
+ onSubmit: handleSubmit,
12891
12794
  className: "flex flex-1 flex-col overflow-hidden",
12892
- onSubmit,
12893
12795
  children: [
12894
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
12895
- /* @__PURE__ */ jsx(
12896
- Form$2.Field,
12897
- {
12898
- control: form.control,
12899
- name: "country_code",
12900
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12901
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12902
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12903
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12904
- ] })
12905
- }
12906
- ),
12907
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12908
- /* @__PURE__ */ jsx(
12909
- Form$2.Field,
12910
- {
12911
- control: form.control,
12912
- name: "first_name",
12913
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12914
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12915
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12916
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12917
- ] })
12918
- }
12919
- ),
12920
- /* @__PURE__ */ jsx(
12921
- Form$2.Field,
12922
- {
12923
- control: form.control,
12924
- name: "last_name",
12925
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12926
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12927
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12928
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12929
- ] })
12930
- }
12931
- )
12932
- ] }),
12933
- /* @__PURE__ */ jsx(
12934
- Form$2.Field,
12935
- {
12936
- control: form.control,
12937
- name: "company",
12938
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12939
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12940
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12941
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12942
- ] })
12943
- }
12944
- ),
12945
- /* @__PURE__ */ jsx(
12946
- Form$2.Field,
12947
- {
12948
- control: form.control,
12949
- name: "address_1",
12950
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12951
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12952
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12953
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12954
- ] })
12955
- }
12956
- ),
12957
- /* @__PURE__ */ jsx(
12958
- Form$2.Field,
12959
- {
12960
- control: form.control,
12961
- name: "address_2",
12962
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12963
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12964
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12965
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12966
- ] })
12967
- }
12968
- ),
12969
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12970
- /* @__PURE__ */ jsx(
12971
- Form$2.Field,
12972
- {
12973
- control: form.control,
12974
- name: "postal_code",
12975
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12976
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12977
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12978
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12979
- ] })
12796
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
12797
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
12798
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
12799
+ /* @__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" }) }),
12800
+ /* @__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" }) })
12801
+ ] }),
12802
+ fields.map((field, index) => {
12803
+ const isDisabled = field.disabled || false;
12804
+ let placeholder = "-";
12805
+ if (typeof field.value === "object") {
12806
+ placeholder = "{ ... }";
12980
12807
  }
12981
- ),
12982
- /* @__PURE__ */ jsx(
12983
- Form$2.Field,
12984
- {
12985
- control: form.control,
12986
- name: "city",
12987
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12988
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12989
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12990
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12991
- ] })
12808
+ if (Array.isArray(field.value)) {
12809
+ placeholder = "[ ... ]";
12992
12810
  }
12993
- )
12811
+ return /* @__PURE__ */ jsx(
12812
+ ConditionalTooltip,
12813
+ {
12814
+ showTooltip: isDisabled,
12815
+ content: "This row is disabled because it contains non-primitive data.",
12816
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
12817
+ /* @__PURE__ */ jsxs(
12818
+ "div",
12819
+ {
12820
+ className: clx("grid grid-cols-2 divide-x", {
12821
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
12822
+ }),
12823
+ children: [
12824
+ /* @__PURE__ */ jsx(
12825
+ Form$2.Field,
12826
+ {
12827
+ control: form.control,
12828
+ name: `metadata.${index}.key`,
12829
+ render: ({ field: field2 }) => {
12830
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12831
+ GridInput,
12832
+ {
12833
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
12834
+ ...field2,
12835
+ disabled: isDisabled,
12836
+ placeholder: "Key"
12837
+ }
12838
+ ) }) });
12839
+ }
12840
+ }
12841
+ ),
12842
+ /* @__PURE__ */ jsx(
12843
+ Form$2.Field,
12844
+ {
12845
+ control: form.control,
12846
+ name: `metadata.${index}.value`,
12847
+ render: ({ field: { value, ...field2 } }) => {
12848
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12849
+ GridInput,
12850
+ {
12851
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
12852
+ ...field2,
12853
+ value: isDisabled ? placeholder : value,
12854
+ disabled: isDisabled,
12855
+ placeholder: "Value"
12856
+ }
12857
+ ) }) });
12858
+ }
12859
+ }
12860
+ )
12861
+ ]
12862
+ }
12863
+ ),
12864
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
12865
+ /* @__PURE__ */ jsx(
12866
+ DropdownMenu.Trigger,
12867
+ {
12868
+ className: clx(
12869
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
12870
+ {
12871
+ hidden: isDisabled
12872
+ }
12873
+ ),
12874
+ disabled: isDisabled,
12875
+ asChild: true,
12876
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
12877
+ }
12878
+ ),
12879
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
12880
+ /* @__PURE__ */ jsxs(
12881
+ DropdownMenu.Item,
12882
+ {
12883
+ className: "gap-x-2",
12884
+ onClick: () => insertRow(index, "above"),
12885
+ children: [
12886
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
12887
+ "Insert row above"
12888
+ ]
12889
+ }
12890
+ ),
12891
+ /* @__PURE__ */ jsxs(
12892
+ DropdownMenu.Item,
12893
+ {
12894
+ className: "gap-x-2",
12895
+ onClick: () => insertRow(index, "below"),
12896
+ children: [
12897
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
12898
+ "Insert row below"
12899
+ ]
12900
+ }
12901
+ ),
12902
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
12903
+ /* @__PURE__ */ jsxs(
12904
+ DropdownMenu.Item,
12905
+ {
12906
+ className: "gap-x-2",
12907
+ onClick: () => deleteRow(index),
12908
+ children: [
12909
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
12910
+ "Delete row"
12911
+ ]
12912
+ }
12913
+ )
12914
+ ] })
12915
+ ] })
12916
+ ] })
12917
+ },
12918
+ field.id
12919
+ );
12920
+ })
12994
12921
  ] }),
12995
- /* @__PURE__ */ jsx(
12996
- Form$2.Field,
12997
- {
12998
- control: form.control,
12999
- name: "province",
13000
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13001
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
13002
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13003
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13004
- ] })
13005
- }
13006
- ),
13007
- /* @__PURE__ */ jsx(
13008
- Form$2.Field,
13009
- {
13010
- control: form.control,
13011
- name: "phone",
13012
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13013
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
13014
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13015
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13016
- ] })
13017
- }
13018
- )
13019
- ] }) }),
13020
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13021
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12922
+ 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." })
12923
+ ] }),
12924
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12925
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
13022
12926
  /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13023
12927
  ] }) })
13024
12928
  ]
13025
12929
  }
13026
12930
  ) });
13027
12931
  };
13028
- const schema = addressSchema;
12932
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
12933
+ return /* @__PURE__ */ jsx(
12934
+ "input",
12935
+ {
12936
+ ref,
12937
+ ...props,
12938
+ autoComplete: "off",
12939
+ className: clx(
12940
+ "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",
12941
+ className
12942
+ )
12943
+ }
12944
+ );
12945
+ });
12946
+ GridInput.displayName = "MetadataForm.GridInput";
12947
+ const PlaceholderInner = () => {
12948
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
12949
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
12950
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12951
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
12952
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
12953
+ ] }) })
12954
+ ] });
12955
+ };
12956
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
12957
+ function getDefaultValues(metadata) {
12958
+ if (!metadata || !Object.keys(metadata).length) {
12959
+ return [
12960
+ {
12961
+ key: "",
12962
+ value: "",
12963
+ disabled: false
12964
+ }
12965
+ ];
12966
+ }
12967
+ return Object.entries(metadata).map(([key, value]) => {
12968
+ if (!EDITABLE_TYPES.includes(typeof value)) {
12969
+ return {
12970
+ key,
12971
+ value,
12972
+ disabled: true
12973
+ };
12974
+ }
12975
+ let stringValue = value;
12976
+ if (typeof value !== "string") {
12977
+ stringValue = JSON.stringify(value);
12978
+ }
12979
+ return {
12980
+ key,
12981
+ value: stringValue,
12982
+ original_key: key
12983
+ };
12984
+ });
12985
+ }
12986
+ function parseValues(values) {
12987
+ const metadata = values.metadata;
12988
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
12989
+ if (isEmpty) {
12990
+ return null;
12991
+ }
12992
+ const update = {};
12993
+ metadata.forEach((field) => {
12994
+ let key = field.key;
12995
+ let value = field.value;
12996
+ const disabled = field.disabled;
12997
+ if (!key || !value) {
12998
+ return;
12999
+ }
13000
+ if (disabled) {
13001
+ update[key] = value;
13002
+ return;
13003
+ }
13004
+ key = key.trim();
13005
+ value = value.trim();
13006
+ if (value === "true") {
13007
+ update[key] = true;
13008
+ } else if (value === "false") {
13009
+ update[key] = false;
13010
+ } else {
13011
+ const parsedNumber = parseFloat(value);
13012
+ if (!isNaN(parsedNumber)) {
13013
+ update[key] = parsedNumber;
13014
+ } else {
13015
+ update[key] = value;
13016
+ }
13017
+ }
13018
+ });
13019
+ return update;
13020
+ }
13021
+ function getHasUneditableRows(metadata) {
13022
+ if (!metadata) {
13023
+ return false;
13024
+ }
13025
+ return Object.values(metadata).some(
13026
+ (value) => !EDITABLE_TYPES.includes(typeof value)
13027
+ );
13028
+ }
13029
13029
  const widgetModule = { widgets: [] };
13030
13030
  const routeModule = {
13031
13031
  routes: [
@@ -13047,21 +13047,21 @@ const routeModule = {
13047
13047
  loader,
13048
13048
  children: [
13049
13049
  {
13050
- Component: CustomItems,
13051
- path: "/draft-orders/:id/custom-items"
13050
+ Component: BillingAddress,
13051
+ path: "/draft-orders/:id/billing-address"
13052
13052
  },
13053
13053
  {
13054
13054
  Component: Email,
13055
13055
  path: "/draft-orders/:id/email"
13056
13056
  },
13057
- {
13058
- Component: Metadata,
13059
- path: "/draft-orders/:id/metadata"
13060
- },
13061
13057
  {
13062
13058
  Component: Items,
13063
13059
  path: "/draft-orders/:id/items"
13064
13060
  },
13061
+ {
13062
+ Component: CustomItems,
13063
+ path: "/draft-orders/:id/custom-items"
13064
+ },
13065
13065
  {
13066
13066
  Component: Promotions,
13067
13067
  path: "/draft-orders/:id/promotions"
@@ -13083,8 +13083,8 @@ const routeModule = {
13083
13083
  path: "/draft-orders/:id/transfer-ownership"
13084
13084
  },
13085
13085
  {
13086
- Component: BillingAddress,
13087
- path: "/draft-orders/:id/billing-address"
13086
+ Component: Metadata,
13087
+ path: "/draft-orders/:id/metadata"
13088
13088
  }
13089
13089
  ]
13090
13090
  }