@medusajs/draft-order 2.11.4-preview-20251106120154 → 2.11.4-snapshot-20251106121614

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, Minus, PencilSquare, EllipsisVertical, ArrowUpMini, ArrowDownMini } 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, EllipsisVertical, ArrowUpMini, ArrowDownMini, Minus, PencilSquare } 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";
@@ -9561,27 +9561,6 @@ const ID = () => {
9561
9561
  /* @__PURE__ */ jsx(Outlet, {})
9562
9562
  ] });
9563
9563
  };
9564
- const CustomItems = () => {
9565
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9566
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9567
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9568
- ] });
9569
- };
9570
- const CustomItemsForm = () => {
9571
- const form = useForm({
9572
- resolver: zodResolver(schema$5)
9573
- });
9574
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9575
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9576
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9577
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9578
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9579
- ] }) })
9580
- ] }) });
9581
- };
9582
- const schema$5 = objectType({
9583
- email: stringType().email()
9584
- });
9585
9564
  const BillingAddress = () => {
9586
9565
  const { id } = useParams();
9587
9566
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9614,7 +9593,7 @@ const BillingAddressForm = ({ order }) => {
9614
9593
  postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9615
9594
  phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9616
9595
  },
9617
- resolver: zodResolver(schema$4)
9596
+ resolver: zodResolver(schema$5)
9618
9597
  });
9619
9598
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9620
9599
  const { handleSuccess } = useRouteModal();
@@ -9771,11 +9750,76 @@ const BillingAddressForm = ({ order }) => {
9771
9750
  }
9772
9751
  ) });
9773
9752
  };
9774
- const schema$4 = addressSchema;
9775
- const Email = () => {
9753
+ const schema$5 = addressSchema;
9754
+ const CustomItems = () => {
9755
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9756
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9757
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
9758
+ ] });
9759
+ };
9760
+ const CustomItemsForm = () => {
9761
+ const form = useForm({
9762
+ resolver: zodResolver(schema$4)
9763
+ });
9764
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9765
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9766
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9767
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9768
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9769
+ ] }) })
9770
+ ] }) });
9771
+ };
9772
+ const schema$4 = objectType({
9773
+ email: stringType().email()
9774
+ });
9775
+ const InlineTip = forwardRef(
9776
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9777
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9778
+ return /* @__PURE__ */ jsxs(
9779
+ "div",
9780
+ {
9781
+ ref,
9782
+ className: clx(
9783
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9784
+ className
9785
+ ),
9786
+ ...props,
9787
+ children: [
9788
+ /* @__PURE__ */ jsx(
9789
+ "div",
9790
+ {
9791
+ role: "presentation",
9792
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9793
+ "bg-ui-tag-orange-icon": variant === "warning"
9794
+ })
9795
+ }
9796
+ ),
9797
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9798
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9799
+ labelValue,
9800
+ ":"
9801
+ ] }),
9802
+ " ",
9803
+ children
9804
+ ] })
9805
+ ]
9806
+ }
9807
+ );
9808
+ }
9809
+ );
9810
+ InlineTip.displayName = "InlineTip";
9811
+ const MetadataFieldSchema = objectType({
9812
+ key: stringType(),
9813
+ disabled: booleanType().optional(),
9814
+ value: anyType()
9815
+ });
9816
+ const MetadataSchema = objectType({
9817
+ metadata: arrayType(MetadataFieldSchema)
9818
+ });
9819
+ const Metadata = () => {
9776
9820
  const { id } = useParams();
9777
9821
  const { order, isPending, isError, error } = useOrder(id, {
9778
- fields: "+email"
9822
+ fields: "metadata"
9779
9823
  });
9780
9824
  if (isError) {
9781
9825
  throw error;
@@ -9783,26 +9827,33 @@ const Email = () => {
9783
9827
  const isReady = !isPending && !!order;
9784
9828
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9785
9829
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9786
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
9787
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9830
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9831
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9788
9832
  ] }),
9789
- isReady && /* @__PURE__ */ jsx(EmailForm, { order })
9833
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9790
9834
  ] });
9791
9835
  };
9792
- const EmailForm = ({ order }) => {
9836
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9837
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9838
+ const MetadataForm = ({ orderId, metadata }) => {
9839
+ const { handleSuccess } = useRouteModal();
9840
+ const hasUneditableRows = getHasUneditableRows(metadata);
9841
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9793
9842
  const form = useForm({
9794
9843
  defaultValues: {
9795
- email: order.email ?? ""
9844
+ metadata: getDefaultValues(metadata)
9796
9845
  },
9797
- resolver: zodResolver(schema$3)
9846
+ resolver: zodResolver(MetadataSchema)
9798
9847
  });
9799
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9800
- const { handleSuccess } = useRouteModal();
9801
- const onSubmit = form.handleSubmit(async (data) => {
9848
+ const handleSubmit = form.handleSubmit(async (data) => {
9849
+ const parsedData = parseValues(data);
9802
9850
  await mutateAsync(
9803
- { email: data.email },
9851
+ {
9852
+ metadata: parsedData
9853
+ },
9804
9854
  {
9805
9855
  onSuccess: () => {
9856
+ toast.success("Metadata updated");
9806
9857
  handleSuccess();
9807
9858
  },
9808
9859
  onError: (error) => {
@@ -9811,112 +9862,411 @@ const EmailForm = ({ order }) => {
9811
9862
  }
9812
9863
  );
9813
9864
  });
9865
+ const { fields, insert, remove } = useFieldArray({
9866
+ control: form.control,
9867
+ name: "metadata"
9868
+ });
9869
+ function deleteRow(index) {
9870
+ remove(index);
9871
+ if (fields.length === 1) {
9872
+ insert(0, {
9873
+ key: "",
9874
+ value: "",
9875
+ disabled: false
9876
+ });
9877
+ }
9878
+ }
9879
+ function insertRow(index, position) {
9880
+ insert(index + (position === "above" ? 0 : 1), {
9881
+ key: "",
9882
+ value: "",
9883
+ disabled: false
9884
+ });
9885
+ }
9814
9886
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9815
9887
  KeyboundForm,
9816
9888
  {
9889
+ onSubmit: handleSubmit,
9817
9890
  className: "flex flex-1 flex-col overflow-hidden",
9818
- onSubmit,
9819
9891
  children: [
9820
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
9821
- Form$2.Field,
9822
- {
9823
- control: form.control,
9824
- name: "email",
9825
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9826
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
9827
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9828
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9829
- ] })
9830
- }
9831
- ) }),
9832
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9833
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9834
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9835
- ] }) })
9836
- ]
9837
- }
9838
- ) });
9839
- };
9840
- const schema$3 = objectType({
9841
- email: stringType().email()
9842
- });
9843
- const NumberInput = forwardRef(
9844
- ({
9845
- value,
9846
- onChange,
9847
- size = "base",
9848
- min = 0,
9849
- max = 100,
9850
- step = 1,
9851
- className,
9852
- disabled,
9853
- ...props
9854
- }, ref) => {
9855
- const handleChange = (event) => {
9856
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9857
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9858
- onChange(newValue);
9859
- }
9860
- };
9861
- const handleIncrement = () => {
9862
- const newValue = value + step;
9863
- if (max === void 0 || newValue <= max) {
9864
- onChange(newValue);
9865
- }
9866
- };
9867
- const handleDecrement = () => {
9868
- const newValue = value - step;
9869
- if (min === void 0 || newValue >= min) {
9870
- onChange(newValue);
9871
- }
9872
- };
9873
- return /* @__PURE__ */ jsxs(
9874
- "div",
9875
- {
9876
- className: clx(
9877
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9878
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9879
- {
9880
- "h-7": size === "small",
9881
- "h-8": size === "base"
9882
- },
9883
- className
9884
- ),
9885
- children: [
9886
- /* @__PURE__ */ jsx(
9887
- "input",
9888
- {
9889
- ref,
9890
- type: "number",
9891
- value,
9892
- onChange: handleChange,
9893
- min,
9894
- max,
9895
- step,
9896
- className: clx(
9897
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9898
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9899
- "placeholder:text-ui-fg-muted"
9900
- ),
9901
- ...props
9902
- }
9903
- ),
9904
- /* @__PURE__ */ jsxs(
9905
- "button",
9906
- {
9907
- className: clx(
9908
- "flex items-center justify-center outline-none transition-fg",
9909
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9910
- "focus:bg-ui-bg-field-component-hover",
9911
- "hover:bg-ui-bg-field-component-hover",
9892
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9893
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9894
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9895
+ /* @__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" }) }),
9896
+ /* @__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" }) })
9897
+ ] }),
9898
+ fields.map((field, index) => {
9899
+ const isDisabled = field.disabled || false;
9900
+ let placeholder = "-";
9901
+ if (typeof field.value === "object") {
9902
+ placeholder = "{ ... }";
9903
+ }
9904
+ if (Array.isArray(field.value)) {
9905
+ placeholder = "[ ... ]";
9906
+ }
9907
+ return /* @__PURE__ */ jsx(
9908
+ ConditionalTooltip,
9912
9909
  {
9913
- "size-7": size === "small",
9914
- "size-8": size === "base"
9915
- }
9916
- ),
9917
- type: "button",
9918
- onClick: handleDecrement,
9919
- disabled: min !== void 0 && value <= min || disabled,
9910
+ showTooltip: isDisabled,
9911
+ content: "This row is disabled because it contains non-primitive data.",
9912
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9913
+ /* @__PURE__ */ jsxs(
9914
+ "div",
9915
+ {
9916
+ className: clx("grid grid-cols-2 divide-x", {
9917
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9918
+ }),
9919
+ children: [
9920
+ /* @__PURE__ */ jsx(
9921
+ Form$2.Field,
9922
+ {
9923
+ control: form.control,
9924
+ name: `metadata.${index}.key`,
9925
+ render: ({ field: field2 }) => {
9926
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9927
+ GridInput,
9928
+ {
9929
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
9930
+ ...field2,
9931
+ disabled: isDisabled,
9932
+ placeholder: "Key"
9933
+ }
9934
+ ) }) });
9935
+ }
9936
+ }
9937
+ ),
9938
+ /* @__PURE__ */ jsx(
9939
+ Form$2.Field,
9940
+ {
9941
+ control: form.control,
9942
+ name: `metadata.${index}.value`,
9943
+ render: ({ field: { value, ...field2 } }) => {
9944
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9945
+ GridInput,
9946
+ {
9947
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
9948
+ ...field2,
9949
+ value: isDisabled ? placeholder : value,
9950
+ disabled: isDisabled,
9951
+ placeholder: "Value"
9952
+ }
9953
+ ) }) });
9954
+ }
9955
+ }
9956
+ )
9957
+ ]
9958
+ }
9959
+ ),
9960
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
9961
+ /* @__PURE__ */ jsx(
9962
+ DropdownMenu.Trigger,
9963
+ {
9964
+ className: clx(
9965
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9966
+ {
9967
+ hidden: isDisabled
9968
+ }
9969
+ ),
9970
+ disabled: isDisabled,
9971
+ asChild: true,
9972
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
9973
+ }
9974
+ ),
9975
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
9976
+ /* @__PURE__ */ jsxs(
9977
+ DropdownMenu.Item,
9978
+ {
9979
+ className: "gap-x-2",
9980
+ onClick: () => insertRow(index, "above"),
9981
+ children: [
9982
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
9983
+ "Insert row above"
9984
+ ]
9985
+ }
9986
+ ),
9987
+ /* @__PURE__ */ jsxs(
9988
+ DropdownMenu.Item,
9989
+ {
9990
+ className: "gap-x-2",
9991
+ onClick: () => insertRow(index, "below"),
9992
+ children: [
9993
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
9994
+ "Insert row below"
9995
+ ]
9996
+ }
9997
+ ),
9998
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
9999
+ /* @__PURE__ */ jsxs(
10000
+ DropdownMenu.Item,
10001
+ {
10002
+ className: "gap-x-2",
10003
+ onClick: () => deleteRow(index),
10004
+ children: [
10005
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
10006
+ "Delete row"
10007
+ ]
10008
+ }
10009
+ )
10010
+ ] })
10011
+ ] })
10012
+ ] })
10013
+ },
10014
+ field.id
10015
+ );
10016
+ })
10017
+ ] }),
10018
+ 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." })
10019
+ ] }),
10020
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10021
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10022
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10023
+ ] }) })
10024
+ ]
10025
+ }
10026
+ ) });
10027
+ };
10028
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
10029
+ return /* @__PURE__ */ jsx(
10030
+ "input",
10031
+ {
10032
+ ref,
10033
+ ...props,
10034
+ autoComplete: "off",
10035
+ className: clx(
10036
+ "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",
10037
+ className
10038
+ )
10039
+ }
10040
+ );
10041
+ });
10042
+ GridInput.displayName = "MetadataForm.GridInput";
10043
+ const PlaceholderInner = () => {
10044
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10045
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10046
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10047
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
10048
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
10049
+ ] }) })
10050
+ ] });
10051
+ };
10052
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
10053
+ function getDefaultValues(metadata) {
10054
+ if (!metadata || !Object.keys(metadata).length) {
10055
+ return [
10056
+ {
10057
+ key: "",
10058
+ value: "",
10059
+ disabled: false
10060
+ }
10061
+ ];
10062
+ }
10063
+ return Object.entries(metadata).map(([key, value]) => {
10064
+ if (!EDITABLE_TYPES.includes(typeof value)) {
10065
+ return {
10066
+ key,
10067
+ value,
10068
+ disabled: true
10069
+ };
10070
+ }
10071
+ let stringValue = value;
10072
+ if (typeof value !== "string") {
10073
+ stringValue = JSON.stringify(value);
10074
+ }
10075
+ return {
10076
+ key,
10077
+ value: stringValue,
10078
+ original_key: key
10079
+ };
10080
+ });
10081
+ }
10082
+ function parseValues(values) {
10083
+ const metadata = values.metadata;
10084
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10085
+ if (isEmpty) {
10086
+ return null;
10087
+ }
10088
+ const update = {};
10089
+ metadata.forEach((field) => {
10090
+ let key = field.key;
10091
+ let value = field.value;
10092
+ const disabled = field.disabled;
10093
+ if (!key || !value) {
10094
+ return;
10095
+ }
10096
+ if (disabled) {
10097
+ update[key] = value;
10098
+ return;
10099
+ }
10100
+ key = key.trim();
10101
+ value = value.trim();
10102
+ if (value === "true") {
10103
+ update[key] = true;
10104
+ } else if (value === "false") {
10105
+ update[key] = false;
10106
+ } else {
10107
+ const parsedNumber = parseFloat(value);
10108
+ if (!isNaN(parsedNumber)) {
10109
+ update[key] = parsedNumber;
10110
+ } else {
10111
+ update[key] = value;
10112
+ }
10113
+ }
10114
+ });
10115
+ return update;
10116
+ }
10117
+ function getHasUneditableRows(metadata) {
10118
+ if (!metadata) {
10119
+ return false;
10120
+ }
10121
+ return Object.values(metadata).some(
10122
+ (value) => !EDITABLE_TYPES.includes(typeof value)
10123
+ );
10124
+ }
10125
+ const Email = () => {
10126
+ const { id } = useParams();
10127
+ const { order, isPending, isError, error } = useOrder(id, {
10128
+ fields: "+email"
10129
+ });
10130
+ if (isError) {
10131
+ throw error;
10132
+ }
10133
+ const isReady = !isPending && !!order;
10134
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10135
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10136
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
10137
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
10138
+ ] }),
10139
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
10140
+ ] });
10141
+ };
10142
+ const EmailForm = ({ order }) => {
10143
+ const form = useForm({
10144
+ defaultValues: {
10145
+ email: order.email ?? ""
10146
+ },
10147
+ resolver: zodResolver(schema$3)
10148
+ });
10149
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10150
+ const { handleSuccess } = useRouteModal();
10151
+ const onSubmit = form.handleSubmit(async (data) => {
10152
+ await mutateAsync(
10153
+ { email: data.email },
10154
+ {
10155
+ onSuccess: () => {
10156
+ handleSuccess();
10157
+ },
10158
+ onError: (error) => {
10159
+ toast.error(error.message);
10160
+ }
10161
+ }
10162
+ );
10163
+ });
10164
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10165
+ KeyboundForm,
10166
+ {
10167
+ className: "flex flex-1 flex-col overflow-hidden",
10168
+ onSubmit,
10169
+ children: [
10170
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
10171
+ Form$2.Field,
10172
+ {
10173
+ control: form.control,
10174
+ name: "email",
10175
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
10176
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
10177
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10178
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10179
+ ] })
10180
+ }
10181
+ ) }),
10182
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10183
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10184
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10185
+ ] }) })
10186
+ ]
10187
+ }
10188
+ ) });
10189
+ };
10190
+ const schema$3 = objectType({
10191
+ email: stringType().email()
10192
+ });
10193
+ const NumberInput = forwardRef(
10194
+ ({
10195
+ value,
10196
+ onChange,
10197
+ size = "base",
10198
+ min = 0,
10199
+ max = 100,
10200
+ step = 1,
10201
+ className,
10202
+ disabled,
10203
+ ...props
10204
+ }, ref) => {
10205
+ const handleChange = (event) => {
10206
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
10207
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
10208
+ onChange(newValue);
10209
+ }
10210
+ };
10211
+ const handleIncrement = () => {
10212
+ const newValue = value + step;
10213
+ if (max === void 0 || newValue <= max) {
10214
+ onChange(newValue);
10215
+ }
10216
+ };
10217
+ const handleDecrement = () => {
10218
+ const newValue = value - step;
10219
+ if (min === void 0 || newValue >= min) {
10220
+ onChange(newValue);
10221
+ }
10222
+ };
10223
+ return /* @__PURE__ */ jsxs(
10224
+ "div",
10225
+ {
10226
+ className: clx(
10227
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
10228
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
10229
+ {
10230
+ "h-7": size === "small",
10231
+ "h-8": size === "base"
10232
+ },
10233
+ className
10234
+ ),
10235
+ children: [
10236
+ /* @__PURE__ */ jsx(
10237
+ "input",
10238
+ {
10239
+ ref,
10240
+ type: "number",
10241
+ value,
10242
+ onChange: handleChange,
10243
+ min,
10244
+ max,
10245
+ step,
10246
+ className: clx(
10247
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
10248
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
10249
+ "placeholder:text-ui-fg-muted"
10250
+ ),
10251
+ ...props
10252
+ }
10253
+ ),
10254
+ /* @__PURE__ */ jsxs(
10255
+ "button",
10256
+ {
10257
+ className: clx(
10258
+ "flex items-center justify-center outline-none transition-fg",
10259
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10260
+ "focus:bg-ui-bg-field-component-hover",
10261
+ "hover:bg-ui-bg-field-component-hover",
10262
+ {
10263
+ "size-7": size === "small",
10264
+ "size-8": size === "base"
10265
+ }
10266
+ ),
10267
+ type: "button",
10268
+ onClick: handleDecrement,
10269
+ disabled: min !== void 0 && value <= min || disabled,
9920
10270
  children: [
9921
10271
  /* @__PURE__ */ jsx(Minus, {}),
9922
10272
  /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
@@ -10245,168 +10595,20 @@ const VariantItem = ({ item, preview, currencyCode }) => {
10245
10595
  const [editing, setEditing] = useState(false);
10246
10596
  const form = useForm({
10247
10597
  defaultValues: {
10248
- quantity: item.quantity,
10249
- unit_price: item.unit_price
10250
- },
10251
- resolver: zodResolver(variantItemSchema)
10252
- });
10253
- const actionId = useMemo(() => {
10254
- var _a, _b;
10255
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10256
- }, [item]);
10257
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10258
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10259
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10260
- const onSubmit = form.handleSubmit(async (data) => {
10261
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10262
- setEditing(false);
10263
- return;
10264
- }
10265
- if (!actionId) {
10266
- await updateOriginalItem(
10267
- {
10268
- item_id: item.id,
10269
- quantity: data.quantity,
10270
- unit_price: convertNumber(data.unit_price)
10271
- },
10272
- {
10273
- onSuccess: () => {
10274
- setEditing(false);
10275
- },
10276
- onError: (e) => {
10277
- toast.error(e.message);
10278
- }
10279
- }
10280
- );
10281
- return;
10282
- }
10283
- await updateActionItem(
10284
- {
10285
- action_id: actionId,
10286
- quantity: data.quantity,
10287
- unit_price: convertNumber(data.unit_price)
10288
- },
10289
- {
10290
- onSuccess: () => {
10291
- setEditing(false);
10292
- },
10293
- onError: (e) => {
10294
- toast.error(e.message);
10295
- }
10296
- }
10297
- );
10298
- });
10299
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10300
- /* @__PURE__ */ jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10301
- /* @__PURE__ */ jsx(
10302
- Thumbnail,
10303
- {
10304
- thumbnail: item.thumbnail,
10305
- alt: item.product_title ?? void 0
10306
- }
10307
- ),
10308
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10309
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
10310
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10311
- /* @__PURE__ */ jsxs(
10312
- Text,
10313
- {
10314
- size: "small",
10315
- leading: "compact",
10316
- className: "text-ui-fg-subtle",
10317
- children: [
10318
- "(",
10319
- item.variant_title,
10320
- ")"
10321
- ]
10322
- }
10323
- )
10324
- ] }),
10325
- /* @__PURE__ */ jsx(
10326
- Text,
10327
- {
10328
- size: "small",
10329
- leading: "compact",
10330
- className: "text-ui-fg-subtle",
10331
- children: item.variant_sku
10332
- }
10333
- )
10334
- ] })
10335
- ] }),
10336
- editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10337
- Form$2.Field,
10338
- {
10339
- control: form.control,
10340
- name: "quantity",
10341
- render: ({ field }) => {
10342
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10343
- }
10344
- }
10345
- ) }) : /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
10346
- editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10347
- Form$2.Field,
10348
- {
10349
- control: form.control,
10350
- name: "unit_price",
10351
- render: ({ field: { onChange, ...field } }) => {
10352
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10353
- CurrencyInput,
10354
- {
10355
- ...field,
10356
- symbol: getNativeSymbol(currencyCode),
10357
- code: currencyCode,
10358
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10359
- }
10360
- ) }) });
10361
- }
10362
- }
10363
- ) }) : /* @__PURE__ */ jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10364
- /* @__PURE__ */ jsx(
10365
- IconButton,
10366
- {
10367
- type: "button",
10368
- size: "small",
10369
- onClick: editing ? onSubmit : () => {
10370
- setEditing(true);
10371
- },
10372
- disabled: isPending,
10373
- children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10374
- }
10375
- )
10376
- ] }) }) });
10377
- };
10378
- const variantItemSchema = objectType({
10379
- quantity: numberType(),
10380
- unit_price: unionType([numberType(), stringType()])
10381
- });
10382
- const CustomItem = ({ item, preview, currencyCode }) => {
10383
- const [editing, setEditing] = useState(false);
10384
- const { quantity, unit_price, title } = item;
10385
- const form = useForm({
10386
- defaultValues: {
10387
- title,
10388
- quantity,
10389
- unit_price
10390
- },
10391
- resolver: zodResolver(customItemSchema)
10392
- });
10393
- useEffect(() => {
10394
- form.reset({
10395
- title,
10396
- quantity,
10397
- unit_price
10398
- });
10399
- }, [form, title, quantity, unit_price]);
10598
+ quantity: item.quantity,
10599
+ unit_price: item.unit_price
10600
+ },
10601
+ resolver: zodResolver(variantItemSchema)
10602
+ });
10400
10603
  const actionId = useMemo(() => {
10401
10604
  var _a, _b;
10402
10605
  return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10403
10606
  }, [item]);
10404
10607
  const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10405
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10406
10608
  const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10407
10609
  const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10408
10610
  const onSubmit = form.handleSubmit(async (data) => {
10409
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10611
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10410
10612
  setEditing(false);
10411
10613
  return;
10412
10614
  }
@@ -10428,17 +10630,6 @@ const CustomItem = ({ item, preview, currencyCode }) => {
10428
10630
  );
10429
10631
  return;
10430
10632
  }
10431
- if (data.quantity === 0) {
10432
- await removeActionItem(actionId, {
10433
- onSuccess: () => {
10434
- setEditing(false);
10435
- },
10436
- onError: (e) => {
10437
- toast.error(e.message);
10438
- }
10439
- });
10440
- return;
10441
- }
10442
10633
  await updateActionItem(
10443
10634
  {
10444
10635
  action_id: actionId,
@@ -10456,26 +10647,43 @@ const CustomItem = ({ item, preview, currencyCode }) => {
10456
10647
  );
10457
10648
  });
10458
10649
  return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10459
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10650
+ /* @__PURE__ */ jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10460
10651
  /* @__PURE__ */ jsx(
10461
10652
  Thumbnail,
10462
10653
  {
10463
10654
  thumbnail: item.thumbnail,
10464
- alt: item.title ?? void 0
10655
+ alt: item.product_title ?? void 0
10465
10656
  }
10466
10657
  ),
10467
- editing ? /* @__PURE__ */ jsx(
10468
- Form$2.Field,
10469
- {
10470
- control: form.control,
10471
- name: "title",
10472
- render: ({ field }) => {
10473
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
10658
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10659
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
10660
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10661
+ /* @__PURE__ */ jsxs(
10662
+ Text,
10663
+ {
10664
+ size: "small",
10665
+ leading: "compact",
10666
+ className: "text-ui-fg-subtle",
10667
+ children: [
10668
+ "(",
10669
+ item.variant_title,
10670
+ ")"
10671
+ ]
10672
+ }
10673
+ )
10674
+ ] }),
10675
+ /* @__PURE__ */ jsx(
10676
+ Text,
10677
+ {
10678
+ size: "small",
10679
+ leading: "compact",
10680
+ className: "text-ui-fg-subtle",
10681
+ children: item.variant_sku
10474
10682
  }
10475
- }
10476
- ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
10683
+ )
10684
+ ] })
10477
10685
  ] }),
10478
- editing ? /* @__PURE__ */ jsx(
10686
+ editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10479
10687
  Form$2.Field,
10480
10688
  {
10481
10689
  control: form.control,
@@ -10484,8 +10692,8 @@ const CustomItem = ({ item, preview, currencyCode }) => {
10484
10692
  return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10485
10693
  }
10486
10694
  }
10487
- ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
10488
- editing ? /* @__PURE__ */ jsx(
10695
+ ) }) : /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
10696
+ editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10489
10697
  Form$2.Field,
10490
10698
  {
10491
10699
  control: form.control,
@@ -10502,230 +10710,94 @@ const CustomItem = ({ item, preview, currencyCode }) => {
10502
10710
  ) }) });
10503
10711
  }
10504
10712
  }
10505
- ) : /* @__PURE__ */ jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10506
- /* @__PURE__ */ jsx(
10507
- IconButton,
10508
- {
10509
- type: "button",
10510
- size: "small",
10511
- onClick: editing ? onSubmit : () => {
10512
- setEditing(true);
10513
- },
10514
- disabled: isPending,
10515
- children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10516
- }
10517
- )
10518
- ] }) }) });
10519
- };
10520
- const StackedModalTrigger$1 = ({
10521
- type,
10522
- setModalContent
10523
- }) => {
10524
- const { setIsOpen } = useStackedModal();
10525
- const onClick = useCallback(() => {
10526
- setModalContent(type);
10527
- setIsOpen(STACKED_MODAL_ID, true);
10528
- }, [setModalContent, setIsOpen, type]);
10529
- return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10530
- };
10531
- const VARIANT_PREFIX = "items";
10532
- const LIMIT = 50;
10533
- const ExistingItemsForm = ({ orderId, items }) => {
10534
- const { setIsOpen } = useStackedModal();
10535
- const [rowSelection, setRowSelection] = useState(
10536
- items.reduce((acc, item) => {
10537
- acc[item.variant_id] = true;
10538
- return acc;
10539
- }, {})
10540
- );
10541
- useEffect(() => {
10542
- setRowSelection(
10543
- items.reduce((acc, item) => {
10544
- if (item.variant_id) {
10545
- acc[item.variant_id] = true;
10546
- }
10547
- return acc;
10548
- }, {})
10549
- );
10550
- }, [items]);
10551
- const { q, order, offset } = useQueryParams(
10552
- ["q", "order", "offset"],
10553
- VARIANT_PREFIX
10554
- );
10555
- const { variants, count, isPending, isError, error } = useProductVariants(
10556
- {
10557
- q,
10558
- order,
10559
- offset: offset ? parseInt(offset) : void 0,
10560
- limit: LIMIT
10561
- },
10562
- {
10563
- placeholderData: keepPreviousData
10564
- }
10565
- );
10566
- const columns = useColumns();
10567
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10568
- const onSubmit = async () => {
10569
- const ids = Object.keys(rowSelection).filter(
10570
- (id) => !items.find((i) => i.variant_id === id)
10571
- );
10572
- await mutateAsync(
10573
- {
10574
- items: ids.map((id) => ({
10575
- variant_id: id,
10576
- quantity: 1
10577
- }))
10578
- },
10579
- {
10580
- onSuccess: () => {
10581
- setRowSelection({});
10582
- setIsOpen(STACKED_MODAL_ID, false);
10583
- },
10584
- onError: (e) => {
10585
- toast.error(e.message);
10586
- }
10587
- }
10588
- );
10589
- };
10590
- if (isError) {
10591
- throw error;
10592
- }
10593
- return /* @__PURE__ */ jsxs(
10594
- StackedFocusModal.Content,
10595
- {
10596
- onOpenAutoFocus: (e) => {
10597
- e.preventDefault();
10598
- const searchInput = document.querySelector(
10599
- "[data-modal-id='modal-search-input']"
10600
- );
10601
- if (searchInput) {
10602
- searchInput.focus();
10603
- }
10604
- },
10605
- children: [
10606
- /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
10607
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10608
- /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10609
- ] }),
10610
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
10611
- DataTable,
10612
- {
10613
- data: variants,
10614
- columns,
10615
- isLoading: isPending,
10616
- getRowId: (row) => row.id,
10617
- rowCount: count,
10618
- prefix: VARIANT_PREFIX,
10619
- layout: "fill",
10620
- rowSelection: {
10621
- state: rowSelection,
10622
- onRowSelectionChange: setRowSelection,
10623
- enableRowSelection: (row) => {
10624
- return !items.find((i) => i.variant_id === row.original.id);
10625
- }
10626
- },
10627
- autoFocusSearch: true
10628
- }
10629
- ) }),
10630
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10631
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10632
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10633
- ] }) })
10634
- ]
10635
- }
10636
- );
10637
- };
10638
- const columnHelper = createDataTableColumnHelper();
10639
- const useColumns = () => {
10640
- return useMemo(() => {
10641
- return [
10642
- columnHelper.select(),
10643
- columnHelper.accessor("product.title", {
10644
- header: "Product",
10645
- cell: ({ row }) => {
10646
- var _a, _b, _c;
10647
- return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
10648
- /* @__PURE__ */ jsx(
10649
- Thumbnail,
10650
- {
10651
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10652
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10653
- }
10654
- ),
10655
- /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10656
- ] });
10657
- },
10658
- enableSorting: true
10659
- }),
10660
- columnHelper.accessor("title", {
10661
- header: "Variant",
10662
- enableSorting: true
10663
- }),
10664
- columnHelper.accessor("sku", {
10665
- header: "SKU",
10666
- cell: ({ getValue }) => {
10667
- return getValue() ?? "-";
10668
- },
10669
- enableSorting: true
10670
- }),
10671
- columnHelper.accessor("updated_at", {
10672
- header: "Updated",
10673
- cell: ({ getValue }) => {
10674
- return /* @__PURE__ */ jsx(
10675
- Tooltip,
10676
- {
10677
- content: getFullDate({ date: getValue(), includeTime: true }),
10678
- children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10679
- }
10680
- );
10681
- },
10682
- enableSorting: true,
10683
- sortAscLabel: "Oldest first",
10684
- sortDescLabel: "Newest first"
10685
- }),
10686
- columnHelper.accessor("created_at", {
10687
- header: "Created",
10688
- cell: ({ getValue }) => {
10689
- return /* @__PURE__ */ jsx(
10690
- Tooltip,
10691
- {
10692
- content: getFullDate({ date: getValue(), includeTime: true }),
10693
- children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10694
- }
10695
- );
10713
+ ) }) : /* @__PURE__ */ jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10714
+ /* @__PURE__ */ jsx(
10715
+ IconButton,
10716
+ {
10717
+ type: "button",
10718
+ size: "small",
10719
+ onClick: editing ? onSubmit : () => {
10720
+ setEditing(true);
10696
10721
  },
10697
- enableSorting: true,
10698
- sortAscLabel: "Oldest first",
10699
- sortDescLabel: "Newest first"
10700
- })
10701
- ];
10702
- }, []);
10722
+ disabled: isPending,
10723
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10724
+ }
10725
+ )
10726
+ ] }) }) });
10703
10727
  };
10704
- const CustomItemForm = ({ orderId, currencyCode }) => {
10705
- const { setIsOpen } = useStackedModal();
10706
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10728
+ const variantItemSchema = objectType({
10729
+ quantity: numberType(),
10730
+ unit_price: unionType([numberType(), stringType()])
10731
+ });
10732
+ const CustomItem = ({ item, preview, currencyCode }) => {
10733
+ const [editing, setEditing] = useState(false);
10734
+ const { quantity, unit_price, title } = item;
10707
10735
  const form = useForm({
10708
10736
  defaultValues: {
10709
- title: "",
10710
- quantity: 1,
10711
- unit_price: ""
10737
+ title,
10738
+ quantity,
10739
+ unit_price
10712
10740
  },
10713
10741
  resolver: zodResolver(customItemSchema)
10714
10742
  });
10743
+ useEffect(() => {
10744
+ form.reset({
10745
+ title,
10746
+ quantity,
10747
+ unit_price
10748
+ });
10749
+ }, [form, title, quantity, unit_price]);
10750
+ const actionId = useMemo(() => {
10751
+ var _a, _b;
10752
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10753
+ }, [item]);
10754
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10755
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10756
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10757
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10715
10758
  const onSubmit = form.handleSubmit(async (data) => {
10716
- await addItems(
10717
- {
10718
- items: [
10719
- {
10720
- title: data.title,
10721
- quantity: data.quantity,
10722
- unit_price: convertNumber(data.unit_price)
10759
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10760
+ setEditing(false);
10761
+ return;
10762
+ }
10763
+ if (!actionId) {
10764
+ await updateOriginalItem(
10765
+ {
10766
+ item_id: item.id,
10767
+ quantity: data.quantity,
10768
+ unit_price: convertNumber(data.unit_price)
10769
+ },
10770
+ {
10771
+ onSuccess: () => {
10772
+ setEditing(false);
10773
+ },
10774
+ onError: (e) => {
10775
+ toast.error(e.message);
10723
10776
  }
10724
- ]
10777
+ }
10778
+ );
10779
+ return;
10780
+ }
10781
+ if (data.quantity === 0) {
10782
+ await removeActionItem(actionId, {
10783
+ onSuccess: () => {
10784
+ setEditing(false);
10785
+ },
10786
+ onError: (e) => {
10787
+ toast.error(e.message);
10788
+ }
10789
+ });
10790
+ return;
10791
+ }
10792
+ await updateActionItem(
10793
+ {
10794
+ action_id: actionId,
10795
+ quantity: data.quantity,
10796
+ unit_price: convertNumber(data.unit_price)
10725
10797
  },
10726
10798
  {
10727
10799
  onSuccess: () => {
10728
- setIsOpen(STACKED_MODAL_ID, false);
10800
+ setEditing(false);
10729
10801
  },
10730
10802
  onError: (e) => {
10731
10803
  toast.error(e.message);
@@ -10733,437 +10805,365 @@ const CustomItemForm = ({ orderId, currencyCode }) => {
10733
10805
  }
10734
10806
  );
10735
10807
  });
10736
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
10737
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
10738
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
10739
- /* @__PURE__ */ jsxs("div", { children: [
10740
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
10741
- /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
10742
- ] }),
10743
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10744
- /* @__PURE__ */ jsx(
10745
- Form$2.Field,
10746
- {
10747
- control: form.control,
10748
- name: "title",
10749
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10750
- /* @__PURE__ */ jsxs("div", { children: [
10751
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
10752
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
10753
- ] }),
10754
- /* @__PURE__ */ jsxs("div", { children: [
10755
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10756
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10757
- ] })
10758
- ] }) })
10759
- }
10760
- ),
10761
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10808
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10809
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10762
10810
  /* @__PURE__ */ jsx(
10763
- Form$2.Field,
10811
+ Thumbnail,
10764
10812
  {
10765
- control: form.control,
10766
- name: "unit_price",
10767
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10768
- /* @__PURE__ */ jsxs("div", { children: [
10769
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
10770
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10771
- ] }),
10772
- /* @__PURE__ */ jsxs("div", { children: [
10773
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10774
- CurrencyInput,
10775
- {
10776
- symbol: getNativeSymbol(currencyCode),
10777
- code: currencyCode,
10778
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10779
- ...field
10780
- }
10781
- ) }),
10782
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10783
- ] })
10784
- ] }) })
10813
+ thumbnail: item.thumbnail,
10814
+ alt: item.title ?? void 0
10785
10815
  }
10786
10816
  ),
10787
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10788
- /* @__PURE__ */ jsx(
10817
+ editing ? /* @__PURE__ */ jsx(
10789
10818
  Form$2.Field,
10790
10819
  {
10791
10820
  control: form.control,
10792
- name: "quantity",
10793
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10794
- /* @__PURE__ */ jsxs("div", { children: [
10795
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
10796
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10797
- ] }),
10798
- /* @__PURE__ */ jsxs("div", { className: "w-full flex-1", children: [
10799
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10800
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10801
- ] })
10802
- ] }) })
10803
- }
10804
- )
10805
- ] }) }) }),
10806
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10807
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10808
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10809
- ] }) })
10810
- ] }) }) });
10811
- };
10812
- const customItemSchema = objectType({
10813
- title: stringType().min(1),
10814
- quantity: numberType(),
10815
- unit_price: unionType([numberType(), stringType()])
10816
- });
10817
- const InlineTip = forwardRef(
10818
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10819
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10820
- return /* @__PURE__ */ jsxs(
10821
- "div",
10822
- {
10823
- ref,
10824
- className: clx(
10825
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10826
- className
10827
- ),
10828
- ...props,
10829
- children: [
10830
- /* @__PURE__ */ jsx(
10831
- "div",
10832
- {
10833
- role: "presentation",
10834
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10835
- "bg-ui-tag-orange-icon": variant === "warning"
10836
- })
10837
- }
10838
- ),
10839
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
10840
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10841
- labelValue,
10842
- ":"
10843
- ] }),
10844
- " ",
10845
- children
10846
- ] })
10847
- ]
10848
- }
10849
- );
10850
- }
10851
- );
10852
- InlineTip.displayName = "InlineTip";
10853
- const MetadataFieldSchema = objectType({
10854
- key: stringType(),
10855
- disabled: booleanType().optional(),
10856
- value: anyType()
10857
- });
10858
- const MetadataSchema = objectType({
10859
- metadata: arrayType(MetadataFieldSchema)
10860
- });
10861
- const Metadata = () => {
10862
- const { id } = useParams();
10863
- const { order, isPending, isError, error } = useOrder(id, {
10864
- fields: "metadata"
10865
- });
10866
- if (isError) {
10867
- throw error;
10868
- }
10869
- const isReady = !isPending && !!order;
10870
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10871
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10872
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
10873
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10874
- ] }),
10875
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10876
- ] });
10877
- };
10878
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10879
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10880
- const MetadataForm = ({ orderId, metadata }) => {
10881
- const { handleSuccess } = useRouteModal();
10882
- const hasUneditableRows = getHasUneditableRows(metadata);
10883
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10884
- const form = useForm({
10885
- defaultValues: {
10886
- metadata: getDefaultValues(metadata)
10887
- },
10888
- resolver: zodResolver(MetadataSchema)
10889
- });
10890
- const handleSubmit = form.handleSubmit(async (data) => {
10891
- const parsedData = parseValues(data);
10892
- await mutateAsync(
10821
+ name: "title",
10822
+ render: ({ field }) => {
10823
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
10824
+ }
10825
+ }
10826
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
10827
+ ] }),
10828
+ editing ? /* @__PURE__ */ jsx(
10829
+ Form$2.Field,
10893
10830
  {
10894
- metadata: parsedData
10895
- },
10831
+ control: form.control,
10832
+ name: "quantity",
10833
+ render: ({ field }) => {
10834
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10835
+ }
10836
+ }
10837
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
10838
+ editing ? /* @__PURE__ */ jsx(
10839
+ Form$2.Field,
10896
10840
  {
10897
- onSuccess: () => {
10898
- toast.success("Metadata updated");
10899
- handleSuccess();
10900
- },
10901
- onError: (error) => {
10902
- toast.error(error.message);
10841
+ control: form.control,
10842
+ name: "unit_price",
10843
+ render: ({ field: { onChange, ...field } }) => {
10844
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10845
+ CurrencyInput,
10846
+ {
10847
+ ...field,
10848
+ symbol: getNativeSymbol(currencyCode),
10849
+ code: currencyCode,
10850
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10851
+ }
10852
+ ) }) });
10903
10853
  }
10904
10854
  }
10855
+ ) : /* @__PURE__ */ jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10856
+ /* @__PURE__ */ jsx(
10857
+ IconButton,
10858
+ {
10859
+ type: "button",
10860
+ size: "small",
10861
+ onClick: editing ? onSubmit : () => {
10862
+ setEditing(true);
10863
+ },
10864
+ disabled: isPending,
10865
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10866
+ }
10867
+ )
10868
+ ] }) }) });
10869
+ };
10870
+ const StackedModalTrigger$1 = ({
10871
+ type,
10872
+ setModalContent
10873
+ }) => {
10874
+ const { setIsOpen } = useStackedModal();
10875
+ const onClick = useCallback(() => {
10876
+ setModalContent(type);
10877
+ setIsOpen(STACKED_MODAL_ID, true);
10878
+ }, [setModalContent, setIsOpen, type]);
10879
+ return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10880
+ };
10881
+ const VARIANT_PREFIX = "items";
10882
+ const LIMIT = 50;
10883
+ const ExistingItemsForm = ({ orderId, items }) => {
10884
+ const { setIsOpen } = useStackedModal();
10885
+ const [rowSelection, setRowSelection] = useState(
10886
+ items.reduce((acc, item) => {
10887
+ acc[item.variant_id] = true;
10888
+ return acc;
10889
+ }, {})
10890
+ );
10891
+ useEffect(() => {
10892
+ setRowSelection(
10893
+ items.reduce((acc, item) => {
10894
+ if (item.variant_id) {
10895
+ acc[item.variant_id] = true;
10896
+ }
10897
+ return acc;
10898
+ }, {})
10905
10899
  );
10906
- });
10907
- const { fields, insert, remove } = useFieldArray({
10908
- control: form.control,
10909
- name: "metadata"
10910
- });
10911
- function deleteRow(index) {
10912
- remove(index);
10913
- if (fields.length === 1) {
10914
- insert(0, {
10915
- key: "",
10916
- value: "",
10917
- disabled: false
10918
- });
10919
- }
10920
- }
10921
- function insertRow(index, position) {
10922
- insert(index + (position === "above" ? 0 : 1), {
10923
- key: "",
10924
- value: "",
10925
- disabled: false
10926
- });
10927
- }
10928
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10929
- KeyboundForm,
10900
+ }, [items]);
10901
+ const { q, order, offset } = useQueryParams(
10902
+ ["q", "order", "offset"],
10903
+ VARIANT_PREFIX
10904
+ );
10905
+ const { variants, count, isPending, isError, error } = useProductVariants(
10930
10906
  {
10931
- onSubmit: handleSubmit,
10932
- className: "flex flex-1 flex-col overflow-hidden",
10933
- children: [
10934
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10935
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10936
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10937
- /* @__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" }) }),
10938
- /* @__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" }) })
10939
- ] }),
10940
- fields.map((field, index) => {
10941
- const isDisabled = field.disabled || false;
10942
- let placeholder = "-";
10943
- if (typeof field.value === "object") {
10944
- placeholder = "{ ... }";
10945
- }
10946
- if (Array.isArray(field.value)) {
10947
- placeholder = "[ ... ]";
10948
- }
10949
- return /* @__PURE__ */ jsx(
10950
- ConditionalTooltip,
10951
- {
10952
- showTooltip: isDisabled,
10953
- content: "This row is disabled because it contains non-primitive data.",
10954
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
10955
- /* @__PURE__ */ jsxs(
10956
- "div",
10957
- {
10958
- className: clx("grid grid-cols-2 divide-x", {
10959
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10960
- }),
10961
- children: [
10962
- /* @__PURE__ */ jsx(
10963
- Form$2.Field,
10964
- {
10965
- control: form.control,
10966
- name: `metadata.${index}.key`,
10967
- render: ({ field: field2 }) => {
10968
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10969
- GridInput,
10970
- {
10971
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10972
- ...field2,
10973
- disabled: isDisabled,
10974
- placeholder: "Key"
10975
- }
10976
- ) }) });
10977
- }
10978
- }
10979
- ),
10980
- /* @__PURE__ */ jsx(
10981
- Form$2.Field,
10982
- {
10983
- control: form.control,
10984
- name: `metadata.${index}.value`,
10985
- render: ({ field: { value, ...field2 } }) => {
10986
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10987
- GridInput,
10988
- {
10989
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
10990
- ...field2,
10991
- value: isDisabled ? placeholder : value,
10992
- disabled: isDisabled,
10993
- placeholder: "Value"
10994
- }
10995
- ) }) });
10996
- }
10997
- }
10998
- )
10999
- ]
11000
- }
11001
- ),
11002
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
11003
- /* @__PURE__ */ jsx(
11004
- DropdownMenu.Trigger,
11005
- {
11006
- className: clx(
11007
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11008
- {
11009
- hidden: isDisabled
11010
- }
11011
- ),
11012
- disabled: isDisabled,
11013
- asChild: true,
11014
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
11015
- }
11016
- ),
11017
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
11018
- /* @__PURE__ */ jsxs(
11019
- DropdownMenu.Item,
11020
- {
11021
- className: "gap-x-2",
11022
- onClick: () => insertRow(index, "above"),
11023
- children: [
11024
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
11025
- "Insert row above"
11026
- ]
11027
- }
11028
- ),
11029
- /* @__PURE__ */ jsxs(
11030
- DropdownMenu.Item,
11031
- {
11032
- className: "gap-x-2",
11033
- onClick: () => insertRow(index, "below"),
11034
- children: [
11035
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
11036
- "Insert row below"
11037
- ]
11038
- }
11039
- ),
11040
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
11041
- /* @__PURE__ */ jsxs(
11042
- DropdownMenu.Item,
11043
- {
11044
- className: "gap-x-2",
11045
- onClick: () => deleteRow(index),
11046
- children: [
11047
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
11048
- "Delete row"
11049
- ]
11050
- }
11051
- )
11052
- ] })
11053
- ] })
11054
- ] })
11055
- },
11056
- field.id
11057
- );
11058
- })
11059
- ] }),
11060
- 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." })
10907
+ q,
10908
+ order,
10909
+ offset: offset ? parseInt(offset) : void 0,
10910
+ limit: LIMIT
10911
+ },
10912
+ {
10913
+ placeholderData: keepPreviousData
10914
+ }
10915
+ );
10916
+ const columns = useColumns();
10917
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
10918
+ const onSubmit = async () => {
10919
+ const ids = Object.keys(rowSelection).filter(
10920
+ (id) => !items.find((i) => i.variant_id === id)
10921
+ );
10922
+ await mutateAsync(
10923
+ {
10924
+ items: ids.map((id) => ({
10925
+ variant_id: id,
10926
+ quantity: 1
10927
+ }))
10928
+ },
10929
+ {
10930
+ onSuccess: () => {
10931
+ setRowSelection({});
10932
+ setIsOpen(STACKED_MODAL_ID, false);
10933
+ },
10934
+ onError: (e) => {
10935
+ toast.error(e.message);
10936
+ }
10937
+ }
10938
+ );
10939
+ };
10940
+ if (isError) {
10941
+ throw error;
10942
+ }
10943
+ return /* @__PURE__ */ jsxs(
10944
+ StackedFocusModal.Content,
10945
+ {
10946
+ onOpenAutoFocus: (e) => {
10947
+ e.preventDefault();
10948
+ const searchInput = document.querySelector(
10949
+ "[data-modal-id='modal-search-input']"
10950
+ );
10951
+ if (searchInput) {
10952
+ searchInput.focus();
10953
+ }
10954
+ },
10955
+ children: [
10956
+ /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
10957
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10958
+ /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
11061
10959
  ] }),
11062
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11063
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11064
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10960
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
10961
+ DataTable,
10962
+ {
10963
+ data: variants,
10964
+ columns,
10965
+ isLoading: isPending,
10966
+ getRowId: (row) => row.id,
10967
+ rowCount: count,
10968
+ prefix: VARIANT_PREFIX,
10969
+ layout: "fill",
10970
+ rowSelection: {
10971
+ state: rowSelection,
10972
+ onRowSelectionChange: setRowSelection,
10973
+ enableRowSelection: (row) => {
10974
+ return !items.find((i) => i.variant_id === row.original.id);
10975
+ }
10976
+ },
10977
+ autoFocusSearch: true
10978
+ }
10979
+ ) }),
10980
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10981
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10982
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
11065
10983
  ] }) })
11066
10984
  ]
11067
10985
  }
11068
- ) });
11069
- };
11070
- const GridInput = forwardRef(({ className, ...props }, ref) => {
11071
- return /* @__PURE__ */ jsx(
11072
- "input",
11073
- {
11074
- ref,
11075
- ...props,
11076
- autoComplete: "off",
11077
- className: clx(
11078
- "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",
11079
- className
11080
- )
11081
- }
11082
10986
  );
11083
- });
11084
- GridInput.displayName = "MetadataForm.GridInput";
11085
- const PlaceholderInner = () => {
11086
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11087
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11088
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11089
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11090
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11091
- ] }) })
11092
- ] });
11093
10987
  };
11094
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11095
- function getDefaultValues(metadata) {
11096
- if (!metadata || !Object.keys(metadata).length) {
10988
+ const columnHelper = createDataTableColumnHelper();
10989
+ const useColumns = () => {
10990
+ return useMemo(() => {
11097
10991
  return [
11098
- {
11099
- key: "",
11100
- value: "",
11101
- disabled: false
11102
- }
10992
+ columnHelper.select(),
10993
+ columnHelper.accessor("product.title", {
10994
+ header: "Product",
10995
+ cell: ({ row }) => {
10996
+ var _a, _b, _c;
10997
+ return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
10998
+ /* @__PURE__ */ jsx(
10999
+ Thumbnail,
11000
+ {
11001
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
11002
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
11003
+ }
11004
+ ),
11005
+ /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
11006
+ ] });
11007
+ },
11008
+ enableSorting: true
11009
+ }),
11010
+ columnHelper.accessor("title", {
11011
+ header: "Variant",
11012
+ enableSorting: true
11013
+ }),
11014
+ columnHelper.accessor("sku", {
11015
+ header: "SKU",
11016
+ cell: ({ getValue }) => {
11017
+ return getValue() ?? "-";
11018
+ },
11019
+ enableSorting: true
11020
+ }),
11021
+ columnHelper.accessor("updated_at", {
11022
+ header: "Updated",
11023
+ cell: ({ getValue }) => {
11024
+ return /* @__PURE__ */ jsx(
11025
+ Tooltip,
11026
+ {
11027
+ content: getFullDate({ date: getValue(), includeTime: true }),
11028
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
11029
+ }
11030
+ );
11031
+ },
11032
+ enableSorting: true,
11033
+ sortAscLabel: "Oldest first",
11034
+ sortDescLabel: "Newest first"
11035
+ }),
11036
+ columnHelper.accessor("created_at", {
11037
+ header: "Created",
11038
+ cell: ({ getValue }) => {
11039
+ return /* @__PURE__ */ jsx(
11040
+ Tooltip,
11041
+ {
11042
+ content: getFullDate({ date: getValue(), includeTime: true }),
11043
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
11044
+ }
11045
+ );
11046
+ },
11047
+ enableSorting: true,
11048
+ sortAscLabel: "Oldest first",
11049
+ sortDescLabel: "Newest first"
11050
+ })
11103
11051
  ];
11104
- }
11105
- return Object.entries(metadata).map(([key, value]) => {
11106
- if (!EDITABLE_TYPES.includes(typeof value)) {
11107
- return {
11108
- key,
11109
- value,
11110
- disabled: true
11111
- };
11112
- }
11113
- let stringValue = value;
11114
- if (typeof value !== "string") {
11115
- stringValue = JSON.stringify(value);
11116
- }
11117
- return {
11118
- key,
11119
- value: stringValue,
11120
- original_key: key
11121
- };
11052
+ }, []);
11053
+ };
11054
+ const CustomItemForm = ({ orderId, currencyCode }) => {
11055
+ const { setIsOpen } = useStackedModal();
11056
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
11057
+ const form = useForm({
11058
+ defaultValues: {
11059
+ title: "",
11060
+ quantity: 1,
11061
+ unit_price: ""
11062
+ },
11063
+ resolver: zodResolver(customItemSchema)
11122
11064
  });
11123
- }
11124
- function parseValues(values) {
11125
- const metadata = values.metadata;
11126
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11127
- if (isEmpty) {
11128
- return null;
11129
- }
11130
- const update = {};
11131
- metadata.forEach((field) => {
11132
- let key = field.key;
11133
- let value = field.value;
11134
- const disabled = field.disabled;
11135
- if (!key || !value) {
11136
- return;
11137
- }
11138
- if (disabled) {
11139
- update[key] = value;
11140
- return;
11141
- }
11142
- key = key.trim();
11143
- value = value.trim();
11144
- if (value === "true") {
11145
- update[key] = true;
11146
- } else if (value === "false") {
11147
- update[key] = false;
11148
- } else {
11149
- const parsedNumber = parseFloat(value);
11150
- if (!isNaN(parsedNumber)) {
11151
- update[key] = parsedNumber;
11152
- } else {
11153
- update[key] = value;
11065
+ const onSubmit = form.handleSubmit(async (data) => {
11066
+ await addItems(
11067
+ {
11068
+ items: [
11069
+ {
11070
+ title: data.title,
11071
+ quantity: data.quantity,
11072
+ unit_price: convertNumber(data.unit_price)
11073
+ }
11074
+ ]
11075
+ },
11076
+ {
11077
+ onSuccess: () => {
11078
+ setIsOpen(STACKED_MODAL_ID, false);
11079
+ },
11080
+ onError: (e) => {
11081
+ toast.error(e.message);
11082
+ }
11154
11083
  }
11155
- }
11084
+ );
11156
11085
  });
11157
- return update;
11158
- }
11159
- function getHasUneditableRows(metadata) {
11160
- if (!metadata) {
11161
- return false;
11162
- }
11163
- return Object.values(metadata).some(
11164
- (value) => !EDITABLE_TYPES.includes(typeof value)
11165
- );
11166
- }
11086
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
11087
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
11088
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
11089
+ /* @__PURE__ */ jsxs("div", { children: [
11090
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
11091
+ /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
11092
+ ] }),
11093
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11094
+ /* @__PURE__ */ jsx(
11095
+ Form$2.Field,
11096
+ {
11097
+ control: form.control,
11098
+ name: "title",
11099
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11100
+ /* @__PURE__ */ jsxs("div", { children: [
11101
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
11102
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
11103
+ ] }),
11104
+ /* @__PURE__ */ jsxs("div", { children: [
11105
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11106
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11107
+ ] })
11108
+ ] }) })
11109
+ }
11110
+ ),
11111
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11112
+ /* @__PURE__ */ jsx(
11113
+ Form$2.Field,
11114
+ {
11115
+ control: form.control,
11116
+ name: "unit_price",
11117
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11118
+ /* @__PURE__ */ jsxs("div", { children: [
11119
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
11120
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
11121
+ ] }),
11122
+ /* @__PURE__ */ jsxs("div", { children: [
11123
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11124
+ CurrencyInput,
11125
+ {
11126
+ symbol: getNativeSymbol(currencyCode),
11127
+ code: currencyCode,
11128
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
11129
+ ...field
11130
+ }
11131
+ ) }),
11132
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11133
+ ] })
11134
+ ] }) })
11135
+ }
11136
+ ),
11137
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11138
+ /* @__PURE__ */ jsx(
11139
+ Form$2.Field,
11140
+ {
11141
+ control: form.control,
11142
+ name: "quantity",
11143
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11144
+ /* @__PURE__ */ jsxs("div", { children: [
11145
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
11146
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
11147
+ ] }),
11148
+ /* @__PURE__ */ jsxs("div", { className: "w-full flex-1", children: [
11149
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
11150
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11151
+ ] })
11152
+ ] }) })
11153
+ }
11154
+ )
11155
+ ] }) }) }),
11156
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11157
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11158
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
11159
+ ] }) })
11160
+ ] }) }) });
11161
+ };
11162
+ const customItemSchema = objectType({
11163
+ title: stringType().min(1),
11164
+ quantity: numberType(),
11165
+ unit_price: unionType([numberType(), stringType()])
11166
+ });
11167
11167
  const PROMOTION_QUERY_KEY = "promotions";
11168
11168
  const promotionsQueryKeys = {
11169
11169
  list: (query2) => [
@@ -12343,17 +12343,220 @@ const CustomAmountField = ({
12343
12343
  /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12344
12344
  CurrencyInput,
12345
12345
  {
12346
- ...field,
12347
- onValueChange: (value) => onChange(value),
12348
- symbol: getNativeSymbol(currencyCode),
12349
- code: currencyCode
12346
+ ...field,
12347
+ onValueChange: (value) => onChange(value),
12348
+ symbol: getNativeSymbol(currencyCode),
12349
+ code: currencyCode
12350
+ }
12351
+ ) })
12352
+ ] });
12353
+ }
12354
+ }
12355
+ );
12356
+ };
12357
+ const ShippingAddress = () => {
12358
+ const { id } = useParams();
12359
+ const { order, isPending, isError, error } = useOrder(id, {
12360
+ fields: "+shipping_address"
12361
+ });
12362
+ if (isError) {
12363
+ throw error;
12364
+ }
12365
+ const isReady = !isPending && !!order;
12366
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12367
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12368
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12369
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12370
+ ] }),
12371
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12372
+ ] });
12373
+ };
12374
+ const ShippingAddressForm = ({ order }) => {
12375
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12376
+ const form = useForm({
12377
+ defaultValues: {
12378
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12379
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12380
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12381
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12382
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12383
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12384
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12385
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12386
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12387
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12388
+ },
12389
+ resolver: zodResolver(schema$1)
12390
+ });
12391
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12392
+ const { handleSuccess } = useRouteModal();
12393
+ const onSubmit = form.handleSubmit(async (data) => {
12394
+ await mutateAsync(
12395
+ {
12396
+ shipping_address: {
12397
+ first_name: data.first_name,
12398
+ last_name: data.last_name,
12399
+ company: data.company,
12400
+ address_1: data.address_1,
12401
+ address_2: data.address_2,
12402
+ city: data.city,
12403
+ province: data.province,
12404
+ country_code: data.country_code,
12405
+ postal_code: data.postal_code,
12406
+ phone: data.phone
12407
+ }
12408
+ },
12409
+ {
12410
+ onSuccess: () => {
12411
+ handleSuccess();
12412
+ },
12413
+ onError: (error) => {
12414
+ toast.error(error.message);
12415
+ }
12416
+ }
12417
+ );
12418
+ });
12419
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12420
+ KeyboundForm,
12421
+ {
12422
+ className: "flex flex-1 flex-col overflow-hidden",
12423
+ onSubmit,
12424
+ children: [
12425
+ /* @__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: [
12426
+ /* @__PURE__ */ jsx(
12427
+ Form$2.Field,
12428
+ {
12429
+ control: form.control,
12430
+ name: "country_code",
12431
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12432
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12433
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12434
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12435
+ ] })
12436
+ }
12437
+ ),
12438
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12439
+ /* @__PURE__ */ jsx(
12440
+ Form$2.Field,
12441
+ {
12442
+ control: form.control,
12443
+ name: "first_name",
12444
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12445
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12446
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12447
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12448
+ ] })
12449
+ }
12450
+ ),
12451
+ /* @__PURE__ */ jsx(
12452
+ Form$2.Field,
12453
+ {
12454
+ control: form.control,
12455
+ name: "last_name",
12456
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12457
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12458
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12459
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12460
+ ] })
12461
+ }
12462
+ )
12463
+ ] }),
12464
+ /* @__PURE__ */ jsx(
12465
+ Form$2.Field,
12466
+ {
12467
+ control: form.control,
12468
+ name: "company",
12469
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12470
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12471
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12472
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12473
+ ] })
12474
+ }
12475
+ ),
12476
+ /* @__PURE__ */ jsx(
12477
+ Form$2.Field,
12478
+ {
12479
+ control: form.control,
12480
+ name: "address_1",
12481
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12482
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12483
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12484
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12485
+ ] })
12486
+ }
12487
+ ),
12488
+ /* @__PURE__ */ jsx(
12489
+ Form$2.Field,
12490
+ {
12491
+ control: form.control,
12492
+ name: "address_2",
12493
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12494
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12495
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12496
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12497
+ ] })
12498
+ }
12499
+ ),
12500
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12501
+ /* @__PURE__ */ jsx(
12502
+ Form$2.Field,
12503
+ {
12504
+ control: form.control,
12505
+ name: "postal_code",
12506
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12507
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12508
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12509
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12510
+ ] })
12511
+ }
12512
+ ),
12513
+ /* @__PURE__ */ jsx(
12514
+ Form$2.Field,
12515
+ {
12516
+ control: form.control,
12517
+ name: "city",
12518
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12519
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12520
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12521
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12522
+ ] })
12523
+ }
12524
+ )
12525
+ ] }),
12526
+ /* @__PURE__ */ jsx(
12527
+ Form$2.Field,
12528
+ {
12529
+ control: form.control,
12530
+ name: "province",
12531
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12532
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12533
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12534
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12535
+ ] })
12536
+ }
12537
+ ),
12538
+ /* @__PURE__ */ jsx(
12539
+ Form$2.Field,
12540
+ {
12541
+ control: form.control,
12542
+ name: "phone",
12543
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12544
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12545
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12546
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12547
+ ] })
12350
12548
  }
12351
- ) })
12352
- ] });
12353
- }
12549
+ )
12550
+ ] }) }),
12551
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12552
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12553
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12554
+ ] }) })
12555
+ ]
12354
12556
  }
12355
- );
12557
+ ) });
12356
12558
  };
12559
+ const schema$1 = addressSchema;
12357
12560
  const TransferOwnership = () => {
12358
12561
  const { id } = useParams();
12359
12562
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12377,7 +12580,7 @@ const TransferOwnershipForm = ({ order }) => {
12377
12580
  defaultValues: {
12378
12581
  customer_id: order.customer_id || ""
12379
12582
  },
12380
- resolver: zodResolver(schema$1)
12583
+ resolver: zodResolver(schema)
12381
12584
  });
12382
12585
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12383
12586
  const { handleSuccess } = useRouteModal();
@@ -12827,212 +13030,9 @@ const Illustration = () => {
12827
13030
  }
12828
13031
  );
12829
13032
  };
12830
- const schema$1 = objectType({
13033
+ const schema = objectType({
12831
13034
  customer_id: stringType().min(1)
12832
13035
  });
12833
- const ShippingAddress = () => {
12834
- const { id } = useParams();
12835
- const { order, isPending, isError, error } = useOrder(id, {
12836
- fields: "+shipping_address"
12837
- });
12838
- if (isError) {
12839
- throw error;
12840
- }
12841
- const isReady = !isPending && !!order;
12842
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12843
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12844
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12845
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12846
- ] }),
12847
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12848
- ] });
12849
- };
12850
- const ShippingAddressForm = ({ order }) => {
12851
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12852
- const form = useForm({
12853
- defaultValues: {
12854
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12855
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12856
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12857
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12858
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12859
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12860
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12861
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12862
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12863
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12864
- },
12865
- resolver: zodResolver(schema)
12866
- });
12867
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12868
- const { handleSuccess } = useRouteModal();
12869
- const onSubmit = form.handleSubmit(async (data) => {
12870
- await mutateAsync(
12871
- {
12872
- shipping_address: {
12873
- first_name: data.first_name,
12874
- last_name: data.last_name,
12875
- company: data.company,
12876
- address_1: data.address_1,
12877
- address_2: data.address_2,
12878
- city: data.city,
12879
- province: data.province,
12880
- country_code: data.country_code,
12881
- postal_code: data.postal_code,
12882
- phone: data.phone
12883
- }
12884
- },
12885
- {
12886
- onSuccess: () => {
12887
- handleSuccess();
12888
- },
12889
- onError: (error) => {
12890
- toast.error(error.message);
12891
- }
12892
- }
12893
- );
12894
- });
12895
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12896
- KeyboundForm,
12897
- {
12898
- className: "flex flex-1 flex-col overflow-hidden",
12899
- onSubmit,
12900
- children: [
12901
- /* @__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: [
12902
- /* @__PURE__ */ jsx(
12903
- Form$2.Field,
12904
- {
12905
- control: form.control,
12906
- name: "country_code",
12907
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12908
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12909
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12910
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12911
- ] })
12912
- }
12913
- ),
12914
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12915
- /* @__PURE__ */ jsx(
12916
- Form$2.Field,
12917
- {
12918
- control: form.control,
12919
- name: "first_name",
12920
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12921
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12922
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12923
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12924
- ] })
12925
- }
12926
- ),
12927
- /* @__PURE__ */ jsx(
12928
- Form$2.Field,
12929
- {
12930
- control: form.control,
12931
- name: "last_name",
12932
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12933
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12934
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12935
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12936
- ] })
12937
- }
12938
- )
12939
- ] }),
12940
- /* @__PURE__ */ jsx(
12941
- Form$2.Field,
12942
- {
12943
- control: form.control,
12944
- name: "company",
12945
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12946
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12947
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12948
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12949
- ] })
12950
- }
12951
- ),
12952
- /* @__PURE__ */ jsx(
12953
- Form$2.Field,
12954
- {
12955
- control: form.control,
12956
- name: "address_1",
12957
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12958
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12959
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12960
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12961
- ] })
12962
- }
12963
- ),
12964
- /* @__PURE__ */ jsx(
12965
- Form$2.Field,
12966
- {
12967
- control: form.control,
12968
- name: "address_2",
12969
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12970
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12971
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12972
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12973
- ] })
12974
- }
12975
- ),
12976
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12977
- /* @__PURE__ */ jsx(
12978
- Form$2.Field,
12979
- {
12980
- control: form.control,
12981
- name: "postal_code",
12982
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12983
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12984
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12985
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12986
- ] })
12987
- }
12988
- ),
12989
- /* @__PURE__ */ jsx(
12990
- Form$2.Field,
12991
- {
12992
- control: form.control,
12993
- name: "city",
12994
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12995
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12996
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12997
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12998
- ] })
12999
- }
13000
- )
13001
- ] }),
13002
- /* @__PURE__ */ jsx(
13003
- Form$2.Field,
13004
- {
13005
- control: form.control,
13006
- name: "province",
13007
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13008
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
13009
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13010
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13011
- ] })
13012
- }
13013
- ),
13014
- /* @__PURE__ */ jsx(
13015
- Form$2.Field,
13016
- {
13017
- control: form.control,
13018
- name: "phone",
13019
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13020
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
13021
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13022
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13023
- ] })
13024
- }
13025
- )
13026
- ] }) }),
13027
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13028
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13029
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13030
- ] }) })
13031
- ]
13032
- }
13033
- ) });
13034
- };
13035
- const schema = addressSchema;
13036
13036
  const widgetModule = { widgets: [] };
13037
13037
  const routeModule = {
13038
13038
  routes: [
@@ -13053,13 +13053,17 @@ const routeModule = {
13053
13053
  handle,
13054
13054
  loader,
13055
13055
  children: [
13056
+ {
13057
+ Component: BillingAddress,
13058
+ path: "/draft-orders/:id/billing-address"
13059
+ },
13056
13060
  {
13057
13061
  Component: CustomItems,
13058
13062
  path: "/draft-orders/:id/custom-items"
13059
13063
  },
13060
13064
  {
13061
- Component: BillingAddress,
13062
- path: "/draft-orders/:id/billing-address"
13065
+ Component: Metadata,
13066
+ path: "/draft-orders/:id/metadata"
13063
13067
  },
13064
13068
  {
13065
13069
  Component: Email,
@@ -13069,10 +13073,6 @@ const routeModule = {
13069
13073
  Component: Items,
13070
13074
  path: "/draft-orders/:id/items"
13071
13075
  },
13072
- {
13073
- Component: Metadata,
13074
- path: "/draft-orders/:id/metadata"
13075
- },
13076
13076
  {
13077
13077
  Component: Promotions,
13078
13078
  path: "/draft-orders/:id/promotions"
@@ -13085,13 +13085,13 @@ const routeModule = {
13085
13085
  Component: Shipping,
13086
13086
  path: "/draft-orders/:id/shipping"
13087
13087
  },
13088
- {
13089
- Component: TransferOwnership,
13090
- path: "/draft-orders/:id/transfer-ownership"
13091
- },
13092
13088
  {
13093
13089
  Component: ShippingAddress,
13094
13090
  path: "/draft-orders/:id/shipping-address"
13091
+ },
13092
+ {
13093
+ Component: TransferOwnership,
13094
+ path: "/draft-orders/:id/transfer-ownership"
13095
13095
  }
13096
13096
  ]
13097
13097
  }