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