@medusajs/draft-order 2.10.0-snapshot-20250828141936 → 2.10.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ import { Tooltip, DropdownMenu, clx, IconButton, useDataTable, DataTable as Data
4
4
  import { useQuery, useQueryClient, useMutation, keepPreviousData, useInfiniteQuery } from "@tanstack/react-query";
5
5
  import React, { useState, useCallback, useMemo, Fragment, createContext, forwardRef, useId, useContext, useTransition, useRef, useImperativeHandle, useDeferredValue, useEffect, Suspense } from "react";
6
6
  import { useSearchParams, Link, useNavigate, Outlet, useBlocker, useLocation, useParams } from "react-router-dom";
7
- import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, Minus, PencilSquare, EllipsisVertical, ArrowUpMini, ArrowDownMini } from "@medusajs/icons";
7
+ import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, EllipsisVertical, ArrowUpMini, ArrowDownMini, Minus, PencilSquare } from "@medusajs/icons";
8
8
  import Medusa from "@medusajs/js-sdk";
9
9
  import { format, formatDistance, sub, subDays, subMonths } from "date-fns";
10
10
  import { enUS } from "date-fns/locale";
@@ -9833,980 +9833,356 @@ const EmailForm = ({ order }) => {
9833
9833
  const schema$3 = objectType({
9834
9834
  email: stringType().email()
9835
9835
  });
9836
- const NumberInput = forwardRef(
9837
- ({
9838
- value,
9839
- onChange,
9840
- size = "base",
9841
- min = 0,
9842
- max = 100,
9843
- step = 1,
9844
- className,
9845
- disabled,
9846
- ...props
9847
- }, ref) => {
9848
- const handleChange = (event) => {
9849
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9850
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9851
- onChange(newValue);
9852
- }
9853
- };
9854
- const handleIncrement = () => {
9855
- const newValue = value + step;
9856
- if (max === void 0 || newValue <= max) {
9857
- onChange(newValue);
9858
- }
9859
- };
9860
- const handleDecrement = () => {
9861
- const newValue = value - step;
9862
- if (min === void 0 || newValue >= min) {
9863
- onChange(newValue);
9864
- }
9865
- };
9836
+ const InlineTip = forwardRef(
9837
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9838
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9866
9839
  return /* @__PURE__ */ jsxs(
9867
9840
  "div",
9868
9841
  {
9842
+ ref,
9869
9843
  className: clx(
9870
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9871
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9872
- {
9873
- "h-7": size === "small",
9874
- "h-8": size === "base"
9875
- },
9844
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9876
9845
  className
9877
9846
  ),
9847
+ ...props,
9878
9848
  children: [
9879
9849
  /* @__PURE__ */ jsx(
9880
- "input",
9881
- {
9882
- ref,
9883
- type: "number",
9884
- value,
9885
- onChange: handleChange,
9886
- min,
9887
- max,
9888
- step,
9889
- className: clx(
9890
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9891
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9892
- "placeholder:text-ui-fg-muted"
9893
- ),
9894
- ...props
9895
- }
9896
- ),
9897
- /* @__PURE__ */ jsxs(
9898
- "button",
9850
+ "div",
9899
9851
  {
9900
- className: clx(
9901
- "flex items-center justify-center outline-none transition-fg",
9902
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9903
- "focus:bg-ui-bg-field-component-hover",
9904
- "hover:bg-ui-bg-field-component-hover",
9905
- {
9906
- "size-7": size === "small",
9907
- "size-8": size === "base"
9908
- }
9909
- ),
9910
- type: "button",
9911
- onClick: handleDecrement,
9912
- disabled: min !== void 0 && value <= min || disabled,
9913
- children: [
9914
- /* @__PURE__ */ jsx(Minus, {}),
9915
- /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9916
- ]
9852
+ role: "presentation",
9853
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9854
+ "bg-ui-tag-orange-icon": variant === "warning"
9855
+ })
9917
9856
  }
9918
9857
  ),
9919
- /* @__PURE__ */ jsxs(
9920
- "button",
9921
- {
9922
- className: clx(
9923
- "flex items-center justify-center outline-none transition-fg",
9924
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9925
- "focus:bg-ui-bg-field-hover",
9926
- "hover:bg-ui-bg-field-hover",
9927
- {
9928
- "size-7": size === "small",
9929
- "size-8": size === "base"
9930
- }
9931
- ),
9932
- type: "button",
9933
- onClick: handleIncrement,
9934
- disabled: max !== void 0 && value >= max || disabled,
9935
- children: [
9936
- /* @__PURE__ */ jsx(Plus, {}),
9937
- /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9938
- ]
9939
- }
9940
- )
9858
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9859
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9860
+ labelValue,
9861
+ ":"
9862
+ ] }),
9863
+ " ",
9864
+ children
9865
+ ] })
9941
9866
  ]
9942
9867
  }
9943
9868
  );
9944
9869
  }
9945
9870
  );
9946
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9947
- const productVariantsQueryKeys = {
9948
- list: (query2) => [
9949
- PRODUCT_VARIANTS_QUERY_KEY,
9950
- query2 ? query2 : void 0
9951
- ]
9952
- };
9953
- const useProductVariants = (query2, options) => {
9954
- const { data, ...rest } = useQuery({
9955
- queryKey: productVariantsQueryKeys.list(query2),
9956
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9957
- ...options
9958
- });
9959
- return { ...data, ...rest };
9960
- };
9961
- const useCancelOrderEdit = ({ preview }) => {
9962
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9963
- const onCancel = useCallback(async () => {
9964
- if (!preview) {
9965
- return true;
9966
- }
9967
- let res = false;
9968
- await cancelOrderEdit(void 0, {
9969
- onError: (e) => {
9970
- toast.error(e.message);
9971
- },
9972
- onSuccess: () => {
9973
- res = true;
9974
- }
9975
- });
9976
- return res;
9977
- }, [preview, cancelOrderEdit]);
9978
- return { onCancel };
9979
- };
9980
- let IS_REQUEST_RUNNING = false;
9981
- const useInitiateOrderEdit = ({
9982
- preview
9983
- }) => {
9984
- const navigate = useNavigate();
9985
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
9986
- useEffect(() => {
9987
- async function run() {
9988
- if (IS_REQUEST_RUNNING || !preview) {
9989
- return;
9990
- }
9991
- if (preview.order_change) {
9992
- return;
9993
- }
9994
- IS_REQUEST_RUNNING = true;
9995
- await mutateAsync(void 0, {
9996
- onError: (e) => {
9997
- toast.error(e.message);
9998
- navigate(`/draft-orders/${preview.id}`, { replace: true });
9999
- return;
10000
- }
10001
- });
10002
- IS_REQUEST_RUNNING = false;
10003
- }
10004
- run();
10005
- }, [preview, navigate, mutateAsync]);
10006
- };
10007
- function convertNumber(value) {
10008
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10009
- }
10010
- const STACKED_MODAL_ID = "items_stacked_modal";
10011
- const Items = () => {
9871
+ InlineTip.displayName = "InlineTip";
9872
+ const MetadataFieldSchema = objectType({
9873
+ key: stringType(),
9874
+ disabled: booleanType().optional(),
9875
+ value: anyType()
9876
+ });
9877
+ const MetadataSchema = objectType({
9878
+ metadata: arrayType(MetadataFieldSchema)
9879
+ });
9880
+ const Metadata = () => {
10012
9881
  const { id } = useParams();
10013
- const {
10014
- order: preview,
10015
- isPending: isPreviewPending,
10016
- isError: isPreviewError,
10017
- error: previewError
10018
- } = useOrderPreview(id, void 0, {
10019
- placeholderData: keepPreviousData
9882
+ const { order, isPending, isError, error } = useOrder(id, {
9883
+ fields: "metadata"
10020
9884
  });
10021
- useInitiateOrderEdit({ preview });
10022
- const { draft_order, isPending, isError, error } = useDraftOrder(
10023
- id,
10024
- {
10025
- fields: "currency_code"
10026
- },
10027
- {
10028
- enabled: !!id
10029
- }
10030
- );
10031
- const { onCancel } = useCancelOrderEdit({ preview });
10032
9885
  if (isError) {
10033
9886
  throw error;
10034
9887
  }
10035
- if (isPreviewError) {
10036
- throw previewError;
10037
- }
10038
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10039
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
10040
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10041
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10042
- ] }) });
9888
+ const isReady = !isPending && !!order;
9889
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9890
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9891
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9892
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9893
+ ] }),
9894
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9895
+ ] });
10043
9896
  };
10044
- const ItemsForm = ({ preview, currencyCode }) => {
10045
- var _a;
10046
- const [isSubmitting, setIsSubmitting] = useState(false);
10047
- const [modalContent, setModalContent] = useState(
10048
- null
10049
- );
9897
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9898
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9899
+ const MetadataForm = ({ orderId, metadata }) => {
10050
9900
  const { handleSuccess } = useRouteModal();
10051
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10052
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10053
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10054
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10055
- const matches = useMemo(() => {
10056
- return matchSorter(preview.items, query2, {
10057
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10058
- });
10059
- }, [preview.items, query2]);
10060
- const onSubmit = async () => {
10061
- setIsSubmitting(true);
10062
- let requestSucceeded = false;
10063
- await requestOrderEdit(void 0, {
10064
- onError: (e) => {
10065
- toast.error(`Failed to request order edit: ${e.message}`);
10066
- },
10067
- onSuccess: () => {
10068
- requestSucceeded = true;
10069
- }
10070
- });
10071
- if (!requestSucceeded) {
10072
- setIsSubmitting(false);
10073
- return;
10074
- }
10075
- await confirmOrderEdit(void 0, {
10076
- onError: (e) => {
10077
- toast.error(`Failed to confirm order edit: ${e.message}`);
10078
- },
10079
- onSuccess: () => {
10080
- handleSuccess();
10081
- },
10082
- onSettled: () => {
10083
- setIsSubmitting(false);
10084
- }
10085
- });
10086
- };
10087
- const onKeyDown = useCallback(
10088
- (e) => {
10089
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10090
- if (modalContent || isSubmitting) {
10091
- return;
10092
- }
10093
- onSubmit();
10094
- }
10095
- },
10096
- [modalContent, isSubmitting, onSubmit]
10097
- );
10098
- useEffect(() => {
10099
- document.addEventListener("keydown", onKeyDown);
10100
- return () => {
10101
- document.removeEventListener("keydown", onKeyDown);
10102
- };
10103
- }, [onKeyDown]);
10104
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10105
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10106
- /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxs(
10107
- StackedFocusModal,
10108
- {
10109
- id: STACKED_MODAL_ID,
10110
- onOpenChangeCallback: (open) => {
10111
- if (!open) {
10112
- setModalContent(null);
10113
- }
10114
- },
10115
- children: [
10116
- /* @__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-6 py-16", children: [
10117
- /* @__PURE__ */ jsxs("div", { children: [
10118
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Items" }) }),
10119
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Edit the items in the draft order." }) })
10120
- ] }),
10121
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10122
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
10123
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10124
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10125
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10126
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10127
- ] }),
10128
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
10129
- /* @__PURE__ */ jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsx(
10130
- Input,
10131
- {
10132
- type: "search",
10133
- placeholder: "Search items",
10134
- value: searchValue,
10135
- onChange: (e) => onSearchValueChange(e.target.value)
10136
- }
10137
- ) }),
10138
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
10139
- /* @__PURE__ */ jsx(DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(IconButton, { type: "button", children: /* @__PURE__ */ jsx(Plus, {}) }) }),
10140
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
10141
- /* @__PURE__ */ jsx(
10142
- StackedModalTrigger$1,
10143
- {
10144
- type: "add-items",
10145
- setModalContent
10146
- }
10147
- ),
10148
- /* @__PURE__ */ jsx(
10149
- StackedModalTrigger$1,
10150
- {
10151
- type: "add-custom-item",
10152
- setModalContent
10153
- }
10154
- )
10155
- ] })
10156
- ] })
10157
- ] })
10158
- ] }),
10159
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10160
- /* @__PURE__ */ jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[1fr_1fr_1fr_28px] gap-3 px-4 py-2 text-ui-fg-muted", children: [
10161
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
10162
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10163
- /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Price" }) }),
10164
- /* @__PURE__ */ jsx("div", {})
10165
- ] }) }),
10166
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-x-3 bg-ui-bg-base rounded-lg p-4 shadow-elevation-card-rest flex-col gap-1", children: [
10167
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10168
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10169
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsx(
10170
- Item,
10171
- {
10172
- item,
10173
- preview,
10174
- currencyCode
10175
- },
10176
- item.id
10177
- )) : /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-x-3 bg-ui-bg-base rounded-lg p-4 shadow-elevation-card-rest flex-col gap-1", children: [
10178
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10179
- /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
10180
- 'No items found for "',
10181
- query2,
10182
- '".'
10183
- ] })
10184
- ] }) })
10185
- ] })
10186
- ] }),
10187
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10188
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10189
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10190
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsxs(
10191
- Text,
10192
- {
10193
- size: "small",
10194
- leading: "compact",
10195
- className: "text-ui-fg-subtle",
10196
- children: [
10197
- itemCount,
10198
- " ",
10199
- itemCount === 1 ? "item" : "items"
10200
- ]
10201
- }
10202
- ) }),
10203
- /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
10204
- ] })
10205
- ] }) }),
10206
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsx(
10207
- CustomItemForm,
10208
- {
10209
- orderId: preview.id,
10210
- currencyCode
10211
- }
10212
- ) : null)
10213
- ]
10214
- }
10215
- ) }),
10216
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
10217
- /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10218
- /* @__PURE__ */ jsx(
10219
- Button,
10220
- {
10221
- size: "small",
10222
- type: "button",
10223
- onClick: onSubmit,
10224
- isLoading: isSubmitting,
10225
- children: "Save"
10226
- }
10227
- )
10228
- ] }) })
10229
- ] });
10230
- };
10231
- const Item = ({ item, preview, currencyCode }) => {
10232
- if (item.variant_id) {
10233
- return /* @__PURE__ */ jsx(VariantItem, { item, preview, currencyCode });
10234
- }
10235
- return /* @__PURE__ */ jsx(CustomItem, { item, preview, currencyCode });
10236
- };
10237
- const VariantItem = ({ item, preview, currencyCode }) => {
10238
- const [editing, setEditing] = useState(false);
9901
+ const hasUneditableRows = getHasUneditableRows(metadata);
9902
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10239
9903
  const form = useForm({
10240
9904
  defaultValues: {
10241
- quantity: item.quantity,
10242
- unit_price: item.unit_price
9905
+ metadata: getDefaultValues(metadata)
10243
9906
  },
10244
- resolver: zodResolver(variantItemSchema)
9907
+ resolver: zodResolver(MetadataSchema)
10245
9908
  });
10246
- const actionId = useMemo(() => {
10247
- var _a, _b;
10248
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10249
- }, [item]);
10250
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10251
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10252
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10253
- const onSubmit = form.handleSubmit(async (data) => {
10254
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10255
- setEditing(false);
10256
- return;
10257
- }
10258
- if (!actionId) {
10259
- await updateOriginalItem(
10260
- {
10261
- item_id: item.id,
10262
- quantity: data.quantity,
10263
- unit_price: convertNumber(data.unit_price)
10264
- },
10265
- {
10266
- onSuccess: () => {
10267
- setEditing(false);
10268
- },
10269
- onError: (e) => {
10270
- toast.error(e.message);
10271
- }
10272
- }
10273
- );
10274
- return;
10275
- }
10276
- await updateActionItem(
9909
+ const handleSubmit = form.handleSubmit(async (data) => {
9910
+ const parsedData = parseValues(data);
9911
+ await mutateAsync(
10277
9912
  {
10278
- action_id: actionId,
10279
- quantity: data.quantity,
10280
- unit_price: convertNumber(data.unit_price)
9913
+ metadata: parsedData
10281
9914
  },
10282
9915
  {
10283
9916
  onSuccess: () => {
10284
- setEditing(false);
9917
+ toast.success("Metadata updated");
9918
+ handleSuccess();
10285
9919
  },
10286
- onError: (e) => {
10287
- toast.error(e.message);
9920
+ onError: (error) => {
9921
+ toast.error(error.message);
10288
9922
  }
10289
9923
  }
10290
9924
  );
10291
9925
  });
10292
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)_28px] gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center", children: [
10293
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full", children: [
10294
- /* @__PURE__ */ jsx(
10295
- Thumbnail,
10296
- {
10297
- thumbnail: item.thumbnail,
10298
- alt: item.product_title ?? void 0
10299
- }
10300
- ),
10301
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10302
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
10303
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10304
- /* @__PURE__ */ jsxs(
10305
- Text,
10306
- {
10307
- size: "small",
10308
- leading: "compact",
10309
- className: "text-ui-fg-subtle",
10310
- children: [
10311
- "(",
10312
- item.variant_title,
10313
- ")"
10314
- ]
10315
- }
10316
- )
10317
- ] }),
10318
- /* @__PURE__ */ jsx(
10319
- Text,
10320
- {
10321
- size: "small",
10322
- leading: "compact",
10323
- className: "text-ui-fg-subtle",
10324
- children: item.variant_sku
10325
- }
10326
- )
10327
- ] })
10328
- ] }),
10329
- editing ? /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(
10330
- Form$2.Field,
10331
- {
10332
- control: form.control,
10333
- name: "quantity",
10334
- render: ({ field }) => {
10335
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10336
- }
10337
- }
10338
- ) }) : /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
10339
- editing ? /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(
10340
- Form$2.Field,
10341
- {
10342
- control: form.control,
10343
- name: "unit_price",
10344
- render: ({ field: { onChange, ...field } }) => {
10345
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10346
- CurrencyInput,
10347
- {
10348
- ...field,
10349
- symbol: getNativeSymbol(currencyCode),
10350
- code: currencyCode,
10351
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10352
- }
10353
- ) }) });
10354
- }
10355
- }
10356
- ) }) : /* @__PURE__ */ jsx("div", { className: "flex-1 flex items-center justify-end w-full", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10357
- /* @__PURE__ */ jsx(
10358
- IconButton,
10359
- {
10360
- type: "button",
10361
- size: "small",
10362
- onClick: editing ? onSubmit : () => {
10363
- setEditing(true);
10364
- },
10365
- disabled: isPending,
10366
- children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10367
- }
10368
- )
10369
- ] }) }) });
10370
- };
10371
- const variantItemSchema = objectType({
10372
- quantity: numberType(),
10373
- unit_price: unionType([numberType(), stringType()])
10374
- });
10375
- const CustomItem = ({ item, preview, currencyCode }) => {
10376
- const [editing, setEditing] = useState(false);
10377
- const { quantity, unit_price, title } = item;
10378
- const form = useForm({
10379
- defaultValues: {
10380
- title,
10381
- quantity,
10382
- unit_price
10383
- },
10384
- resolver: zodResolver(customItemSchema)
9926
+ const { fields, insert, remove } = useFieldArray({
9927
+ control: form.control,
9928
+ name: "metadata"
10385
9929
  });
10386
- useEffect(() => {
10387
- form.reset({
10388
- title,
10389
- quantity,
10390
- unit_price
10391
- });
10392
- }, [form, title, quantity, unit_price]);
10393
- const actionId = useMemo(() => {
10394
- var _a, _b;
10395
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10396
- }, [item]);
10397
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10398
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10399
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10400
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10401
- const onSubmit = form.handleSubmit(async (data) => {
10402
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10403
- setEditing(false);
10404
- return;
10405
- }
10406
- if (!actionId) {
10407
- await updateOriginalItem(
10408
- {
10409
- item_id: item.id,
10410
- quantity: data.quantity,
10411
- unit_price: convertNumber(data.unit_price)
10412
- },
10413
- {
10414
- onSuccess: () => {
10415
- setEditing(false);
10416
- },
10417
- onError: (e) => {
10418
- toast.error(e.message);
10419
- }
10420
- }
10421
- );
10422
- return;
10423
- }
10424
- if (data.quantity === 0) {
10425
- await removeActionItem(actionId, {
10426
- onSuccess: () => {
10427
- setEditing(false);
10428
- },
10429
- onError: (e) => {
10430
- toast.error(e.message);
10431
- }
9930
+ function deleteRow(index) {
9931
+ remove(index);
9932
+ if (fields.length === 1) {
9933
+ insert(0, {
9934
+ key: "",
9935
+ value: "",
9936
+ disabled: false
10432
9937
  });
10433
- return;
10434
- }
10435
- await updateActionItem(
10436
- {
10437
- action_id: actionId,
10438
- quantity: data.quantity,
10439
- unit_price: convertNumber(data.unit_price)
10440
- },
10441
- {
10442
- onSuccess: () => {
10443
- setEditing(false);
10444
- },
10445
- onError: (e) => {
10446
- toast.error(e.message);
10447
- }
10448
- }
10449
- );
10450
- });
10451
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)_28px] gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center", children: [
10452
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10453
- /* @__PURE__ */ jsx(
10454
- Thumbnail,
10455
- {
10456
- thumbnail: item.thumbnail,
10457
- alt: item.title ?? void 0
10458
- }
10459
- ),
10460
- editing ? /* @__PURE__ */ jsx(
10461
- Form$2.Field,
10462
- {
10463
- control: form.control,
10464
- name: "title",
10465
- render: ({ field }) => {
10466
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
10467
- }
10468
- }
10469
- ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
10470
- ] }),
10471
- editing ? /* @__PURE__ */ jsx(
10472
- Form$2.Field,
10473
- {
10474
- control: form.control,
10475
- name: "quantity",
10476
- render: ({ field }) => {
10477
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10478
- }
10479
- }
10480
- ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
10481
- editing ? /* @__PURE__ */ jsx(
10482
- Form$2.Field,
10483
- {
10484
- control: form.control,
10485
- name: "unit_price",
10486
- render: ({ field: { onChange, ...field } }) => {
10487
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10488
- CurrencyInput,
10489
- {
10490
- ...field,
10491
- symbol: getNativeSymbol(currencyCode),
10492
- code: currencyCode,
10493
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10494
- }
10495
- ) }) });
10496
- }
10497
- }
10498
- ) : /* @__PURE__ */ jsx("div", { className: "flex-1 flex items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10499
- /* @__PURE__ */ jsx(
10500
- IconButton,
10501
- {
10502
- type: "button",
10503
- size: "small",
10504
- onClick: editing ? onSubmit : () => {
10505
- setEditing(true);
10506
- },
10507
- disabled: isPending,
10508
- children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10509
- }
10510
- )
10511
- ] }) }) });
10512
- };
10513
- const StackedModalTrigger$1 = ({
10514
- type,
10515
- setModalContent
10516
- }) => {
10517
- const { setIsOpen } = useStackedModal();
10518
- const onClick = useCallback(() => {
10519
- setModalContent(type);
10520
- setIsOpen(STACKED_MODAL_ID, true);
10521
- }, [setModalContent, setIsOpen, type]);
10522
- return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10523
- };
10524
- const VARIANT_PREFIX = "items";
10525
- const LIMIT = 50;
10526
- const ExistingItemsForm = ({ orderId, items }) => {
10527
- const { setIsOpen } = useStackedModal();
10528
- const [rowSelection, setRowSelection] = useState(
10529
- items.reduce((acc, item) => {
10530
- acc[item.variant_id] = true;
10531
- return acc;
10532
- }, {})
10533
- );
10534
- useEffect(() => {
10535
- setRowSelection(
10536
- items.reduce((acc, item) => {
10537
- if (item.variant_id) {
10538
- acc[item.variant_id] = true;
10539
- }
10540
- return acc;
10541
- }, {})
10542
- );
10543
- }, [items]);
10544
- const { q, order, offset } = useQueryParams(
10545
- ["q", "order", "offset"],
10546
- VARIANT_PREFIX
10547
- );
10548
- const { variants, count, isPending, isError, error } = useProductVariants(
10549
- {
10550
- q,
10551
- order,
10552
- offset: offset ? parseInt(offset) : void 0,
10553
- limit: LIMIT
10554
- },
10555
- {
10556
- placeholderData: keepPreviousData
10557
9938
  }
10558
- );
10559
- const columns = useColumns();
10560
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10561
- const onSubmit = async () => {
10562
- const ids = Object.keys(rowSelection).filter(
10563
- (id) => !items.find((i) => i.variant_id === id)
10564
- );
10565
- await mutateAsync(
10566
- {
10567
- items: ids.map((id) => ({
10568
- variant_id: id,
10569
- quantity: 1
10570
- }))
10571
- },
10572
- {
10573
- onSuccess: () => {
10574
- setRowSelection({});
10575
- setIsOpen(STACKED_MODAL_ID, false);
10576
- },
10577
- onError: (e) => {
10578
- toast.error(e.message);
10579
- }
10580
- }
10581
- );
10582
- };
10583
- if (isError) {
10584
- throw error;
10585
9939
  }
10586
- return /* @__PURE__ */ jsxs(
10587
- StackedFocusModal.Content,
9940
+ function insertRow(index, position) {
9941
+ insert(index + (position === "above" ? 0 : 1), {
9942
+ key: "",
9943
+ value: "",
9944
+ disabled: false
9945
+ });
9946
+ }
9947
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9948
+ KeyboundForm,
10588
9949
  {
10589
- onOpenAutoFocus: (e) => {
10590
- e.preventDefault();
10591
- const searchInput = document.querySelector(
10592
- "[data-modal-id='modal-search-input']"
10593
- );
10594
- if (searchInput) {
10595
- searchInput.focus();
10596
- }
10597
- },
9950
+ onSubmit: handleSubmit,
9951
+ className: "flex flex-1 flex-col overflow-hidden",
10598
9952
  children: [
10599
- /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
10600
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10601
- /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10602
- ] }),
10603
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
10604
- DataTable,
10605
- {
10606
- data: variants,
10607
- columns,
10608
- isLoading: isPending,
10609
- getRowId: (row) => row.id,
10610
- rowCount: count,
10611
- prefix: VARIANT_PREFIX,
10612
- layout: "fill",
10613
- rowSelection: {
10614
- state: rowSelection,
10615
- onRowSelectionChange: setRowSelection,
10616
- enableRowSelection: (row) => {
10617
- return !items.find((i) => i.variant_id === row.original.id);
9953
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9954
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9955
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9956
+ /* @__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" }) }),
9957
+ /* @__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" }) })
9958
+ ] }),
9959
+ fields.map((field, index) => {
9960
+ const isDisabled = field.disabled || false;
9961
+ let placeholder = "-";
9962
+ if (typeof field.value === "object") {
9963
+ placeholder = "{ ... }";
10618
9964
  }
10619
- },
10620
- autoFocusSearch: true
10621
- }
10622
- ) }),
10623
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
10624
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10625
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
9965
+ if (Array.isArray(field.value)) {
9966
+ placeholder = "[ ... ]";
9967
+ }
9968
+ return /* @__PURE__ */ jsx(
9969
+ ConditionalTooltip,
9970
+ {
9971
+ showTooltip: isDisabled,
9972
+ content: "This row is disabled because it contains non-primitive data.",
9973
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9974
+ /* @__PURE__ */ jsxs(
9975
+ "div",
9976
+ {
9977
+ className: clx("grid grid-cols-2 divide-x", {
9978
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9979
+ }),
9980
+ children: [
9981
+ /* @__PURE__ */ jsx(
9982
+ Form$2.Field,
9983
+ {
9984
+ control: form.control,
9985
+ name: `metadata.${index}.key`,
9986
+ render: ({ field: field2 }) => {
9987
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9988
+ GridInput,
9989
+ {
9990
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
9991
+ ...field2,
9992
+ disabled: isDisabled,
9993
+ placeholder: "Key"
9994
+ }
9995
+ ) }) });
9996
+ }
9997
+ }
9998
+ ),
9999
+ /* @__PURE__ */ jsx(
10000
+ Form$2.Field,
10001
+ {
10002
+ control: form.control,
10003
+ name: `metadata.${index}.value`,
10004
+ render: ({ field: { value, ...field2 } }) => {
10005
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10006
+ GridInput,
10007
+ {
10008
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
10009
+ ...field2,
10010
+ value: isDisabled ? placeholder : value,
10011
+ disabled: isDisabled,
10012
+ placeholder: "Value"
10013
+ }
10014
+ ) }) });
10015
+ }
10016
+ }
10017
+ )
10018
+ ]
10019
+ }
10020
+ ),
10021
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
10022
+ /* @__PURE__ */ jsx(
10023
+ DropdownMenu.Trigger,
10024
+ {
10025
+ className: clx(
10026
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
10027
+ {
10028
+ hidden: isDisabled
10029
+ }
10030
+ ),
10031
+ disabled: isDisabled,
10032
+ asChild: true,
10033
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
10034
+ }
10035
+ ),
10036
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
10037
+ /* @__PURE__ */ jsxs(
10038
+ DropdownMenu.Item,
10039
+ {
10040
+ className: "gap-x-2",
10041
+ onClick: () => insertRow(index, "above"),
10042
+ children: [
10043
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
10044
+ "Insert row above"
10045
+ ]
10046
+ }
10047
+ ),
10048
+ /* @__PURE__ */ jsxs(
10049
+ DropdownMenu.Item,
10050
+ {
10051
+ className: "gap-x-2",
10052
+ onClick: () => insertRow(index, "below"),
10053
+ children: [
10054
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
10055
+ "Insert row below"
10056
+ ]
10057
+ }
10058
+ ),
10059
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
10060
+ /* @__PURE__ */ jsxs(
10061
+ DropdownMenu.Item,
10062
+ {
10063
+ className: "gap-x-2",
10064
+ onClick: () => deleteRow(index),
10065
+ children: [
10066
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
10067
+ "Delete row"
10068
+ ]
10069
+ }
10070
+ )
10071
+ ] })
10072
+ ] })
10073
+ ] })
10074
+ },
10075
+ field.id
10076
+ );
10077
+ })
10078
+ ] }),
10079
+ 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." })
10080
+ ] }),
10081
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10082
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10083
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10626
10084
  ] }) })
10627
10085
  ]
10628
10086
  }
10087
+ ) });
10088
+ };
10089
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
10090
+ return /* @__PURE__ */ jsx(
10091
+ "input",
10092
+ {
10093
+ ref,
10094
+ ...props,
10095
+ autoComplete: "off",
10096
+ className: clx(
10097
+ "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",
10098
+ className
10099
+ )
10100
+ }
10629
10101
  );
10102
+ });
10103
+ GridInput.displayName = "MetadataForm.GridInput";
10104
+ const PlaceholderInner = () => {
10105
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10106
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10107
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10108
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
10109
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
10110
+ ] }) })
10111
+ ] });
10630
10112
  };
10631
- const columnHelper = createDataTableColumnHelper();
10632
- const useColumns = () => {
10633
- return useMemo(() => {
10113
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
10114
+ function getDefaultValues(metadata) {
10115
+ if (!metadata || !Object.keys(metadata).length) {
10634
10116
  return [
10635
- columnHelper.select(),
10636
- columnHelper.accessor("product.title", {
10637
- header: "Product",
10638
- cell: ({ row }) => {
10639
- var _a, _b, _c;
10640
- return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
10641
- /* @__PURE__ */ jsx(
10642
- Thumbnail,
10643
- {
10644
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10645
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10646
- }
10647
- ),
10648
- /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10649
- ] });
10650
- },
10651
- enableSorting: true
10652
- }),
10653
- columnHelper.accessor("title", {
10654
- header: "Variant",
10655
- enableSorting: true
10656
- }),
10657
- columnHelper.accessor("sku", {
10658
- header: "SKU",
10659
- cell: ({ getValue }) => {
10660
- return getValue() ?? "-";
10661
- },
10662
- enableSorting: true
10663
- }),
10664
- columnHelper.accessor("updated_at", {
10665
- header: "Updated",
10666
- cell: ({ getValue }) => {
10667
- return /* @__PURE__ */ jsx(
10668
- Tooltip,
10669
- {
10670
- content: getFullDate({ date: getValue(), includeTime: true }),
10671
- children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10672
- }
10673
- );
10674
- },
10675
- enableSorting: true,
10676
- sortAscLabel: "Oldest first",
10677
- sortDescLabel: "Newest first"
10678
- }),
10679
- columnHelper.accessor("created_at", {
10680
- header: "Created",
10681
- cell: ({ getValue }) => {
10682
- return /* @__PURE__ */ jsx(
10683
- Tooltip,
10684
- {
10685
- content: getFullDate({ date: getValue(), includeTime: true }),
10686
- children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10687
- }
10688
- );
10689
- },
10690
- enableSorting: true,
10691
- sortAscLabel: "Oldest first",
10692
- sortDescLabel: "Newest first"
10693
- })
10117
+ {
10118
+ key: "",
10119
+ value: "",
10120
+ disabled: false
10121
+ }
10694
10122
  ];
10695
- }, []);
10696
- };
10697
- const CustomItemForm = ({ orderId, currencyCode }) => {
10698
- const { setIsOpen } = useStackedModal();
10699
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10700
- const form = useForm({
10701
- defaultValues: {
10702
- title: "",
10703
- quantity: 1,
10704
- unit_price: ""
10705
- },
10706
- resolver: zodResolver(customItemSchema)
10123
+ }
10124
+ return Object.entries(metadata).map(([key, value]) => {
10125
+ if (!EDITABLE_TYPES.includes(typeof value)) {
10126
+ return {
10127
+ key,
10128
+ value,
10129
+ disabled: true
10130
+ };
10131
+ }
10132
+ let stringValue = value;
10133
+ if (typeof value !== "string") {
10134
+ stringValue = JSON.stringify(value);
10135
+ }
10136
+ return {
10137
+ key,
10138
+ value: stringValue,
10139
+ original_key: key
10140
+ };
10707
10141
  });
10708
- const onSubmit = form.handleSubmit(async (data) => {
10709
- await addItems(
10710
- {
10711
- items: [
10712
- {
10713
- title: data.title,
10714
- quantity: data.quantity,
10715
- unit_price: convertNumber(data.unit_price)
10716
- }
10717
- ]
10718
- },
10719
- {
10720
- onSuccess: () => {
10721
- setIsOpen(STACKED_MODAL_ID, false);
10722
- },
10723
- onError: (e) => {
10724
- toast.error(e.message);
10725
- }
10142
+ }
10143
+ function parseValues(values) {
10144
+ const metadata = values.metadata;
10145
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10146
+ if (isEmpty) {
10147
+ return null;
10148
+ }
10149
+ const update = {};
10150
+ metadata.forEach((field) => {
10151
+ let key = field.key;
10152
+ let value = field.value;
10153
+ const disabled = field.disabled;
10154
+ if (!key || !value) {
10155
+ return;
10156
+ }
10157
+ if (disabled) {
10158
+ update[key] = value;
10159
+ return;
10160
+ }
10161
+ key = key.trim();
10162
+ value = value.trim();
10163
+ if (value === "true") {
10164
+ update[key] = true;
10165
+ } else if (value === "false") {
10166
+ update[key] = false;
10167
+ } else {
10168
+ const parsedNumber = parseFloat(value);
10169
+ if (!isNaN(parsedNumber)) {
10170
+ update[key] = parsedNumber;
10171
+ } else {
10172
+ update[key] = value;
10726
10173
  }
10727
- );
10174
+ }
10728
10175
  });
10729
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
10730
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
10731
- /* @__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: [
10732
- /* @__PURE__ */ jsxs("div", { children: [
10733
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
10734
- /* @__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." }) })
10735
- ] }),
10736
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10737
- /* @__PURE__ */ jsx(
10738
- Form$2.Field,
10739
- {
10740
- control: form.control,
10741
- name: "title",
10742
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10743
- /* @__PURE__ */ jsxs("div", { children: [
10744
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
10745
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
10746
- ] }),
10747
- /* @__PURE__ */ jsxs("div", { children: [
10748
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10749
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10750
- ] })
10751
- ] }) })
10752
- }
10753
- ),
10754
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10755
- /* @__PURE__ */ jsx(
10756
- Form$2.Field,
10757
- {
10758
- control: form.control,
10759
- name: "unit_price",
10760
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10761
- /* @__PURE__ */ jsxs("div", { children: [
10762
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
10763
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10764
- ] }),
10765
- /* @__PURE__ */ jsxs("div", { children: [
10766
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10767
- CurrencyInput,
10768
- {
10769
- symbol: getNativeSymbol(currencyCode),
10770
- code: currencyCode,
10771
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10772
- ...field
10773
- }
10774
- ) }),
10775
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10776
- ] })
10777
- ] }) })
10778
- }
10779
- ),
10780
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10781
- /* @__PURE__ */ jsx(
10782
- Form$2.Field,
10783
- {
10784
- control: form.control,
10785
- name: "quantity",
10786
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10787
- /* @__PURE__ */ jsxs("div", { children: [
10788
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
10789
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10790
- ] }),
10791
- /* @__PURE__ */ jsxs("div", { className: "flex-1 w-full", children: [
10792
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10793
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10794
- ] })
10795
- ] }) })
10796
- }
10797
- )
10798
- ] }) }) }),
10799
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
10800
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10801
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10802
- ] }) })
10803
- ] }) }) });
10804
- };
10805
- const customItemSchema = objectType({
10806
- title: stringType().min(1),
10807
- quantity: numberType(),
10808
- unit_price: unionType([numberType(), stringType()])
10809
- });
10176
+ return update;
10177
+ }
10178
+ function getHasUneditableRows(metadata) {
10179
+ if (!metadata) {
10180
+ return false;
10181
+ }
10182
+ return Object.values(metadata).some(
10183
+ (value) => !EDITABLE_TYPES.includes(typeof value)
10184
+ );
10185
+ }
10810
10186
  const PROMOTION_QUERY_KEY = "promotions";
10811
10187
  const promotionsQueryKeys = {
10812
10188
  list: (query2) => [
@@ -10827,6 +10203,52 @@ const usePromotions = (query2, options) => {
10827
10203
  });
10828
10204
  return { ...data, ...rest };
10829
10205
  };
10206
+ const useCancelOrderEdit = ({ preview }) => {
10207
+ const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10208
+ const onCancel = useCallback(async () => {
10209
+ if (!preview) {
10210
+ return true;
10211
+ }
10212
+ let res = false;
10213
+ await cancelOrderEdit(void 0, {
10214
+ onError: (e) => {
10215
+ toast.error(e.message);
10216
+ },
10217
+ onSuccess: () => {
10218
+ res = true;
10219
+ }
10220
+ });
10221
+ return res;
10222
+ }, [preview, cancelOrderEdit]);
10223
+ return { onCancel };
10224
+ };
10225
+ let IS_REQUEST_RUNNING = false;
10226
+ const useInitiateOrderEdit = ({
10227
+ preview
10228
+ }) => {
10229
+ const navigate = useNavigate();
10230
+ const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10231
+ useEffect(() => {
10232
+ async function run() {
10233
+ if (IS_REQUEST_RUNNING || !preview) {
10234
+ return;
10235
+ }
10236
+ if (preview.order_change) {
10237
+ return;
10238
+ }
10239
+ IS_REQUEST_RUNNING = true;
10240
+ await mutateAsync(void 0, {
10241
+ onError: (e) => {
10242
+ toast.error(e.message);
10243
+ navigate(`/draft-orders/${preview.id}`, { replace: true });
10244
+ return;
10245
+ }
10246
+ });
10247
+ IS_REQUEST_RUNNING = false;
10248
+ }
10249
+ run();
10250
+ }, [preview, navigate, mutateAsync]);
10251
+ };
10830
10252
  const Promotions = () => {
10831
10253
  const { id } = useParams();
10832
10254
  const {
@@ -11190,6 +10612,9 @@ const SalesChannelField = ({ control, order }) => {
11190
10612
  const schema$2 = objectType({
11191
10613
  sales_channel_id: stringType().min(1)
11192
10614
  });
10615
+ function convertNumber(value) {
10616
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10617
+ }
11193
10618
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
11194
10619
  const Shipping = () => {
11195
10620
  var _a;
@@ -11497,7 +10922,7 @@ const ShippingForm = ({ preview, order }) => {
11497
10922
  ]
11498
10923
  }
11499
10924
  ) : /* @__PURE__ */ jsx(
11500
- StackedModalTrigger,
10925
+ StackedModalTrigger$1,
11501
10926
  {
11502
10927
  shippingProfileId: profile.id,
11503
10928
  shippingOption,
@@ -11608,7 +11033,7 @@ const ShippingForm = ({ preview, order }) => {
11608
11033
  ] }) })
11609
11034
  ] });
11610
11035
  };
11611
- const StackedModalTrigger = ({
11036
+ const StackedModalTrigger$1 = ({
11612
11037
  shippingProfileId,
11613
11038
  shippingOption,
11614
11039
  shippingMethod,
@@ -11983,223 +11408,20 @@ const CustomAmountField = ({
11983
11408
  /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
11984
11409
  /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
11985
11410
  ] }),
11986
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11987
- CurrencyInput,
11988
- {
11989
- ...field,
11990
- onValueChange: (value) => onChange(value),
11991
- symbol: getNativeSymbol(currencyCode),
11992
- code: currencyCode
11993
- }
11994
- ) })
11995
- ] });
11996
- }
11997
- }
11998
- );
11999
- };
12000
- const ShippingAddress = () => {
12001
- const { id } = useParams();
12002
- const { order, isPending, isError, error } = useOrder(id, {
12003
- fields: "+shipping_address"
12004
- });
12005
- if (isError) {
12006
- throw error;
12007
- }
12008
- const isReady = !isPending && !!order;
12009
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12010
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12011
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12012
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12013
- ] }),
12014
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12015
- ] });
12016
- };
12017
- const ShippingAddressForm = ({ order }) => {
12018
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12019
- const form = useForm({
12020
- defaultValues: {
12021
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12022
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12023
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12024
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12025
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12026
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12027
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12028
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12029
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12030
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12031
- },
12032
- resolver: zodResolver(schema$1)
12033
- });
12034
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12035
- const { handleSuccess } = useRouteModal();
12036
- const onSubmit = form.handleSubmit(async (data) => {
12037
- await mutateAsync(
12038
- {
12039
- shipping_address: {
12040
- first_name: data.first_name,
12041
- last_name: data.last_name,
12042
- company: data.company,
12043
- address_1: data.address_1,
12044
- address_2: data.address_2,
12045
- city: data.city,
12046
- province: data.province,
12047
- country_code: data.country_code,
12048
- postal_code: data.postal_code,
12049
- phone: data.phone
12050
- }
12051
- },
12052
- {
12053
- onSuccess: () => {
12054
- handleSuccess();
12055
- },
12056
- onError: (error) => {
12057
- toast.error(error.message);
12058
- }
12059
- }
12060
- );
12061
- });
12062
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12063
- KeyboundForm,
12064
- {
12065
- className: "flex flex-1 flex-col overflow-hidden",
12066
- onSubmit,
12067
- children: [
12068
- /* @__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: [
12069
- /* @__PURE__ */ jsx(
12070
- Form$2.Field,
12071
- {
12072
- control: form.control,
12073
- name: "country_code",
12074
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12075
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12076
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12077
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12078
- ] })
12079
- }
12080
- ),
12081
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12082
- /* @__PURE__ */ jsx(
12083
- Form$2.Field,
12084
- {
12085
- control: form.control,
12086
- name: "first_name",
12087
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12088
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12089
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12090
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12091
- ] })
12092
- }
12093
- ),
12094
- /* @__PURE__ */ jsx(
12095
- Form$2.Field,
12096
- {
12097
- control: form.control,
12098
- name: "last_name",
12099
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12100
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12101
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12102
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12103
- ] })
12104
- }
12105
- )
12106
- ] }),
12107
- /* @__PURE__ */ jsx(
12108
- Form$2.Field,
12109
- {
12110
- control: form.control,
12111
- name: "company",
12112
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12113
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12114
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12115
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12116
- ] })
12117
- }
12118
- ),
12119
- /* @__PURE__ */ jsx(
12120
- Form$2.Field,
12121
- {
12122
- control: form.control,
12123
- name: "address_1",
12124
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12125
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12126
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12127
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12128
- ] })
12129
- }
12130
- ),
12131
- /* @__PURE__ */ jsx(
12132
- Form$2.Field,
12133
- {
12134
- control: form.control,
12135
- name: "address_2",
12136
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12137
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12138
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12139
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12140
- ] })
12141
- }
12142
- ),
12143
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12144
- /* @__PURE__ */ jsx(
12145
- Form$2.Field,
12146
- {
12147
- control: form.control,
12148
- name: "postal_code",
12149
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12150
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12151
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12152
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12153
- ] })
12154
- }
12155
- ),
12156
- /* @__PURE__ */ jsx(
12157
- Form$2.Field,
12158
- {
12159
- control: form.control,
12160
- name: "city",
12161
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12162
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12163
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12164
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12165
- ] })
12166
- }
12167
- )
12168
- ] }),
12169
- /* @__PURE__ */ jsx(
12170
- Form$2.Field,
12171
- {
12172
- control: form.control,
12173
- name: "province",
12174
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12175
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12176
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12177
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12178
- ] })
12179
- }
12180
- ),
12181
- /* @__PURE__ */ jsx(
12182
- Form$2.Field,
12183
- {
12184
- control: form.control,
12185
- name: "phone",
12186
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12187
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12188
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12189
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12190
- ] })
11411
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11412
+ CurrencyInput,
11413
+ {
11414
+ ...field,
11415
+ onValueChange: (value) => onChange(value),
11416
+ symbol: getNativeSymbol(currencyCode),
11417
+ code: currencyCode
12191
11418
  }
12192
- )
12193
- ] }) }),
12194
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12195
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12196
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12197
- ] }) })
12198
- ]
11419
+ ) })
11420
+ ] });
11421
+ }
12199
11422
  }
12200
- ) });
11423
+ );
12201
11424
  };
12202
- const schema$1 = addressSchema;
12203
11425
  const TransferOwnership = () => {
12204
11426
  const { id } = useParams();
12205
11427
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12223,7 +11445,7 @@ const TransferOwnershipForm = ({ order }) => {
12223
11445
  defaultValues: {
12224
11446
  customer_id: order.customer_id || ""
12225
11447
  },
12226
- resolver: zodResolver(schema)
11448
+ resolver: zodResolver(schema$1)
12227
11449
  });
12228
11450
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12229
11451
  const { handleSuccess } = useRouteModal();
@@ -12622,410 +11844,1188 @@ const Illustration = () => {
12622
11844
  fill: "white",
12623
11845
  transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12624
11846
  }
12625
- ) }),
12626
- /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
12627
- "rect",
11847
+ ) }),
11848
+ /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
11849
+ "rect",
11850
+ {
11851
+ width: "12",
11852
+ height: "12",
11853
+ fill: "white",
11854
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
11855
+ }
11856
+ ) }),
11857
+ /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
11858
+ "rect",
11859
+ {
11860
+ width: "12",
11861
+ height: "12",
11862
+ fill: "white",
11863
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
11864
+ }
11865
+ ) }),
11866
+ /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
11867
+ "rect",
11868
+ {
11869
+ width: "12",
11870
+ height: "12",
11871
+ fill: "white",
11872
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
11873
+ }
11874
+ ) }),
11875
+ /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
11876
+ "rect",
11877
+ {
11878
+ width: "12",
11879
+ height: "12",
11880
+ fill: "white",
11881
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
11882
+ }
11883
+ ) }),
11884
+ /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
11885
+ "rect",
11886
+ {
11887
+ width: "12",
11888
+ height: "12",
11889
+ fill: "white",
11890
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
11891
+ }
11892
+ ) })
11893
+ ] })
11894
+ ]
11895
+ }
11896
+ );
11897
+ };
11898
+ const schema$1 = objectType({
11899
+ customer_id: stringType().min(1)
11900
+ });
11901
+ const ShippingAddress = () => {
11902
+ const { id } = useParams();
11903
+ const { order, isPending, isError, error } = useOrder(id, {
11904
+ fields: "+shipping_address"
11905
+ });
11906
+ if (isError) {
11907
+ throw error;
11908
+ }
11909
+ const isReady = !isPending && !!order;
11910
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11911
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11912
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
11913
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11914
+ ] }),
11915
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
11916
+ ] });
11917
+ };
11918
+ const ShippingAddressForm = ({ order }) => {
11919
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11920
+ const form = useForm({
11921
+ defaultValues: {
11922
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11923
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11924
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11925
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11926
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11927
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11928
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11929
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11930
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11931
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11932
+ },
11933
+ resolver: zodResolver(schema)
11934
+ });
11935
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11936
+ const { handleSuccess } = useRouteModal();
11937
+ const onSubmit = form.handleSubmit(async (data) => {
11938
+ await mutateAsync(
11939
+ {
11940
+ shipping_address: {
11941
+ first_name: data.first_name,
11942
+ last_name: data.last_name,
11943
+ company: data.company,
11944
+ address_1: data.address_1,
11945
+ address_2: data.address_2,
11946
+ city: data.city,
11947
+ province: data.province,
11948
+ country_code: data.country_code,
11949
+ postal_code: data.postal_code,
11950
+ phone: data.phone
11951
+ }
11952
+ },
11953
+ {
11954
+ onSuccess: () => {
11955
+ handleSuccess();
11956
+ },
11957
+ onError: (error) => {
11958
+ toast.error(error.message);
11959
+ }
11960
+ }
11961
+ );
11962
+ });
11963
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11964
+ KeyboundForm,
11965
+ {
11966
+ className: "flex flex-1 flex-col overflow-hidden",
11967
+ onSubmit,
11968
+ children: [
11969
+ /* @__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: [
11970
+ /* @__PURE__ */ jsx(
11971
+ Form$2.Field,
11972
+ {
11973
+ control: form.control,
11974
+ name: "country_code",
11975
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11976
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
11977
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
11978
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11979
+ ] })
11980
+ }
11981
+ ),
11982
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11983
+ /* @__PURE__ */ jsx(
11984
+ Form$2.Field,
11985
+ {
11986
+ control: form.control,
11987
+ name: "first_name",
11988
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11989
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
11990
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11991
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11992
+ ] })
11993
+ }
11994
+ ),
11995
+ /* @__PURE__ */ jsx(
11996
+ Form$2.Field,
11997
+ {
11998
+ control: form.control,
11999
+ name: "last_name",
12000
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12001
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12002
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12003
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12004
+ ] })
12005
+ }
12006
+ )
12007
+ ] }),
12008
+ /* @__PURE__ */ jsx(
12009
+ Form$2.Field,
12010
+ {
12011
+ control: form.control,
12012
+ name: "company",
12013
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12014
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12015
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12016
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12017
+ ] })
12018
+ }
12019
+ ),
12020
+ /* @__PURE__ */ jsx(
12021
+ Form$2.Field,
12022
+ {
12023
+ control: form.control,
12024
+ name: "address_1",
12025
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12026
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12027
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12028
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12029
+ ] })
12030
+ }
12031
+ ),
12032
+ /* @__PURE__ */ jsx(
12033
+ Form$2.Field,
12034
+ {
12035
+ control: form.control,
12036
+ name: "address_2",
12037
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12038
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12039
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12040
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12041
+ ] })
12042
+ }
12043
+ ),
12044
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12045
+ /* @__PURE__ */ jsx(
12046
+ Form$2.Field,
12047
+ {
12048
+ control: form.control,
12049
+ name: "postal_code",
12050
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12051
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12052
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12053
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12054
+ ] })
12055
+ }
12056
+ ),
12057
+ /* @__PURE__ */ jsx(
12058
+ Form$2.Field,
12059
+ {
12060
+ control: form.control,
12061
+ name: "city",
12062
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12063
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12064
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12065
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12066
+ ] })
12067
+ }
12068
+ )
12069
+ ] }),
12070
+ /* @__PURE__ */ jsx(
12071
+ Form$2.Field,
12072
+ {
12073
+ control: form.control,
12074
+ name: "province",
12075
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12076
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12077
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12078
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12079
+ ] })
12080
+ }
12081
+ ),
12082
+ /* @__PURE__ */ jsx(
12083
+ Form$2.Field,
12084
+ {
12085
+ control: form.control,
12086
+ name: "phone",
12087
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12088
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12089
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12090
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12091
+ ] })
12092
+ }
12093
+ )
12094
+ ] }) }),
12095
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12096
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12097
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12098
+ ] }) })
12099
+ ]
12100
+ }
12101
+ ) });
12102
+ };
12103
+ const schema = addressSchema;
12104
+ const NumberInput = forwardRef(
12105
+ ({
12106
+ value,
12107
+ onChange,
12108
+ size = "base",
12109
+ min = 0,
12110
+ max = 100,
12111
+ step = 1,
12112
+ className,
12113
+ disabled,
12114
+ ...props
12115
+ }, ref) => {
12116
+ const handleChange = (event) => {
12117
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
12118
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
12119
+ onChange(newValue);
12120
+ }
12121
+ };
12122
+ const handleIncrement = () => {
12123
+ const newValue = value + step;
12124
+ if (max === void 0 || newValue <= max) {
12125
+ onChange(newValue);
12126
+ }
12127
+ };
12128
+ const handleDecrement = () => {
12129
+ const newValue = value - step;
12130
+ if (min === void 0 || newValue >= min) {
12131
+ onChange(newValue);
12132
+ }
12133
+ };
12134
+ return /* @__PURE__ */ jsxs(
12135
+ "div",
12136
+ {
12137
+ className: clx(
12138
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
12139
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
12140
+ {
12141
+ "h-7": size === "small",
12142
+ "h-8": size === "base"
12143
+ },
12144
+ className
12145
+ ),
12146
+ children: [
12147
+ /* @__PURE__ */ jsx(
12148
+ "input",
12628
12149
  {
12629
- width: "12",
12630
- height: "12",
12631
- fill: "white",
12632
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12150
+ ref,
12151
+ type: "number",
12152
+ value,
12153
+ onChange: handleChange,
12154
+ min,
12155
+ max,
12156
+ step,
12157
+ className: clx(
12158
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
12159
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
12160
+ "placeholder:text-ui-fg-muted"
12161
+ ),
12162
+ ...props
12633
12163
  }
12634
- ) }),
12635
- /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
12636
- "rect",
12164
+ ),
12165
+ /* @__PURE__ */ jsxs(
12166
+ "button",
12637
12167
  {
12638
- width: "12",
12639
- height: "12",
12640
- fill: "white",
12641
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12168
+ className: clx(
12169
+ "flex items-center justify-center outline-none transition-fg",
12170
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12171
+ "focus:bg-ui-bg-field-component-hover",
12172
+ "hover:bg-ui-bg-field-component-hover",
12173
+ {
12174
+ "size-7": size === "small",
12175
+ "size-8": size === "base"
12176
+ }
12177
+ ),
12178
+ type: "button",
12179
+ onClick: handleDecrement,
12180
+ disabled: min !== void 0 && value <= min || disabled,
12181
+ children: [
12182
+ /* @__PURE__ */ jsx(Minus, {}),
12183
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
12184
+ ]
12642
12185
  }
12643
- ) }),
12644
- /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
12645
- "rect",
12186
+ ),
12187
+ /* @__PURE__ */ jsxs(
12188
+ "button",
12646
12189
  {
12647
- width: "12",
12648
- height: "12",
12649
- fill: "white",
12650
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12190
+ className: clx(
12191
+ "flex items-center justify-center outline-none transition-fg",
12192
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12193
+ "focus:bg-ui-bg-field-hover",
12194
+ "hover:bg-ui-bg-field-hover",
12195
+ {
12196
+ "size-7": size === "small",
12197
+ "size-8": size === "base"
12198
+ }
12199
+ ),
12200
+ type: "button",
12201
+ onClick: handleIncrement,
12202
+ disabled: max !== void 0 && value >= max || disabled,
12203
+ children: [
12204
+ /* @__PURE__ */ jsx(Plus, {}),
12205
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
12206
+ ]
12651
12207
  }
12652
- ) }),
12653
- /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
12654
- "rect",
12208
+ )
12209
+ ]
12210
+ }
12211
+ );
12212
+ }
12213
+ );
12214
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
12215
+ const productVariantsQueryKeys = {
12216
+ list: (query2) => [
12217
+ PRODUCT_VARIANTS_QUERY_KEY,
12218
+ query2 ? query2 : void 0
12219
+ ]
12220
+ };
12221
+ const useProductVariants = (query2, options) => {
12222
+ const { data, ...rest } = useQuery({
12223
+ queryKey: productVariantsQueryKeys.list(query2),
12224
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
12225
+ ...options
12226
+ });
12227
+ return { ...data, ...rest };
12228
+ };
12229
+ const STACKED_MODAL_ID = "items_stacked_modal";
12230
+ const Items = () => {
12231
+ const { id } = useParams();
12232
+ const {
12233
+ order: preview,
12234
+ isPending: isPreviewPending,
12235
+ isError: isPreviewError,
12236
+ error: previewError
12237
+ } = useOrderPreview(id, void 0, {
12238
+ placeholderData: keepPreviousData
12239
+ });
12240
+ useInitiateOrderEdit({ preview });
12241
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12242
+ id,
12243
+ {
12244
+ fields: "currency_code"
12245
+ },
12246
+ {
12247
+ enabled: !!id
12248
+ }
12249
+ );
12250
+ const { onCancel } = useCancelOrderEdit({ preview });
12251
+ if (isError) {
12252
+ throw error;
12253
+ }
12254
+ if (isPreviewError) {
12255
+ throw previewError;
12256
+ }
12257
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
12258
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
12259
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
12260
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
12261
+ ] }) });
12262
+ };
12263
+ const ItemsForm = ({ preview, currencyCode }) => {
12264
+ var _a;
12265
+ const [isSubmitting, setIsSubmitting] = useState(false);
12266
+ const [modalContent, setModalContent] = useState(
12267
+ null
12268
+ );
12269
+ const { handleSuccess } = useRouteModal();
12270
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
12271
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12272
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
12273
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
12274
+ const matches = useMemo(() => {
12275
+ return matchSorter(preview.items, query2, {
12276
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
12277
+ });
12278
+ }, [preview.items, query2]);
12279
+ const onSubmit = async () => {
12280
+ setIsSubmitting(true);
12281
+ let requestSucceeded = false;
12282
+ await requestOrderEdit(void 0, {
12283
+ onError: (e) => {
12284
+ toast.error(`Failed to request order edit: ${e.message}`);
12285
+ },
12286
+ onSuccess: () => {
12287
+ requestSucceeded = true;
12288
+ }
12289
+ });
12290
+ if (!requestSucceeded) {
12291
+ setIsSubmitting(false);
12292
+ return;
12293
+ }
12294
+ await confirmOrderEdit(void 0, {
12295
+ onError: (e) => {
12296
+ toast.error(`Failed to confirm order edit: ${e.message}`);
12297
+ },
12298
+ onSuccess: () => {
12299
+ handleSuccess();
12300
+ },
12301
+ onSettled: () => {
12302
+ setIsSubmitting(false);
12303
+ }
12304
+ });
12305
+ };
12306
+ const onKeyDown = useCallback(
12307
+ (e) => {
12308
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
12309
+ if (modalContent || isSubmitting) {
12310
+ return;
12311
+ }
12312
+ onSubmit();
12313
+ }
12314
+ },
12315
+ [modalContent, isSubmitting, onSubmit]
12316
+ );
12317
+ useEffect(() => {
12318
+ document.addEventListener("keydown", onKeyDown);
12319
+ return () => {
12320
+ document.removeEventListener("keydown", onKeyDown);
12321
+ };
12322
+ }, [onKeyDown]);
12323
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
12324
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
12325
+ /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxs(
12326
+ StackedFocusModal,
12327
+ {
12328
+ id: STACKED_MODAL_ID,
12329
+ onOpenChangeCallback: (open) => {
12330
+ if (!open) {
12331
+ setModalContent(null);
12332
+ }
12333
+ },
12334
+ children: [
12335
+ /* @__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-6 py-16", children: [
12336
+ /* @__PURE__ */ jsxs("div", { children: [
12337
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Items" }) }),
12338
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Edit the items in the draft order." }) })
12339
+ ] }),
12340
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12341
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12342
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
12343
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12344
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
12345
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
12346
+ ] }),
12347
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
12348
+ /* @__PURE__ */ jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsx(
12349
+ Input,
12350
+ {
12351
+ type: "search",
12352
+ placeholder: "Search items",
12353
+ value: searchValue,
12354
+ onChange: (e) => onSearchValueChange(e.target.value)
12355
+ }
12356
+ ) }),
12357
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
12358
+ /* @__PURE__ */ jsx(DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(IconButton, { type: "button", children: /* @__PURE__ */ jsx(Plus, {}) }) }),
12359
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
12360
+ /* @__PURE__ */ jsx(
12361
+ StackedModalTrigger,
12362
+ {
12363
+ type: "add-items",
12364
+ setModalContent
12365
+ }
12366
+ ),
12367
+ /* @__PURE__ */ jsx(
12368
+ StackedModalTrigger,
12369
+ {
12370
+ type: "add-custom-item",
12371
+ setModalContent
12372
+ }
12373
+ )
12374
+ ] })
12375
+ ] })
12376
+ ] })
12377
+ ] }),
12378
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12379
+ /* @__PURE__ */ jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[1fr_1fr_1fr_28px] gap-3 px-4 py-2 text-ui-fg-muted", children: [
12380
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12381
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) }),
12382
+ /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Price" }) }),
12383
+ /* @__PURE__ */ jsx("div", {})
12384
+ ] }) }),
12385
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-x-3 bg-ui-bg-base rounded-lg p-4 shadow-elevation-card-rest flex-col gap-1", children: [
12386
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
12387
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
12388
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsx(
12389
+ Item,
12390
+ {
12391
+ item,
12392
+ preview,
12393
+ currencyCode
12394
+ },
12395
+ item.id
12396
+ )) : /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-center gap-x-3 bg-ui-bg-base rounded-lg p-4 shadow-elevation-card-rest flex-col gap-1", children: [
12397
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12398
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12399
+ 'No items found for "',
12400
+ query2,
12401
+ '".'
12402
+ ] })
12403
+ ] }) })
12404
+ ] })
12405
+ ] }),
12406
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12407
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
12408
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
12409
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsxs(
12410
+ Text,
12411
+ {
12412
+ size: "small",
12413
+ leading: "compact",
12414
+ className: "text-ui-fg-subtle",
12415
+ children: [
12416
+ itemCount,
12417
+ " ",
12418
+ itemCount === 1 ? "item" : "items"
12419
+ ]
12420
+ }
12421
+ ) }),
12422
+ /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
12423
+ ] })
12424
+ ] }) }),
12425
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsx(
12426
+ CustomItemForm,
12655
12427
  {
12656
- width: "12",
12657
- height: "12",
12658
- fill: "white",
12659
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12428
+ orderId: preview.id,
12429
+ currencyCode
12660
12430
  }
12661
- ) }),
12662
- /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
12663
- "rect",
12431
+ ) : null)
12432
+ ]
12433
+ }
12434
+ ) }),
12435
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
12436
+ /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12437
+ /* @__PURE__ */ jsx(
12438
+ Button,
12439
+ {
12440
+ size: "small",
12441
+ type: "button",
12442
+ onClick: onSubmit,
12443
+ isLoading: isSubmitting,
12444
+ children: "Save"
12445
+ }
12446
+ )
12447
+ ] }) })
12448
+ ] });
12449
+ };
12450
+ const Item = ({ item, preview, currencyCode }) => {
12451
+ if (item.variant_id) {
12452
+ return /* @__PURE__ */ jsx(VariantItem, { item, preview, currencyCode });
12453
+ }
12454
+ return /* @__PURE__ */ jsx(CustomItem, { item, preview, currencyCode });
12455
+ };
12456
+ const VariantItem = ({ item, preview, currencyCode }) => {
12457
+ const [editing, setEditing] = useState(false);
12458
+ const form = useForm({
12459
+ defaultValues: {
12460
+ quantity: item.quantity,
12461
+ unit_price: item.unit_price
12462
+ },
12463
+ resolver: zodResolver(variantItemSchema)
12464
+ });
12465
+ const actionId = useMemo(() => {
12466
+ var _a, _b;
12467
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12468
+ }, [item]);
12469
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12470
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12471
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12472
+ const onSubmit = form.handleSubmit(async (data) => {
12473
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
12474
+ setEditing(false);
12475
+ return;
12476
+ }
12477
+ if (!actionId) {
12478
+ await updateOriginalItem(
12479
+ {
12480
+ item_id: item.id,
12481
+ quantity: data.quantity,
12482
+ unit_price: convertNumber(data.unit_price)
12483
+ },
12484
+ {
12485
+ onSuccess: () => {
12486
+ setEditing(false);
12487
+ },
12488
+ onError: (e) => {
12489
+ toast.error(e.message);
12490
+ }
12491
+ }
12492
+ );
12493
+ return;
12494
+ }
12495
+ await updateActionItem(
12496
+ {
12497
+ action_id: actionId,
12498
+ quantity: data.quantity,
12499
+ unit_price: convertNumber(data.unit_price)
12500
+ },
12501
+ {
12502
+ onSuccess: () => {
12503
+ setEditing(false);
12504
+ },
12505
+ onError: (e) => {
12506
+ toast.error(e.message);
12507
+ }
12508
+ }
12509
+ );
12510
+ });
12511
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)_28px] gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center", children: [
12512
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3 w-full", children: [
12513
+ /* @__PURE__ */ jsx(
12514
+ Thumbnail,
12515
+ {
12516
+ thumbnail: item.thumbnail,
12517
+ alt: item.product_title ?? void 0
12518
+ }
12519
+ ),
12520
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12521
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12522
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12523
+ /* @__PURE__ */ jsxs(
12524
+ Text,
12664
12525
  {
12665
- width: "12",
12666
- height: "12",
12667
- fill: "white",
12668
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12526
+ size: "small",
12527
+ leading: "compact",
12528
+ className: "text-ui-fg-subtle",
12529
+ children: [
12530
+ "(",
12531
+ item.variant_title,
12532
+ ")"
12533
+ ]
12669
12534
  }
12670
- ) })
12671
- ] })
12672
- ]
12673
- }
12674
- );
12675
- };
12676
- const schema = objectType({
12677
- customer_id: stringType().min(1)
12678
- });
12679
- const InlineTip = forwardRef(
12680
- ({ variant = "tip", label, className, children, ...props }, ref) => {
12681
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
12682
- return /* @__PURE__ */ jsxs(
12683
- "div",
12535
+ )
12536
+ ] }),
12537
+ /* @__PURE__ */ jsx(
12538
+ Text,
12539
+ {
12540
+ size: "small",
12541
+ leading: "compact",
12542
+ className: "text-ui-fg-subtle",
12543
+ children: item.variant_sku
12544
+ }
12545
+ )
12546
+ ] })
12547
+ ] }),
12548
+ editing ? /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(
12549
+ Form$2.Field,
12550
+ {
12551
+ control: form.control,
12552
+ name: "quantity",
12553
+ render: ({ field }) => {
12554
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
12555
+ }
12556
+ }
12557
+ ) }) : /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
12558
+ editing ? /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(
12559
+ Form$2.Field,
12684
12560
  {
12685
- ref,
12686
- className: clx(
12687
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
12688
- className
12689
- ),
12690
- ...props,
12691
- children: [
12692
- /* @__PURE__ */ jsx(
12693
- "div",
12561
+ control: form.control,
12562
+ name: "unit_price",
12563
+ render: ({ field: { onChange, ...field } }) => {
12564
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12565
+ CurrencyInput,
12694
12566
  {
12695
- role: "presentation",
12696
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
12697
- "bg-ui-tag-orange-icon": variant === "warning"
12698
- })
12567
+ ...field,
12568
+ symbol: getNativeSymbol(currencyCode),
12569
+ code: currencyCode,
12570
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12699
12571
  }
12700
- ),
12701
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
12702
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
12703
- labelValue,
12704
- ":"
12705
- ] }),
12706
- " ",
12707
- children
12708
- ] })
12709
- ]
12572
+ ) }) });
12573
+ }
12710
12574
  }
12711
- );
12712
- }
12713
- );
12714
- InlineTip.displayName = "InlineTip";
12715
- const MetadataFieldSchema = objectType({
12716
- key: stringType(),
12717
- disabled: booleanType().optional(),
12718
- value: anyType()
12719
- });
12720
- const MetadataSchema = objectType({
12721
- metadata: arrayType(MetadataFieldSchema)
12575
+ ) }) : /* @__PURE__ */ jsx("div", { className: "flex-1 flex items-center justify-end w-full", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12576
+ /* @__PURE__ */ jsx(
12577
+ IconButton,
12578
+ {
12579
+ type: "button",
12580
+ size: "small",
12581
+ onClick: editing ? onSubmit : () => {
12582
+ setEditing(true);
12583
+ },
12584
+ disabled: isPending,
12585
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
12586
+ }
12587
+ )
12588
+ ] }) }) });
12589
+ };
12590
+ const variantItemSchema = objectType({
12591
+ quantity: numberType(),
12592
+ unit_price: unionType([numberType(), stringType()])
12722
12593
  });
12723
- const Metadata = () => {
12724
- const { id } = useParams();
12725
- const { order, isPending, isError, error } = useOrder(id, {
12726
- fields: "metadata"
12594
+ const CustomItem = ({ item, preview, currencyCode }) => {
12595
+ const [editing, setEditing] = useState(false);
12596
+ const { quantity, unit_price, title } = item;
12597
+ const form = useForm({
12598
+ defaultValues: {
12599
+ title,
12600
+ quantity,
12601
+ unit_price
12602
+ },
12603
+ resolver: zodResolver(customItemSchema)
12727
12604
  });
12728
- if (isError) {
12729
- throw error;
12730
- }
12731
- const isReady = !isPending && !!order;
12732
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12733
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12734
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
12735
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
12605
+ useEffect(() => {
12606
+ form.reset({
12607
+ title,
12608
+ quantity,
12609
+ unit_price
12610
+ });
12611
+ }, [form, title, quantity, unit_price]);
12612
+ const actionId = useMemo(() => {
12613
+ var _a, _b;
12614
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12615
+ }, [item]);
12616
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12617
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
12618
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12619
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12620
+ const onSubmit = form.handleSubmit(async (data) => {
12621
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
12622
+ setEditing(false);
12623
+ return;
12624
+ }
12625
+ if (!actionId) {
12626
+ await updateOriginalItem(
12627
+ {
12628
+ item_id: item.id,
12629
+ quantity: data.quantity,
12630
+ unit_price: convertNumber(data.unit_price)
12631
+ },
12632
+ {
12633
+ onSuccess: () => {
12634
+ setEditing(false);
12635
+ },
12636
+ onError: (e) => {
12637
+ toast.error(e.message);
12638
+ }
12639
+ }
12640
+ );
12641
+ return;
12642
+ }
12643
+ if (data.quantity === 0) {
12644
+ await removeActionItem(actionId, {
12645
+ onSuccess: () => {
12646
+ setEditing(false);
12647
+ },
12648
+ onError: (e) => {
12649
+ toast.error(e.message);
12650
+ }
12651
+ });
12652
+ return;
12653
+ }
12654
+ await updateActionItem(
12655
+ {
12656
+ action_id: actionId,
12657
+ quantity: data.quantity,
12658
+ unit_price: convertNumber(data.unit_price)
12659
+ },
12660
+ {
12661
+ onSuccess: () => {
12662
+ setEditing(false);
12663
+ },
12664
+ onError: (e) => {
12665
+ toast.error(e.message);
12666
+ }
12667
+ }
12668
+ );
12669
+ });
12670
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[minmax(0,1fr)_minmax(0,1fr)_minmax(0,1fr)_28px] gap-3 px-4 py-2 bg-ui-bg-base shadow-elevation-card-rest rounded-lg items-center", children: [
12671
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12672
+ /* @__PURE__ */ jsx(
12673
+ Thumbnail,
12674
+ {
12675
+ thumbnail: item.thumbnail,
12676
+ alt: item.title ?? void 0
12677
+ }
12678
+ ),
12679
+ editing ? /* @__PURE__ */ jsx(
12680
+ Form$2.Field,
12681
+ {
12682
+ control: form.control,
12683
+ name: "title",
12684
+ render: ({ field }) => {
12685
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
12686
+ }
12687
+ }
12688
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
12736
12689
  ] }),
12737
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
12738
- ] });
12690
+ editing ? /* @__PURE__ */ jsx(
12691
+ Form$2.Field,
12692
+ {
12693
+ control: form.control,
12694
+ name: "quantity",
12695
+ render: ({ field }) => {
12696
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
12697
+ }
12698
+ }
12699
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
12700
+ editing ? /* @__PURE__ */ jsx(
12701
+ Form$2.Field,
12702
+ {
12703
+ control: form.control,
12704
+ name: "unit_price",
12705
+ render: ({ field: { onChange, ...field } }) => {
12706
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12707
+ CurrencyInput,
12708
+ {
12709
+ ...field,
12710
+ symbol: getNativeSymbol(currencyCode),
12711
+ code: currencyCode,
12712
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12713
+ }
12714
+ ) }) });
12715
+ }
12716
+ }
12717
+ ) : /* @__PURE__ */ jsx("div", { className: "flex-1 flex items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12718
+ /* @__PURE__ */ jsx(
12719
+ IconButton,
12720
+ {
12721
+ type: "button",
12722
+ size: "small",
12723
+ onClick: editing ? onSubmit : () => {
12724
+ setEditing(true);
12725
+ },
12726
+ disabled: isPending,
12727
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
12728
+ }
12729
+ )
12730
+ ] }) }) });
12739
12731
  };
12740
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
12741
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
12742
- const MetadataForm = ({ orderId, metadata }) => {
12743
- const { handleSuccess } = useRouteModal();
12744
- const hasUneditableRows = getHasUneditableRows(metadata);
12745
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
12746
- const form = useForm({
12747
- defaultValues: {
12748
- metadata: getDefaultValues(metadata)
12732
+ const StackedModalTrigger = ({
12733
+ type,
12734
+ setModalContent
12735
+ }) => {
12736
+ const { setIsOpen } = useStackedModal();
12737
+ const onClick = useCallback(() => {
12738
+ setModalContent(type);
12739
+ setIsOpen(STACKED_MODAL_ID, true);
12740
+ }, [setModalContent, setIsOpen, type]);
12741
+ return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
12742
+ };
12743
+ const VARIANT_PREFIX = "items";
12744
+ const LIMIT = 50;
12745
+ const ExistingItemsForm = ({ orderId, items }) => {
12746
+ const { setIsOpen } = useStackedModal();
12747
+ const [rowSelection, setRowSelection] = useState(
12748
+ items.reduce((acc, item) => {
12749
+ acc[item.variant_id] = true;
12750
+ return acc;
12751
+ }, {})
12752
+ );
12753
+ useEffect(() => {
12754
+ setRowSelection(
12755
+ items.reduce((acc, item) => {
12756
+ if (item.variant_id) {
12757
+ acc[item.variant_id] = true;
12758
+ }
12759
+ return acc;
12760
+ }, {})
12761
+ );
12762
+ }, [items]);
12763
+ const { q, order, offset } = useQueryParams(
12764
+ ["q", "order", "offset"],
12765
+ VARIANT_PREFIX
12766
+ );
12767
+ const { variants, count, isPending, isError, error } = useProductVariants(
12768
+ {
12769
+ q,
12770
+ order,
12771
+ offset: offset ? parseInt(offset) : void 0,
12772
+ limit: LIMIT
12749
12773
  },
12750
- resolver: zodResolver(MetadataSchema)
12751
- });
12752
- const handleSubmit = form.handleSubmit(async (data) => {
12753
- const parsedData = parseValues(data);
12774
+ {
12775
+ placeholderData: keepPreviousData
12776
+ }
12777
+ );
12778
+ const columns = useColumns();
12779
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
12780
+ const onSubmit = async () => {
12781
+ const ids = Object.keys(rowSelection).filter(
12782
+ (id) => !items.find((i) => i.variant_id === id)
12783
+ );
12754
12784
  await mutateAsync(
12755
12785
  {
12756
- metadata: parsedData
12786
+ items: ids.map((id) => ({
12787
+ variant_id: id,
12788
+ quantity: 1
12789
+ }))
12757
12790
  },
12758
12791
  {
12759
12792
  onSuccess: () => {
12760
- toast.success("Metadata updated");
12761
- handleSuccess();
12793
+ setRowSelection({});
12794
+ setIsOpen(STACKED_MODAL_ID, false);
12762
12795
  },
12763
- onError: (error) => {
12764
- toast.error(error.message);
12796
+ onError: (e) => {
12797
+ toast.error(e.message);
12765
12798
  }
12766
12799
  }
12767
12800
  );
12768
- });
12769
- const { fields, insert, remove } = useFieldArray({
12770
- control: form.control,
12771
- name: "metadata"
12772
- });
12773
- function deleteRow(index) {
12774
- remove(index);
12775
- if (fields.length === 1) {
12776
- insert(0, {
12777
- key: "",
12778
- value: "",
12779
- disabled: false
12780
- });
12781
- }
12782
- }
12783
- function insertRow(index, position) {
12784
- insert(index + (position === "above" ? 0 : 1), {
12785
- key: "",
12786
- value: "",
12787
- disabled: false
12788
- });
12801
+ };
12802
+ if (isError) {
12803
+ throw error;
12789
12804
  }
12790
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12791
- KeyboundForm,
12805
+ return /* @__PURE__ */ jsxs(
12806
+ StackedFocusModal.Content,
12792
12807
  {
12793
- onSubmit: handleSubmit,
12794
- className: "flex flex-1 flex-col overflow-hidden",
12808
+ onOpenAutoFocus: (e) => {
12809
+ e.preventDefault();
12810
+ const searchInput = document.querySelector(
12811
+ "[data-modal-id='modal-search-input']"
12812
+ );
12813
+ if (searchInput) {
12814
+ searchInput.focus();
12815
+ }
12816
+ },
12795
12817
  children: [
12796
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
12797
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
12798
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
12799
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
12800
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
12801
- ] }),
12802
- fields.map((field, index) => {
12803
- const isDisabled = field.disabled || false;
12804
- let placeholder = "-";
12805
- if (typeof field.value === "object") {
12806
- placeholder = "{ ... }";
12807
- }
12808
- if (Array.isArray(field.value)) {
12809
- placeholder = "[ ... ]";
12810
- }
12811
- return /* @__PURE__ */ jsx(
12812
- ConditionalTooltip,
12813
- {
12814
- showTooltip: isDisabled,
12815
- content: "This row is disabled because it contains non-primitive data.",
12816
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
12817
- /* @__PURE__ */ jsxs(
12818
- "div",
12819
- {
12820
- className: clx("grid grid-cols-2 divide-x", {
12821
- "overflow-hidden rounded-b-lg": index === fields.length - 1
12822
- }),
12823
- children: [
12824
- /* @__PURE__ */ jsx(
12825
- Form$2.Field,
12826
- {
12827
- control: form.control,
12828
- name: `metadata.${index}.key`,
12829
- render: ({ field: field2 }) => {
12830
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12831
- GridInput,
12832
- {
12833
- "aria-labelledby": METADATA_KEY_LABEL_ID,
12834
- ...field2,
12835
- disabled: isDisabled,
12836
- placeholder: "Key"
12837
- }
12838
- ) }) });
12839
- }
12840
- }
12841
- ),
12842
- /* @__PURE__ */ jsx(
12843
- Form$2.Field,
12844
- {
12845
- control: form.control,
12846
- name: `metadata.${index}.value`,
12847
- render: ({ field: { value, ...field2 } }) => {
12848
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12849
- GridInput,
12850
- {
12851
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
12852
- ...field2,
12853
- value: isDisabled ? placeholder : value,
12854
- disabled: isDisabled,
12855
- placeholder: "Value"
12856
- }
12857
- ) }) });
12858
- }
12859
- }
12860
- )
12861
- ]
12862
- }
12863
- ),
12864
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
12865
- /* @__PURE__ */ jsx(
12866
- DropdownMenu.Trigger,
12867
- {
12868
- className: clx(
12869
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
12870
- {
12871
- hidden: isDisabled
12872
- }
12873
- ),
12874
- disabled: isDisabled,
12875
- asChild: true,
12876
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
12877
- }
12878
- ),
12879
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
12880
- /* @__PURE__ */ jsxs(
12881
- DropdownMenu.Item,
12882
- {
12883
- className: "gap-x-2",
12884
- onClick: () => insertRow(index, "above"),
12885
- children: [
12886
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
12887
- "Insert row above"
12888
- ]
12889
- }
12890
- ),
12891
- /* @__PURE__ */ jsxs(
12892
- DropdownMenu.Item,
12893
- {
12894
- className: "gap-x-2",
12895
- onClick: () => insertRow(index, "below"),
12896
- children: [
12897
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
12898
- "Insert row below"
12899
- ]
12900
- }
12901
- ),
12902
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
12903
- /* @__PURE__ */ jsxs(
12904
- DropdownMenu.Item,
12905
- {
12906
- className: "gap-x-2",
12907
- onClick: () => deleteRow(index),
12908
- children: [
12909
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
12910
- "Delete row"
12911
- ]
12912
- }
12913
- )
12914
- ] })
12915
- ] })
12916
- ] })
12917
- },
12918
- field.id
12919
- );
12920
- })
12921
- ] }),
12922
- hasUneditableRows && /* @__PURE__ */ jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
12818
+ /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
12819
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
12820
+ /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
12923
12821
  ] }),
12924
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12925
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12926
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12822
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
12823
+ DataTable,
12824
+ {
12825
+ data: variants,
12826
+ columns,
12827
+ isLoading: isPending,
12828
+ getRowId: (row) => row.id,
12829
+ rowCount: count,
12830
+ prefix: VARIANT_PREFIX,
12831
+ layout: "fill",
12832
+ rowSelection: {
12833
+ state: rowSelection,
12834
+ onRowSelectionChange: setRowSelection,
12835
+ enableRowSelection: (row) => {
12836
+ return !items.find((i) => i.variant_id === row.original.id);
12837
+ }
12838
+ },
12839
+ autoFocusSearch: true
12840
+ }
12841
+ ) }),
12842
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
12843
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12844
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
12927
12845
  ] }) })
12928
12846
  ]
12929
12847
  }
12930
- ) });
12931
- };
12932
- const GridInput = forwardRef(({ className, ...props }, ref) => {
12933
- return /* @__PURE__ */ jsx(
12934
- "input",
12935
- {
12936
- ref,
12937
- ...props,
12938
- autoComplete: "off",
12939
- className: clx(
12940
- "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
12941
- className
12942
- )
12943
- }
12944
12848
  );
12945
- });
12946
- GridInput.displayName = "MetadataForm.GridInput";
12947
- const PlaceholderInner = () => {
12948
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
12949
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
12950
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12951
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
12952
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
12953
- ] }) })
12954
- ] });
12955
- };
12956
- const EDITABLE_TYPES = ["string", "number", "boolean"];
12957
- function getDefaultValues(metadata) {
12958
- if (!metadata || !Object.keys(metadata).length) {
12849
+ };
12850
+ const columnHelper = createDataTableColumnHelper();
12851
+ const useColumns = () => {
12852
+ return useMemo(() => {
12959
12853
  return [
12960
- {
12961
- key: "",
12962
- value: "",
12963
- disabled: false
12964
- }
12854
+ columnHelper.select(),
12855
+ columnHelper.accessor("product.title", {
12856
+ header: "Product",
12857
+ cell: ({ row }) => {
12858
+ var _a, _b, _c;
12859
+ return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
12860
+ /* @__PURE__ */ jsx(
12861
+ Thumbnail,
12862
+ {
12863
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
12864
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
12865
+ }
12866
+ ),
12867
+ /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
12868
+ ] });
12869
+ },
12870
+ enableSorting: true
12871
+ }),
12872
+ columnHelper.accessor("title", {
12873
+ header: "Variant",
12874
+ enableSorting: true
12875
+ }),
12876
+ columnHelper.accessor("sku", {
12877
+ header: "SKU",
12878
+ cell: ({ getValue }) => {
12879
+ return getValue() ?? "-";
12880
+ },
12881
+ enableSorting: true
12882
+ }),
12883
+ columnHelper.accessor("updated_at", {
12884
+ header: "Updated",
12885
+ cell: ({ getValue }) => {
12886
+ return /* @__PURE__ */ jsx(
12887
+ Tooltip,
12888
+ {
12889
+ content: getFullDate({ date: getValue(), includeTime: true }),
12890
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
12891
+ }
12892
+ );
12893
+ },
12894
+ enableSorting: true,
12895
+ sortAscLabel: "Oldest first",
12896
+ sortDescLabel: "Newest first"
12897
+ }),
12898
+ columnHelper.accessor("created_at", {
12899
+ header: "Created",
12900
+ cell: ({ getValue }) => {
12901
+ return /* @__PURE__ */ jsx(
12902
+ Tooltip,
12903
+ {
12904
+ content: getFullDate({ date: getValue(), includeTime: true }),
12905
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
12906
+ }
12907
+ );
12908
+ },
12909
+ enableSorting: true,
12910
+ sortAscLabel: "Oldest first",
12911
+ sortDescLabel: "Newest first"
12912
+ })
12965
12913
  ];
12966
- }
12967
- return Object.entries(metadata).map(([key, value]) => {
12968
- if (!EDITABLE_TYPES.includes(typeof value)) {
12969
- return {
12970
- key,
12971
- value,
12972
- disabled: true
12973
- };
12974
- }
12975
- let stringValue = value;
12976
- if (typeof value !== "string") {
12977
- stringValue = JSON.stringify(value);
12978
- }
12979
- return {
12980
- key,
12981
- value: stringValue,
12982
- original_key: key
12983
- };
12914
+ }, []);
12915
+ };
12916
+ const CustomItemForm = ({ orderId, currencyCode }) => {
12917
+ const { setIsOpen } = useStackedModal();
12918
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
12919
+ const form = useForm({
12920
+ defaultValues: {
12921
+ title: "",
12922
+ quantity: 1,
12923
+ unit_price: ""
12924
+ },
12925
+ resolver: zodResolver(customItemSchema)
12984
12926
  });
12985
- }
12986
- function parseValues(values) {
12987
- const metadata = values.metadata;
12988
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
12989
- if (isEmpty) {
12990
- return null;
12991
- }
12992
- const update = {};
12993
- metadata.forEach((field) => {
12994
- let key = field.key;
12995
- let value = field.value;
12996
- const disabled = field.disabled;
12997
- if (!key || !value) {
12998
- return;
12999
- }
13000
- if (disabled) {
13001
- update[key] = value;
13002
- return;
13003
- }
13004
- key = key.trim();
13005
- value = value.trim();
13006
- if (value === "true") {
13007
- update[key] = true;
13008
- } else if (value === "false") {
13009
- update[key] = false;
13010
- } else {
13011
- const parsedNumber = parseFloat(value);
13012
- if (!isNaN(parsedNumber)) {
13013
- update[key] = parsedNumber;
13014
- } else {
13015
- update[key] = value;
12927
+ const onSubmit = form.handleSubmit(async (data) => {
12928
+ await addItems(
12929
+ {
12930
+ items: [
12931
+ {
12932
+ title: data.title,
12933
+ quantity: data.quantity,
12934
+ unit_price: convertNumber(data.unit_price)
12935
+ }
12936
+ ]
12937
+ },
12938
+ {
12939
+ onSuccess: () => {
12940
+ setIsOpen(STACKED_MODAL_ID, false);
12941
+ },
12942
+ onError: (e) => {
12943
+ toast.error(e.message);
12944
+ }
13016
12945
  }
13017
- }
12946
+ );
13018
12947
  });
13019
- return update;
13020
- }
13021
- function getHasUneditableRows(metadata) {
13022
- if (!metadata) {
13023
- return false;
13024
- }
13025
- return Object.values(metadata).some(
13026
- (value) => !EDITABLE_TYPES.includes(typeof value)
13027
- );
13028
- }
12948
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
12949
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12950
+ /* @__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: [
12951
+ /* @__PURE__ */ jsxs("div", { children: [
12952
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
12953
+ /* @__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." }) })
12954
+ ] }),
12955
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12956
+ /* @__PURE__ */ jsx(
12957
+ Form$2.Field,
12958
+ {
12959
+ control: form.control,
12960
+ name: "title",
12961
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12962
+ /* @__PURE__ */ jsxs("div", { children: [
12963
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
12964
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
12965
+ ] }),
12966
+ /* @__PURE__ */ jsxs("div", { children: [
12967
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12968
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12969
+ ] })
12970
+ ] }) })
12971
+ }
12972
+ ),
12973
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12974
+ /* @__PURE__ */ jsx(
12975
+ Form$2.Field,
12976
+ {
12977
+ control: form.control,
12978
+ name: "unit_price",
12979
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12980
+ /* @__PURE__ */ jsxs("div", { children: [
12981
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
12982
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
12983
+ ] }),
12984
+ /* @__PURE__ */ jsxs("div", { children: [
12985
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12986
+ CurrencyInput,
12987
+ {
12988
+ symbol: getNativeSymbol(currencyCode),
12989
+ code: currencyCode,
12990
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
12991
+ ...field
12992
+ }
12993
+ ) }),
12994
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12995
+ ] })
12996
+ ] }) })
12997
+ }
12998
+ ),
12999
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
13000
+ /* @__PURE__ */ jsx(
13001
+ Form$2.Field,
13002
+ {
13003
+ control: form.control,
13004
+ name: "quantity",
13005
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
13006
+ /* @__PURE__ */ jsxs("div", { children: [
13007
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
13008
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
13009
+ ] }),
13010
+ /* @__PURE__ */ jsxs("div", { className: "flex-1 w-full", children: [
13011
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
13012
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13013
+ ] })
13014
+ ] }) })
13015
+ }
13016
+ )
13017
+ ] }) }) }),
13018
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
13019
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
13020
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
13021
+ ] }) })
13022
+ ] }) }) });
13023
+ };
13024
+ const customItemSchema = objectType({
13025
+ title: stringType().min(1),
13026
+ quantity: numberType(),
13027
+ unit_price: unionType([numberType(), stringType()])
13028
+ });
13029
13029
  const widgetModule = { widgets: [] };
13030
13030
  const routeModule = {
13031
13031
  routes: [
@@ -13059,8 +13059,8 @@ const routeModule = {
13059
13059
  path: "/draft-orders/:id/email"
13060
13060
  },
13061
13061
  {
13062
- Component: Items,
13063
- path: "/draft-orders/:id/items"
13062
+ Component: Metadata,
13063
+ path: "/draft-orders/:id/metadata"
13064
13064
  },
13065
13065
  {
13066
13066
  Component: Promotions,
@@ -13074,17 +13074,17 @@ const routeModule = {
13074
13074
  Component: Shipping,
13075
13075
  path: "/draft-orders/:id/shipping"
13076
13076
  },
13077
- {
13078
- Component: ShippingAddress,
13079
- path: "/draft-orders/:id/shipping-address"
13080
- },
13081
13077
  {
13082
13078
  Component: TransferOwnership,
13083
13079
  path: "/draft-orders/:id/transfer-ownership"
13084
13080
  },
13085
13081
  {
13086
- Component: Metadata,
13087
- path: "/draft-orders/:id/metadata"
13082
+ Component: ShippingAddress,
13083
+ path: "/draft-orders/:id/shipping-address"
13084
+ },
13085
+ {
13086
+ Component: Items,
13087
+ path: "/draft-orders/:id/items"
13088
13088
  }
13089
13089
  ]
13090
13090
  }