@medusajs/draft-order 2.11.2-snapshot-20251029131556 → 2.11.2-snapshot-20251029143232

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