@medusajs/draft-order 2.12.2-preview-20251204090232 → 2.12.2-preview-20251204150146

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