@medusajs/draft-order 2.11.0-preview-20251018060206 → 2.11.0-preview-20251018120201

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ import { Tooltip, DropdownMenu, clx, IconButton, useDataTable, DataTable as Data
4
4
  import { useQuery, useQueryClient, useMutation, keepPreviousData, useInfiniteQuery } from "@tanstack/react-query";
5
5
  import React, { useState, useCallback, useMemo, Fragment, createContext, forwardRef, useId, useContext, useTransition, useRef, useImperativeHandle, useDeferredValue, useEffect, Suspense } from "react";
6
6
  import { useSearchParams, Link, useNavigate, Outlet, useBlocker, useLocation, useParams } from "react-router-dom";
7
- import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, Minus, PencilSquare, EllipsisVertical, ArrowUpMini, ArrowDownMini } from "@medusajs/icons";
7
+ import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, EllipsisVertical, ArrowUpMini, ArrowDownMini, Minus, PencilSquare } from "@medusajs/icons";
8
8
  import Medusa from "@medusajs/js-sdk";
9
9
  import { format, formatDistance, sub, subDays, subMonths } from "date-fns";
10
10
  import { enUS } from "date-fns/locale";
@@ -9846,1429 +9846,859 @@ const EmailForm = ({ order }) => {
9846
9846
  const schema$3 = objectType({
9847
9847
  email: stringType().email()
9848
9848
  });
9849
- const NumberInput = forwardRef(
9850
- ({
9851
- value,
9852
- onChange,
9853
- size = "base",
9854
- min = 0,
9855
- max = 100,
9856
- step = 1,
9857
- className,
9858
- disabled,
9859
- ...props
9860
- }, ref) => {
9861
- const handleChange = (event) => {
9862
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9863
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9864
- onChange(newValue);
9865
- }
9866
- };
9867
- const handleIncrement = () => {
9868
- const newValue = value + step;
9869
- if (max === void 0 || newValue <= max) {
9870
- onChange(newValue);
9871
- }
9872
- };
9873
- const handleDecrement = () => {
9874
- const newValue = value - step;
9875
- if (min === void 0 || newValue >= min) {
9876
- onChange(newValue);
9877
- }
9878
- };
9849
+ const InlineTip = forwardRef(
9850
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9851
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9879
9852
  return /* @__PURE__ */ jsxs(
9880
9853
  "div",
9881
9854
  {
9855
+ ref,
9882
9856
  className: clx(
9883
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9884
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9885
- {
9886
- "h-7": size === "small",
9887
- "h-8": size === "base"
9888
- },
9857
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9889
9858
  className
9890
9859
  ),
9860
+ ...props,
9891
9861
  children: [
9892
9862
  /* @__PURE__ */ jsx(
9893
- "input",
9894
- {
9895
- ref,
9896
- type: "number",
9897
- value,
9898
- onChange: handleChange,
9899
- min,
9900
- max,
9901
- step,
9902
- className: clx(
9903
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9904
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9905
- "placeholder:text-ui-fg-muted"
9906
- ),
9907
- ...props
9908
- }
9909
- ),
9910
- /* @__PURE__ */ jsxs(
9911
- "button",
9863
+ "div",
9912
9864
  {
9913
- className: clx(
9914
- "flex items-center justify-center outline-none transition-fg",
9915
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9916
- "focus:bg-ui-bg-field-component-hover",
9917
- "hover:bg-ui-bg-field-component-hover",
9918
- {
9919
- "size-7": size === "small",
9920
- "size-8": size === "base"
9921
- }
9922
- ),
9923
- type: "button",
9924
- onClick: handleDecrement,
9925
- disabled: min !== void 0 && value <= min || disabled,
9926
- children: [
9927
- /* @__PURE__ */ jsx(Minus, {}),
9928
- /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9929
- ]
9865
+ role: "presentation",
9866
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9867
+ "bg-ui-tag-orange-icon": variant === "warning"
9868
+ })
9930
9869
  }
9931
9870
  ),
9932
- /* @__PURE__ */ jsxs(
9933
- "button",
9934
- {
9935
- className: clx(
9936
- "flex items-center justify-center outline-none transition-fg",
9937
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9938
- "focus:bg-ui-bg-field-hover",
9939
- "hover:bg-ui-bg-field-hover",
9940
- {
9941
- "size-7": size === "small",
9942
- "size-8": size === "base"
9943
- }
9944
- ),
9945
- type: "button",
9946
- onClick: handleIncrement,
9947
- disabled: max !== void 0 && value >= max || disabled,
9948
- children: [
9949
- /* @__PURE__ */ jsx(Plus, {}),
9950
- /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9951
- ]
9952
- }
9953
- )
9871
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9872
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9873
+ labelValue,
9874
+ ":"
9875
+ ] }),
9876
+ " ",
9877
+ children
9878
+ ] })
9954
9879
  ]
9955
9880
  }
9956
9881
  );
9957
9882
  }
9958
9883
  );
9959
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9960
- const productVariantsQueryKeys = {
9961
- list: (query2) => [
9962
- PRODUCT_VARIANTS_QUERY_KEY,
9963
- query2 ? query2 : void 0
9964
- ]
9965
- };
9966
- const useProductVariants = (query2, options) => {
9967
- const { data, ...rest } = useQuery({
9968
- queryKey: productVariantsQueryKeys.list(query2),
9969
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9970
- ...options
9971
- });
9972
- return { ...data, ...rest };
9973
- };
9974
- const useCancelOrderEdit = ({ preview }) => {
9975
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9976
- const onCancel = useCallback(async () => {
9977
- if (!preview) {
9978
- return true;
9979
- }
9980
- let res = false;
9981
- await cancelOrderEdit(void 0, {
9982
- onError: (e) => {
9983
- toast.error(e.message);
9984
- },
9985
- onSuccess: () => {
9986
- res = true;
9987
- }
9988
- });
9989
- return res;
9990
- }, [preview, cancelOrderEdit]);
9991
- return { onCancel };
9992
- };
9993
- let IS_REQUEST_RUNNING = false;
9994
- const useInitiateOrderEdit = ({
9995
- preview
9996
- }) => {
9997
- const navigate = useNavigate();
9998
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
9999
- useEffect(() => {
10000
- async function run() {
10001
- if (IS_REQUEST_RUNNING || !preview) {
10002
- return;
10003
- }
10004
- if (preview.order_change) {
10005
- return;
10006
- }
10007
- IS_REQUEST_RUNNING = true;
10008
- await mutateAsync(void 0, {
10009
- onError: (e) => {
10010
- toast.error(e.message);
10011
- navigate(`/draft-orders/${preview.id}`, { replace: true });
10012
- return;
10013
- }
10014
- });
10015
- IS_REQUEST_RUNNING = false;
10016
- }
10017
- run();
10018
- }, [preview, navigate, mutateAsync]);
10019
- };
10020
- function convertNumber(value) {
10021
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10022
- }
10023
- const STACKED_MODAL_ID = "items_stacked_modal";
10024
- const Items = () => {
9884
+ InlineTip.displayName = "InlineTip";
9885
+ const MetadataFieldSchema = objectType({
9886
+ key: stringType(),
9887
+ disabled: booleanType().optional(),
9888
+ value: anyType()
9889
+ });
9890
+ const MetadataSchema = objectType({
9891
+ metadata: arrayType(MetadataFieldSchema)
9892
+ });
9893
+ const Metadata = () => {
10025
9894
  const { id } = useParams();
10026
- const {
10027
- order: preview,
10028
- isPending: isPreviewPending,
10029
- isError: isPreviewError,
10030
- error: previewError
10031
- } = useOrderPreview(id, void 0, {
10032
- placeholderData: keepPreviousData
9895
+ const { order, isPending, isError, error } = useOrder(id, {
9896
+ fields: "metadata"
10033
9897
  });
10034
- useInitiateOrderEdit({ preview });
10035
- const { draft_order, isPending, isError, error } = useDraftOrder(
10036
- id,
10037
- {
10038
- fields: "currency_code"
10039
- },
10040
- {
10041
- enabled: !!id
10042
- }
10043
- );
10044
- const { onCancel } = useCancelOrderEdit({ preview });
10045
9898
  if (isError) {
10046
9899
  throw error;
10047
9900
  }
10048
- if (isPreviewError) {
10049
- throw previewError;
10050
- }
10051
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10052
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
10053
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10054
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10055
- ] }) });
9901
+ const isReady = !isPending && !!order;
9902
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9903
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9904
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9905
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9906
+ ] }),
9907
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9908
+ ] });
10056
9909
  };
10057
- const ItemsForm = ({ preview, currencyCode }) => {
10058
- var _a;
10059
- const [isSubmitting, setIsSubmitting] = useState(false);
10060
- const [modalContent, setModalContent] = useState(
10061
- null
10062
- );
9910
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9911
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9912
+ const MetadataForm = ({ orderId, metadata }) => {
10063
9913
  const { handleSuccess } = useRouteModal();
10064
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10065
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10066
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10067
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10068
- const matches = useMemo(() => {
10069
- return matchSorter(preview.items, query2, {
10070
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10071
- });
10072
- }, [preview.items, query2]);
10073
- const onSubmit = async () => {
10074
- setIsSubmitting(true);
10075
- let requestSucceeded = false;
10076
- await requestOrderEdit(void 0, {
10077
- onError: (e) => {
10078
- toast.error(`Failed to request order edit: ${e.message}`);
10079
- },
10080
- onSuccess: () => {
10081
- requestSucceeded = true;
10082
- }
10083
- });
10084
- if (!requestSucceeded) {
10085
- setIsSubmitting(false);
10086
- return;
10087
- }
10088
- await confirmOrderEdit(void 0, {
10089
- onError: (e) => {
10090
- toast.error(`Failed to confirm order edit: ${e.message}`);
10091
- },
10092
- onSuccess: () => {
10093
- handleSuccess();
10094
- },
10095
- onSettled: () => {
10096
- setIsSubmitting(false);
10097
- }
10098
- });
10099
- };
10100
- const onKeyDown = useCallback(
10101
- (e) => {
10102
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10103
- if (modalContent || isSubmitting) {
10104
- return;
10105
- }
10106
- onSubmit();
10107
- }
10108
- },
10109
- [modalContent, isSubmitting, onSubmit]
10110
- );
10111
- useEffect(() => {
10112
- document.addEventListener("keydown", onKeyDown);
10113
- return () => {
10114
- document.removeEventListener("keydown", onKeyDown);
10115
- };
10116
- }, [onKeyDown]);
10117
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10118
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10119
- /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxs(
10120
- StackedFocusModal,
10121
- {
10122
- id: STACKED_MODAL_ID,
10123
- onOpenChangeCallback: (open) => {
10124
- if (!open) {
10125
- setModalContent(null);
10126
- }
10127
- },
10128
- children: [
10129
- /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
10130
- /* @__PURE__ */ jsxs("div", { children: [
10131
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Items" }) }),
10132
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Edit the items in the draft order" }) })
10133
- ] }),
10134
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10135
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
10136
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10137
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10138
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10139
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10140
- ] }),
10141
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
10142
- /* @__PURE__ */ jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsx(
10143
- Input,
10144
- {
10145
- type: "search",
10146
- placeholder: "Search items",
10147
- value: searchValue,
10148
- onChange: (e) => onSearchValueChange(e.target.value)
10149
- }
10150
- ) }),
10151
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
10152
- /* @__PURE__ */ jsx(DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(IconButton, { type: "button", children: /* @__PURE__ */ jsx(Plus, {}) }) }),
10153
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
10154
- /* @__PURE__ */ jsx(
10155
- StackedModalTrigger$1,
10156
- {
10157
- type: "add-items",
10158
- setModalContent
10159
- }
10160
- ),
10161
- /* @__PURE__ */ jsx(
10162
- StackedModalTrigger$1,
10163
- {
10164
- type: "add-custom-item",
10165
- setModalContent
10166
- }
10167
- )
10168
- ] })
10169
- ] })
10170
- ] })
10171
- ] }),
10172
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10173
- /* @__PURE__ */ jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-muted grid grid-cols-[2fr_1fr_2fr_28px] gap-3 px-4 py-2", children: [
10174
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
10175
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10176
- /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Price" }) }),
10177
- /* @__PURE__ */ jsx("div", {})
10178
- ] }) }),
10179
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
10180
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10181
- /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10182
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsx(
10183
- Item,
10184
- {
10185
- item,
10186
- preview,
10187
- currencyCode
10188
- },
10189
- item.id
10190
- )) : /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
10191
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10192
- /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
10193
- 'No items found for "',
10194
- query2,
10195
- '".'
10196
- ] })
10197
- ] }) })
10198
- ] })
10199
- ] }),
10200
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10201
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10202
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10203
- /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsxs(
10204
- Text,
10205
- {
10206
- size: "small",
10207
- leading: "compact",
10208
- className: "text-ui-fg-subtle",
10209
- children: [
10210
- itemCount,
10211
- " ",
10212
- itemCount === 1 ? "item" : "items"
10213
- ]
10214
- }
10215
- ) }),
10216
- /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
10217
- ] })
10218
- ] }) }),
10219
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsx(
10220
- CustomItemForm,
10221
- {
10222
- orderId: preview.id,
10223
- currencyCode
10224
- }
10225
- ) : null)
10226
- ]
10227
- }
10228
- ) }),
10229
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10230
- /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10231
- /* @__PURE__ */ jsx(
10232
- Button,
10233
- {
10234
- size: "small",
10235
- type: "button",
10236
- onClick: onSubmit,
10237
- isLoading: isSubmitting,
10238
- children: "Save"
10239
- }
10240
- )
10241
- ] }) })
10242
- ] });
10243
- };
10244
- const Item = ({ item, preview, currencyCode }) => {
10245
- if (item.variant_id) {
10246
- return /* @__PURE__ */ jsx(VariantItem, { item, preview, currencyCode });
10247
- }
10248
- return /* @__PURE__ */ jsx(CustomItem, { item, preview, currencyCode });
10249
- };
10250
- const VariantItem = ({ item, preview, currencyCode }) => {
10251
- const [editing, setEditing] = useState(false);
9914
+ const hasUneditableRows = getHasUneditableRows(metadata);
9915
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10252
9916
  const form = useForm({
10253
9917
  defaultValues: {
10254
- quantity: item.quantity,
10255
- unit_price: item.unit_price
9918
+ metadata: getDefaultValues(metadata)
10256
9919
  },
10257
- resolver: zodResolver(variantItemSchema)
9920
+ resolver: zodResolver(MetadataSchema)
10258
9921
  });
10259
- const actionId = useMemo(() => {
10260
- var _a, _b;
10261
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10262
- }, [item]);
10263
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10264
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10265
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10266
- const onSubmit = form.handleSubmit(async (data) => {
10267
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10268
- setEditing(false);
10269
- return;
10270
- }
10271
- if (!actionId) {
10272
- await updateOriginalItem(
10273
- {
10274
- item_id: item.id,
10275
- quantity: data.quantity,
10276
- unit_price: convertNumber(data.unit_price)
10277
- },
10278
- {
10279
- onSuccess: () => {
10280
- setEditing(false);
10281
- },
10282
- onError: (e) => {
10283
- toast.error(e.message);
10284
- }
10285
- }
10286
- );
10287
- return;
10288
- }
10289
- await updateActionItem(
9922
+ const handleSubmit = form.handleSubmit(async (data) => {
9923
+ const parsedData = parseValues(data);
9924
+ await mutateAsync(
10290
9925
  {
10291
- action_id: actionId,
10292
- quantity: data.quantity,
10293
- unit_price: convertNumber(data.unit_price)
9926
+ metadata: parsedData
10294
9927
  },
10295
9928
  {
10296
9929
  onSuccess: () => {
10297
- setEditing(false);
9930
+ toast.success("Metadata updated");
9931
+ handleSuccess();
10298
9932
  },
10299
- onError: (e) => {
10300
- toast.error(e.message);
9933
+ onError: (error) => {
9934
+ toast.error(error.message);
10301
9935
  }
10302
9936
  }
10303
9937
  );
10304
9938
  });
10305
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10306
- /* @__PURE__ */ jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10307
- /* @__PURE__ */ jsx(
10308
- Thumbnail,
10309
- {
10310
- thumbnail: item.thumbnail,
10311
- alt: item.product_title ?? void 0
10312
- }
10313
- ),
10314
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10315
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
10316
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10317
- /* @__PURE__ */ jsxs(
10318
- Text,
10319
- {
10320
- size: "small",
10321
- leading: "compact",
10322
- className: "text-ui-fg-subtle",
10323
- children: [
10324
- "(",
10325
- item.variant_title,
10326
- ")"
10327
- ]
10328
- }
10329
- )
10330
- ] }),
10331
- /* @__PURE__ */ jsx(
10332
- Text,
10333
- {
10334
- size: "small",
10335
- leading: "compact",
10336
- className: "text-ui-fg-subtle",
10337
- children: item.variant_sku
10338
- }
10339
- )
10340
- ] })
10341
- ] }),
10342
- editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10343
- Form$2.Field,
10344
- {
10345
- control: form.control,
10346
- name: "quantity",
10347
- render: ({ field }) => {
10348
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10349
- }
10350
- }
10351
- ) }) : /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
10352
- editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10353
- Form$2.Field,
10354
- {
10355
- control: form.control,
10356
- name: "unit_price",
10357
- render: ({ field: { onChange, ...field } }) => {
10358
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10359
- CurrencyInput,
10360
- {
10361
- ...field,
10362
- symbol: getNativeSymbol(currencyCode),
10363
- code: currencyCode,
10364
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10365
- }
10366
- ) }) });
10367
- }
10368
- }
10369
- ) }) : /* @__PURE__ */ jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10370
- /* @__PURE__ */ jsx(
10371
- IconButton,
10372
- {
10373
- type: "button",
10374
- size: "small",
10375
- onClick: editing ? onSubmit : () => {
10376
- setEditing(true);
10377
- },
10378
- disabled: isPending,
10379
- children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10380
- }
10381
- )
10382
- ] }) }) });
10383
- };
10384
- const variantItemSchema = objectType({
10385
- quantity: numberType(),
10386
- unit_price: unionType([numberType(), stringType()])
10387
- });
10388
- const CustomItem = ({ item, preview, currencyCode }) => {
10389
- const [editing, setEditing] = useState(false);
10390
- const { quantity, unit_price, title } = item;
10391
- const form = useForm({
10392
- defaultValues: {
10393
- title,
10394
- quantity,
10395
- unit_price
10396
- },
10397
- resolver: zodResolver(customItemSchema)
9939
+ const { fields, insert, remove } = useFieldArray({
9940
+ control: form.control,
9941
+ name: "metadata"
10398
9942
  });
10399
- useEffect(() => {
10400
- form.reset({
10401
- title,
10402
- quantity,
10403
- unit_price
10404
- });
10405
- }, [form, title, quantity, unit_price]);
10406
- const actionId = useMemo(() => {
10407
- var _a, _b;
10408
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10409
- }, [item]);
10410
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10411
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10412
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10413
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10414
- const onSubmit = form.handleSubmit(async (data) => {
10415
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10416
- setEditing(false);
10417
- return;
10418
- }
10419
- if (!actionId) {
10420
- await updateOriginalItem(
10421
- {
10422
- item_id: item.id,
10423
- quantity: data.quantity,
10424
- unit_price: convertNumber(data.unit_price)
10425
- },
10426
- {
10427
- onSuccess: () => {
10428
- setEditing(false);
10429
- },
10430
- onError: (e) => {
10431
- toast.error(e.message);
10432
- }
10433
- }
10434
- );
10435
- return;
10436
- }
10437
- if (data.quantity === 0) {
10438
- await removeActionItem(actionId, {
10439
- onSuccess: () => {
10440
- setEditing(false);
10441
- },
10442
- onError: (e) => {
10443
- toast.error(e.message);
10444
- }
9943
+ function deleteRow(index) {
9944
+ remove(index);
9945
+ if (fields.length === 1) {
9946
+ insert(0, {
9947
+ key: "",
9948
+ value: "",
9949
+ disabled: false
10445
9950
  });
10446
- return;
10447
9951
  }
10448
- await updateActionItem(
10449
- {
10450
- action_id: actionId,
10451
- quantity: data.quantity,
10452
- unit_price: convertNumber(data.unit_price)
10453
- },
10454
- {
10455
- onSuccess: () => {
10456
- setEditing(false);
10457
- },
10458
- onError: (e) => {
10459
- toast.error(e.message);
10460
- }
10461
- }
10462
- );
10463
- });
10464
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10465
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10466
- /* @__PURE__ */ jsx(
10467
- Thumbnail,
10468
- {
10469
- thumbnail: item.thumbnail,
10470
- alt: item.title ?? void 0
10471
- }
10472
- ),
10473
- editing ? /* @__PURE__ */ jsx(
10474
- Form$2.Field,
10475
- {
10476
- control: form.control,
10477
- name: "title",
10478
- render: ({ field }) => {
10479
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
10480
- }
10481
- }
10482
- ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
10483
- ] }),
10484
- editing ? /* @__PURE__ */ jsx(
10485
- Form$2.Field,
10486
- {
10487
- control: form.control,
10488
- name: "quantity",
10489
- render: ({ field }) => {
10490
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10491
- }
10492
- }
10493
- ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
10494
- editing ? /* @__PURE__ */ jsx(
10495
- Form$2.Field,
10496
- {
10497
- control: form.control,
10498
- name: "unit_price",
10499
- render: ({ field: { onChange, ...field } }) => {
10500
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10501
- CurrencyInput,
10502
- {
10503
- ...field,
10504
- symbol: getNativeSymbol(currencyCode),
10505
- code: currencyCode,
10506
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10507
- }
10508
- ) }) });
10509
- }
10510
- }
10511
- ) : /* @__PURE__ */ jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10512
- /* @__PURE__ */ jsx(
10513
- IconButton,
10514
- {
10515
- type: "button",
10516
- size: "small",
10517
- onClick: editing ? onSubmit : () => {
10518
- setEditing(true);
10519
- },
10520
- disabled: isPending,
10521
- children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10522
- }
10523
- )
10524
- ] }) }) });
10525
- };
10526
- const StackedModalTrigger$1 = ({
10527
- type,
10528
- setModalContent
10529
- }) => {
10530
- const { setIsOpen } = useStackedModal();
10531
- const onClick = useCallback(() => {
10532
- setModalContent(type);
10533
- setIsOpen(STACKED_MODAL_ID, true);
10534
- }, [setModalContent, setIsOpen, type]);
10535
- return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10536
- };
10537
- const VARIANT_PREFIX = "items";
10538
- const LIMIT = 50;
10539
- const ExistingItemsForm = ({ orderId, items }) => {
10540
- const { setIsOpen } = useStackedModal();
10541
- const [rowSelection, setRowSelection] = useState(
10542
- items.reduce((acc, item) => {
10543
- acc[item.variant_id] = true;
10544
- return acc;
10545
- }, {})
10546
- );
10547
- useEffect(() => {
10548
- setRowSelection(
10549
- items.reduce((acc, item) => {
10550
- if (item.variant_id) {
10551
- acc[item.variant_id] = true;
10552
- }
10553
- return acc;
10554
- }, {})
10555
- );
10556
- }, [items]);
10557
- const { q, order, offset } = useQueryParams(
10558
- ["q", "order", "offset"],
10559
- VARIANT_PREFIX
10560
- );
10561
- const { variants, count, isPending, isError, error } = useProductVariants(
10562
- {
10563
- q,
10564
- order,
10565
- offset: offset ? parseInt(offset) : void 0,
10566
- limit: LIMIT
10567
- },
9952
+ }
9953
+ function insertRow(index, position) {
9954
+ insert(index + (position === "above" ? 0 : 1), {
9955
+ key: "",
9956
+ value: "",
9957
+ disabled: false
9958
+ });
9959
+ }
9960
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9961
+ KeyboundForm,
10568
9962
  {
10569
- placeholderData: keepPreviousData
10570
- }
10571
- );
10572
- const columns = useColumns();
10573
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10574
- const onSubmit = async () => {
10575
- const ids = Object.keys(rowSelection).filter(
10576
- (id) => !items.find((i) => i.variant_id === id)
10577
- );
10578
- await mutateAsync(
10579
- {
10580
- items: ids.map((id) => ({
10581
- variant_id: id,
10582
- quantity: 1
10583
- }))
10584
- },
10585
- {
10586
- onSuccess: () => {
10587
- setRowSelection({});
10588
- setIsOpen(STACKED_MODAL_ID, false);
10589
- },
10590
- onError: (e) => {
10591
- toast.error(e.message);
10592
- }
10593
- }
10594
- );
10595
- };
10596
- if (isError) {
10597
- throw error;
10598
- }
10599
- return /* @__PURE__ */ jsxs(
10600
- StackedFocusModal.Content,
10601
- {
10602
- onOpenAutoFocus: (e) => {
10603
- e.preventDefault();
10604
- const searchInput = document.querySelector(
10605
- "[data-modal-id='modal-search-input']"
10606
- );
10607
- if (searchInput) {
10608
- searchInput.focus();
10609
- }
10610
- },
9963
+ onSubmit: handleSubmit,
9964
+ className: "flex flex-1 flex-col overflow-hidden",
10611
9965
  children: [
10612
- /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
10613
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10614
- /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10615
- ] }),
10616
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
10617
- DataTable,
10618
- {
10619
- data: variants,
10620
- columns,
10621
- isLoading: isPending,
10622
- getRowId: (row) => row.id,
10623
- rowCount: count,
10624
- prefix: VARIANT_PREFIX,
10625
- layout: "fill",
10626
- rowSelection: {
10627
- state: rowSelection,
10628
- onRowSelectionChange: setRowSelection,
10629
- enableRowSelection: (row) => {
10630
- return !items.find((i) => i.variant_id === row.original.id);
9966
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9967
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9968
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9969
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
9970
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
9971
+ ] }),
9972
+ fields.map((field, index) => {
9973
+ const isDisabled = field.disabled || false;
9974
+ let placeholder = "-";
9975
+ if (typeof field.value === "object") {
9976
+ placeholder = "{ ... }";
10631
9977
  }
10632
- },
10633
- autoFocusSearch: true
10634
- }
10635
- ) }),
10636
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10637
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10638
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
9978
+ if (Array.isArray(field.value)) {
9979
+ placeholder = "[ ... ]";
9980
+ }
9981
+ return /* @__PURE__ */ jsx(
9982
+ ConditionalTooltip,
9983
+ {
9984
+ showTooltip: isDisabled,
9985
+ content: "This row is disabled because it contains non-primitive data.",
9986
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9987
+ /* @__PURE__ */ jsxs(
9988
+ "div",
9989
+ {
9990
+ className: clx("grid grid-cols-2 divide-x", {
9991
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9992
+ }),
9993
+ children: [
9994
+ /* @__PURE__ */ jsx(
9995
+ Form$2.Field,
9996
+ {
9997
+ control: form.control,
9998
+ name: `metadata.${index}.key`,
9999
+ render: ({ field: field2 }) => {
10000
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10001
+ GridInput,
10002
+ {
10003
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
10004
+ ...field2,
10005
+ disabled: isDisabled,
10006
+ placeholder: "Key"
10007
+ }
10008
+ ) }) });
10009
+ }
10010
+ }
10011
+ ),
10012
+ /* @__PURE__ */ jsx(
10013
+ Form$2.Field,
10014
+ {
10015
+ control: form.control,
10016
+ name: `metadata.${index}.value`,
10017
+ render: ({ field: { value, ...field2 } }) => {
10018
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10019
+ GridInput,
10020
+ {
10021
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
10022
+ ...field2,
10023
+ value: isDisabled ? placeholder : value,
10024
+ disabled: isDisabled,
10025
+ placeholder: "Value"
10026
+ }
10027
+ ) }) });
10028
+ }
10029
+ }
10030
+ )
10031
+ ]
10032
+ }
10033
+ ),
10034
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
10035
+ /* @__PURE__ */ jsx(
10036
+ DropdownMenu.Trigger,
10037
+ {
10038
+ className: clx(
10039
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
10040
+ {
10041
+ hidden: isDisabled
10042
+ }
10043
+ ),
10044
+ disabled: isDisabled,
10045
+ asChild: true,
10046
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
10047
+ }
10048
+ ),
10049
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
10050
+ /* @__PURE__ */ jsxs(
10051
+ DropdownMenu.Item,
10052
+ {
10053
+ className: "gap-x-2",
10054
+ onClick: () => insertRow(index, "above"),
10055
+ children: [
10056
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
10057
+ "Insert row above"
10058
+ ]
10059
+ }
10060
+ ),
10061
+ /* @__PURE__ */ jsxs(
10062
+ DropdownMenu.Item,
10063
+ {
10064
+ className: "gap-x-2",
10065
+ onClick: () => insertRow(index, "below"),
10066
+ children: [
10067
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
10068
+ "Insert row below"
10069
+ ]
10070
+ }
10071
+ ),
10072
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
10073
+ /* @__PURE__ */ jsxs(
10074
+ DropdownMenu.Item,
10075
+ {
10076
+ className: "gap-x-2",
10077
+ onClick: () => deleteRow(index),
10078
+ children: [
10079
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
10080
+ "Delete row"
10081
+ ]
10082
+ }
10083
+ )
10084
+ ] })
10085
+ ] })
10086
+ ] })
10087
+ },
10088
+ field.id
10089
+ );
10090
+ })
10091
+ ] }),
10092
+ hasUneditableRows && /* @__PURE__ */ jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
10093
+ ] }),
10094
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10095
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10096
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10639
10097
  ] }) })
10640
10098
  ]
10641
10099
  }
10100
+ ) });
10101
+ };
10102
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
10103
+ return /* @__PURE__ */ jsx(
10104
+ "input",
10105
+ {
10106
+ ref,
10107
+ ...props,
10108
+ autoComplete: "off",
10109
+ className: clx(
10110
+ "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",
10111
+ className
10112
+ )
10113
+ }
10642
10114
  );
10115
+ });
10116
+ GridInput.displayName = "MetadataForm.GridInput";
10117
+ const PlaceholderInner = () => {
10118
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10119
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10120
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10121
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
10122
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
10123
+ ] }) })
10124
+ ] });
10643
10125
  };
10644
- const columnHelper = createDataTableColumnHelper();
10645
- const useColumns = () => {
10646
- return useMemo(() => {
10126
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
10127
+ function getDefaultValues(metadata) {
10128
+ if (!metadata || !Object.keys(metadata).length) {
10647
10129
  return [
10648
- columnHelper.select(),
10649
- columnHelper.accessor("product.title", {
10650
- header: "Product",
10651
- cell: ({ row }) => {
10652
- var _a, _b, _c;
10653
- return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
10654
- /* @__PURE__ */ jsx(
10655
- Thumbnail,
10656
- {
10657
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10658
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10659
- }
10660
- ),
10661
- /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10662
- ] });
10663
- },
10664
- enableSorting: true
10665
- }),
10666
- columnHelper.accessor("title", {
10667
- header: "Variant",
10668
- enableSorting: true
10669
- }),
10670
- columnHelper.accessor("sku", {
10671
- header: "SKU",
10672
- cell: ({ getValue }) => {
10673
- return getValue() ?? "-";
10674
- },
10675
- enableSorting: true
10676
- }),
10677
- columnHelper.accessor("updated_at", {
10678
- header: "Updated",
10679
- cell: ({ getValue }) => {
10680
- return /* @__PURE__ */ jsx(
10681
- Tooltip,
10682
- {
10683
- content: getFullDate({ date: getValue(), includeTime: true }),
10684
- children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10685
- }
10686
- );
10687
- },
10688
- enableSorting: true,
10689
- sortAscLabel: "Oldest first",
10690
- sortDescLabel: "Newest first"
10691
- }),
10692
- columnHelper.accessor("created_at", {
10693
- header: "Created",
10694
- cell: ({ getValue }) => {
10695
- return /* @__PURE__ */ jsx(
10696
- Tooltip,
10697
- {
10698
- content: getFullDate({ date: getValue(), includeTime: true }),
10699
- children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10700
- }
10701
- );
10702
- },
10703
- enableSorting: true,
10704
- sortAscLabel: "Oldest first",
10705
- sortDescLabel: "Newest first"
10706
- })
10130
+ {
10131
+ key: "",
10132
+ value: "",
10133
+ disabled: false
10134
+ }
10707
10135
  ];
10708
- }, []);
10136
+ }
10137
+ return Object.entries(metadata).map(([key, value]) => {
10138
+ if (!EDITABLE_TYPES.includes(typeof value)) {
10139
+ return {
10140
+ key,
10141
+ value,
10142
+ disabled: true
10143
+ };
10144
+ }
10145
+ let stringValue = value;
10146
+ if (typeof value !== "string") {
10147
+ stringValue = JSON.stringify(value);
10148
+ }
10149
+ return {
10150
+ key,
10151
+ value: stringValue,
10152
+ original_key: key
10153
+ };
10154
+ });
10155
+ }
10156
+ function parseValues(values) {
10157
+ const metadata = values.metadata;
10158
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10159
+ if (isEmpty) {
10160
+ return null;
10161
+ }
10162
+ const update = {};
10163
+ metadata.forEach((field) => {
10164
+ let key = field.key;
10165
+ let value = field.value;
10166
+ const disabled = field.disabled;
10167
+ if (!key || !value) {
10168
+ return;
10169
+ }
10170
+ if (disabled) {
10171
+ update[key] = value;
10172
+ return;
10173
+ }
10174
+ key = key.trim();
10175
+ value = value.trim();
10176
+ if (value === "true") {
10177
+ update[key] = true;
10178
+ } else if (value === "false") {
10179
+ update[key] = false;
10180
+ } else {
10181
+ const parsedNumber = parseFloat(value);
10182
+ if (!isNaN(parsedNumber)) {
10183
+ update[key] = parsedNumber;
10184
+ } else {
10185
+ update[key] = value;
10186
+ }
10187
+ }
10188
+ });
10189
+ return update;
10190
+ }
10191
+ function getHasUneditableRows(metadata) {
10192
+ if (!metadata) {
10193
+ return false;
10194
+ }
10195
+ return Object.values(metadata).some(
10196
+ (value) => !EDITABLE_TYPES.includes(typeof value)
10197
+ );
10198
+ }
10199
+ const PROMOTION_QUERY_KEY = "promotions";
10200
+ const promotionsQueryKeys = {
10201
+ list: (query2) => [
10202
+ PROMOTION_QUERY_KEY,
10203
+ query2 ? query2 : void 0
10204
+ ],
10205
+ detail: (id, query2) => [
10206
+ PROMOTION_QUERY_KEY,
10207
+ id,
10208
+ query2 ? query2 : void 0
10209
+ ]
10709
10210
  };
10710
- const CustomItemForm = ({ orderId, currencyCode }) => {
10711
- const { setIsOpen } = useStackedModal();
10712
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10713
- const form = useForm({
10714
- defaultValues: {
10715
- title: "",
10716
- quantity: 1,
10717
- unit_price: ""
10211
+ const usePromotions = (query2, options) => {
10212
+ const { data, ...rest } = useQuery({
10213
+ queryKey: promotionsQueryKeys.list(query2),
10214
+ queryFn: async () => sdk.admin.promotion.list(query2),
10215
+ ...options
10216
+ });
10217
+ return { ...data, ...rest };
10218
+ };
10219
+ const useCancelOrderEdit = ({ preview }) => {
10220
+ const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10221
+ const onCancel = useCallback(async () => {
10222
+ if (!preview) {
10223
+ return true;
10224
+ }
10225
+ let res = false;
10226
+ await cancelOrderEdit(void 0, {
10227
+ onError: (e) => {
10228
+ toast.error(e.message);
10229
+ },
10230
+ onSuccess: () => {
10231
+ res = true;
10232
+ }
10233
+ });
10234
+ return res;
10235
+ }, [preview, cancelOrderEdit]);
10236
+ return { onCancel };
10237
+ };
10238
+ let IS_REQUEST_RUNNING = false;
10239
+ const useInitiateOrderEdit = ({
10240
+ preview
10241
+ }) => {
10242
+ const navigate = useNavigate();
10243
+ const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10244
+ useEffect(() => {
10245
+ async function run() {
10246
+ if (IS_REQUEST_RUNNING || !preview) {
10247
+ return;
10248
+ }
10249
+ if (preview.order_change) {
10250
+ return;
10251
+ }
10252
+ IS_REQUEST_RUNNING = true;
10253
+ await mutateAsync(void 0, {
10254
+ onError: (e) => {
10255
+ toast.error(e.message);
10256
+ navigate(`/draft-orders/${preview.id}`, { replace: true });
10257
+ return;
10258
+ }
10259
+ });
10260
+ IS_REQUEST_RUNNING = false;
10261
+ }
10262
+ run();
10263
+ }, [preview, navigate, mutateAsync]);
10264
+ };
10265
+ const Promotions = () => {
10266
+ const { id } = useParams();
10267
+ const {
10268
+ order: preview,
10269
+ isError: isPreviewError,
10270
+ error: previewError
10271
+ } = useOrderPreview(id, void 0);
10272
+ useInitiateOrderEdit({ preview });
10273
+ const { onCancel } = useCancelOrderEdit({ preview });
10274
+ if (isPreviewError) {
10275
+ throw previewError;
10276
+ }
10277
+ const isReady = !!preview;
10278
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10279
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10280
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10281
+ ] });
10282
+ };
10283
+ const PromotionForm = ({ preview }) => {
10284
+ const { items, shipping_methods } = preview;
10285
+ const [isSubmitting, setIsSubmitting] = useState(false);
10286
+ const [comboboxValue, setComboboxValue] = useState("");
10287
+ const { handleSuccess } = useRouteModal();
10288
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10289
+ const promoIds = getPromotionIds(items, shipping_methods);
10290
+ const { promotions, isPending, isError, error } = usePromotions(
10291
+ {
10292
+ id: promoIds
10718
10293
  },
10719
- resolver: zodResolver(customItemSchema)
10294
+ {
10295
+ enabled: !!promoIds.length
10296
+ }
10297
+ );
10298
+ const comboboxData = useComboboxData({
10299
+ queryKey: ["promotions", "combobox", promoIds],
10300
+ queryFn: async (params) => {
10301
+ return await sdk.admin.promotion.list({
10302
+ ...params,
10303
+ id: {
10304
+ $nin: promoIds
10305
+ }
10306
+ });
10307
+ },
10308
+ getOptions: (data) => {
10309
+ return data.promotions.map((promotion) => ({
10310
+ label: promotion.code,
10311
+ value: promotion.code
10312
+ }));
10313
+ }
10720
10314
  });
10721
- const onSubmit = form.handleSubmit(async (data) => {
10722
- await addItems(
10315
+ const add = async (value) => {
10316
+ if (!value) {
10317
+ return;
10318
+ }
10319
+ addPromotions(
10723
10320
  {
10724
- items: [
10725
- {
10726
- title: data.title,
10727
- quantity: data.quantity,
10728
- unit_price: convertNumber(data.unit_price)
10729
- }
10730
- ]
10321
+ promo_codes: [value]
10731
10322
  },
10732
10323
  {
10733
- onSuccess: () => {
10734
- setIsOpen(STACKED_MODAL_ID, false);
10735
- },
10736
10324
  onError: (e) => {
10737
10325
  toast.error(e.message);
10326
+ comboboxData.onSearchValueChange("");
10327
+ setComboboxValue("");
10328
+ },
10329
+ onSuccess: () => {
10330
+ comboboxData.onSearchValueChange("");
10331
+ setComboboxValue("");
10738
10332
  }
10739
10333
  }
10740
10334
  );
10741
- });
10742
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
10743
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
10744
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
10745
- /* @__PURE__ */ jsxs("div", { children: [
10746
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
10747
- /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
10748
- ] }),
10749
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10750
- /* @__PURE__ */ jsx(
10751
- Form$2.Field,
10752
- {
10753
- control: form.control,
10754
- name: "title",
10755
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10756
- /* @__PURE__ */ jsxs("div", { children: [
10757
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
10758
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
10759
- ] }),
10760
- /* @__PURE__ */ jsxs("div", { children: [
10761
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10762
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10763
- ] })
10764
- ] }) })
10765
- }
10766
- ),
10335
+ };
10336
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10337
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10338
+ const onSubmit = async () => {
10339
+ setIsSubmitting(true);
10340
+ let requestSucceeded = false;
10341
+ await requestOrderEdit(void 0, {
10342
+ onError: (e) => {
10343
+ toast.error(e.message);
10344
+ },
10345
+ onSuccess: () => {
10346
+ requestSucceeded = true;
10347
+ }
10348
+ });
10349
+ if (!requestSucceeded) {
10350
+ setIsSubmitting(false);
10351
+ return;
10352
+ }
10353
+ await confirmOrderEdit(void 0, {
10354
+ onError: (e) => {
10355
+ toast.error(e.message);
10356
+ },
10357
+ onSuccess: () => {
10358
+ handleSuccess();
10359
+ },
10360
+ onSettled: () => {
10361
+ setIsSubmitting(false);
10362
+ }
10363
+ });
10364
+ };
10365
+ if (isError) {
10366
+ throw error;
10367
+ }
10368
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10369
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
10370
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
10371
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10372
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10373
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10374
+ ] }),
10375
+ /* @__PURE__ */ jsx(
10376
+ Combobox,
10377
+ {
10378
+ id: "promotion-combobox",
10379
+ "aria-describedby": "promotion-combobox-hint",
10380
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10381
+ fetchNextPage: comboboxData.fetchNextPage,
10382
+ options: comboboxData.options,
10383
+ onSearchValueChange: comboboxData.onSearchValueChange,
10384
+ searchValue: comboboxData.searchValue,
10385
+ disabled: comboboxData.disabled || isAddingPromotions,
10386
+ onChange: add,
10387
+ value: comboboxValue
10388
+ }
10389
+ )
10390
+ ] }),
10767
10391
  /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10768
- /* @__PURE__ */ jsx(
10769
- Form$2.Field,
10392
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
10393
+ PromotionItem,
10770
10394
  {
10771
- control: form.control,
10772
- name: "unit_price",
10773
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10774
- /* @__PURE__ */ jsxs("div", { children: [
10775
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
10776
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10777
- ] }),
10778
- /* @__PURE__ */ jsxs("div", { children: [
10779
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10780
- CurrencyInput,
10781
- {
10782
- symbol: getNativeSymbol(currencyCode),
10783
- code: currencyCode,
10784
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10785
- ...field
10786
- }
10787
- ) }),
10788
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10789
- ] })
10790
- ] }) })
10791
- }
10792
- ),
10793
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10395
+ promotion,
10396
+ orderId: preview.id,
10397
+ isLoading: isPending
10398
+ },
10399
+ promotion.id
10400
+ )) })
10401
+ ] }) }),
10402
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10403
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10794
10404
  /* @__PURE__ */ jsx(
10795
- Form$2.Field,
10405
+ Button,
10796
10406
  {
10797
- control: form.control,
10798
- name: "quantity",
10799
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10800
- /* @__PURE__ */ jsxs("div", { children: [
10801
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
10802
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10803
- ] }),
10804
- /* @__PURE__ */ jsxs("div", { className: "w-full flex-1", children: [
10805
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10806
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10807
- ] })
10808
- ] }) })
10407
+ size: "small",
10408
+ type: "submit",
10409
+ isLoading: isSubmitting || isAddingPromotions,
10410
+ children: "Save"
10809
10411
  }
10810
10412
  )
10811
- ] }) }) }),
10812
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10813
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10814
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10815
10413
  ] }) })
10816
- ] }) }) });
10817
- };
10818
- const customItemSchema = objectType({
10819
- title: stringType().min(1),
10820
- quantity: numberType(),
10821
- unit_price: unionType([numberType(), stringType()])
10822
- });
10823
- const InlineTip = forwardRef(
10824
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10825
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10826
- return /* @__PURE__ */ jsxs(
10827
- "div",
10828
- {
10829
- ref,
10830
- className: clx(
10831
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10832
- className
10833
- ),
10834
- ...props,
10835
- children: [
10836
- /* @__PURE__ */ jsx(
10837
- "div",
10838
- {
10839
- role: "presentation",
10840
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10841
- "bg-ui-tag-orange-icon": variant === "warning"
10842
- })
10843
- }
10844
- ),
10845
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
10846
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10847
- labelValue,
10848
- ":"
10849
- ] }),
10850
- " ",
10851
- children
10852
- ] })
10853
- ]
10854
- }
10855
- );
10856
- }
10857
- );
10858
- InlineTip.displayName = "InlineTip";
10859
- const MetadataFieldSchema = objectType({
10860
- key: stringType(),
10861
- disabled: booleanType().optional(),
10862
- value: anyType()
10863
- });
10864
- const MetadataSchema = objectType({
10865
- metadata: arrayType(MetadataFieldSchema)
10866
- });
10867
- const Metadata = () => {
10868
- const { id } = useParams();
10869
- const { order, isPending, isError, error } = useOrder(id, {
10870
- fields: "metadata"
10871
- });
10872
- if (isError) {
10873
- throw error;
10874
- }
10875
- const isReady = !isPending && !!order;
10876
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10877
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10878
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
10879
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10880
- ] }),
10881
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10882
10414
  ] });
10883
10415
  };
10884
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10885
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10886
- const MetadataForm = ({ orderId, metadata }) => {
10887
- const { handleSuccess } = useRouteModal();
10888
- const hasUneditableRows = getHasUneditableRows(metadata);
10889
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10890
- const form = useForm({
10891
- defaultValues: {
10892
- metadata: getDefaultValues(metadata)
10893
- },
10894
- resolver: zodResolver(MetadataSchema)
10895
- });
10896
- const handleSubmit = form.handleSubmit(async (data) => {
10897
- const parsedData = parseValues(data);
10898
- await mutateAsync(
10416
+ const PromotionItem = ({
10417
+ promotion,
10418
+ orderId,
10419
+ isLoading
10420
+ }) => {
10421
+ var _a;
10422
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10423
+ const onRemove = async () => {
10424
+ removePromotions(
10899
10425
  {
10900
- metadata: parsedData
10426
+ promo_codes: [promotion.code]
10901
10427
  },
10902
10428
  {
10903
- onSuccess: () => {
10904
- toast.success("Metadata updated");
10905
- handleSuccess();
10906
- },
10907
- onError: (error) => {
10908
- toast.error(error.message);
10429
+ onError: (e) => {
10430
+ toast.error(e.message);
10909
10431
  }
10910
10432
  }
10911
10433
  );
10912
- });
10913
- const { fields, insert, remove } = useFieldArray({
10914
- control: form.control,
10915
- name: "metadata"
10916
- });
10917
- function deleteRow(index) {
10918
- remove(index);
10919
- if (fields.length === 1) {
10920
- insert(0, {
10921
- key: "",
10922
- value: "",
10923
- disabled: false
10924
- });
10925
- }
10926
- }
10927
- function insertRow(index, position) {
10928
- insert(index + (position === "above" ? 0 : 1), {
10929
- key: "",
10930
- value: "",
10931
- disabled: false
10932
- });
10933
- }
10934
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10935
- KeyboundForm,
10434
+ };
10435
+ const displayValue = getDisplayValue(promotion);
10436
+ return /* @__PURE__ */ jsxs(
10437
+ "div",
10936
10438
  {
10937
- onSubmit: handleSubmit,
10938
- className: "flex flex-1 flex-col overflow-hidden",
10439
+ className: clx(
10440
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
10441
+ {
10442
+ "animate-pulse": isLoading
10443
+ }
10444
+ ),
10939
10445
  children: [
10940
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10941
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10942
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10943
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
10944
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
10446
+ /* @__PURE__ */ jsxs("div", { children: [
10447
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
10448
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
10449
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
10450
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
10451
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
10945
10452
  ] }),
10946
- fields.map((field, index) => {
10947
- const isDisabled = field.disabled || false;
10948
- let placeholder = "-";
10949
- if (typeof field.value === "object") {
10950
- placeholder = "{ ... }";
10951
- }
10952
- if (Array.isArray(field.value)) {
10953
- placeholder = "[ ... ]";
10954
- }
10955
- return /* @__PURE__ */ jsx(
10956
- ConditionalTooltip,
10957
- {
10958
- showTooltip: isDisabled,
10959
- content: "This row is disabled because it contains non-primitive data.",
10960
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
10961
- /* @__PURE__ */ jsxs(
10962
- "div",
10963
- {
10964
- className: clx("grid grid-cols-2 divide-x", {
10965
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10966
- }),
10967
- children: [
10968
- /* @__PURE__ */ jsx(
10969
- Form$2.Field,
10970
- {
10971
- control: form.control,
10972
- name: `metadata.${index}.key`,
10973
- render: ({ field: field2 }) => {
10974
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10975
- GridInput,
10976
- {
10977
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10978
- ...field2,
10979
- disabled: isDisabled,
10980
- placeholder: "Key"
10981
- }
10982
- ) }) });
10983
- }
10984
- }
10985
- ),
10986
- /* @__PURE__ */ jsx(
10987
- Form$2.Field,
10988
- {
10989
- control: form.control,
10990
- name: `metadata.${index}.value`,
10991
- render: ({ field: { value, ...field2 } }) => {
10992
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10993
- GridInput,
10994
- {
10995
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
10996
- ...field2,
10997
- value: isDisabled ? placeholder : value,
10998
- disabled: isDisabled,
10999
- placeholder: "Value"
11000
- }
11001
- ) }) });
11002
- }
11003
- }
11004
- )
11005
- ]
11006
- }
11007
- ),
11008
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
11009
- /* @__PURE__ */ jsx(
11010
- DropdownMenu.Trigger,
11011
- {
11012
- className: clx(
11013
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11014
- {
11015
- hidden: isDisabled
11016
- }
11017
- ),
11018
- disabled: isDisabled,
11019
- asChild: true,
11020
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
11021
- }
11022
- ),
11023
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
11024
- /* @__PURE__ */ jsxs(
11025
- DropdownMenu.Item,
11026
- {
11027
- className: "gap-x-2",
11028
- onClick: () => insertRow(index, "above"),
11029
- children: [
11030
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
11031
- "Insert row above"
11032
- ]
11033
- }
11034
- ),
11035
- /* @__PURE__ */ jsxs(
11036
- DropdownMenu.Item,
11037
- {
11038
- className: "gap-x-2",
11039
- onClick: () => insertRow(index, "below"),
11040
- children: [
11041
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
11042
- "Insert row below"
11043
- ]
11044
- }
11045
- ),
11046
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
11047
- /* @__PURE__ */ jsxs(
11048
- DropdownMenu.Item,
11049
- {
11050
- className: "gap-x-2",
11051
- onClick: () => deleteRow(index),
11052
- children: [
11053
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
11054
- "Delete row"
11055
- ]
11056
- }
11057
- )
11058
- ] })
11059
- ] })
11060
- ] })
11061
- },
11062
- field.id
11063
- );
11064
- })
11065
- ] }),
11066
- hasUneditableRows && /* @__PURE__ */ jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
10453
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
10454
+ ] })
11067
10455
  ] }),
11068
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11069
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11070
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11071
- ] }) })
10456
+ /* @__PURE__ */ jsx(
10457
+ IconButton,
10458
+ {
10459
+ size: "small",
10460
+ type: "button",
10461
+ variant: "transparent",
10462
+ onClick: onRemove,
10463
+ isLoading: isPending || isLoading,
10464
+ children: /* @__PURE__ */ jsx(XMark, {})
10465
+ }
10466
+ )
11072
10467
  ]
11073
- }
11074
- ) });
11075
- };
11076
- const GridInput = forwardRef(({ className, ...props }, ref) => {
11077
- return /* @__PURE__ */ jsx(
11078
- "input",
11079
- {
11080
- ref,
11081
- ...props,
11082
- autoComplete: "off",
11083
- className: clx(
11084
- "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",
11085
- className
11086
- )
11087
- }
10468
+ },
10469
+ promotion.id
11088
10470
  );
11089
- });
11090
- GridInput.displayName = "MetadataForm.GridInput";
11091
- const PlaceholderInner = () => {
11092
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11093
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11094
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11095
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11096
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11097
- ] }) })
11098
- ] });
11099
10471
  };
11100
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11101
- function getDefaultValues(metadata) {
11102
- if (!metadata || !Object.keys(metadata).length) {
11103
- return [
11104
- {
11105
- key: "",
11106
- value: "",
11107
- disabled: false
11108
- }
11109
- ];
10472
+ function getDisplayValue(promotion) {
10473
+ var _a, _b, _c, _d;
10474
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
10475
+ if (!value) {
10476
+ return null;
11110
10477
  }
11111
- return Object.entries(metadata).map(([key, value]) => {
11112
- if (!EDITABLE_TYPES.includes(typeof value)) {
11113
- return {
11114
- key,
11115
- value,
11116
- disabled: true
11117
- };
11118
- }
11119
- let stringValue = value;
11120
- if (typeof value !== "string") {
11121
- stringValue = JSON.stringify(value);
10478
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
10479
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
10480
+ if (!currency) {
10481
+ return null;
11122
10482
  }
11123
- return {
11124
- key,
11125
- value: stringValue,
11126
- original_key: key
11127
- };
11128
- });
10483
+ return getLocaleAmount(value, currency);
10484
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
10485
+ return formatPercentage(value);
10486
+ }
10487
+ return null;
11129
10488
  }
11130
- function parseValues(values) {
11131
- const metadata = values.metadata;
11132
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11133
- if (isEmpty) {
11134
- return null;
10489
+ const formatter = new Intl.NumberFormat([], {
10490
+ style: "percent",
10491
+ minimumFractionDigits: 2
10492
+ });
10493
+ const formatPercentage = (value, isPercentageValue = false) => {
10494
+ let val = value || 0;
10495
+ if (!isPercentageValue) {
10496
+ val = val / 100;
11135
10497
  }
11136
- const update = {};
11137
- metadata.forEach((field) => {
11138
- let key = field.key;
11139
- let value = field.value;
11140
- const disabled = field.disabled;
11141
- if (!key || !value) {
11142
- return;
10498
+ return formatter.format(val);
10499
+ };
10500
+ function getPromotionIds(items, shippingMethods) {
10501
+ const promotionIds = /* @__PURE__ */ new Set();
10502
+ for (const item of items) {
10503
+ if (item.adjustments) {
10504
+ for (const adjustment of item.adjustments) {
10505
+ if (adjustment.promotion_id) {
10506
+ promotionIds.add(adjustment.promotion_id);
10507
+ }
10508
+ }
11143
10509
  }
11144
- if (disabled) {
11145
- update[key] = value;
11146
- return;
10510
+ }
10511
+ for (const shippingMethod of shippingMethods) {
10512
+ if (shippingMethod.adjustments) {
10513
+ for (const adjustment of shippingMethod.adjustments) {
10514
+ if (adjustment.promotion_id) {
10515
+ promotionIds.add(adjustment.promotion_id);
10516
+ }
10517
+ }
11147
10518
  }
11148
- key = key.trim();
11149
- value = value.trim();
11150
- if (value === "true") {
11151
- update[key] = true;
11152
- } else if (value === "false") {
11153
- update[key] = false;
11154
- } else {
11155
- const parsedNumber = parseFloat(value);
11156
- if (!isNaN(parsedNumber)) {
11157
- update[key] = parsedNumber;
11158
- } else {
11159
- update[key] = value;
11160
- }
11161
- }
11162
- });
11163
- return update;
11164
- }
11165
- function getHasUneditableRows(metadata) {
11166
- if (!metadata) {
11167
- return false;
11168
10519
  }
11169
- return Object.values(metadata).some(
11170
- (value) => !EDITABLE_TYPES.includes(typeof value)
11171
- );
10520
+ return Array.from(promotionIds);
11172
10521
  }
11173
- const PROMOTION_QUERY_KEY = "promotions";
11174
- const promotionsQueryKeys = {
11175
- list: (query2) => [
11176
- PROMOTION_QUERY_KEY,
11177
- query2 ? query2 : void 0
11178
- ],
11179
- detail: (id, query2) => [
11180
- PROMOTION_QUERY_KEY,
10522
+ const SalesChannel = () => {
10523
+ const { id } = useParams();
10524
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11181
10525
  id,
11182
- query2 ? query2 : void 0
11183
- ]
10526
+ {
10527
+ fields: "+sales_channel_id"
10528
+ },
10529
+ {
10530
+ enabled: !!id
10531
+ }
10532
+ );
10533
+ if (isError) {
10534
+ throw error;
10535
+ }
10536
+ const ISrEADY = !!draft_order && !isPending;
10537
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10538
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10539
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
10540
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
10541
+ ] }),
10542
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
10543
+ ] });
11184
10544
  };
11185
- const usePromotions = (query2, options) => {
11186
- const { data, ...rest } = useQuery({
11187
- queryKey: promotionsQueryKeys.list(query2),
11188
- queryFn: async () => sdk.admin.promotion.list(query2),
11189
- ...options
10545
+ const SalesChannelForm = ({ order }) => {
10546
+ const form = useForm({
10547
+ defaultValues: {
10548
+ sales_channel_id: order.sales_channel_id || ""
10549
+ },
10550
+ resolver: zodResolver(schema$2)
11190
10551
  });
11191
- return { ...data, ...rest };
10552
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10553
+ const { handleSuccess } = useRouteModal();
10554
+ const onSubmit = form.handleSubmit(async (data) => {
10555
+ await mutateAsync(
10556
+ {
10557
+ sales_channel_id: data.sales_channel_id
10558
+ },
10559
+ {
10560
+ onSuccess: () => {
10561
+ toast.success("Sales channel updated");
10562
+ handleSuccess();
10563
+ },
10564
+ onError: (error) => {
10565
+ toast.error(error.message);
10566
+ }
10567
+ }
10568
+ );
10569
+ });
10570
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10571
+ KeyboundForm,
10572
+ {
10573
+ className: "flex flex-1 flex-col overflow-hidden",
10574
+ onSubmit,
10575
+ children: [
10576
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
10577
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10578
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10579
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10580
+ ] }) })
10581
+ ]
10582
+ }
10583
+ ) });
11192
10584
  };
11193
- const Promotions = () => {
10585
+ const SalesChannelField = ({ control, order }) => {
10586
+ const salesChannels = useComboboxData({
10587
+ queryFn: async (params) => {
10588
+ return await sdk.admin.salesChannel.list(params);
10589
+ },
10590
+ queryKey: ["sales-channels"],
10591
+ getOptions: (data) => {
10592
+ return data.sales_channels.map((salesChannel) => ({
10593
+ label: salesChannel.name,
10594
+ value: salesChannel.id
10595
+ }));
10596
+ },
10597
+ defaultValue: order.sales_channel_id || void 0
10598
+ });
10599
+ return /* @__PURE__ */ jsx(
10600
+ Form$2.Field,
10601
+ {
10602
+ control,
10603
+ name: "sales_channel_id",
10604
+ render: ({ field }) => {
10605
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
10606
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
10607
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10608
+ Combobox,
10609
+ {
10610
+ options: salesChannels.options,
10611
+ fetchNextPage: salesChannels.fetchNextPage,
10612
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
10613
+ searchValue: salesChannels.searchValue,
10614
+ onSearchValueChange: salesChannels.onSearchValueChange,
10615
+ placeholder: "Select sales channel",
10616
+ ...field
10617
+ }
10618
+ ) }),
10619
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10620
+ ] });
10621
+ }
10622
+ }
10623
+ );
10624
+ };
10625
+ const schema$2 = objectType({
10626
+ sales_channel_id: stringType().min(1)
10627
+ });
10628
+ function convertNumber(value) {
10629
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10630
+ }
10631
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
10632
+ const Shipping = () => {
10633
+ var _a;
11194
10634
  const { id } = useParams();
10635
+ const { order, isPending, isError, error } = useOrder(id, {
10636
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
10637
+ });
11195
10638
  const {
11196
10639
  order: preview,
10640
+ isPending: isPreviewPending,
11197
10641
  isError: isPreviewError,
11198
10642
  error: previewError
11199
- } = useOrderPreview(id, void 0);
10643
+ } = useOrderPreview(id);
11200
10644
  useInitiateOrderEdit({ preview });
11201
10645
  const { onCancel } = useCancelOrderEdit({ preview });
10646
+ if (isError) {
10647
+ throw error;
10648
+ }
11202
10649
  if (isPreviewError) {
11203
10650
  throw previewError;
11204
10651
  }
11205
- const isReady = !!preview;
11206
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11207
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11208
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11209
- ] });
10652
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
10653
+ const isReady = preview && !isPreviewPending && order && !isPending;
10654
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
10655
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10656
+ /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
10657
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10658
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
10659
+ ] }) }) }),
10660
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
10661
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
10662
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
10663
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10664
+ ] }) });
11210
10665
  };
11211
- const PromotionForm = ({ preview }) => {
11212
- const { items, shipping_methods } = preview;
10666
+ const ShippingForm = ({ preview, order }) => {
10667
+ var _a;
10668
+ const { setIsOpen } = useStackedModal();
11213
10669
  const [isSubmitting, setIsSubmitting] = useState(false);
11214
- const [comboboxValue, setComboboxValue] = useState("");
11215
- const { handleSuccess } = useRouteModal();
11216
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11217
- const promoIds = getPromotionIds(items, shipping_methods);
11218
- const { promotions, isPending, isError, error } = usePromotions(
10670
+ const [data, setData] = useState(null);
10671
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
10672
+ const { shipping_options } = useShippingOptions(
11219
10673
  {
11220
- id: promoIds
10674
+ id: appliedShippingOptionIds,
10675
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11221
10676
  },
11222
10677
  {
11223
- enabled: !!promoIds.length
10678
+ enabled: appliedShippingOptionIds.length > 0
11224
10679
  }
11225
10680
  );
11226
- const comboboxData = useComboboxData({
11227
- queryKey: ["promotions", "combobox", promoIds],
11228
- queryFn: async (params) => {
11229
- return await sdk.admin.promotion.list({
11230
- ...params,
11231
- id: {
11232
- $nin: promoIds
11233
- }
11234
- });
11235
- },
11236
- getOptions: (data) => {
11237
- return data.promotions.map((promotion) => ({
11238
- label: promotion.code,
11239
- value: promotion.code
11240
- }));
11241
- }
11242
- });
11243
- const add = async (value) => {
11244
- if (!value) {
11245
- return;
11246
- }
11247
- addPromotions(
11248
- {
11249
- promo_codes: [value]
11250
- },
11251
- {
11252
- onError: (e) => {
11253
- toast.error(e.message);
11254
- comboboxData.onSearchValueChange("");
11255
- setComboboxValue("");
11256
- },
11257
- onSuccess: () => {
11258
- comboboxData.onSearchValueChange("");
11259
- setComboboxValue("");
11260
- }
11261
- }
11262
- );
11263
- };
10681
+ const uniqueShippingProfiles = useMemo(() => {
10682
+ const profiles = /* @__PURE__ */ new Map();
10683
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
10684
+ profiles.set(profile.id, profile);
10685
+ });
10686
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
10687
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
10688
+ });
10689
+ return Array.from(profiles.values());
10690
+ }, [order.items, shipping_options]);
10691
+ const { handleSuccess } = useRouteModal();
11264
10692
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11265
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10693
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10694
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
10695
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11266
10696
  const onSubmit = async () => {
11267
10697
  setIsSubmitting(true);
11268
10698
  let requestSucceeded = false;
11269
10699
  await requestOrderEdit(void 0, {
11270
10700
  onError: (e) => {
11271
- toast.error(e.message);
10701
+ toast.error(`Failed to request order edit: ${e.message}`);
11272
10702
  },
11273
10703
  onSuccess: () => {
11274
10704
  requestSucceeded = true;
@@ -11280,7 +10710,7 @@ const PromotionForm = ({ preview }) => {
11280
10710
  }
11281
10711
  await confirmOrderEdit(void 0, {
11282
10712
  onError: (e) => {
11283
- toast.error(e.message);
10713
+ toast.error(`Failed to confirm order edit: ${e.message}`);
11284
10714
  },
11285
10715
  onSuccess: () => {
11286
10716
  handleSuccess();
@@ -11290,488 +10720,36 @@ const PromotionForm = ({ preview }) => {
11290
10720
  }
11291
10721
  });
11292
10722
  };
11293
- if (isError) {
11294
- throw error;
11295
- }
11296
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11297
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11298
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11299
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11300
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11301
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11302
- ] }),
11303
- /* @__PURE__ */ jsx(
11304
- Combobox,
11305
- {
11306
- id: "promotion-combobox",
11307
- "aria-describedby": "promotion-combobox-hint",
11308
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11309
- fetchNextPage: comboboxData.fetchNextPage,
11310
- options: comboboxData.options,
11311
- onSearchValueChange: comboboxData.onSearchValueChange,
11312
- searchValue: comboboxData.searchValue,
11313
- disabled: comboboxData.disabled || isAddingPromotions,
11314
- onChange: add,
11315
- value: comboboxValue
11316
- }
11317
- )
11318
- ] }),
11319
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11320
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11321
- PromotionItem,
11322
- {
11323
- promotion,
11324
- orderId: preview.id,
11325
- isLoading: isPending
11326
- },
11327
- promotion.id
11328
- )) })
11329
- ] }) }),
11330
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11331
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11332
- /* @__PURE__ */ jsx(
11333
- Button,
11334
- {
11335
- size: "small",
11336
- type: "submit",
11337
- isLoading: isSubmitting || isAddingPromotions,
11338
- children: "Save"
11339
- }
11340
- )
11341
- ] }) })
11342
- ] });
11343
- };
11344
- const PromotionItem = ({
11345
- promotion,
11346
- orderId,
11347
- isLoading
11348
- }) => {
11349
- var _a;
11350
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11351
- const onRemove = async () => {
11352
- removePromotions(
11353
- {
11354
- promo_codes: [promotion.code]
11355
- },
11356
- {
11357
- onError: (e) => {
11358
- toast.error(e.message);
10723
+ const onKeydown = useCallback(
10724
+ (e) => {
10725
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10726
+ if (data || isSubmitting) {
10727
+ return;
11359
10728
  }
10729
+ onSubmit();
11360
10730
  }
11361
- );
11362
- };
11363
- const displayValue = getDisplayValue(promotion);
11364
- return /* @__PURE__ */ jsxs(
11365
- "div",
11366
- {
11367
- className: clx(
11368
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11369
- {
11370
- "animate-pulse": isLoading
11371
- }
11372
- ),
11373
- children: [
11374
- /* @__PURE__ */ jsxs("div", { children: [
11375
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11376
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11377
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11378
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11379
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11380
- ] }),
11381
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11382
- ] })
11383
- ] }),
11384
- /* @__PURE__ */ jsx(
11385
- IconButton,
11386
- {
11387
- size: "small",
11388
- type: "button",
11389
- variant: "transparent",
11390
- onClick: onRemove,
11391
- isLoading: isPending || isLoading,
11392
- children: /* @__PURE__ */ jsx(XMark, {})
11393
- }
11394
- )
11395
- ]
11396
10731
  },
11397
- promotion.id
10732
+ [data, isSubmitting, onSubmit]
11398
10733
  );
11399
- };
11400
- function getDisplayValue(promotion) {
11401
- var _a, _b, _c, _d;
11402
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11403
- if (!value) {
11404
- return null;
11405
- }
11406
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11407
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11408
- if (!currency) {
11409
- return null;
11410
- }
11411
- return getLocaleAmount(value, currency);
11412
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11413
- return formatPercentage(value);
11414
- }
11415
- return null;
11416
- }
11417
- const formatter = new Intl.NumberFormat([], {
11418
- style: "percent",
11419
- minimumFractionDigits: 2
11420
- });
11421
- const formatPercentage = (value, isPercentageValue = false) => {
11422
- let val = value || 0;
11423
- if (!isPercentageValue) {
11424
- val = val / 100;
11425
- }
11426
- return formatter.format(val);
11427
- };
11428
- function getPromotionIds(items, shippingMethods) {
11429
- const promotionIds = /* @__PURE__ */ new Set();
11430
- for (const item of items) {
11431
- if (item.adjustments) {
11432
- for (const adjustment of item.adjustments) {
11433
- if (adjustment.promotion_id) {
11434
- promotionIds.add(adjustment.promotion_id);
11435
- }
11436
- }
11437
- }
11438
- }
11439
- for (const shippingMethod of shippingMethods) {
11440
- if (shippingMethod.adjustments) {
11441
- for (const adjustment of shippingMethod.adjustments) {
11442
- if (adjustment.promotion_id) {
11443
- promotionIds.add(adjustment.promotion_id);
11444
- }
11445
- }
11446
- }
11447
- }
11448
- return Array.from(promotionIds);
11449
- }
11450
- const ShippingAddress = () => {
11451
- const { id } = useParams();
11452
- const { order, isPending, isError, error } = useOrder(id, {
11453
- fields: "+shipping_address"
11454
- });
11455
- if (isError) {
11456
- throw error;
11457
- }
11458
- const isReady = !isPending && !!order;
11459
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11460
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11461
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
11462
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
11463
- ] }),
11464
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
11465
- ] });
11466
- };
11467
- const ShippingAddressForm = ({ order }) => {
11468
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
11469
- const form = useForm({
11470
- defaultValues: {
11471
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11472
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11473
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11474
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11475
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11476
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11477
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11478
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11479
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11480
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
11481
- },
11482
- resolver: zodResolver(schema$2)
11483
- });
11484
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11485
- const { handleSuccess } = useRouteModal();
11486
- const onSubmit = form.handleSubmit(async (data) => {
11487
- await mutateAsync(
11488
- {
11489
- shipping_address: {
11490
- first_name: data.first_name,
11491
- last_name: data.last_name,
11492
- company: data.company,
11493
- address_1: data.address_1,
11494
- address_2: data.address_2,
11495
- city: data.city,
11496
- province: data.province,
11497
- country_code: data.country_code,
11498
- postal_code: data.postal_code,
11499
- phone: data.phone
11500
- }
11501
- },
11502
- {
11503
- onSuccess: () => {
11504
- handleSuccess();
11505
- },
11506
- onError: (error) => {
11507
- toast.error(error.message);
11508
- }
11509
- }
11510
- );
11511
- });
11512
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11513
- KeyboundForm,
11514
- {
11515
- className: "flex flex-1 flex-col overflow-hidden",
11516
- onSubmit,
11517
- children: [
11518
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
11519
- /* @__PURE__ */ jsx(
11520
- Form$2.Field,
11521
- {
11522
- control: form.control,
11523
- name: "country_code",
11524
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11525
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
11526
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
11527
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11528
- ] })
11529
- }
11530
- ),
11531
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
10734
+ useEffect(() => {
10735
+ document.addEventListener("keydown", onKeydown);
10736
+ return () => {
10737
+ document.removeEventListener("keydown", onKeydown);
10738
+ };
10739
+ }, [onKeydown]);
10740
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10741
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10742
+ /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
10743
+ /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
10744
+ /* @__PURE__ */ jsxs("div", { children: [
10745
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10746
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
10747
+ ] }),
10748
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10749
+ /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
10750
+ /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11532
10751
  /* @__PURE__ */ jsx(
11533
- Form$2.Field,
11534
- {
11535
- control: form.control,
11536
- name: "first_name",
11537
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11538
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
11539
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11540
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11541
- ] })
11542
- }
11543
- ),
11544
- /* @__PURE__ */ jsx(
11545
- Form$2.Field,
11546
- {
11547
- control: form.control,
11548
- name: "last_name",
11549
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11550
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
11551
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11552
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11553
- ] })
11554
- }
11555
- )
11556
- ] }),
11557
- /* @__PURE__ */ jsx(
11558
- Form$2.Field,
11559
- {
11560
- control: form.control,
11561
- name: "company",
11562
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11563
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
11564
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11565
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11566
- ] })
11567
- }
11568
- ),
11569
- /* @__PURE__ */ jsx(
11570
- Form$2.Field,
11571
- {
11572
- control: form.control,
11573
- name: "address_1",
11574
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11575
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
11576
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11577
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11578
- ] })
11579
- }
11580
- ),
11581
- /* @__PURE__ */ jsx(
11582
- Form$2.Field,
11583
- {
11584
- control: form.control,
11585
- name: "address_2",
11586
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11587
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11588
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11589
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11590
- ] })
11591
- }
11592
- ),
11593
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11594
- /* @__PURE__ */ jsx(
11595
- Form$2.Field,
11596
- {
11597
- control: form.control,
11598
- name: "postal_code",
11599
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11600
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
11601
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11602
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11603
- ] })
11604
- }
11605
- ),
11606
- /* @__PURE__ */ jsx(
11607
- Form$2.Field,
11608
- {
11609
- control: form.control,
11610
- name: "city",
11611
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11612
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
11613
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11614
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11615
- ] })
11616
- }
11617
- )
11618
- ] }),
11619
- /* @__PURE__ */ jsx(
11620
- Form$2.Field,
11621
- {
11622
- control: form.control,
11623
- name: "province",
11624
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11625
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11626
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11627
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11628
- ] })
11629
- }
11630
- ),
11631
- /* @__PURE__ */ jsx(
11632
- Form$2.Field,
11633
- {
11634
- control: form.control,
11635
- name: "phone",
11636
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11637
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
11638
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11639
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11640
- ] })
11641
- }
11642
- )
11643
- ] }) }),
11644
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11645
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11646
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11647
- ] }) })
11648
- ]
11649
- }
11650
- ) });
11651
- };
11652
- const schema$2 = addressSchema;
11653
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11654
- const Shipping = () => {
11655
- var _a;
11656
- const { id } = useParams();
11657
- const { order, isPending, isError, error } = useOrder(id, {
11658
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11659
- });
11660
- const {
11661
- order: preview,
11662
- isPending: isPreviewPending,
11663
- isError: isPreviewError,
11664
- error: previewError
11665
- } = useOrderPreview(id);
11666
- useInitiateOrderEdit({ preview });
11667
- const { onCancel } = useCancelOrderEdit({ preview });
11668
- if (isError) {
11669
- throw error;
11670
- }
11671
- if (isPreviewError) {
11672
- throw previewError;
11673
- }
11674
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11675
- const isReady = preview && !isPreviewPending && order && !isPending;
11676
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11677
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11678
- /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11679
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11680
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11681
- ] }) }) }),
11682
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11683
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11684
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11685
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11686
- ] }) });
11687
- };
11688
- const ShippingForm = ({ preview, order }) => {
11689
- var _a;
11690
- const { setIsOpen } = useStackedModal();
11691
- const [isSubmitting, setIsSubmitting] = useState(false);
11692
- const [data, setData] = useState(null);
11693
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11694
- const { shipping_options } = useShippingOptions(
11695
- {
11696
- id: appliedShippingOptionIds,
11697
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11698
- },
11699
- {
11700
- enabled: appliedShippingOptionIds.length > 0
11701
- }
11702
- );
11703
- const uniqueShippingProfiles = useMemo(() => {
11704
- const profiles = /* @__PURE__ */ new Map();
11705
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11706
- profiles.set(profile.id, profile);
11707
- });
11708
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11709
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11710
- });
11711
- return Array.from(profiles.values());
11712
- }, [order.items, shipping_options]);
11713
- const { handleSuccess } = useRouteModal();
11714
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11715
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11716
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11717
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11718
- const onSubmit = async () => {
11719
- setIsSubmitting(true);
11720
- let requestSucceeded = false;
11721
- await requestOrderEdit(void 0, {
11722
- onError: (e) => {
11723
- toast.error(`Failed to request order edit: ${e.message}`);
11724
- },
11725
- onSuccess: () => {
11726
- requestSucceeded = true;
11727
- }
11728
- });
11729
- if (!requestSucceeded) {
11730
- setIsSubmitting(false);
11731
- return;
11732
- }
11733
- await confirmOrderEdit(void 0, {
11734
- onError: (e) => {
11735
- toast.error(`Failed to confirm order edit: ${e.message}`);
11736
- },
11737
- onSuccess: () => {
11738
- handleSuccess();
11739
- },
11740
- onSettled: () => {
11741
- setIsSubmitting(false);
11742
- }
11743
- });
11744
- };
11745
- const onKeydown = useCallback(
11746
- (e) => {
11747
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11748
- if (data || isSubmitting) {
11749
- return;
11750
- }
11751
- onSubmit();
11752
- }
11753
- },
11754
- [data, isSubmitting, onSubmit]
11755
- );
11756
- useEffect(() => {
11757
- document.addEventListener("keydown", onKeydown);
11758
- return () => {
11759
- document.removeEventListener("keydown", onKeydown);
11760
- };
11761
- }, [onKeydown]);
11762
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11763
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11764
- /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11765
- /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 py-16 px-6", children: [
11766
- /* @__PURE__ */ jsxs("div", { children: [
11767
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11768
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
11769
- ] }),
11770
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11771
- /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11772
- /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11773
- /* @__PURE__ */ jsx(
11774
- Text,
10752
+ Text,
11775
10753
  {
11776
10754
  size: "xsmall",
11777
10755
  weight: "plus",
@@ -11957,7 +10935,7 @@ const ShippingForm = ({ preview, order }) => {
11957
10935
  ]
11958
10936
  }
11959
10937
  ) : /* @__PURE__ */ jsx(
11960
- StackedModalTrigger,
10938
+ StackedModalTrigger$1,
11961
10939
  {
11962
10940
  shippingProfileId: profile.id,
11963
10941
  shippingOption,
@@ -12068,7 +11046,7 @@ const ShippingForm = ({ preview, order }) => {
12068
11046
  ] }) })
12069
11047
  ] });
12070
11048
  };
12071
- const StackedModalTrigger = ({
11049
+ const StackedModalTrigger$1 = ({
12072
11050
  shippingProfileId,
12073
11051
  shippingOption,
12074
11052
  shippingMethod,
@@ -12457,33 +11435,236 @@ const CustomAmountField = ({
12457
11435
  }
12458
11436
  );
12459
11437
  };
12460
- const TransferOwnership = () => {
11438
+ const ShippingAddress = () => {
12461
11439
  const { id } = useParams();
12462
- const { draft_order, isPending, isError, error } = useDraftOrder(id, {
12463
- fields: "id,customer_id,customer.*"
11440
+ const { order, isPending, isError, error } = useOrder(id, {
11441
+ fields: "+shipping_address"
12464
11442
  });
12465
11443
  if (isError) {
12466
11444
  throw error;
12467
11445
  }
12468
- const isReady = !isPending && !!draft_order;
11446
+ const isReady = !isPending && !!order;
12469
11447
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12470
11448
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12471
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
12472
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
11449
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
11450
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12473
11451
  ] }),
12474
- isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
11452
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12475
11453
  ] });
12476
11454
  };
12477
- const TransferOwnershipForm = ({ order }) => {
12478
- var _a, _b;
11455
+ const ShippingAddressForm = ({ order }) => {
11456
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12479
11457
  const form = useForm({
12480
11458
  defaultValues: {
12481
- customer_id: order.customer_id || ""
11459
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
11460
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
11461
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
11462
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
11463
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
11464
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
11465
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
11466
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
11467
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
11468
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12482
11469
  },
12483
11470
  resolver: zodResolver(schema$1)
12484
11471
  });
12485
11472
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12486
11473
  const { handleSuccess } = useRouteModal();
11474
+ const onSubmit = form.handleSubmit(async (data) => {
11475
+ await mutateAsync(
11476
+ {
11477
+ shipping_address: {
11478
+ first_name: data.first_name,
11479
+ last_name: data.last_name,
11480
+ company: data.company,
11481
+ address_1: data.address_1,
11482
+ address_2: data.address_2,
11483
+ city: data.city,
11484
+ province: data.province,
11485
+ country_code: data.country_code,
11486
+ postal_code: data.postal_code,
11487
+ phone: data.phone
11488
+ }
11489
+ },
11490
+ {
11491
+ onSuccess: () => {
11492
+ handleSuccess();
11493
+ },
11494
+ onError: (error) => {
11495
+ toast.error(error.message);
11496
+ }
11497
+ }
11498
+ );
11499
+ });
11500
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11501
+ KeyboundForm,
11502
+ {
11503
+ className: "flex flex-1 flex-col overflow-hidden",
11504
+ onSubmit,
11505
+ children: [
11506
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
11507
+ /* @__PURE__ */ jsx(
11508
+ Form$2.Field,
11509
+ {
11510
+ control: form.control,
11511
+ name: "country_code",
11512
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11513
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
11514
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
11515
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11516
+ ] })
11517
+ }
11518
+ ),
11519
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11520
+ /* @__PURE__ */ jsx(
11521
+ Form$2.Field,
11522
+ {
11523
+ control: form.control,
11524
+ name: "first_name",
11525
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11526
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
11527
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11528
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11529
+ ] })
11530
+ }
11531
+ ),
11532
+ /* @__PURE__ */ jsx(
11533
+ Form$2.Field,
11534
+ {
11535
+ control: form.control,
11536
+ name: "last_name",
11537
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11538
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
11539
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11540
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11541
+ ] })
11542
+ }
11543
+ )
11544
+ ] }),
11545
+ /* @__PURE__ */ jsx(
11546
+ Form$2.Field,
11547
+ {
11548
+ control: form.control,
11549
+ name: "company",
11550
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11551
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
11552
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11553
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11554
+ ] })
11555
+ }
11556
+ ),
11557
+ /* @__PURE__ */ jsx(
11558
+ Form$2.Field,
11559
+ {
11560
+ control: form.control,
11561
+ name: "address_1",
11562
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11563
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
11564
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11565
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11566
+ ] })
11567
+ }
11568
+ ),
11569
+ /* @__PURE__ */ jsx(
11570
+ Form$2.Field,
11571
+ {
11572
+ control: form.control,
11573
+ name: "address_2",
11574
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11575
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
11576
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11577
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11578
+ ] })
11579
+ }
11580
+ ),
11581
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
11582
+ /* @__PURE__ */ jsx(
11583
+ Form$2.Field,
11584
+ {
11585
+ control: form.control,
11586
+ name: "postal_code",
11587
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11588
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
11589
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11590
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11591
+ ] })
11592
+ }
11593
+ ),
11594
+ /* @__PURE__ */ jsx(
11595
+ Form$2.Field,
11596
+ {
11597
+ control: form.control,
11598
+ name: "city",
11599
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11600
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
11601
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11602
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11603
+ ] })
11604
+ }
11605
+ )
11606
+ ] }),
11607
+ /* @__PURE__ */ jsx(
11608
+ Form$2.Field,
11609
+ {
11610
+ control: form.control,
11611
+ name: "province",
11612
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11613
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
11614
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11615
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11616
+ ] })
11617
+ }
11618
+ ),
11619
+ /* @__PURE__ */ jsx(
11620
+ Form$2.Field,
11621
+ {
11622
+ control: form.control,
11623
+ name: "phone",
11624
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11625
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
11626
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11627
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11628
+ ] })
11629
+ }
11630
+ )
11631
+ ] }) }),
11632
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11633
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11634
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11635
+ ] }) })
11636
+ ]
11637
+ }
11638
+ ) });
11639
+ };
11640
+ const schema$1 = addressSchema;
11641
+ const TransferOwnership = () => {
11642
+ const { id } = useParams();
11643
+ const { draft_order, isPending, isError, error } = useDraftOrder(id, {
11644
+ fields: "id,customer_id,customer.*"
11645
+ });
11646
+ if (isError) {
11647
+ throw error;
11648
+ }
11649
+ const isReady = !isPending && !!draft_order;
11650
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11651
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11652
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Transfer Ownership" }) }),
11653
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Transfer the ownership of this draft order to a new customer" }) })
11654
+ ] }),
11655
+ isReady && /* @__PURE__ */ jsx(TransferOwnershipForm, { order: draft_order })
11656
+ ] });
11657
+ };
11658
+ const TransferOwnershipForm = ({ order }) => {
11659
+ var _a, _b;
11660
+ const form = useForm({
11661
+ defaultValues: {
11662
+ customer_id: order.customer_id || ""
11663
+ },
11664
+ resolver: zodResolver(schema)
11665
+ });
11666
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11667
+ const { handleSuccess } = useRouteModal();
12487
11668
  const name = [(_a = order.customer) == null ? void 0 : _a.first_name, (_b = order.customer) == null ? void 0 : _b.last_name].filter(Boolean).join(" ");
12488
11669
  const currentCustomer = order.customer ? {
12489
11670
  label: name ? `${name} (${order.customer.email})` : order.customer.email,
@@ -12802,242 +11983,1061 @@ const Illustration = () => {
12802
11983
  transform: "matrix(0.865865 0.500278 -0.871576 0.490261 105.4 62.5457)",
12803
11984
  fill: "#A1A1AA"
12804
11985
  }
12805
- ),
11986
+ ),
11987
+ /* @__PURE__ */ jsx(
11988
+ "path",
11989
+ {
11990
+ d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
11991
+ fill: "#52525B"
11992
+ }
11993
+ ),
11994
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsx(
11995
+ "path",
11996
+ {
11997
+ d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
11998
+ stroke: "#A1A1AA",
11999
+ strokeWidth: "1.5",
12000
+ strokeLinecap: "round",
12001
+ strokeLinejoin: "round"
12002
+ }
12003
+ ) }),
12004
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsx(
12005
+ "path",
12006
+ {
12007
+ d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
12008
+ stroke: "#A1A1AA",
12009
+ strokeWidth: "1.5",
12010
+ strokeLinecap: "round",
12011
+ strokeLinejoin: "round"
12012
+ }
12013
+ ) }),
12014
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsx(
12015
+ "path",
12016
+ {
12017
+ d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12018
+ stroke: "#A1A1AA",
12019
+ strokeWidth: "1.5",
12020
+ strokeLinecap: "round",
12021
+ strokeLinejoin: "round"
12022
+ }
12023
+ ) }),
12024
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsx(
12025
+ "path",
12026
+ {
12027
+ d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12028
+ stroke: "#A1A1AA",
12029
+ strokeWidth: "1.5",
12030
+ strokeLinecap: "round",
12031
+ strokeLinejoin: "round"
12032
+ }
12033
+ ) }),
12034
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsx(
12035
+ "path",
12036
+ {
12037
+ d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12038
+ stroke: "#A1A1AA",
12039
+ strokeWidth: "1.5",
12040
+ strokeLinecap: "round",
12041
+ strokeLinejoin: "round"
12042
+ }
12043
+ ) }),
12044
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsx(
12045
+ "path",
12046
+ {
12047
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12048
+ stroke: "#A1A1AA",
12049
+ strokeWidth: "1.5",
12050
+ strokeLinecap: "round",
12051
+ strokeLinejoin: "round"
12052
+ }
12053
+ ) }),
12054
+ /* @__PURE__ */ jsxs("defs", { children: [
12055
+ /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
12056
+ "rect",
12057
+ {
12058
+ width: "12",
12059
+ height: "12",
12060
+ fill: "white",
12061
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12062
+ }
12063
+ ) }),
12064
+ /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
12065
+ "rect",
12066
+ {
12067
+ width: "12",
12068
+ height: "12",
12069
+ fill: "white",
12070
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12071
+ }
12072
+ ) }),
12073
+ /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
12074
+ "rect",
12075
+ {
12076
+ width: "12",
12077
+ height: "12",
12078
+ fill: "white",
12079
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12080
+ }
12081
+ ) }),
12082
+ /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
12083
+ "rect",
12084
+ {
12085
+ width: "12",
12086
+ height: "12",
12087
+ fill: "white",
12088
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12089
+ }
12090
+ ) }),
12091
+ /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
12092
+ "rect",
12093
+ {
12094
+ width: "12",
12095
+ height: "12",
12096
+ fill: "white",
12097
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12098
+ }
12099
+ ) }),
12100
+ /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
12101
+ "rect",
12102
+ {
12103
+ width: "12",
12104
+ height: "12",
12105
+ fill: "white",
12106
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12107
+ }
12108
+ ) })
12109
+ ] })
12110
+ ]
12111
+ }
12112
+ );
12113
+ };
12114
+ const schema = objectType({
12115
+ customer_id: stringType().min(1)
12116
+ });
12117
+ const NumberInput = forwardRef(
12118
+ ({
12119
+ value,
12120
+ onChange,
12121
+ size = "base",
12122
+ min = 0,
12123
+ max = 100,
12124
+ step = 1,
12125
+ className,
12126
+ disabled,
12127
+ ...props
12128
+ }, ref) => {
12129
+ const handleChange = (event) => {
12130
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
12131
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
12132
+ onChange(newValue);
12133
+ }
12134
+ };
12135
+ const handleIncrement = () => {
12136
+ const newValue = value + step;
12137
+ if (max === void 0 || newValue <= max) {
12138
+ onChange(newValue);
12139
+ }
12140
+ };
12141
+ const handleDecrement = () => {
12142
+ const newValue = value - step;
12143
+ if (min === void 0 || newValue >= min) {
12144
+ onChange(newValue);
12145
+ }
12146
+ };
12147
+ return /* @__PURE__ */ jsxs(
12148
+ "div",
12149
+ {
12150
+ className: clx(
12151
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
12152
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
12153
+ {
12154
+ "h-7": size === "small",
12155
+ "h-8": size === "base"
12156
+ },
12157
+ className
12158
+ ),
12159
+ children: [
12160
+ /* @__PURE__ */ jsx(
12161
+ "input",
12162
+ {
12163
+ ref,
12164
+ type: "number",
12165
+ value,
12166
+ onChange: handleChange,
12167
+ min,
12168
+ max,
12169
+ step,
12170
+ className: clx(
12171
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
12172
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
12173
+ "placeholder:text-ui-fg-muted"
12174
+ ),
12175
+ ...props
12176
+ }
12177
+ ),
12178
+ /* @__PURE__ */ jsxs(
12179
+ "button",
12180
+ {
12181
+ className: clx(
12182
+ "flex items-center justify-center outline-none transition-fg",
12183
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12184
+ "focus:bg-ui-bg-field-component-hover",
12185
+ "hover:bg-ui-bg-field-component-hover",
12186
+ {
12187
+ "size-7": size === "small",
12188
+ "size-8": size === "base"
12189
+ }
12190
+ ),
12191
+ type: "button",
12192
+ onClick: handleDecrement,
12193
+ disabled: min !== void 0 && value <= min || disabled,
12194
+ children: [
12195
+ /* @__PURE__ */ jsx(Minus, {}),
12196
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
12197
+ ]
12198
+ }
12199
+ ),
12200
+ /* @__PURE__ */ jsxs(
12201
+ "button",
12202
+ {
12203
+ className: clx(
12204
+ "flex items-center justify-center outline-none transition-fg",
12205
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12206
+ "focus:bg-ui-bg-field-hover",
12207
+ "hover:bg-ui-bg-field-hover",
12208
+ {
12209
+ "size-7": size === "small",
12210
+ "size-8": size === "base"
12211
+ }
12212
+ ),
12213
+ type: "button",
12214
+ onClick: handleIncrement,
12215
+ disabled: max !== void 0 && value >= max || disabled,
12216
+ children: [
12217
+ /* @__PURE__ */ jsx(Plus, {}),
12218
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
12219
+ ]
12220
+ }
12221
+ )
12222
+ ]
12223
+ }
12224
+ );
12225
+ }
12226
+ );
12227
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
12228
+ const productVariantsQueryKeys = {
12229
+ list: (query2) => [
12230
+ PRODUCT_VARIANTS_QUERY_KEY,
12231
+ query2 ? query2 : void 0
12232
+ ]
12233
+ };
12234
+ const useProductVariants = (query2, options) => {
12235
+ const { data, ...rest } = useQuery({
12236
+ queryKey: productVariantsQueryKeys.list(query2),
12237
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
12238
+ ...options
12239
+ });
12240
+ return { ...data, ...rest };
12241
+ };
12242
+ const STACKED_MODAL_ID = "items_stacked_modal";
12243
+ const Items = () => {
12244
+ const { id } = useParams();
12245
+ const {
12246
+ order: preview,
12247
+ isPending: isPreviewPending,
12248
+ isError: isPreviewError,
12249
+ error: previewError
12250
+ } = useOrderPreview(id, void 0, {
12251
+ placeholderData: keepPreviousData
12252
+ });
12253
+ useInitiateOrderEdit({ preview });
12254
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12255
+ id,
12256
+ {
12257
+ fields: "currency_code"
12258
+ },
12259
+ {
12260
+ enabled: !!id
12261
+ }
12262
+ );
12263
+ const { onCancel } = useCancelOrderEdit({ preview });
12264
+ if (isError) {
12265
+ throw error;
12266
+ }
12267
+ if (isPreviewError) {
12268
+ throw previewError;
12269
+ }
12270
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
12271
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
12272
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
12273
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
12274
+ ] }) });
12275
+ };
12276
+ const ItemsForm = ({ preview, currencyCode }) => {
12277
+ var _a;
12278
+ const [isSubmitting, setIsSubmitting] = useState(false);
12279
+ const [modalContent, setModalContent] = useState(
12280
+ null
12281
+ );
12282
+ const { handleSuccess } = useRouteModal();
12283
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
12284
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12285
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
12286
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
12287
+ const matches = useMemo(() => {
12288
+ return matchSorter(preview.items, query2, {
12289
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
12290
+ });
12291
+ }, [preview.items, query2]);
12292
+ const onSubmit = async () => {
12293
+ setIsSubmitting(true);
12294
+ let requestSucceeded = false;
12295
+ await requestOrderEdit(void 0, {
12296
+ onError: (e) => {
12297
+ toast.error(`Failed to request order edit: ${e.message}`);
12298
+ },
12299
+ onSuccess: () => {
12300
+ requestSucceeded = true;
12301
+ }
12302
+ });
12303
+ if (!requestSucceeded) {
12304
+ setIsSubmitting(false);
12305
+ return;
12306
+ }
12307
+ await confirmOrderEdit(void 0, {
12308
+ onError: (e) => {
12309
+ toast.error(`Failed to confirm order edit: ${e.message}`);
12310
+ },
12311
+ onSuccess: () => {
12312
+ handleSuccess();
12313
+ },
12314
+ onSettled: () => {
12315
+ setIsSubmitting(false);
12316
+ }
12317
+ });
12318
+ };
12319
+ const onKeyDown = useCallback(
12320
+ (e) => {
12321
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
12322
+ if (modalContent || isSubmitting) {
12323
+ return;
12324
+ }
12325
+ onSubmit();
12326
+ }
12327
+ },
12328
+ [modalContent, isSubmitting, onSubmit]
12329
+ );
12330
+ useEffect(() => {
12331
+ document.addEventListener("keydown", onKeyDown);
12332
+ return () => {
12333
+ document.removeEventListener("keydown", onKeyDown);
12334
+ };
12335
+ }, [onKeyDown]);
12336
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
12337
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
12338
+ /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxs(
12339
+ StackedFocusModal,
12340
+ {
12341
+ id: STACKED_MODAL_ID,
12342
+ onOpenChangeCallback: (open) => {
12343
+ if (!open) {
12344
+ setModalContent(null);
12345
+ }
12346
+ },
12347
+ children: [
12348
+ /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
12349
+ /* @__PURE__ */ jsxs("div", { children: [
12350
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Items" }) }),
12351
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Edit the items in the draft order" }) })
12352
+ ] }),
12353
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12354
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12355
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
12356
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12357
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
12358
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
12359
+ ] }),
12360
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
12361
+ /* @__PURE__ */ jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsx(
12362
+ Input,
12363
+ {
12364
+ type: "search",
12365
+ placeholder: "Search items",
12366
+ value: searchValue,
12367
+ onChange: (e) => onSearchValueChange(e.target.value)
12368
+ }
12369
+ ) }),
12370
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
12371
+ /* @__PURE__ */ jsx(DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(IconButton, { type: "button", children: /* @__PURE__ */ jsx(Plus, {}) }) }),
12372
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
12373
+ /* @__PURE__ */ jsx(
12374
+ StackedModalTrigger,
12375
+ {
12376
+ type: "add-items",
12377
+ setModalContent
12378
+ }
12379
+ ),
12380
+ /* @__PURE__ */ jsx(
12381
+ StackedModalTrigger,
12382
+ {
12383
+ type: "add-custom-item",
12384
+ setModalContent
12385
+ }
12386
+ )
12387
+ ] })
12388
+ ] })
12389
+ ] })
12390
+ ] }),
12391
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12392
+ /* @__PURE__ */ jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-muted grid grid-cols-[2fr_1fr_2fr_28px] gap-3 px-4 py-2", children: [
12393
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12394
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) }),
12395
+ /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Price" }) }),
12396
+ /* @__PURE__ */ jsx("div", {})
12397
+ ] }) }),
12398
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12399
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
12400
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
12401
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsx(
12402
+ Item,
12403
+ {
12404
+ item,
12405
+ preview,
12406
+ currencyCode
12407
+ },
12408
+ item.id
12409
+ )) : /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12410
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12411
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12412
+ 'No items found for "',
12413
+ query2,
12414
+ '".'
12415
+ ] })
12416
+ ] }) })
12417
+ ] })
12418
+ ] }),
12419
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12420
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
12421
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
12422
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsxs(
12423
+ Text,
12424
+ {
12425
+ size: "small",
12426
+ leading: "compact",
12427
+ className: "text-ui-fg-subtle",
12428
+ children: [
12429
+ itemCount,
12430
+ " ",
12431
+ itemCount === 1 ? "item" : "items"
12432
+ ]
12433
+ }
12434
+ ) }),
12435
+ /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
12436
+ ] })
12437
+ ] }) }),
12438
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsx(
12439
+ CustomItemForm,
12440
+ {
12441
+ orderId: preview.id,
12442
+ currencyCode
12443
+ }
12444
+ ) : null)
12445
+ ]
12446
+ }
12447
+ ) }),
12448
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12449
+ /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12450
+ /* @__PURE__ */ jsx(
12451
+ Button,
12452
+ {
12453
+ size: "small",
12454
+ type: "button",
12455
+ onClick: onSubmit,
12456
+ isLoading: isSubmitting,
12457
+ children: "Save"
12458
+ }
12459
+ )
12460
+ ] }) })
12461
+ ] });
12462
+ };
12463
+ const Item = ({ item, preview, currencyCode }) => {
12464
+ if (item.variant_id) {
12465
+ return /* @__PURE__ */ jsx(VariantItem, { item, preview, currencyCode });
12466
+ }
12467
+ return /* @__PURE__ */ jsx(CustomItem, { item, preview, currencyCode });
12468
+ };
12469
+ const VariantItem = ({ item, preview, currencyCode }) => {
12470
+ const [editing, setEditing] = useState(false);
12471
+ const form = useForm({
12472
+ defaultValues: {
12473
+ quantity: item.quantity,
12474
+ unit_price: item.unit_price
12475
+ },
12476
+ resolver: zodResolver(variantItemSchema)
12477
+ });
12478
+ const actionId = useMemo(() => {
12479
+ var _a, _b;
12480
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12481
+ }, [item]);
12482
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12483
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12484
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12485
+ const onSubmit = form.handleSubmit(async (data) => {
12486
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
12487
+ setEditing(false);
12488
+ return;
12489
+ }
12490
+ if (!actionId) {
12491
+ await updateOriginalItem(
12492
+ {
12493
+ item_id: item.id,
12494
+ quantity: data.quantity,
12495
+ unit_price: convertNumber(data.unit_price)
12496
+ },
12497
+ {
12498
+ onSuccess: () => {
12499
+ setEditing(false);
12500
+ },
12501
+ onError: (e) => {
12502
+ toast.error(e.message);
12503
+ }
12504
+ }
12505
+ );
12506
+ return;
12507
+ }
12508
+ await updateActionItem(
12509
+ {
12510
+ action_id: actionId,
12511
+ quantity: data.quantity,
12512
+ unit_price: convertNumber(data.unit_price)
12513
+ },
12514
+ {
12515
+ onSuccess: () => {
12516
+ setEditing(false);
12517
+ },
12518
+ onError: (e) => {
12519
+ toast.error(e.message);
12520
+ }
12521
+ }
12522
+ );
12523
+ });
12524
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
12525
+ /* @__PURE__ */ jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
12526
+ /* @__PURE__ */ jsx(
12527
+ Thumbnail,
12528
+ {
12529
+ thumbnail: item.thumbnail,
12530
+ alt: item.product_title ?? void 0
12531
+ }
12532
+ ),
12533
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12534
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12535
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12536
+ /* @__PURE__ */ jsxs(
12537
+ Text,
12538
+ {
12539
+ size: "small",
12540
+ leading: "compact",
12541
+ className: "text-ui-fg-subtle",
12542
+ children: [
12543
+ "(",
12544
+ item.variant_title,
12545
+ ")"
12546
+ ]
12547
+ }
12548
+ )
12549
+ ] }),
12806
12550
  /* @__PURE__ */ jsx(
12807
- "path",
12808
- {
12809
- d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
12810
- fill: "#52525B"
12811
- }
12812
- ),
12813
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsx(
12814
- "path",
12815
- {
12816
- d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
12817
- stroke: "#A1A1AA",
12818
- strokeWidth: "1.5",
12819
- strokeLinecap: "round",
12820
- strokeLinejoin: "round"
12821
- }
12822
- ) }),
12823
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsx(
12824
- "path",
12825
- {
12826
- d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
12827
- stroke: "#A1A1AA",
12828
- strokeWidth: "1.5",
12829
- strokeLinecap: "round",
12830
- strokeLinejoin: "round"
12831
- }
12832
- ) }),
12833
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsx(
12834
- "path",
12551
+ Text,
12835
12552
  {
12836
- d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12837
- stroke: "#A1A1AA",
12838
- strokeWidth: "1.5",
12839
- strokeLinecap: "round",
12840
- strokeLinejoin: "round"
12553
+ size: "small",
12554
+ leading: "compact",
12555
+ className: "text-ui-fg-subtle",
12556
+ children: item.variant_sku
12841
12557
  }
12842
- ) }),
12843
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsx(
12844
- "path",
12845
- {
12846
- d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12847
- stroke: "#A1A1AA",
12848
- strokeWidth: "1.5",
12849
- strokeLinecap: "round",
12850
- strokeLinejoin: "round"
12558
+ )
12559
+ ] })
12560
+ ] }),
12561
+ editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
12562
+ Form$2.Field,
12563
+ {
12564
+ control: form.control,
12565
+ name: "quantity",
12566
+ render: ({ field }) => {
12567
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
12568
+ }
12569
+ }
12570
+ ) }) : /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
12571
+ editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
12572
+ Form$2.Field,
12573
+ {
12574
+ control: form.control,
12575
+ name: "unit_price",
12576
+ render: ({ field: { onChange, ...field } }) => {
12577
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12578
+ CurrencyInput,
12579
+ {
12580
+ ...field,
12581
+ symbol: getNativeSymbol(currencyCode),
12582
+ code: currencyCode,
12583
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12584
+ }
12585
+ ) }) });
12586
+ }
12587
+ }
12588
+ ) }) : /* @__PURE__ */ jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12589
+ /* @__PURE__ */ jsx(
12590
+ IconButton,
12591
+ {
12592
+ type: "button",
12593
+ size: "small",
12594
+ onClick: editing ? onSubmit : () => {
12595
+ setEditing(true);
12596
+ },
12597
+ disabled: isPending,
12598
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
12599
+ }
12600
+ )
12601
+ ] }) }) });
12602
+ };
12603
+ const variantItemSchema = objectType({
12604
+ quantity: numberType(),
12605
+ unit_price: unionType([numberType(), stringType()])
12606
+ });
12607
+ const CustomItem = ({ item, preview, currencyCode }) => {
12608
+ const [editing, setEditing] = useState(false);
12609
+ const { quantity, unit_price, title } = item;
12610
+ const form = useForm({
12611
+ defaultValues: {
12612
+ title,
12613
+ quantity,
12614
+ unit_price
12615
+ },
12616
+ resolver: zodResolver(customItemSchema)
12617
+ });
12618
+ useEffect(() => {
12619
+ form.reset({
12620
+ title,
12621
+ quantity,
12622
+ unit_price
12623
+ });
12624
+ }, [form, title, quantity, unit_price]);
12625
+ const actionId = useMemo(() => {
12626
+ var _a, _b;
12627
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12628
+ }, [item]);
12629
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12630
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
12631
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12632
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12633
+ const onSubmit = form.handleSubmit(async (data) => {
12634
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
12635
+ setEditing(false);
12636
+ return;
12637
+ }
12638
+ if (!actionId) {
12639
+ await updateOriginalItem(
12640
+ {
12641
+ item_id: item.id,
12642
+ quantity: data.quantity,
12643
+ unit_price: convertNumber(data.unit_price)
12644
+ },
12645
+ {
12646
+ onSuccess: () => {
12647
+ setEditing(false);
12648
+ },
12649
+ onError: (e) => {
12650
+ toast.error(e.message);
12851
12651
  }
12852
- ) }),
12853
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsx(
12854
- "path",
12855
- {
12856
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12857
- stroke: "#A1A1AA",
12858
- strokeWidth: "1.5",
12859
- strokeLinecap: "round",
12860
- strokeLinejoin: "round"
12652
+ }
12653
+ );
12654
+ return;
12655
+ }
12656
+ if (data.quantity === 0) {
12657
+ await removeActionItem(actionId, {
12658
+ onSuccess: () => {
12659
+ setEditing(false);
12660
+ },
12661
+ onError: (e) => {
12662
+ toast.error(e.message);
12663
+ }
12664
+ });
12665
+ return;
12666
+ }
12667
+ await updateActionItem(
12668
+ {
12669
+ action_id: actionId,
12670
+ quantity: data.quantity,
12671
+ unit_price: convertNumber(data.unit_price)
12672
+ },
12673
+ {
12674
+ onSuccess: () => {
12675
+ setEditing(false);
12676
+ },
12677
+ onError: (e) => {
12678
+ toast.error(e.message);
12679
+ }
12680
+ }
12681
+ );
12682
+ });
12683
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
12684
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12685
+ /* @__PURE__ */ jsx(
12686
+ Thumbnail,
12687
+ {
12688
+ thumbnail: item.thumbnail,
12689
+ alt: item.title ?? void 0
12690
+ }
12691
+ ),
12692
+ editing ? /* @__PURE__ */ jsx(
12693
+ Form$2.Field,
12694
+ {
12695
+ control: form.control,
12696
+ name: "title",
12697
+ render: ({ field }) => {
12698
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
12861
12699
  }
12862
- ) }),
12863
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsx(
12864
- "path",
12700
+ }
12701
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
12702
+ ] }),
12703
+ editing ? /* @__PURE__ */ jsx(
12704
+ Form$2.Field,
12705
+ {
12706
+ control: form.control,
12707
+ name: "quantity",
12708
+ render: ({ field }) => {
12709
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
12710
+ }
12711
+ }
12712
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
12713
+ editing ? /* @__PURE__ */ jsx(
12714
+ Form$2.Field,
12715
+ {
12716
+ control: form.control,
12717
+ name: "unit_price",
12718
+ render: ({ field: { onChange, ...field } }) => {
12719
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12720
+ CurrencyInput,
12721
+ {
12722
+ ...field,
12723
+ symbol: getNativeSymbol(currencyCode),
12724
+ code: currencyCode,
12725
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12726
+ }
12727
+ ) }) });
12728
+ }
12729
+ }
12730
+ ) : /* @__PURE__ */ jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12731
+ /* @__PURE__ */ jsx(
12732
+ IconButton,
12733
+ {
12734
+ type: "button",
12735
+ size: "small",
12736
+ onClick: editing ? onSubmit : () => {
12737
+ setEditing(true);
12738
+ },
12739
+ disabled: isPending,
12740
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
12741
+ }
12742
+ )
12743
+ ] }) }) });
12744
+ };
12745
+ const StackedModalTrigger = ({
12746
+ type,
12747
+ setModalContent
12748
+ }) => {
12749
+ const { setIsOpen } = useStackedModal();
12750
+ const onClick = useCallback(() => {
12751
+ setModalContent(type);
12752
+ setIsOpen(STACKED_MODAL_ID, true);
12753
+ }, [setModalContent, setIsOpen, type]);
12754
+ return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
12755
+ };
12756
+ const VARIANT_PREFIX = "items";
12757
+ const LIMIT = 50;
12758
+ const ExistingItemsForm = ({ orderId, items }) => {
12759
+ const { setIsOpen } = useStackedModal();
12760
+ const [rowSelection, setRowSelection] = useState(
12761
+ items.reduce((acc, item) => {
12762
+ acc[item.variant_id] = true;
12763
+ return acc;
12764
+ }, {})
12765
+ );
12766
+ useEffect(() => {
12767
+ setRowSelection(
12768
+ items.reduce((acc, item) => {
12769
+ if (item.variant_id) {
12770
+ acc[item.variant_id] = true;
12771
+ }
12772
+ return acc;
12773
+ }, {})
12774
+ );
12775
+ }, [items]);
12776
+ const { q, order, offset } = useQueryParams(
12777
+ ["q", "order", "offset"],
12778
+ VARIANT_PREFIX
12779
+ );
12780
+ const { variants, count, isPending, isError, error } = useProductVariants(
12781
+ {
12782
+ q,
12783
+ order,
12784
+ offset: offset ? parseInt(offset) : void 0,
12785
+ limit: LIMIT
12786
+ },
12787
+ {
12788
+ placeholderData: keepPreviousData
12789
+ }
12790
+ );
12791
+ const columns = useColumns();
12792
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
12793
+ const onSubmit = async () => {
12794
+ const ids = Object.keys(rowSelection).filter(
12795
+ (id) => !items.find((i) => i.variant_id === id)
12796
+ );
12797
+ await mutateAsync(
12798
+ {
12799
+ items: ids.map((id) => ({
12800
+ variant_id: id,
12801
+ quantity: 1
12802
+ }))
12803
+ },
12804
+ {
12805
+ onSuccess: () => {
12806
+ setRowSelection({});
12807
+ setIsOpen(STACKED_MODAL_ID, false);
12808
+ },
12809
+ onError: (e) => {
12810
+ toast.error(e.message);
12811
+ }
12812
+ }
12813
+ );
12814
+ };
12815
+ if (isError) {
12816
+ throw error;
12817
+ }
12818
+ return /* @__PURE__ */ jsxs(
12819
+ StackedFocusModal.Content,
12820
+ {
12821
+ onOpenAutoFocus: (e) => {
12822
+ e.preventDefault();
12823
+ const searchInput = document.querySelector(
12824
+ "[data-modal-id='modal-search-input']"
12825
+ );
12826
+ if (searchInput) {
12827
+ searchInput.focus();
12828
+ }
12829
+ },
12830
+ children: [
12831
+ /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
12832
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
12833
+ /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
12834
+ ] }),
12835
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
12836
+ DataTable,
12865
12837
  {
12866
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12867
- stroke: "#A1A1AA",
12868
- strokeWidth: "1.5",
12869
- strokeLinecap: "round",
12870
- strokeLinejoin: "round"
12838
+ data: variants,
12839
+ columns,
12840
+ isLoading: isPending,
12841
+ getRowId: (row) => row.id,
12842
+ rowCount: count,
12843
+ prefix: VARIANT_PREFIX,
12844
+ layout: "fill",
12845
+ rowSelection: {
12846
+ state: rowSelection,
12847
+ onRowSelectionChange: setRowSelection,
12848
+ enableRowSelection: (row) => {
12849
+ return !items.find((i) => i.variant_id === row.original.id);
12850
+ }
12851
+ },
12852
+ autoFocusSearch: true
12871
12853
  }
12872
12854
  ) }),
12873
- /* @__PURE__ */ jsxs("defs", { children: [
12874
- /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
12875
- "rect",
12876
- {
12877
- width: "12",
12878
- height: "12",
12879
- fill: "white",
12880
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12881
- }
12882
- ) }),
12883
- /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
12884
- "rect",
12885
- {
12886
- width: "12",
12887
- height: "12",
12888
- fill: "white",
12889
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12890
- }
12891
- ) }),
12892
- /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
12893
- "rect",
12894
- {
12895
- width: "12",
12896
- height: "12",
12897
- fill: "white",
12898
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12899
- }
12900
- ) }),
12901
- /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
12902
- "rect",
12903
- {
12904
- width: "12",
12905
- height: "12",
12906
- fill: "white",
12907
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12908
- }
12909
- ) }),
12910
- /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
12911
- "rect",
12855
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12856
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12857
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
12858
+ ] }) })
12859
+ ]
12860
+ }
12861
+ );
12862
+ };
12863
+ const columnHelper = createDataTableColumnHelper();
12864
+ const useColumns = () => {
12865
+ return useMemo(() => {
12866
+ return [
12867
+ columnHelper.select(),
12868
+ columnHelper.accessor("product.title", {
12869
+ header: "Product",
12870
+ cell: ({ row }) => {
12871
+ var _a, _b, _c;
12872
+ return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
12873
+ /* @__PURE__ */ jsx(
12874
+ Thumbnail,
12875
+ {
12876
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
12877
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
12878
+ }
12879
+ ),
12880
+ /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
12881
+ ] });
12882
+ },
12883
+ enableSorting: true
12884
+ }),
12885
+ columnHelper.accessor("title", {
12886
+ header: "Variant",
12887
+ enableSorting: true
12888
+ }),
12889
+ columnHelper.accessor("sku", {
12890
+ header: "SKU",
12891
+ cell: ({ getValue }) => {
12892
+ return getValue() ?? "-";
12893
+ },
12894
+ enableSorting: true
12895
+ }),
12896
+ columnHelper.accessor("updated_at", {
12897
+ header: "Updated",
12898
+ cell: ({ getValue }) => {
12899
+ return /* @__PURE__ */ jsx(
12900
+ Tooltip,
12912
12901
  {
12913
- width: "12",
12914
- height: "12",
12915
- fill: "white",
12916
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12902
+ content: getFullDate({ date: getValue(), includeTime: true }),
12903
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
12917
12904
  }
12918
- ) }),
12919
- /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
12920
- "rect",
12905
+ );
12906
+ },
12907
+ enableSorting: true,
12908
+ sortAscLabel: "Oldest first",
12909
+ sortDescLabel: "Newest first"
12910
+ }),
12911
+ columnHelper.accessor("created_at", {
12912
+ header: "Created",
12913
+ cell: ({ getValue }) => {
12914
+ return /* @__PURE__ */ jsx(
12915
+ Tooltip,
12921
12916
  {
12922
- width: "12",
12923
- height: "12",
12924
- fill: "white",
12925
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12917
+ content: getFullDate({ date: getValue(), includeTime: true }),
12918
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
12926
12919
  }
12927
- ) })
12928
- ] })
12929
- ]
12930
- }
12931
- );
12932
- };
12933
- const schema$1 = objectType({
12934
- customer_id: stringType().min(1)
12935
- });
12936
- const SalesChannel = () => {
12937
- const { id } = useParams();
12938
- const { draft_order, isPending, isError, error } = useDraftOrder(
12939
- id,
12940
- {
12941
- fields: "+sales_channel_id"
12942
- },
12943
- {
12944
- enabled: !!id
12945
- }
12946
- );
12947
- if (isError) {
12948
- throw error;
12949
- }
12950
- const ISrEADY = !!draft_order && !isPending;
12951
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12952
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12953
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
12954
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12955
- ] }),
12956
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
12957
- ] });
12920
+ );
12921
+ },
12922
+ enableSorting: true,
12923
+ sortAscLabel: "Oldest first",
12924
+ sortDescLabel: "Newest first"
12925
+ })
12926
+ ];
12927
+ }, []);
12958
12928
  };
12959
- const SalesChannelForm = ({ order }) => {
12929
+ const CustomItemForm = ({ orderId, currencyCode }) => {
12930
+ const { setIsOpen } = useStackedModal();
12931
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
12960
12932
  const form = useForm({
12961
12933
  defaultValues: {
12962
- sales_channel_id: order.sales_channel_id || ""
12934
+ title: "",
12935
+ quantity: 1,
12936
+ unit_price: ""
12963
12937
  },
12964
- resolver: zodResolver(schema)
12938
+ resolver: zodResolver(customItemSchema)
12965
12939
  });
12966
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12967
- const { handleSuccess } = useRouteModal();
12968
12940
  const onSubmit = form.handleSubmit(async (data) => {
12969
- await mutateAsync(
12941
+ await addItems(
12970
12942
  {
12971
- sales_channel_id: data.sales_channel_id
12943
+ items: [
12944
+ {
12945
+ title: data.title,
12946
+ quantity: data.quantity,
12947
+ unit_price: convertNumber(data.unit_price)
12948
+ }
12949
+ ]
12972
12950
  },
12973
12951
  {
12974
12952
  onSuccess: () => {
12975
- toast.success("Sales channel updated");
12976
- handleSuccess();
12953
+ setIsOpen(STACKED_MODAL_ID, false);
12977
12954
  },
12978
- onError: (error) => {
12979
- toast.error(error.message);
12955
+ onError: (e) => {
12956
+ toast.error(e.message);
12980
12957
  }
12981
12958
  }
12982
12959
  );
12983
12960
  });
12984
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12985
- KeyboundForm,
12986
- {
12987
- className: "flex flex-1 flex-col overflow-hidden",
12988
- onSubmit,
12989
- children: [
12990
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
12991
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12992
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12993
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12994
- ] }) })
12995
- ]
12996
- }
12997
- ) });
12998
- };
12999
- const SalesChannelField = ({ control, order }) => {
13000
- const salesChannels = useComboboxData({
13001
- queryFn: async (params) => {
13002
- return await sdk.admin.salesChannel.list(params);
13003
- },
13004
- queryKey: ["sales-channels"],
13005
- getOptions: (data) => {
13006
- return data.sales_channels.map((salesChannel) => ({
13007
- label: salesChannel.name,
13008
- value: salesChannel.id
13009
- }));
13010
- },
13011
- defaultValue: order.sales_channel_id || void 0
13012
- });
13013
- return /* @__PURE__ */ jsx(
13014
- Form$2.Field,
13015
- {
13016
- control,
13017
- name: "sales_channel_id",
13018
- render: ({ field }) => {
13019
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13020
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
13021
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
13022
- Combobox,
13023
- {
13024
- options: salesChannels.options,
13025
- fetchNextPage: salesChannels.fetchNextPage,
13026
- isFetchingNextPage: salesChannels.isFetchingNextPage,
13027
- searchValue: salesChannels.searchValue,
13028
- onSearchValueChange: salesChannels.onSearchValueChange,
13029
- placeholder: "Select sales channel",
13030
- ...field
13031
- }
13032
- ) }),
13033
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13034
- ] });
13035
- }
13036
- }
13037
- );
12961
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
12962
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12963
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
12964
+ /* @__PURE__ */ jsxs("div", { children: [
12965
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
12966
+ /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
12967
+ ] }),
12968
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12969
+ /* @__PURE__ */ jsx(
12970
+ Form$2.Field,
12971
+ {
12972
+ control: form.control,
12973
+ name: "title",
12974
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12975
+ /* @__PURE__ */ jsxs("div", { children: [
12976
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
12977
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
12978
+ ] }),
12979
+ /* @__PURE__ */ jsxs("div", { children: [
12980
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12981
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12982
+ ] })
12983
+ ] }) })
12984
+ }
12985
+ ),
12986
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12987
+ /* @__PURE__ */ jsx(
12988
+ Form$2.Field,
12989
+ {
12990
+ control: form.control,
12991
+ name: "unit_price",
12992
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12993
+ /* @__PURE__ */ jsxs("div", { children: [
12994
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
12995
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
12996
+ ] }),
12997
+ /* @__PURE__ */ jsxs("div", { children: [
12998
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12999
+ CurrencyInput,
13000
+ {
13001
+ symbol: getNativeSymbol(currencyCode),
13002
+ code: currencyCode,
13003
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
13004
+ ...field
13005
+ }
13006
+ ) }),
13007
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13008
+ ] })
13009
+ ] }) })
13010
+ }
13011
+ ),
13012
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
13013
+ /* @__PURE__ */ jsx(
13014
+ Form$2.Field,
13015
+ {
13016
+ control: form.control,
13017
+ name: "quantity",
13018
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
13019
+ /* @__PURE__ */ jsxs("div", { children: [
13020
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
13021
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
13022
+ ] }),
13023
+ /* @__PURE__ */ jsxs("div", { className: "w-full flex-1", children: [
13024
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
13025
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13026
+ ] })
13027
+ ] }) })
13028
+ }
13029
+ )
13030
+ ] }) }) }),
13031
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
13032
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
13033
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
13034
+ ] }) })
13035
+ ] }) }) });
13038
13036
  };
13039
- const schema = objectType({
13040
- sales_channel_id: stringType().min(1)
13037
+ const customItemSchema = objectType({
13038
+ title: stringType().min(1),
13039
+ quantity: numberType(),
13040
+ unit_price: unionType([numberType(), stringType()])
13041
13041
  });
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
@@ -13071,10 +13071,6 @@ const routeModule = {
13071
13071
  Component: Email,
13072
13072
  path: "/draft-orders/:id/email"
13073
13073
  },
13074
- {
13075
- Component: Items,
13076
- path: "/draft-orders/:id/items"
13077
- },
13078
13074
  {
13079
13075
  Component: Metadata,
13080
13076
  path: "/draft-orders/:id/metadata"
@@ -13084,20 +13080,24 @@ const routeModule = {
13084
13080
  path: "/draft-orders/:id/promotions"
13085
13081
  },
13086
13082
  {
13087
- Component: ShippingAddress,
13088
- path: "/draft-orders/:id/shipping-address"
13083
+ Component: SalesChannel,
13084
+ path: "/draft-orders/:id/sales-channel"
13089
13085
  },
13090
13086
  {
13091
13087
  Component: Shipping,
13092
13088
  path: "/draft-orders/:id/shipping"
13093
13089
  },
13090
+ {
13091
+ Component: ShippingAddress,
13092
+ path: "/draft-orders/:id/shipping-address"
13093
+ },
13094
13094
  {
13095
13095
  Component: TransferOwnership,
13096
13096
  path: "/draft-orders/:id/transfer-ownership"
13097
13097
  },
13098
13098
  {
13099
- Component: SalesChannel,
13100
- path: "/draft-orders/:id/sales-channel"
13099
+ Component: Items,
13100
+ path: "/draft-orders/:id/items"
13101
13101
  }
13102
13102
  ]
13103
13103
  }