@medusajs/draft-order 2.10.4-preview-20250928000328 → 2.10.4-preview-20250928060202

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.
@@ -9831,1628 +9831,1351 @@ const EmailForm = ({ order }) => {
9831
9831
  const schema$4 = objectType({
9832
9832
  email: stringType().email()
9833
9833
  });
9834
- const NumberInput = React.forwardRef(
9835
- ({
9836
- value,
9837
- onChange,
9838
- size = "base",
9839
- min = 0,
9840
- max = 100,
9841
- step = 1,
9842
- className,
9843
- disabled,
9844
- ...props
9845
- }, ref) => {
9846
- const handleChange = (event) => {
9847
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9848
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9849
- onChange(newValue);
9850
- }
9851
- };
9852
- const handleIncrement = () => {
9853
- const newValue = value + step;
9854
- if (max === void 0 || newValue <= max) {
9855
- onChange(newValue);
9856
- }
9857
- };
9858
- const handleDecrement = () => {
9859
- const newValue = value - step;
9860
- if (min === void 0 || newValue >= min) {
9861
- onChange(newValue);
9862
- }
9863
- };
9834
+ const CustomItems = () => {
9835
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9836
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9837
+ /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9838
+ ] });
9839
+ };
9840
+ const CustomItemsForm = () => {
9841
+ const form = reactHookForm.useForm({
9842
+ resolver: zod.zodResolver(schema$3)
9843
+ });
9844
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9845
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9846
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9847
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9848
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9849
+ ] }) })
9850
+ ] }) });
9851
+ };
9852
+ const schema$3 = objectType({
9853
+ email: stringType().email()
9854
+ });
9855
+ const InlineTip = React.forwardRef(
9856
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9857
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9864
9858
  return /* @__PURE__ */ jsxRuntime.jsxs(
9865
9859
  "div",
9866
9860
  {
9861
+ ref,
9867
9862
  className: ui.clx(
9868
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9869
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9870
- {
9871
- "h-7": size === "small",
9872
- "h-8": size === "base"
9873
- },
9863
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9874
9864
  className
9875
9865
  ),
9866
+ ...props,
9876
9867
  children: [
9877
9868
  /* @__PURE__ */ jsxRuntime.jsx(
9878
- "input",
9879
- {
9880
- ref,
9881
- type: "number",
9882
- value,
9883
- onChange: handleChange,
9884
- min,
9885
- max,
9886
- step,
9887
- className: ui.clx(
9888
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9889
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9890
- "placeholder:text-ui-fg-muted"
9891
- ),
9892
- ...props
9893
- }
9894
- ),
9895
- /* @__PURE__ */ jsxRuntime.jsxs(
9896
- "button",
9869
+ "div",
9897
9870
  {
9898
- className: ui.clx(
9899
- "flex items-center justify-center outline-none transition-fg",
9900
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9901
- "focus:bg-ui-bg-field-component-hover",
9902
- "hover:bg-ui-bg-field-component-hover",
9903
- {
9904
- "size-7": size === "small",
9905
- "size-8": size === "base"
9906
- }
9907
- ),
9908
- type: "button",
9909
- onClick: handleDecrement,
9910
- disabled: min !== void 0 && value <= min || disabled,
9911
- children: [
9912
- /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
9913
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9914
- ]
9871
+ role: "presentation",
9872
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9873
+ "bg-ui-tag-orange-icon": variant === "warning"
9874
+ })
9915
9875
  }
9916
9876
  ),
9917
- /* @__PURE__ */ jsxRuntime.jsxs(
9918
- "button",
9919
- {
9920
- className: ui.clx(
9921
- "flex items-center justify-center outline-none transition-fg",
9922
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9923
- "focus:bg-ui-bg-field-hover",
9924
- "hover:bg-ui-bg-field-hover",
9925
- {
9926
- "size-7": size === "small",
9927
- "size-8": size === "base"
9928
- }
9929
- ),
9930
- type: "button",
9931
- onClick: handleIncrement,
9932
- disabled: max !== void 0 && value >= max || disabled,
9933
- children: [
9934
- /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
9935
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9936
- ]
9937
- }
9938
- )
9877
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
9878
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9879
+ labelValue,
9880
+ ":"
9881
+ ] }),
9882
+ " ",
9883
+ children
9884
+ ] })
9939
9885
  ]
9940
9886
  }
9941
9887
  );
9942
9888
  }
9943
9889
  );
9944
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9945
- const productVariantsQueryKeys = {
9946
- list: (query2) => [
9947
- PRODUCT_VARIANTS_QUERY_KEY,
9948
- query2 ? query2 : void 0
9949
- ]
9950
- };
9951
- const useProductVariants = (query2, options) => {
9952
- const { data, ...rest } = reactQuery.useQuery({
9953
- queryKey: productVariantsQueryKeys.list(query2),
9954
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9955
- ...options
9956
- });
9957
- return { ...data, ...rest };
9958
- };
9959
- const useCancelOrderEdit = ({ preview }) => {
9960
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9961
- const onCancel = React.useCallback(async () => {
9962
- if (!preview) {
9963
- return true;
9964
- }
9965
- let res = false;
9966
- await cancelOrderEdit(void 0, {
9967
- onError: (e) => {
9968
- ui.toast.error(e.message);
9969
- },
9970
- onSuccess: () => {
9971
- res = true;
9972
- }
9973
- });
9974
- return res;
9975
- }, [preview, cancelOrderEdit]);
9976
- return { onCancel };
9977
- };
9978
- let IS_REQUEST_RUNNING = false;
9979
- const useInitiateOrderEdit = ({
9980
- preview
9981
- }) => {
9982
- const navigate = reactRouterDom.useNavigate();
9983
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
9984
- React.useEffect(() => {
9985
- async function run() {
9986
- if (IS_REQUEST_RUNNING || !preview) {
9987
- return;
9988
- }
9989
- if (preview.order_change) {
9990
- return;
9991
- }
9992
- IS_REQUEST_RUNNING = true;
9993
- await mutateAsync(void 0, {
9994
- onError: (e) => {
9995
- ui.toast.error(e.message);
9996
- navigate(`/draft-orders/${preview.id}`, { replace: true });
9997
- return;
9998
- }
9999
- });
10000
- IS_REQUEST_RUNNING = false;
10001
- }
10002
- run();
10003
- }, [preview, navigate, mutateAsync]);
10004
- };
10005
- function convertNumber(value) {
10006
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10007
- }
10008
- const STACKED_MODAL_ID = "items_stacked_modal";
10009
- const Items = () => {
9890
+ InlineTip.displayName = "InlineTip";
9891
+ const MetadataFieldSchema = objectType({
9892
+ key: stringType(),
9893
+ disabled: booleanType().optional(),
9894
+ value: anyType()
9895
+ });
9896
+ const MetadataSchema = objectType({
9897
+ metadata: arrayType(MetadataFieldSchema)
9898
+ });
9899
+ const Metadata = () => {
10010
9900
  const { id } = reactRouterDom.useParams();
10011
- const {
10012
- order: preview,
10013
- isPending: isPreviewPending,
10014
- isError: isPreviewError,
10015
- error: previewError
10016
- } = useOrderPreview(id, void 0, {
10017
- placeholderData: reactQuery.keepPreviousData
9901
+ const { order, isPending, isError, error } = useOrder(id, {
9902
+ fields: "metadata"
10018
9903
  });
10019
- useInitiateOrderEdit({ preview });
10020
- const { draft_order, isPending, isError, error } = useDraftOrder(
10021
- id,
10022
- {
10023
- fields: "currency_code"
10024
- },
10025
- {
10026
- enabled: !!id
10027
- }
10028
- );
10029
- const { onCancel } = useCancelOrderEdit({ preview });
10030
9904
  if (isError) {
10031
9905
  throw error;
10032
9906
  }
10033
- if (isPreviewError) {
10034
- throw previewError;
10035
- }
10036
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10037
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10038
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10039
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10040
- ] }) });
9907
+ const isReady = !isPending && !!order;
9908
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9909
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9910
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
9911
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9912
+ ] }),
9913
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9914
+ ] });
10041
9915
  };
10042
- const ItemsForm = ({ preview, currencyCode }) => {
10043
- var _a;
10044
- const [isSubmitting, setIsSubmitting] = React.useState(false);
10045
- const [modalContent, setModalContent] = React.useState(
10046
- null
10047
- );
9916
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9917
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9918
+ const MetadataForm = ({ orderId, metadata }) => {
10048
9919
  const { handleSuccess } = useRouteModal();
10049
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10050
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10051
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10052
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10053
- const matches = React.useMemo(() => {
10054
- return matchSorter.matchSorter(preview.items, query2, {
10055
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10056
- });
10057
- }, [preview.items, query2]);
10058
- const onSubmit = async () => {
10059
- setIsSubmitting(true);
10060
- let requestSucceeded = false;
10061
- await requestOrderEdit(void 0, {
10062
- onError: (e) => {
10063
- ui.toast.error(`Failed to request order edit: ${e.message}`);
10064
- },
10065
- onSuccess: () => {
10066
- requestSucceeded = true;
10067
- }
10068
- });
10069
- if (!requestSucceeded) {
10070
- setIsSubmitting(false);
10071
- return;
10072
- }
10073
- await confirmOrderEdit(void 0, {
10074
- onError: (e) => {
10075
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10076
- },
10077
- onSuccess: () => {
10078
- handleSuccess();
10079
- },
10080
- onSettled: () => {
10081
- setIsSubmitting(false);
10082
- }
10083
- });
10084
- };
10085
- const onKeyDown = React.useCallback(
10086
- (e) => {
10087
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10088
- if (modalContent || isSubmitting) {
10089
- return;
10090
- }
10091
- onSubmit();
10092
- }
10093
- },
10094
- [modalContent, isSubmitting, onSubmit]
10095
- );
10096
- React.useEffect(() => {
10097
- document.addEventListener("keydown", onKeyDown);
10098
- return () => {
10099
- document.removeEventListener("keydown", onKeyDown);
10100
- };
10101
- }, [onKeyDown]);
10102
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10103
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10104
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10105
- StackedFocusModal,
10106
- {
10107
- id: STACKED_MODAL_ID,
10108
- onOpenChangeCallback: (open) => {
10109
- if (!open) {
10110
- setModalContent(null);
10111
- }
10112
- },
10113
- children: [
10114
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
10115
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10116
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10117
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Edit the items in the draft order" }) })
10118
- ] }),
10119
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10120
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10121
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10122
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10123
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10124
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10125
- ] }),
10126
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10127
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10128
- ui.Input,
10129
- {
10130
- type: "search",
10131
- placeholder: "Search items",
10132
- value: searchValue,
10133
- onChange: (e) => onSearchValueChange(e.target.value)
10134
- }
10135
- ) }),
10136
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10137
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10138
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10139
- /* @__PURE__ */ jsxRuntime.jsx(
10140
- StackedModalTrigger$1,
10141
- {
10142
- type: "add-items",
10143
- setModalContent
10144
- }
10145
- ),
10146
- /* @__PURE__ */ jsxRuntime.jsx(
10147
- StackedModalTrigger$1,
10148
- {
10149
- type: "add-custom-item",
10150
- setModalContent
10151
- }
10152
- )
10153
- ] })
10154
- ] })
10155
- ] })
10156
- ] }),
10157
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10158
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-muted grid grid-cols-[2fr_1fr_2fr_28px] gap-3 px-4 py-2", children: [
10159
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10160
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10161
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10162
- /* @__PURE__ */ jsxRuntime.jsx("div", {})
10163
- ] }) }),
10164
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxRuntime.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: [
10165
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10166
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10167
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10168
- Item,
10169
- {
10170
- item,
10171
- preview,
10172
- currencyCode
10173
- },
10174
- item.id
10175
- )) : /* @__PURE__ */ jsxRuntime.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: [
10176
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10177
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10178
- 'No items found for "',
10179
- query2,
10180
- '".'
10181
- ] })
10182
- ] }) })
10183
- ] })
10184
- ] }),
10185
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10186
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10187
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10188
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10189
- ui.Text,
10190
- {
10191
- size: "small",
10192
- leading: "compact",
10193
- className: "text-ui-fg-subtle",
10194
- children: [
10195
- itemCount,
10196
- " ",
10197
- itemCount === 1 ? "item" : "items"
10198
- ]
10199
- }
10200
- ) }),
10201
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
10202
- ] })
10203
- ] }) }),
10204
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10205
- CustomItemForm,
10206
- {
10207
- orderId: preview.id,
10208
- currencyCode
10209
- }
10210
- ) : null)
10211
- ]
10212
- }
10213
- ) }),
10214
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10215
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10216
- /* @__PURE__ */ jsxRuntime.jsx(
10217
- ui.Button,
10218
- {
10219
- size: "small",
10220
- type: "button",
10221
- onClick: onSubmit,
10222
- isLoading: isSubmitting,
10223
- children: "Save"
10224
- }
10225
- )
10226
- ] }) })
10227
- ] });
10228
- };
10229
- const Item = ({ item, preview, currencyCode }) => {
10230
- if (item.variant_id) {
10231
- return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10232
- }
10233
- return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10234
- };
10235
- const VariantItem = ({ item, preview, currencyCode }) => {
10236
- const [editing, setEditing] = React.useState(false);
9920
+ const hasUneditableRows = getHasUneditableRows(metadata);
9921
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10237
9922
  const form = reactHookForm.useForm({
10238
9923
  defaultValues: {
10239
- quantity: item.quantity,
10240
- unit_price: item.unit_price
9924
+ metadata: getDefaultValues(metadata)
10241
9925
  },
10242
- resolver: zod.zodResolver(variantItemSchema)
9926
+ resolver: zod.zodResolver(MetadataSchema)
10243
9927
  });
10244
- const actionId = React.useMemo(() => {
10245
- var _a, _b;
10246
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10247
- }, [item]);
10248
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10249
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10250
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10251
- const onSubmit = form.handleSubmit(async (data) => {
10252
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10253
- setEditing(false);
10254
- return;
10255
- }
10256
- if (!actionId) {
10257
- await updateOriginalItem(
10258
- {
10259
- item_id: item.id,
10260
- quantity: data.quantity,
10261
- unit_price: convertNumber(data.unit_price)
10262
- },
10263
- {
10264
- onSuccess: () => {
10265
- setEditing(false);
10266
- },
10267
- onError: (e) => {
10268
- ui.toast.error(e.message);
10269
- }
10270
- }
10271
- );
10272
- return;
10273
- }
10274
- await updateActionItem(
9928
+ const handleSubmit = form.handleSubmit(async (data) => {
9929
+ const parsedData = parseValues(data);
9930
+ await mutateAsync(
10275
9931
  {
10276
- action_id: actionId,
10277
- quantity: data.quantity,
10278
- unit_price: convertNumber(data.unit_price)
9932
+ metadata: parsedData
10279
9933
  },
10280
9934
  {
10281
9935
  onSuccess: () => {
10282
- setEditing(false);
9936
+ ui.toast.success("Metadata updated");
9937
+ handleSuccess();
10283
9938
  },
10284
- onError: (e) => {
10285
- ui.toast.error(e.message);
9939
+ onError: (error) => {
9940
+ ui.toast.error(error.message);
10286
9941
  }
10287
9942
  }
10288
9943
  );
10289
9944
  });
10290
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.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: [
10291
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10292
- /* @__PURE__ */ jsxRuntime.jsx(
10293
- Thumbnail,
10294
- {
10295
- thumbnail: item.thumbnail,
10296
- alt: item.product_title ?? void 0
10297
- }
10298
- ),
10299
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10300
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10301
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10302
- /* @__PURE__ */ jsxRuntime.jsxs(
10303
- ui.Text,
10304
- {
10305
- size: "small",
10306
- leading: "compact",
10307
- className: "text-ui-fg-subtle",
10308
- children: [
10309
- "(",
10310
- item.variant_title,
10311
- ")"
10312
- ]
10313
- }
10314
- )
10315
- ] }),
10316
- /* @__PURE__ */ jsxRuntime.jsx(
10317
- ui.Text,
10318
- {
10319
- size: "small",
10320
- leading: "compact",
10321
- className: "text-ui-fg-subtle",
10322
- children: item.variant_sku
10323
- }
10324
- )
10325
- ] })
10326
- ] }),
10327
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10328
- Form$2.Field,
10329
- {
10330
- control: form.control,
10331
- name: "quantity",
10332
- render: ({ field }) => {
10333
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10334
- }
10335
- }
10336
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10337
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10338
- Form$2.Field,
10339
- {
10340
- control: form.control,
10341
- name: "unit_price",
10342
- render: ({ field: { onChange, ...field } }) => {
10343
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10344
- ui.CurrencyInput,
10345
- {
10346
- ...field,
10347
- symbol: getNativeSymbol(currencyCode),
10348
- code: currencyCode,
10349
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10350
- }
10351
- ) }) });
10352
- }
10353
- }
10354
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10355
- /* @__PURE__ */ jsxRuntime.jsx(
10356
- ui.IconButton,
10357
- {
10358
- type: "button",
10359
- size: "small",
10360
- onClick: editing ? onSubmit : () => {
10361
- setEditing(true);
10362
- },
10363
- disabled: isPending,
10364
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10365
- }
10366
- )
10367
- ] }) }) });
10368
- };
10369
- const variantItemSchema = objectType({
10370
- quantity: numberType(),
10371
- unit_price: unionType([numberType(), stringType()])
10372
- });
10373
- const CustomItem = ({ item, preview, currencyCode }) => {
10374
- const [editing, setEditing] = React.useState(false);
10375
- const { quantity, unit_price, title } = item;
10376
- const form = reactHookForm.useForm({
10377
- defaultValues: {
10378
- title,
10379
- quantity,
10380
- unit_price
10381
- },
10382
- resolver: zod.zodResolver(customItemSchema)
10383
- });
10384
- React.useEffect(() => {
10385
- form.reset({
10386
- title,
10387
- quantity,
10388
- unit_price
10389
- });
10390
- }, [form, title, quantity, unit_price]);
10391
- const actionId = React.useMemo(() => {
10392
- var _a, _b;
10393
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10394
- }, [item]);
10395
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10396
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10397
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10398
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10399
- const onSubmit = form.handleSubmit(async (data) => {
10400
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10401
- setEditing(false);
10402
- return;
10403
- }
10404
- if (!actionId) {
10405
- await updateOriginalItem(
10406
- {
10407
- item_id: item.id,
10408
- quantity: data.quantity,
10409
- unit_price: convertNumber(data.unit_price)
10410
- },
10411
- {
10412
- onSuccess: () => {
10413
- setEditing(false);
10414
- },
10415
- onError: (e) => {
10416
- ui.toast.error(e.message);
10417
- }
10418
- }
10419
- );
10420
- return;
10421
- }
10422
- if (data.quantity === 0) {
10423
- await removeActionItem(actionId, {
10424
- onSuccess: () => {
10425
- setEditing(false);
10426
- },
10427
- onError: (e) => {
10428
- ui.toast.error(e.message);
10429
- }
10430
- });
10431
- return;
10432
- }
10433
- await updateActionItem(
10434
- {
10435
- action_id: actionId,
10436
- quantity: data.quantity,
10437
- unit_price: convertNumber(data.unit_price)
10438
- },
10439
- {
10440
- onSuccess: () => {
10441
- setEditing(false);
10442
- },
10443
- onError: (e) => {
10444
- ui.toast.error(e.message);
10445
- }
10446
- }
10447
- );
10448
- });
10449
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.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: [
10450
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10451
- /* @__PURE__ */ jsxRuntime.jsx(
10452
- Thumbnail,
10453
- {
10454
- thumbnail: item.thumbnail,
10455
- alt: item.title ?? void 0
10456
- }
10457
- ),
10458
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10459
- Form$2.Field,
10460
- {
10461
- control: form.control,
10462
- name: "title",
10463
- render: ({ field }) => {
10464
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
10465
- }
10466
- }
10467
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
10468
- ] }),
10469
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10470
- Form$2.Field,
10471
- {
10472
- control: form.control,
10473
- name: "quantity",
10474
- render: ({ field }) => {
10475
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10476
- }
10477
- }
10478
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
10479
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10480
- Form$2.Field,
10481
- {
10482
- control: form.control,
10483
- name: "unit_price",
10484
- render: ({ field: { onChange, ...field } }) => {
10485
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10486
- ui.CurrencyInput,
10487
- {
10488
- ...field,
10489
- symbol: getNativeSymbol(currencyCode),
10490
- code: currencyCode,
10491
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10492
- }
10493
- ) }) });
10494
- }
10495
- }
10496
- ) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10497
- /* @__PURE__ */ jsxRuntime.jsx(
10498
- ui.IconButton,
10499
- {
10500
- type: "button",
10501
- size: "small",
10502
- onClick: editing ? onSubmit : () => {
10503
- setEditing(true);
10504
- },
10505
- disabled: isPending,
10506
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10507
- }
10508
- )
10509
- ] }) }) });
10510
- };
10511
- const StackedModalTrigger$1 = ({
10512
- type,
10513
- setModalContent
10514
- }) => {
10515
- const { setIsOpen } = useStackedModal();
10516
- const onClick = React.useCallback(() => {
10517
- setModalContent(type);
10518
- setIsOpen(STACKED_MODAL_ID, true);
10519
- }, [setModalContent, setIsOpen, type]);
10520
- return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10521
- };
10522
- const VARIANT_PREFIX = "items";
10523
- const LIMIT = 50;
10524
- const ExistingItemsForm = ({ orderId, items }) => {
10525
- const { setIsOpen } = useStackedModal();
10526
- const [rowSelection, setRowSelection] = React.useState(
10527
- items.reduce((acc, item) => {
10528
- acc[item.variant_id] = true;
10529
- return acc;
10530
- }, {})
10531
- );
10532
- React.useEffect(() => {
10533
- setRowSelection(
10534
- items.reduce((acc, item) => {
10535
- if (item.variant_id) {
10536
- acc[item.variant_id] = true;
10537
- }
10538
- return acc;
10539
- }, {})
10540
- );
10541
- }, [items]);
10542
- const { q, order, offset } = useQueryParams(
10543
- ["q", "order", "offset"],
10544
- VARIANT_PREFIX
10545
- );
10546
- const { variants, count, isPending, isError, error } = useProductVariants(
10547
- {
10548
- q,
10549
- order,
10550
- offset: offset ? parseInt(offset) : void 0,
10551
- limit: LIMIT
10552
- },
10553
- {
10554
- placeholderData: reactQuery.keepPreviousData
10555
- }
10556
- );
10557
- const columns = useColumns();
10558
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10559
- const onSubmit = async () => {
10560
- const ids = Object.keys(rowSelection).filter(
10561
- (id) => !items.find((i) => i.variant_id === id)
10562
- );
10563
- await mutateAsync(
10564
- {
10565
- items: ids.map((id) => ({
10566
- variant_id: id,
10567
- quantity: 1
10568
- }))
10569
- },
10570
- {
10571
- onSuccess: () => {
10572
- setRowSelection({});
10573
- setIsOpen(STACKED_MODAL_ID, false);
10574
- },
10575
- onError: (e) => {
10576
- ui.toast.error(e.message);
10577
- }
10578
- }
10579
- );
10580
- };
10581
- if (isError) {
10582
- throw error;
10583
- }
10584
- return /* @__PURE__ */ jsxRuntime.jsxs(
10585
- StackedFocusModal.Content,
10586
- {
10587
- onOpenAutoFocus: (e) => {
10588
- e.preventDefault();
10589
- const searchInput = document.querySelector(
10590
- "[data-modal-id='modal-search-input']"
10591
- );
10592
- if (searchInput) {
10593
- searchInput.focus();
10594
- }
10595
- },
10596
- children: [
10597
- /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
10598
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10599
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10600
- ] }),
10601
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
10602
- DataTable,
10603
- {
10604
- data: variants,
10605
- columns,
10606
- isLoading: isPending,
10607
- getRowId: (row) => row.id,
10608
- rowCount: count,
10609
- prefix: VARIANT_PREFIX,
10610
- layout: "fill",
10611
- rowSelection: {
10612
- state: rowSelection,
10613
- onRowSelectionChange: setRowSelection,
10614
- enableRowSelection: (row) => {
10615
- return !items.find((i) => i.variant_id === row.original.id);
10616
- }
10617
- },
10618
- autoFocusSearch: true
10619
- }
10620
- ) }),
10621
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10622
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10623
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10624
- ] }) })
10625
- ]
10626
- }
10627
- );
10628
- };
10629
- const columnHelper = ui.createDataTableColumnHelper();
10630
- const useColumns = () => {
10631
- return React.useMemo(() => {
10632
- return [
10633
- columnHelper.select(),
10634
- columnHelper.accessor("product.title", {
10635
- header: "Product",
10636
- cell: ({ row }) => {
10637
- var _a, _b, _c;
10638
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
10639
- /* @__PURE__ */ jsxRuntime.jsx(
10640
- Thumbnail,
10641
- {
10642
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10643
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10644
- }
10645
- ),
10646
- /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10647
- ] });
10648
- },
10649
- enableSorting: true
10650
- }),
10651
- columnHelper.accessor("title", {
10652
- header: "Variant",
10653
- enableSorting: true
10654
- }),
10655
- columnHelper.accessor("sku", {
10656
- header: "SKU",
10657
- cell: ({ getValue }) => {
10658
- return getValue() ?? "-";
10659
- },
10660
- enableSorting: true
10661
- }),
10662
- columnHelper.accessor("updated_at", {
10663
- header: "Updated",
10664
- cell: ({ getValue }) => {
10665
- return /* @__PURE__ */ jsxRuntime.jsx(
10666
- ui.Tooltip,
10667
- {
10668
- content: getFullDate({ date: getValue(), includeTime: true }),
10669
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10670
- }
10671
- );
10672
- },
10673
- enableSorting: true,
10674
- sortAscLabel: "Oldest first",
10675
- sortDescLabel: "Newest first"
10676
- }),
10677
- columnHelper.accessor("created_at", {
10678
- header: "Created",
10679
- cell: ({ getValue }) => {
10680
- return /* @__PURE__ */ jsxRuntime.jsx(
10681
- ui.Tooltip,
10682
- {
10683
- content: getFullDate({ date: getValue(), includeTime: true }),
10684
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10685
- }
10686
- );
10687
- },
10688
- enableSorting: true,
10689
- sortAscLabel: "Oldest first",
10690
- sortDescLabel: "Newest first"
10691
- })
10692
- ];
10693
- }, []);
10694
- };
10695
- const CustomItemForm = ({ orderId, currencyCode }) => {
10696
- const { setIsOpen } = useStackedModal();
10697
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10698
- const form = reactHookForm.useForm({
10699
- defaultValues: {
10700
- title: "",
10701
- quantity: 1,
10702
- unit_price: ""
10703
- },
10704
- resolver: zod.zodResolver(customItemSchema)
10705
- });
10706
- const onSubmit = form.handleSubmit(async (data) => {
10707
- await addItems(
10708
- {
10709
- items: [
10710
- {
10711
- title: data.title,
10712
- quantity: data.quantity,
10713
- unit_price: convertNumber(data.unit_price)
10714
- }
10715
- ]
10716
- },
10717
- {
10718
- onSuccess: () => {
10719
- setIsOpen(STACKED_MODAL_ID, false);
10720
- },
10721
- onError: (e) => {
10722
- ui.toast.error(e.message);
10723
- }
10724
- }
10725
- );
9945
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
9946
+ control: form.control,
9947
+ name: "metadata"
10726
9948
  });
10727
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
10728
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
10729
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
10730
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10731
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
10732
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
10733
- ] }),
10734
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10735
- /* @__PURE__ */ jsxRuntime.jsx(
10736
- Form$2.Field,
10737
- {
10738
- control: form.control,
10739
- name: "title",
10740
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10741
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10742
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
10743
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
10744
- ] }),
10745
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10746
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10747
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10748
- ] })
10749
- ] }) })
10750
- }
10751
- ),
10752
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10753
- /* @__PURE__ */ jsxRuntime.jsx(
10754
- Form$2.Field,
10755
- {
10756
- control: form.control,
10757
- name: "unit_price",
10758
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10759
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10760
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
10761
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
9949
+ function deleteRow(index) {
9950
+ remove(index);
9951
+ if (fields.length === 1) {
9952
+ insert(0, {
9953
+ key: "",
9954
+ value: "",
9955
+ disabled: false
9956
+ });
9957
+ }
9958
+ }
9959
+ function insertRow(index, position) {
9960
+ insert(index + (position === "above" ? 0 : 1), {
9961
+ key: "",
9962
+ value: "",
9963
+ disabled: false
9964
+ });
9965
+ }
9966
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9967
+ KeyboundForm,
9968
+ {
9969
+ onSubmit: handleSubmit,
9970
+ className: "flex flex-1 flex-col overflow-hidden",
9971
+ children: [
9972
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9973
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9974
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9975
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
9976
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
10762
9977
  ] }),
10763
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10764
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10765
- ui.CurrencyInput,
9978
+ fields.map((field, index) => {
9979
+ const isDisabled = field.disabled || false;
9980
+ let placeholder = "-";
9981
+ if (typeof field.value === "object") {
9982
+ placeholder = "{ ... }";
9983
+ }
9984
+ if (Array.isArray(field.value)) {
9985
+ placeholder = "[ ... ]";
9986
+ }
9987
+ return /* @__PURE__ */ jsxRuntime.jsx(
9988
+ ConditionalTooltip,
10766
9989
  {
10767
- symbol: getNativeSymbol(currencyCode),
10768
- code: currencyCode,
10769
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10770
- ...field
10771
- }
10772
- ) }),
10773
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10774
- ] })
10775
- ] }) })
10776
- }
10777
- ),
10778
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10779
- /* @__PURE__ */ jsxRuntime.jsx(
10780
- Form$2.Field,
10781
- {
10782
- control: form.control,
10783
- name: "quantity",
10784
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10785
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10786
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
10787
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10788
- ] }),
10789
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
10790
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10791
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10792
- ] })
10793
- ] }) })
10794
- }
10795
- )
10796
- ] }) }) }),
10797
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10798
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10799
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10800
- ] }) })
10801
- ] }) }) });
9990
+ showTooltip: isDisabled,
9991
+ content: "This row is disabled because it contains non-primitive data.",
9992
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
9993
+ /* @__PURE__ */ jsxRuntime.jsxs(
9994
+ "div",
9995
+ {
9996
+ className: ui.clx("grid grid-cols-2 divide-x", {
9997
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9998
+ }),
9999
+ children: [
10000
+ /* @__PURE__ */ jsxRuntime.jsx(
10001
+ Form$2.Field,
10002
+ {
10003
+ control: form.control,
10004
+ name: `metadata.${index}.key`,
10005
+ render: ({ field: field2 }) => {
10006
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10007
+ GridInput,
10008
+ {
10009
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
10010
+ ...field2,
10011
+ disabled: isDisabled,
10012
+ placeholder: "Key"
10013
+ }
10014
+ ) }) });
10015
+ }
10016
+ }
10017
+ ),
10018
+ /* @__PURE__ */ jsxRuntime.jsx(
10019
+ Form$2.Field,
10020
+ {
10021
+ control: form.control,
10022
+ name: `metadata.${index}.value`,
10023
+ render: ({ field: { value, ...field2 } }) => {
10024
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10025
+ GridInput,
10026
+ {
10027
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
10028
+ ...field2,
10029
+ value: isDisabled ? placeholder : value,
10030
+ disabled: isDisabled,
10031
+ placeholder: "Value"
10032
+ }
10033
+ ) }) });
10034
+ }
10035
+ }
10036
+ )
10037
+ ]
10038
+ }
10039
+ ),
10040
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10041
+ /* @__PURE__ */ jsxRuntime.jsx(
10042
+ ui.DropdownMenu.Trigger,
10043
+ {
10044
+ className: ui.clx(
10045
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
10046
+ {
10047
+ hidden: isDisabled
10048
+ }
10049
+ ),
10050
+ disabled: isDisabled,
10051
+ asChild: true,
10052
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
10053
+ }
10054
+ ),
10055
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10056
+ /* @__PURE__ */ jsxRuntime.jsxs(
10057
+ ui.DropdownMenu.Item,
10058
+ {
10059
+ className: "gap-x-2",
10060
+ onClick: () => insertRow(index, "above"),
10061
+ children: [
10062
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
10063
+ "Insert row above"
10064
+ ]
10065
+ }
10066
+ ),
10067
+ /* @__PURE__ */ jsxRuntime.jsxs(
10068
+ ui.DropdownMenu.Item,
10069
+ {
10070
+ className: "gap-x-2",
10071
+ onClick: () => insertRow(index, "below"),
10072
+ children: [
10073
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
10074
+ "Insert row below"
10075
+ ]
10076
+ }
10077
+ ),
10078
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
10079
+ /* @__PURE__ */ jsxRuntime.jsxs(
10080
+ ui.DropdownMenu.Item,
10081
+ {
10082
+ className: "gap-x-2",
10083
+ onClick: () => deleteRow(index),
10084
+ children: [
10085
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
10086
+ "Delete row"
10087
+ ]
10088
+ }
10089
+ )
10090
+ ] })
10091
+ ] })
10092
+ ] })
10093
+ },
10094
+ field.id
10095
+ );
10096
+ })
10097
+ ] }),
10098
+ hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
10099
+ ] }),
10100
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10101
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10102
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10103
+ ] }) })
10104
+ ]
10105
+ }
10106
+ ) });
10802
10107
  };
10803
- const customItemSchema = objectType({
10804
- title: stringType().min(1),
10805
- quantity: numberType(),
10806
- unit_price: unionType([numberType(), stringType()])
10108
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
10109
+ return /* @__PURE__ */ jsxRuntime.jsx(
10110
+ "input",
10111
+ {
10112
+ ref,
10113
+ ...props,
10114
+ autoComplete: "off",
10115
+ className: ui.clx(
10116
+ "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",
10117
+ className
10118
+ )
10119
+ }
10120
+ );
10807
10121
  });
10808
- const CustomItems = () => {
10809
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10810
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
10811
- /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
10122
+ GridInput.displayName = "MetadataForm.GridInput";
10123
+ const PlaceholderInner = () => {
10124
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10125
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10126
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10127
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
10128
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
10129
+ ] }) })
10812
10130
  ] });
10813
10131
  };
10814
- const CustomItemsForm = () => {
10815
- const form = reactHookForm.useForm({
10816
- resolver: zod.zodResolver(schema$3)
10132
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
10133
+ function getDefaultValues(metadata) {
10134
+ if (!metadata || !Object.keys(metadata).length) {
10135
+ return [
10136
+ {
10137
+ key: "",
10138
+ value: "",
10139
+ disabled: false
10140
+ }
10141
+ ];
10142
+ }
10143
+ return Object.entries(metadata).map(([key, value]) => {
10144
+ if (!EDITABLE_TYPES.includes(typeof value)) {
10145
+ return {
10146
+ key,
10147
+ value,
10148
+ disabled: true
10149
+ };
10150
+ }
10151
+ let stringValue = value;
10152
+ if (typeof value !== "string") {
10153
+ stringValue = JSON.stringify(value);
10154
+ }
10155
+ return {
10156
+ key,
10157
+ value: stringValue,
10158
+ original_key: key
10159
+ };
10817
10160
  });
10818
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
10819
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
10820
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10821
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10822
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
10823
- ] }) })
10824
- ] }) });
10825
- };
10826
- const schema$3 = objectType({
10827
- email: stringType().email()
10828
- });
10829
- const InlineTip = React.forwardRef(
10830
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10831
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10161
+ }
10162
+ function parseValues(values) {
10163
+ const metadata = values.metadata;
10164
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10165
+ if (isEmpty) {
10166
+ return null;
10167
+ }
10168
+ const update = {};
10169
+ metadata.forEach((field) => {
10170
+ let key = field.key;
10171
+ let value = field.value;
10172
+ const disabled = field.disabled;
10173
+ if (!key || !value) {
10174
+ return;
10175
+ }
10176
+ if (disabled) {
10177
+ update[key] = value;
10178
+ return;
10179
+ }
10180
+ key = key.trim();
10181
+ value = value.trim();
10182
+ if (value === "true") {
10183
+ update[key] = true;
10184
+ } else if (value === "false") {
10185
+ update[key] = false;
10186
+ } else {
10187
+ const parsedNumber = parseFloat(value);
10188
+ if (!isNaN(parsedNumber)) {
10189
+ update[key] = parsedNumber;
10190
+ } else {
10191
+ update[key] = value;
10192
+ }
10193
+ }
10194
+ });
10195
+ return update;
10196
+ }
10197
+ function getHasUneditableRows(metadata) {
10198
+ if (!metadata) {
10199
+ return false;
10200
+ }
10201
+ return Object.values(metadata).some(
10202
+ (value) => !EDITABLE_TYPES.includes(typeof value)
10203
+ );
10204
+ }
10205
+ const NumberInput = React.forwardRef(
10206
+ ({
10207
+ value,
10208
+ onChange,
10209
+ size = "base",
10210
+ min = 0,
10211
+ max = 100,
10212
+ step = 1,
10213
+ className,
10214
+ disabled,
10215
+ ...props
10216
+ }, ref) => {
10217
+ const handleChange = (event) => {
10218
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
10219
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
10220
+ onChange(newValue);
10221
+ }
10222
+ };
10223
+ const handleIncrement = () => {
10224
+ const newValue = value + step;
10225
+ if (max === void 0 || newValue <= max) {
10226
+ onChange(newValue);
10227
+ }
10228
+ };
10229
+ const handleDecrement = () => {
10230
+ const newValue = value - step;
10231
+ if (min === void 0 || newValue >= min) {
10232
+ onChange(newValue);
10233
+ }
10234
+ };
10832
10235
  return /* @__PURE__ */ jsxRuntime.jsxs(
10833
10236
  "div",
10834
10237
  {
10835
- ref,
10836
10238
  className: ui.clx(
10837
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10239
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
10240
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
10241
+ {
10242
+ "h-7": size === "small",
10243
+ "h-8": size === "base"
10244
+ },
10838
10245
  className
10839
10246
  ),
10840
- ...props,
10841
10247
  children: [
10842
10248
  /* @__PURE__ */ jsxRuntime.jsx(
10843
- "div",
10249
+ "input",
10844
10250
  {
10845
- role: "presentation",
10846
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10847
- "bg-ui-tag-orange-icon": variant === "warning"
10848
- })
10251
+ ref,
10252
+ type: "number",
10253
+ value,
10254
+ onChange: handleChange,
10255
+ min,
10256
+ max,
10257
+ step,
10258
+ className: ui.clx(
10259
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
10260
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
10261
+ "placeholder:text-ui-fg-muted"
10262
+ ),
10263
+ ...props
10264
+ }
10265
+ ),
10266
+ /* @__PURE__ */ jsxRuntime.jsxs(
10267
+ "button",
10268
+ {
10269
+ className: ui.clx(
10270
+ "flex items-center justify-center outline-none transition-fg",
10271
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10272
+ "focus:bg-ui-bg-field-component-hover",
10273
+ "hover:bg-ui-bg-field-component-hover",
10274
+ {
10275
+ "size-7": size === "small",
10276
+ "size-8": size === "base"
10277
+ }
10278
+ ),
10279
+ type: "button",
10280
+ onClick: handleDecrement,
10281
+ disabled: min !== void 0 && value <= min || disabled,
10282
+ children: [
10283
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
10284
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
10285
+ ]
10849
10286
  }
10850
10287
  ),
10851
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10852
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10853
- labelValue,
10854
- ":"
10855
- ] }),
10856
- " ",
10857
- children
10858
- ] })
10288
+ /* @__PURE__ */ jsxRuntime.jsxs(
10289
+ "button",
10290
+ {
10291
+ className: ui.clx(
10292
+ "flex items-center justify-center outline-none transition-fg",
10293
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10294
+ "focus:bg-ui-bg-field-hover",
10295
+ "hover:bg-ui-bg-field-hover",
10296
+ {
10297
+ "size-7": size === "small",
10298
+ "size-8": size === "base"
10299
+ }
10300
+ ),
10301
+ type: "button",
10302
+ onClick: handleIncrement,
10303
+ disabled: max !== void 0 && value >= max || disabled,
10304
+ children: [
10305
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
10306
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
10307
+ ]
10308
+ }
10309
+ )
10859
10310
  ]
10860
10311
  }
10861
10312
  );
10862
10313
  }
10863
10314
  );
10864
- InlineTip.displayName = "InlineTip";
10865
- const MetadataFieldSchema = objectType({
10866
- key: stringType(),
10867
- disabled: booleanType().optional(),
10868
- value: anyType()
10869
- });
10870
- const MetadataSchema = objectType({
10871
- metadata: arrayType(MetadataFieldSchema)
10872
- });
10873
- const Metadata = () => {
10315
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
10316
+ const productVariantsQueryKeys = {
10317
+ list: (query2) => [
10318
+ PRODUCT_VARIANTS_QUERY_KEY,
10319
+ query2 ? query2 : void 0
10320
+ ]
10321
+ };
10322
+ const useProductVariants = (query2, options) => {
10323
+ const { data, ...rest } = reactQuery.useQuery({
10324
+ queryKey: productVariantsQueryKeys.list(query2),
10325
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
10326
+ ...options
10327
+ });
10328
+ return { ...data, ...rest };
10329
+ };
10330
+ const useCancelOrderEdit = ({ preview }) => {
10331
+ const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10332
+ const onCancel = React.useCallback(async () => {
10333
+ if (!preview) {
10334
+ return true;
10335
+ }
10336
+ let res = false;
10337
+ await cancelOrderEdit(void 0, {
10338
+ onError: (e) => {
10339
+ ui.toast.error(e.message);
10340
+ },
10341
+ onSuccess: () => {
10342
+ res = true;
10343
+ }
10344
+ });
10345
+ return res;
10346
+ }, [preview, cancelOrderEdit]);
10347
+ return { onCancel };
10348
+ };
10349
+ let IS_REQUEST_RUNNING = false;
10350
+ const useInitiateOrderEdit = ({
10351
+ preview
10352
+ }) => {
10353
+ const navigate = reactRouterDom.useNavigate();
10354
+ const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10355
+ React.useEffect(() => {
10356
+ async function run() {
10357
+ if (IS_REQUEST_RUNNING || !preview) {
10358
+ return;
10359
+ }
10360
+ if (preview.order_change) {
10361
+ return;
10362
+ }
10363
+ IS_REQUEST_RUNNING = true;
10364
+ await mutateAsync(void 0, {
10365
+ onError: (e) => {
10366
+ ui.toast.error(e.message);
10367
+ navigate(`/draft-orders/${preview.id}`, { replace: true });
10368
+ return;
10369
+ }
10370
+ });
10371
+ IS_REQUEST_RUNNING = false;
10372
+ }
10373
+ run();
10374
+ }, [preview, navigate, mutateAsync]);
10375
+ };
10376
+ function convertNumber(value) {
10377
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10378
+ }
10379
+ const STACKED_MODAL_ID = "items_stacked_modal";
10380
+ const Items = () => {
10874
10381
  const { id } = reactRouterDom.useParams();
10875
- const { order, isPending, isError, error } = useOrder(id, {
10876
- fields: "metadata"
10382
+ const {
10383
+ order: preview,
10384
+ isPending: isPreviewPending,
10385
+ isError: isPreviewError,
10386
+ error: previewError
10387
+ } = useOrderPreview(id, void 0, {
10388
+ placeholderData: reactQuery.keepPreviousData
10877
10389
  });
10390
+ useInitiateOrderEdit({ preview });
10391
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10392
+ id,
10393
+ {
10394
+ fields: "currency_code"
10395
+ },
10396
+ {
10397
+ enabled: !!id
10398
+ }
10399
+ );
10400
+ const { onCancel } = useCancelOrderEdit({ preview });
10878
10401
  if (isError) {
10879
10402
  throw error;
10880
10403
  }
10881
- const isReady = !isPending && !!order;
10882
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10883
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10884
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10885
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10886
- ] }),
10887
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10888
- ] });
10404
+ if (isPreviewError) {
10405
+ throw previewError;
10406
+ }
10407
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10408
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10409
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10410
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10411
+ ] }) });
10889
10412
  };
10890
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10891
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10892
- const MetadataForm = ({ orderId, metadata }) => {
10413
+ const ItemsForm = ({ preview, currencyCode }) => {
10414
+ var _a;
10415
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
10416
+ const [modalContent, setModalContent] = React.useState(
10417
+ null
10418
+ );
10893
10419
  const { handleSuccess } = useRouteModal();
10894
- const hasUneditableRows = getHasUneditableRows(metadata);
10895
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10896
- const form = reactHookForm.useForm({
10897
- defaultValues: {
10898
- metadata: getDefaultValues(metadata)
10899
- },
10900
- resolver: zod.zodResolver(MetadataSchema)
10901
- });
10902
- const handleSubmit = form.handleSubmit(async (data) => {
10903
- const parsedData = parseValues(data);
10904
- await mutateAsync(
10905
- {
10906
- metadata: parsedData
10420
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10421
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10422
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10423
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10424
+ const matches = React.useMemo(() => {
10425
+ return matchSorter.matchSorter(preview.items, query2, {
10426
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
10427
+ });
10428
+ }, [preview.items, query2]);
10429
+ const onSubmit = async () => {
10430
+ setIsSubmitting(true);
10431
+ let requestSucceeded = false;
10432
+ await requestOrderEdit(void 0, {
10433
+ onError: (e) => {
10434
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
10907
10435
  },
10908
- {
10909
- onSuccess: () => {
10910
- ui.toast.success("Metadata updated");
10911
- handleSuccess();
10912
- },
10913
- onError: (error) => {
10914
- ui.toast.error(error.message);
10915
- }
10436
+ onSuccess: () => {
10437
+ requestSucceeded = true;
10916
10438
  }
10917
- );
10918
- });
10919
- const { fields, insert, remove } = reactHookForm.useFieldArray({
10920
- control: form.control,
10921
- name: "metadata"
10922
- });
10923
- function deleteRow(index) {
10924
- remove(index);
10925
- if (fields.length === 1) {
10926
- insert(0, {
10927
- key: "",
10928
- value: "",
10929
- disabled: false
10930
- });
10439
+ });
10440
+ if (!requestSucceeded) {
10441
+ setIsSubmitting(false);
10442
+ return;
10931
10443
  }
10932
- }
10933
- function insertRow(index, position) {
10934
- insert(index + (position === "above" ? 0 : 1), {
10935
- key: "",
10936
- value: "",
10937
- disabled: false
10444
+ await confirmOrderEdit(void 0, {
10445
+ onError: (e) => {
10446
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10447
+ },
10448
+ onSuccess: () => {
10449
+ handleSuccess();
10450
+ },
10451
+ onSettled: () => {
10452
+ setIsSubmitting(false);
10453
+ }
10938
10454
  });
10939
- }
10940
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10941
- KeyboundForm,
10942
- {
10943
- onSubmit: handleSubmit,
10944
- className: "flex flex-1 flex-col overflow-hidden",
10945
- children: [
10946
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10947
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10948
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10949
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
10950
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
10455
+ };
10456
+ const onKeyDown = React.useCallback(
10457
+ (e) => {
10458
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10459
+ if (modalContent || isSubmitting) {
10460
+ return;
10461
+ }
10462
+ onSubmit();
10463
+ }
10464
+ },
10465
+ [modalContent, isSubmitting, onSubmit]
10466
+ );
10467
+ React.useEffect(() => {
10468
+ document.addEventListener("keydown", onKeyDown);
10469
+ return () => {
10470
+ document.removeEventListener("keydown", onKeyDown);
10471
+ };
10472
+ }, [onKeyDown]);
10473
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10474
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10475
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10476
+ StackedFocusModal,
10477
+ {
10478
+ id: STACKED_MODAL_ID,
10479
+ onOpenChangeCallback: (open) => {
10480
+ if (!open) {
10481
+ setModalContent(null);
10482
+ }
10483
+ },
10484
+ children: [
10485
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-6 py-16", children: [
10486
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10487
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10488
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Edit the items in the draft order" }) })
10951
10489
  ] }),
10952
- fields.map((field, index) => {
10953
- const isDisabled = field.disabled || false;
10954
- let placeholder = "-";
10955
- if (typeof field.value === "object") {
10956
- placeholder = "{ ... }";
10957
- }
10958
- if (Array.isArray(field.value)) {
10959
- placeholder = "[ ... ]";
10960
- }
10961
- return /* @__PURE__ */ jsxRuntime.jsx(
10962
- ConditionalTooltip,
10963
- {
10964
- showTooltip: isDisabled,
10965
- content: "This row is disabled because it contains non-primitive data.",
10966
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
10967
- /* @__PURE__ */ jsxRuntime.jsxs(
10968
- "div",
10969
- {
10970
- className: ui.clx("grid grid-cols-2 divide-x", {
10971
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10972
- }),
10973
- children: [
10974
- /* @__PURE__ */ jsxRuntime.jsx(
10975
- Form$2.Field,
10976
- {
10977
- control: form.control,
10978
- name: `metadata.${index}.key`,
10979
- render: ({ field: field2 }) => {
10980
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10981
- GridInput,
10982
- {
10983
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10984
- ...field2,
10985
- disabled: isDisabled,
10986
- placeholder: "Key"
10987
- }
10988
- ) }) });
10989
- }
10990
- }
10991
- ),
10992
- /* @__PURE__ */ jsxRuntime.jsx(
10993
- Form$2.Field,
10994
- {
10995
- control: form.control,
10996
- name: `metadata.${index}.value`,
10997
- render: ({ field: { value, ...field2 } }) => {
10998
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10999
- GridInput,
11000
- {
11001
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
11002
- ...field2,
11003
- value: isDisabled ? placeholder : value,
11004
- disabled: isDisabled,
11005
- placeholder: "Value"
11006
- }
11007
- ) }) });
11008
- }
11009
- }
11010
- )
11011
- ]
11012
- }
11013
- ),
11014
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10490
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10491
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10492
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10493
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10494
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10495
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10496
+ ] }),
10497
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10498
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10499
+ ui.Input,
10500
+ {
10501
+ type: "search",
10502
+ placeholder: "Search items",
10503
+ value: searchValue,
10504
+ onChange: (e) => onSearchValueChange(e.target.value)
10505
+ }
10506
+ ) }),
10507
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10508
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10509
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11015
10510
  /* @__PURE__ */ jsxRuntime.jsx(
11016
- ui.DropdownMenu.Trigger,
10511
+ StackedModalTrigger$1,
11017
10512
  {
11018
- className: ui.clx(
11019
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11020
- {
11021
- hidden: isDisabled
11022
- }
11023
- ),
11024
- disabled: isDisabled,
11025
- asChild: true,
11026
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
10513
+ type: "add-items",
10514
+ setModalContent
11027
10515
  }
11028
10516
  ),
11029
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11030
- /* @__PURE__ */ jsxRuntime.jsxs(
11031
- ui.DropdownMenu.Item,
11032
- {
11033
- className: "gap-x-2",
11034
- onClick: () => insertRow(index, "above"),
11035
- children: [
11036
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11037
- "Insert row above"
11038
- ]
11039
- }
11040
- ),
11041
- /* @__PURE__ */ jsxRuntime.jsxs(
11042
- ui.DropdownMenu.Item,
11043
- {
11044
- className: "gap-x-2",
11045
- onClick: () => insertRow(index, "below"),
11046
- children: [
11047
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11048
- "Insert row below"
11049
- ]
11050
- }
11051
- ),
11052
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11053
- /* @__PURE__ */ jsxRuntime.jsxs(
11054
- ui.DropdownMenu.Item,
11055
- {
11056
- className: "gap-x-2",
11057
- onClick: () => deleteRow(index),
11058
- children: [
11059
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11060
- "Delete row"
11061
- ]
11062
- }
11063
- )
11064
- ] })
10517
+ /* @__PURE__ */ jsxRuntime.jsx(
10518
+ StackedModalTrigger$1,
10519
+ {
10520
+ type: "add-custom-item",
10521
+ setModalContent
10522
+ }
10523
+ )
11065
10524
  ] })
11066
10525
  ] })
11067
- },
11068
- field.id
11069
- );
11070
- })
11071
- ] }),
11072
- hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
11073
- ] }),
11074
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11075
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11076
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11077
- ] }) })
11078
- ]
11079
- }
11080
- ) });
11081
- };
11082
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11083
- return /* @__PURE__ */ jsxRuntime.jsx(
11084
- "input",
11085
- {
11086
- ref,
11087
- ...props,
11088
- autoComplete: "off",
11089
- className: ui.clx(
11090
- "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",
11091
- className
10526
+ ] })
10527
+ ] }),
10528
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10529
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-muted grid grid-cols-[2fr_1fr_2fr_28px] gap-3 px-4 py-2", children: [
10530
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10531
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10532
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10533
+ /* @__PURE__ */ jsxRuntime.jsx("div", {})
10534
+ ] }) }),
10535
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxRuntime.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: [
10536
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10537
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10538
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10539
+ Item,
10540
+ {
10541
+ item,
10542
+ preview,
10543
+ currencyCode
10544
+ },
10545
+ item.id
10546
+ )) : /* @__PURE__ */ jsxRuntime.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: [
10547
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10548
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10549
+ 'No items found for "',
10550
+ query2,
10551
+ '".'
10552
+ ] })
10553
+ ] }) })
10554
+ ] })
10555
+ ] }),
10556
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10557
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10558
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10559
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10560
+ ui.Text,
10561
+ {
10562
+ size: "small",
10563
+ leading: "compact",
10564
+ className: "text-ui-fg-subtle",
10565
+ children: [
10566
+ itemCount,
10567
+ " ",
10568
+ itemCount === 1 ? "item" : "items"
10569
+ ]
10570
+ }
10571
+ ) }),
10572
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
10573
+ ] })
10574
+ ] }) }),
10575
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10576
+ CustomItemForm,
10577
+ {
10578
+ orderId: preview.id,
10579
+ currencyCode
10580
+ }
10581
+ ) : null)
10582
+ ]
10583
+ }
10584
+ ) }),
10585
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10586
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10587
+ /* @__PURE__ */ jsxRuntime.jsx(
10588
+ ui.Button,
10589
+ {
10590
+ size: "small",
10591
+ type: "button",
10592
+ onClick: onSubmit,
10593
+ isLoading: isSubmitting,
10594
+ children: "Save"
10595
+ }
11092
10596
  )
11093
- }
11094
- );
11095
- });
11096
- GridInput.displayName = "MetadataForm.GridInput";
11097
- const PlaceholderInner = () => {
11098
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11099
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11100
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11101
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11102
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11103
10597
  ] }) })
11104
10598
  ] });
11105
10599
  };
11106
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11107
- function getDefaultValues(metadata) {
11108
- if (!metadata || !Object.keys(metadata).length) {
11109
- return [
11110
- {
11111
- key: "",
11112
- value: "",
11113
- disabled: false
11114
- }
11115
- ];
10600
+ const Item = ({ item, preview, currencyCode }) => {
10601
+ if (item.variant_id) {
10602
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
11116
10603
  }
11117
- return Object.entries(metadata).map(([key, value]) => {
11118
- if (!EDITABLE_TYPES.includes(typeof value)) {
11119
- return {
11120
- key,
11121
- value,
11122
- disabled: true
11123
- };
11124
- }
11125
- let stringValue = value;
11126
- if (typeof value !== "string") {
11127
- stringValue = JSON.stringify(value);
11128
- }
11129
- return {
11130
- key,
11131
- value: stringValue,
11132
- original_key: key
11133
- };
10604
+ return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10605
+ };
10606
+ const VariantItem = ({ item, preview, currencyCode }) => {
10607
+ const [editing, setEditing] = React.useState(false);
10608
+ const form = reactHookForm.useForm({
10609
+ defaultValues: {
10610
+ quantity: item.quantity,
10611
+ unit_price: item.unit_price
10612
+ },
10613
+ resolver: zod.zodResolver(variantItemSchema)
11134
10614
  });
11135
- }
11136
- function parseValues(values) {
11137
- const metadata = values.metadata;
11138
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11139
- if (isEmpty) {
11140
- return null;
11141
- }
11142
- const update = {};
11143
- metadata.forEach((field) => {
11144
- let key = field.key;
11145
- let value = field.value;
11146
- const disabled = field.disabled;
11147
- if (!key || !value) {
10615
+ const actionId = React.useMemo(() => {
10616
+ var _a, _b;
10617
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10618
+ }, [item]);
10619
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10620
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10621
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10622
+ const onSubmit = form.handleSubmit(async (data) => {
10623
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10624
+ setEditing(false);
11148
10625
  return;
11149
10626
  }
11150
- if (disabled) {
11151
- update[key] = value;
10627
+ if (!actionId) {
10628
+ await updateOriginalItem(
10629
+ {
10630
+ item_id: item.id,
10631
+ quantity: data.quantity,
10632
+ unit_price: convertNumber(data.unit_price)
10633
+ },
10634
+ {
10635
+ onSuccess: () => {
10636
+ setEditing(false);
10637
+ },
10638
+ onError: (e) => {
10639
+ ui.toast.error(e.message);
10640
+ }
10641
+ }
10642
+ );
11152
10643
  return;
11153
10644
  }
11154
- key = key.trim();
11155
- value = value.trim();
11156
- if (value === "true") {
11157
- update[key] = true;
11158
- } else if (value === "false") {
11159
- update[key] = false;
11160
- } else {
11161
- const parsedNumber = parseFloat(value);
11162
- if (!isNaN(parsedNumber)) {
11163
- update[key] = parsedNumber;
11164
- } else {
11165
- update[key] = value;
10645
+ await updateActionItem(
10646
+ {
10647
+ action_id: actionId,
10648
+ quantity: data.quantity,
10649
+ unit_price: convertNumber(data.unit_price)
10650
+ },
10651
+ {
10652
+ onSuccess: () => {
10653
+ setEditing(false);
10654
+ },
10655
+ onError: (e) => {
10656
+ ui.toast.error(e.message);
10657
+ }
11166
10658
  }
11167
- }
11168
- });
11169
- return update;
11170
- }
11171
- function getHasUneditableRows(metadata) {
11172
- if (!metadata) {
11173
- return false;
11174
- }
11175
- return Object.values(metadata).some(
11176
- (value) => !EDITABLE_TYPES.includes(typeof value)
11177
- );
11178
- }
11179
- const PROMOTION_QUERY_KEY = "promotions";
11180
- const promotionsQueryKeys = {
11181
- list: (query2) => [
11182
- PROMOTION_QUERY_KEY,
11183
- query2 ? query2 : void 0
11184
- ],
11185
- detail: (id, query2) => [
11186
- PROMOTION_QUERY_KEY,
11187
- id,
11188
- query2 ? query2 : void 0
11189
- ]
11190
- };
11191
- const usePromotions = (query2, options) => {
11192
- const { data, ...rest } = reactQuery.useQuery({
11193
- queryKey: promotionsQueryKeys.list(query2),
11194
- queryFn: async () => sdk.admin.promotion.list(query2),
11195
- ...options
10659
+ );
11196
10660
  });
11197
- return { ...data, ...rest };
11198
- };
11199
- const Promotions = () => {
11200
- const { id } = reactRouterDom.useParams();
11201
- const {
11202
- order: preview,
11203
- isError: isPreviewError,
11204
- error: previewError
11205
- } = useOrderPreview(id, void 0);
11206
- useInitiateOrderEdit({ preview });
11207
- const { onCancel } = useCancelOrderEdit({ preview });
11208
- if (isPreviewError) {
11209
- throw previewError;
11210
- }
11211
- const isReady = !!preview;
11212
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11213
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11214
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11215
- ] });
11216
- };
11217
- const PromotionForm = ({ preview }) => {
11218
- const { items, shipping_methods } = preview;
11219
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11220
- const [comboboxValue, setComboboxValue] = React.useState("");
11221
- const { handleSuccess } = useRouteModal();
11222
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11223
- const promoIds = getPromotionIds(items, shipping_methods);
11224
- const { promotions, isPending, isError, error } = usePromotions(
11225
- {
11226
- id: promoIds
10661
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.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: [
10662
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10663
+ /* @__PURE__ */ jsxRuntime.jsx(
10664
+ Thumbnail,
10665
+ {
10666
+ thumbnail: item.thumbnail,
10667
+ alt: item.product_title ?? void 0
10668
+ }
10669
+ ),
10670
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10671
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10672
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10673
+ /* @__PURE__ */ jsxRuntime.jsxs(
10674
+ ui.Text,
10675
+ {
10676
+ size: "small",
10677
+ leading: "compact",
10678
+ className: "text-ui-fg-subtle",
10679
+ children: [
10680
+ "(",
10681
+ item.variant_title,
10682
+ ")"
10683
+ ]
10684
+ }
10685
+ )
10686
+ ] }),
10687
+ /* @__PURE__ */ jsxRuntime.jsx(
10688
+ ui.Text,
10689
+ {
10690
+ size: "small",
10691
+ leading: "compact",
10692
+ className: "text-ui-fg-subtle",
10693
+ children: item.variant_sku
10694
+ }
10695
+ )
10696
+ ] })
10697
+ ] }),
10698
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10699
+ Form$2.Field,
10700
+ {
10701
+ control: form.control,
10702
+ name: "quantity",
10703
+ render: ({ field }) => {
10704
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10705
+ }
10706
+ }
10707
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10708
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10709
+ Form$2.Field,
10710
+ {
10711
+ control: form.control,
10712
+ name: "unit_price",
10713
+ render: ({ field: { onChange, ...field } }) => {
10714
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10715
+ ui.CurrencyInput,
10716
+ {
10717
+ ...field,
10718
+ symbol: getNativeSymbol(currencyCode),
10719
+ code: currencyCode,
10720
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10721
+ }
10722
+ ) }) });
10723
+ }
10724
+ }
10725
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10726
+ /* @__PURE__ */ jsxRuntime.jsx(
10727
+ ui.IconButton,
10728
+ {
10729
+ type: "button",
10730
+ size: "small",
10731
+ onClick: editing ? onSubmit : () => {
10732
+ setEditing(true);
10733
+ },
10734
+ disabled: isPending,
10735
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10736
+ }
10737
+ )
10738
+ ] }) }) });
10739
+ };
10740
+ const variantItemSchema = objectType({
10741
+ quantity: numberType(),
10742
+ unit_price: unionType([numberType(), stringType()])
10743
+ });
10744
+ const CustomItem = ({ item, preview, currencyCode }) => {
10745
+ const [editing, setEditing] = React.useState(false);
10746
+ const { quantity, unit_price, title } = item;
10747
+ const form = reactHookForm.useForm({
10748
+ defaultValues: {
10749
+ title,
10750
+ quantity,
10751
+ unit_price
11227
10752
  },
11228
- {
11229
- enabled: !!promoIds.length
10753
+ resolver: zod.zodResolver(customItemSchema)
10754
+ });
10755
+ React.useEffect(() => {
10756
+ form.reset({
10757
+ title,
10758
+ quantity,
10759
+ unit_price
10760
+ });
10761
+ }, [form, title, quantity, unit_price]);
10762
+ const actionId = React.useMemo(() => {
10763
+ var _a, _b;
10764
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10765
+ }, [item]);
10766
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10767
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10768
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10769
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10770
+ const onSubmit = form.handleSubmit(async (data) => {
10771
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10772
+ setEditing(false);
10773
+ return;
11230
10774
  }
11231
- );
11232
- const comboboxData = useComboboxData({
11233
- queryKey: ["promotions", "combobox", promoIds],
11234
- queryFn: async (params) => {
11235
- return await sdk.admin.promotion.list({
11236
- ...params,
11237
- id: {
11238
- $nin: promoIds
10775
+ if (!actionId) {
10776
+ await updateOriginalItem(
10777
+ {
10778
+ item_id: item.id,
10779
+ quantity: data.quantity,
10780
+ unit_price: convertNumber(data.unit_price)
10781
+ },
10782
+ {
10783
+ onSuccess: () => {
10784
+ setEditing(false);
10785
+ },
10786
+ onError: (e) => {
10787
+ ui.toast.error(e.message);
10788
+ }
11239
10789
  }
11240
- });
11241
- },
11242
- getOptions: (data) => {
11243
- return data.promotions.map((promotion) => ({
11244
- label: promotion.code,
11245
- value: promotion.code
11246
- }));
10790
+ );
10791
+ return;
11247
10792
  }
11248
- });
11249
- const add = async (value) => {
11250
- if (!value) {
10793
+ if (data.quantity === 0) {
10794
+ await removeActionItem(actionId, {
10795
+ onSuccess: () => {
10796
+ setEditing(false);
10797
+ },
10798
+ onError: (e) => {
10799
+ ui.toast.error(e.message);
10800
+ }
10801
+ });
11251
10802
  return;
11252
10803
  }
11253
- addPromotions(
10804
+ await updateActionItem(
11254
10805
  {
11255
- promo_codes: [value]
10806
+ action_id: actionId,
10807
+ quantity: data.quantity,
10808
+ unit_price: convertNumber(data.unit_price)
11256
10809
  },
11257
10810
  {
10811
+ onSuccess: () => {
10812
+ setEditing(false);
10813
+ },
11258
10814
  onError: (e) => {
11259
10815
  ui.toast.error(e.message);
11260
- comboboxData.onSearchValueChange("");
11261
- setComboboxValue("");
11262
- },
11263
- onSuccess: () => {
11264
- comboboxData.onSearchValueChange("");
11265
- setComboboxValue("");
11266
10816
  }
11267
10817
  }
11268
10818
  );
11269
- };
11270
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11271
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11272
- const onSubmit = async () => {
11273
- setIsSubmitting(true);
11274
- let requestSucceeded = false;
11275
- await requestOrderEdit(void 0, {
11276
- onError: (e) => {
11277
- ui.toast.error(e.message);
11278
- },
11279
- onSuccess: () => {
11280
- requestSucceeded = true;
11281
- }
11282
- });
11283
- if (!requestSucceeded) {
11284
- setIsSubmitting(false);
11285
- return;
11286
- }
11287
- await confirmOrderEdit(void 0, {
11288
- onError: (e) => {
11289
- ui.toast.error(e.message);
11290
- },
11291
- onSuccess: () => {
11292
- handleSuccess();
11293
- },
11294
- onSettled: () => {
11295
- setIsSubmitting(false);
11296
- }
11297
- });
11298
- };
11299
- if (isError) {
11300
- throw error;
11301
- }
11302
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11303
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11304
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11305
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11306
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11307
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11308
- ] }),
11309
- /* @__PURE__ */ jsxRuntime.jsx(
11310
- Combobox,
11311
- {
11312
- id: "promotion-combobox",
11313
- "aria-describedby": "promotion-combobox-hint",
11314
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11315
- fetchNextPage: comboboxData.fetchNextPage,
11316
- options: comboboxData.options,
11317
- onSearchValueChange: comboboxData.onSearchValueChange,
11318
- searchValue: comboboxData.searchValue,
11319
- disabled: comboboxData.disabled || isAddingPromotions,
11320
- onChange: add,
11321
- value: comboboxValue
11322
- }
11323
- )
11324
- ] }),
11325
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11326
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11327
- PromotionItem,
11328
- {
11329
- promotion,
11330
- orderId: preview.id,
11331
- isLoading: isPending
11332
- },
11333
- promotion.id
11334
- )) })
11335
- ] }) }),
11336
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11337
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10819
+ });
10820
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.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: [
10821
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
11338
10822
  /* @__PURE__ */ jsxRuntime.jsx(
11339
- ui.Button,
10823
+ Thumbnail,
11340
10824
  {
11341
- size: "small",
11342
- type: "submit",
11343
- isLoading: isSubmitting || isAddingPromotions,
11344
- children: "Save"
10825
+ thumbnail: item.thumbnail,
10826
+ alt: item.title ?? void 0
11345
10827
  }
11346
- )
11347
- ] }) })
11348
- ] });
10828
+ ),
10829
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
10830
+ Form$2.Field,
10831
+ {
10832
+ control: form.control,
10833
+ name: "title",
10834
+ render: ({ field }) => {
10835
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
10836
+ }
10837
+ }
10838
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
10839
+ ] }),
10840
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
10841
+ Form$2.Field,
10842
+ {
10843
+ control: form.control,
10844
+ name: "quantity",
10845
+ render: ({ field }) => {
10846
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10847
+ }
10848
+ }
10849
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
10850
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
10851
+ Form$2.Field,
10852
+ {
10853
+ control: form.control,
10854
+ name: "unit_price",
10855
+ render: ({ field: { onChange, ...field } }) => {
10856
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10857
+ ui.CurrencyInput,
10858
+ {
10859
+ ...field,
10860
+ symbol: getNativeSymbol(currencyCode),
10861
+ code: currencyCode,
10862
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10863
+ }
10864
+ ) }) });
10865
+ }
10866
+ }
10867
+ ) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10868
+ /* @__PURE__ */ jsxRuntime.jsx(
10869
+ ui.IconButton,
10870
+ {
10871
+ type: "button",
10872
+ size: "small",
10873
+ onClick: editing ? onSubmit : () => {
10874
+ setEditing(true);
10875
+ },
10876
+ disabled: isPending,
10877
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10878
+ }
10879
+ )
10880
+ ] }) }) });
11349
10881
  };
11350
- const PromotionItem = ({
11351
- promotion,
11352
- orderId,
11353
- isLoading
10882
+ const StackedModalTrigger$1 = ({
10883
+ type,
10884
+ setModalContent
11354
10885
  }) => {
11355
- var _a;
11356
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11357
- const onRemove = async () => {
11358
- removePromotions(
10886
+ const { setIsOpen } = useStackedModal();
10887
+ const onClick = React.useCallback(() => {
10888
+ setModalContent(type);
10889
+ setIsOpen(STACKED_MODAL_ID, true);
10890
+ }, [setModalContent, setIsOpen, type]);
10891
+ return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10892
+ };
10893
+ const VARIANT_PREFIX = "items";
10894
+ const LIMIT = 50;
10895
+ const ExistingItemsForm = ({ orderId, items }) => {
10896
+ const { setIsOpen } = useStackedModal();
10897
+ const [rowSelection, setRowSelection] = React.useState(
10898
+ items.reduce((acc, item) => {
10899
+ acc[item.variant_id] = true;
10900
+ return acc;
10901
+ }, {})
10902
+ );
10903
+ React.useEffect(() => {
10904
+ setRowSelection(
10905
+ items.reduce((acc, item) => {
10906
+ if (item.variant_id) {
10907
+ acc[item.variant_id] = true;
10908
+ }
10909
+ return acc;
10910
+ }, {})
10911
+ );
10912
+ }, [items]);
10913
+ const { q, order, offset } = useQueryParams(
10914
+ ["q", "order", "offset"],
10915
+ VARIANT_PREFIX
10916
+ );
10917
+ const { variants, count, isPending, isError, error } = useProductVariants(
10918
+ {
10919
+ q,
10920
+ order,
10921
+ offset: offset ? parseInt(offset) : void 0,
10922
+ limit: LIMIT
10923
+ },
10924
+ {
10925
+ placeholderData: reactQuery.keepPreviousData
10926
+ }
10927
+ );
10928
+ const columns = useColumns();
10929
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
10930
+ const onSubmit = async () => {
10931
+ const ids = Object.keys(rowSelection).filter(
10932
+ (id) => !items.find((i) => i.variant_id === id)
10933
+ );
10934
+ await mutateAsync(
11359
10935
  {
11360
- promo_codes: [promotion.code]
10936
+ items: ids.map((id) => ({
10937
+ variant_id: id,
10938
+ quantity: 1
10939
+ }))
11361
10940
  },
11362
10941
  {
10942
+ onSuccess: () => {
10943
+ setRowSelection({});
10944
+ setIsOpen(STACKED_MODAL_ID, false);
10945
+ },
11363
10946
  onError: (e) => {
11364
10947
  ui.toast.error(e.message);
11365
10948
  }
11366
10949
  }
11367
10950
  );
11368
10951
  };
11369
- const displayValue = getDisplayValue(promotion);
10952
+ if (isError) {
10953
+ throw error;
10954
+ }
11370
10955
  return /* @__PURE__ */ jsxRuntime.jsxs(
11371
- "div",
10956
+ StackedFocusModal.Content,
11372
10957
  {
11373
- className: ui.clx(
11374
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11375
- {
11376
- "animate-pulse": isLoading
10958
+ onOpenAutoFocus: (e) => {
10959
+ e.preventDefault();
10960
+ const searchInput = document.querySelector(
10961
+ "[data-modal-id='modal-search-input']"
10962
+ );
10963
+ if (searchInput) {
10964
+ searchInput.focus();
11377
10965
  }
11378
- ),
10966
+ },
11379
10967
  children: [
11380
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11381
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11382
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11383
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11384
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11385
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11386
- ] }),
11387
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11388
- ] })
10968
+ /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
10969
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10970
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
11389
10971
  ] }),
11390
- /* @__PURE__ */ jsxRuntime.jsx(
11391
- ui.IconButton,
10972
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
10973
+ DataTable,
11392
10974
  {
11393
- size: "small",
11394
- type: "button",
11395
- variant: "transparent",
11396
- onClick: onRemove,
11397
- isLoading: isPending || isLoading,
11398
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
10975
+ data: variants,
10976
+ columns,
10977
+ isLoading: isPending,
10978
+ getRowId: (row) => row.id,
10979
+ rowCount: count,
10980
+ prefix: VARIANT_PREFIX,
10981
+ layout: "fill",
10982
+ rowSelection: {
10983
+ state: rowSelection,
10984
+ onRowSelectionChange: setRowSelection,
10985
+ enableRowSelection: (row) => {
10986
+ return !items.find((i) => i.variant_id === row.original.id);
10987
+ }
10988
+ },
10989
+ autoFocusSearch: true
11399
10990
  }
11400
- )
10991
+ ) }),
10992
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10993
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10994
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10995
+ ] }) })
11401
10996
  ]
11402
- },
11403
- promotion.id
10997
+ }
11404
10998
  );
11405
10999
  };
11406
- function getDisplayValue(promotion) {
11407
- var _a, _b, _c, _d;
11408
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11409
- if (!value) {
11410
- return null;
11411
- }
11412
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11413
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11414
- if (!currency) {
11415
- return null;
11416
- }
11417
- return getLocaleAmount(value, currency);
11418
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11419
- return formatPercentage(value);
11420
- }
11421
- return null;
11422
- }
11423
- const formatter = new Intl.NumberFormat([], {
11424
- style: "percent",
11425
- minimumFractionDigits: 2
11426
- });
11427
- const formatPercentage = (value, isPercentageValue = false) => {
11428
- let val = value || 0;
11429
- if (!isPercentageValue) {
11430
- val = val / 100;
11431
- }
11432
- return formatter.format(val);
11000
+ const columnHelper = ui.createDataTableColumnHelper();
11001
+ const useColumns = () => {
11002
+ return React.useMemo(() => {
11003
+ return [
11004
+ columnHelper.select(),
11005
+ columnHelper.accessor("product.title", {
11006
+ header: "Product",
11007
+ cell: ({ row }) => {
11008
+ var _a, _b, _c;
11009
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
11010
+ /* @__PURE__ */ jsxRuntime.jsx(
11011
+ Thumbnail,
11012
+ {
11013
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
11014
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
11015
+ }
11016
+ ),
11017
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
11018
+ ] });
11019
+ },
11020
+ enableSorting: true
11021
+ }),
11022
+ columnHelper.accessor("title", {
11023
+ header: "Variant",
11024
+ enableSorting: true
11025
+ }),
11026
+ columnHelper.accessor("sku", {
11027
+ header: "SKU",
11028
+ cell: ({ getValue }) => {
11029
+ return getValue() ?? "-";
11030
+ },
11031
+ enableSorting: true
11032
+ }),
11033
+ columnHelper.accessor("updated_at", {
11034
+ header: "Updated",
11035
+ cell: ({ getValue }) => {
11036
+ return /* @__PURE__ */ jsxRuntime.jsx(
11037
+ ui.Tooltip,
11038
+ {
11039
+ content: getFullDate({ date: getValue(), includeTime: true }),
11040
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
11041
+ }
11042
+ );
11043
+ },
11044
+ enableSorting: true,
11045
+ sortAscLabel: "Oldest first",
11046
+ sortDescLabel: "Newest first"
11047
+ }),
11048
+ columnHelper.accessor("created_at", {
11049
+ header: "Created",
11050
+ cell: ({ getValue }) => {
11051
+ return /* @__PURE__ */ jsxRuntime.jsx(
11052
+ ui.Tooltip,
11053
+ {
11054
+ content: getFullDate({ date: getValue(), includeTime: true }),
11055
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
11056
+ }
11057
+ );
11058
+ },
11059
+ enableSorting: true,
11060
+ sortAscLabel: "Oldest first",
11061
+ sortDescLabel: "Newest first"
11062
+ })
11063
+ ];
11064
+ }, []);
11433
11065
  };
11434
- function getPromotionIds(items, shippingMethods) {
11435
- const promotionIds = /* @__PURE__ */ new Set();
11436
- for (const item of items) {
11437
- if (item.adjustments) {
11438
- for (const adjustment of item.adjustments) {
11439
- if (adjustment.promotion_id) {
11440
- promotionIds.add(adjustment.promotion_id);
11066
+ const CustomItemForm = ({ orderId, currencyCode }) => {
11067
+ const { setIsOpen } = useStackedModal();
11068
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
11069
+ const form = reactHookForm.useForm({
11070
+ defaultValues: {
11071
+ title: "",
11072
+ quantity: 1,
11073
+ unit_price: ""
11074
+ },
11075
+ resolver: zod.zodResolver(customItemSchema)
11076
+ });
11077
+ const onSubmit = form.handleSubmit(async (data) => {
11078
+ await addItems(
11079
+ {
11080
+ items: [
11081
+ {
11082
+ title: data.title,
11083
+ quantity: data.quantity,
11084
+ unit_price: convertNumber(data.unit_price)
11085
+ }
11086
+ ]
11087
+ },
11088
+ {
11089
+ onSuccess: () => {
11090
+ setIsOpen(STACKED_MODAL_ID, false);
11091
+ },
11092
+ onError: (e) => {
11093
+ ui.toast.error(e.message);
11094
+ }
11095
+ }
11096
+ );
11097
+ });
11098
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
11099
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
11100
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
11101
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11102
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
11103
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
11104
+ ] }),
11105
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11106
+ /* @__PURE__ */ jsxRuntime.jsx(
11107
+ Form$2.Field,
11108
+ {
11109
+ control: form.control,
11110
+ name: "title",
11111
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11112
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11113
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
11114
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
11115
+ ] }),
11116
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11117
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11118
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11119
+ ] })
11120
+ ] }) })
11441
11121
  }
11442
- }
11443
- }
11444
- }
11445
- for (const shippingMethod of shippingMethods) {
11446
- if (shippingMethod.adjustments) {
11447
- for (const adjustment of shippingMethod.adjustments) {
11448
- if (adjustment.promotion_id) {
11449
- promotionIds.add(adjustment.promotion_id);
11122
+ ),
11123
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11124
+ /* @__PURE__ */ jsxRuntime.jsx(
11125
+ Form$2.Field,
11126
+ {
11127
+ control: form.control,
11128
+ name: "unit_price",
11129
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11130
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11131
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
11132
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
11133
+ ] }),
11134
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11135
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11136
+ ui.CurrencyInput,
11137
+ {
11138
+ symbol: getNativeSymbol(currencyCode),
11139
+ code: currencyCode,
11140
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
11141
+ ...field
11142
+ }
11143
+ ) }),
11144
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11145
+ ] })
11146
+ ] }) })
11450
11147
  }
11451
- }
11452
- }
11453
- }
11454
- return Array.from(promotionIds);
11455
- }
11148
+ ),
11149
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11150
+ /* @__PURE__ */ jsxRuntime.jsx(
11151
+ Form$2.Field,
11152
+ {
11153
+ control: form.control,
11154
+ name: "quantity",
11155
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11156
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11157
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
11158
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
11159
+ ] }),
11160
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
11161
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field, className: "w-full" }) }) }),
11162
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11163
+ ] })
11164
+ ] }) })
11165
+ }
11166
+ )
11167
+ ] }) }) }),
11168
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11169
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11170
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
11171
+ ] }) })
11172
+ ] }) }) });
11173
+ };
11174
+ const customItemSchema = objectType({
11175
+ title: stringType().min(1),
11176
+ quantity: numberType(),
11177
+ unit_price: unionType([numberType(), stringType()])
11178
+ });
11456
11179
  const SalesChannel = () => {
11457
11180
  const { id } = reactRouterDom.useParams();
11458
11181
  const { draft_order, isPending, isError, error } = useDraftOrder(
@@ -12965,86 +12688,363 @@ const Illustration = () => {
12965
12688
  /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12966
12689
  "path",
12967
12690
  {
12968
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12969
- stroke: "#A1A1AA",
12970
- strokeWidth: "1.5",
12971
- strokeLinecap: "round",
12972
- strokeLinejoin: "round"
12691
+ d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12692
+ stroke: "#A1A1AA",
12693
+ strokeWidth: "1.5",
12694
+ strokeLinecap: "round",
12695
+ strokeLinejoin: "round"
12696
+ }
12697
+ ) }),
12698
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12699
+ "path",
12700
+ {
12701
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12702
+ stroke: "#A1A1AA",
12703
+ strokeWidth: "1.5",
12704
+ strokeLinecap: "round",
12705
+ strokeLinejoin: "round"
12706
+ }
12707
+ ) }),
12708
+ /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12709
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12710
+ "rect",
12711
+ {
12712
+ width: "12",
12713
+ height: "12",
12714
+ fill: "white",
12715
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12716
+ }
12717
+ ) }),
12718
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12719
+ "rect",
12720
+ {
12721
+ width: "12",
12722
+ height: "12",
12723
+ fill: "white",
12724
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12725
+ }
12726
+ ) }),
12727
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12728
+ "rect",
12729
+ {
12730
+ width: "12",
12731
+ height: "12",
12732
+ fill: "white",
12733
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12734
+ }
12735
+ ) }),
12736
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12737
+ "rect",
12738
+ {
12739
+ width: "12",
12740
+ height: "12",
12741
+ fill: "white",
12742
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12743
+ }
12744
+ ) }),
12745
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12746
+ "rect",
12747
+ {
12748
+ width: "12",
12749
+ height: "12",
12750
+ fill: "white",
12751
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12752
+ }
12753
+ ) }),
12754
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12755
+ "rect",
12756
+ {
12757
+ width: "12",
12758
+ height: "12",
12759
+ fill: "white",
12760
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12761
+ }
12762
+ ) })
12763
+ ] })
12764
+ ]
12765
+ }
12766
+ );
12767
+ };
12768
+ const schema = objectType({
12769
+ customer_id: stringType().min(1)
12770
+ });
12771
+ const PROMOTION_QUERY_KEY = "promotions";
12772
+ const promotionsQueryKeys = {
12773
+ list: (query2) => [
12774
+ PROMOTION_QUERY_KEY,
12775
+ query2 ? query2 : void 0
12776
+ ],
12777
+ detail: (id, query2) => [
12778
+ PROMOTION_QUERY_KEY,
12779
+ id,
12780
+ query2 ? query2 : void 0
12781
+ ]
12782
+ };
12783
+ const usePromotions = (query2, options) => {
12784
+ const { data, ...rest } = reactQuery.useQuery({
12785
+ queryKey: promotionsQueryKeys.list(query2),
12786
+ queryFn: async () => sdk.admin.promotion.list(query2),
12787
+ ...options
12788
+ });
12789
+ return { ...data, ...rest };
12790
+ };
12791
+ const Promotions = () => {
12792
+ const { id } = reactRouterDom.useParams();
12793
+ const {
12794
+ order: preview,
12795
+ isError: isPreviewError,
12796
+ error: previewError
12797
+ } = useOrderPreview(id, void 0);
12798
+ useInitiateOrderEdit({ preview });
12799
+ const { onCancel } = useCancelOrderEdit({ preview });
12800
+ if (isPreviewError) {
12801
+ throw previewError;
12802
+ }
12803
+ const isReady = !!preview;
12804
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
12805
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
12806
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
12807
+ ] });
12808
+ };
12809
+ const PromotionForm = ({ preview }) => {
12810
+ const { items, shipping_methods } = preview;
12811
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
12812
+ const [comboboxValue, setComboboxValue] = React.useState("");
12813
+ const { handleSuccess } = useRouteModal();
12814
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
12815
+ const promoIds = getPromotionIds(items, shipping_methods);
12816
+ const { promotions, isPending, isError, error } = usePromotions(
12817
+ {
12818
+ id: promoIds
12819
+ },
12820
+ {
12821
+ enabled: !!promoIds.length
12822
+ }
12823
+ );
12824
+ const comboboxData = useComboboxData({
12825
+ queryKey: ["promotions", "combobox", promoIds],
12826
+ queryFn: async (params) => {
12827
+ return await sdk.admin.promotion.list({
12828
+ ...params,
12829
+ id: {
12830
+ $nin: promoIds
12831
+ }
12832
+ });
12833
+ },
12834
+ getOptions: (data) => {
12835
+ return data.promotions.map((promotion) => ({
12836
+ label: promotion.code,
12837
+ value: promotion.code
12838
+ }));
12839
+ }
12840
+ });
12841
+ const add = async (value) => {
12842
+ if (!value) {
12843
+ return;
12844
+ }
12845
+ addPromotions(
12846
+ {
12847
+ promo_codes: [value]
12848
+ },
12849
+ {
12850
+ onError: (e) => {
12851
+ ui.toast.error(e.message);
12852
+ comboboxData.onSearchValueChange("");
12853
+ setComboboxValue("");
12854
+ },
12855
+ onSuccess: () => {
12856
+ comboboxData.onSearchValueChange("");
12857
+ setComboboxValue("");
12858
+ }
12859
+ }
12860
+ );
12861
+ };
12862
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12863
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
12864
+ const onSubmit = async () => {
12865
+ setIsSubmitting(true);
12866
+ let requestSucceeded = false;
12867
+ await requestOrderEdit(void 0, {
12868
+ onError: (e) => {
12869
+ ui.toast.error(e.message);
12870
+ },
12871
+ onSuccess: () => {
12872
+ requestSucceeded = true;
12873
+ }
12874
+ });
12875
+ if (!requestSucceeded) {
12876
+ setIsSubmitting(false);
12877
+ return;
12878
+ }
12879
+ await confirmOrderEdit(void 0, {
12880
+ onError: (e) => {
12881
+ ui.toast.error(e.message);
12882
+ },
12883
+ onSuccess: () => {
12884
+ handleSuccess();
12885
+ },
12886
+ onSettled: () => {
12887
+ setIsSubmitting(false);
12888
+ }
12889
+ });
12890
+ };
12891
+ if (isError) {
12892
+ throw error;
12893
+ }
12894
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
12895
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
12896
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
12897
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12898
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
12899
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
12900
+ ] }),
12901
+ /* @__PURE__ */ jsxRuntime.jsx(
12902
+ Combobox,
12903
+ {
12904
+ id: "promotion-combobox",
12905
+ "aria-describedby": "promotion-combobox-hint",
12906
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
12907
+ fetchNextPage: comboboxData.fetchNextPage,
12908
+ options: comboboxData.options,
12909
+ onSearchValueChange: comboboxData.onSearchValueChange,
12910
+ searchValue: comboboxData.searchValue,
12911
+ disabled: comboboxData.disabled || isAddingPromotions,
12912
+ onChange: add,
12913
+ value: comboboxValue
12973
12914
  }
12974
- ) }),
12975
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12976
- "path",
12915
+ )
12916
+ ] }),
12917
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12918
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
12919
+ PromotionItem,
12920
+ {
12921
+ promotion,
12922
+ orderId: preview.id,
12923
+ isLoading: isPending
12924
+ },
12925
+ promotion.id
12926
+ )) })
12927
+ ] }) }),
12928
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12929
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12930
+ /* @__PURE__ */ jsxRuntime.jsx(
12931
+ ui.Button,
12932
+ {
12933
+ size: "small",
12934
+ type: "submit",
12935
+ isLoading: isSubmitting || isAddingPromotions,
12936
+ children: "Save"
12937
+ }
12938
+ )
12939
+ ] }) })
12940
+ ] });
12941
+ };
12942
+ const PromotionItem = ({
12943
+ promotion,
12944
+ orderId,
12945
+ isLoading
12946
+ }) => {
12947
+ var _a;
12948
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
12949
+ const onRemove = async () => {
12950
+ removePromotions(
12951
+ {
12952
+ promo_codes: [promotion.code]
12953
+ },
12954
+ {
12955
+ onError: (e) => {
12956
+ ui.toast.error(e.message);
12957
+ }
12958
+ }
12959
+ );
12960
+ };
12961
+ const displayValue = getDisplayValue(promotion);
12962
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12963
+ "div",
12964
+ {
12965
+ className: ui.clx(
12966
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
12967
+ {
12968
+ "animate-pulse": isLoading
12969
+ }
12970
+ ),
12971
+ children: [
12972
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12973
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
12974
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
12975
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
12976
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
12977
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
12978
+ ] }),
12979
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
12980
+ ] })
12981
+ ] }),
12982
+ /* @__PURE__ */ jsxRuntime.jsx(
12983
+ ui.IconButton,
12977
12984
  {
12978
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12979
- stroke: "#A1A1AA",
12980
- strokeWidth: "1.5",
12981
- strokeLinecap: "round",
12982
- strokeLinejoin: "round"
12985
+ size: "small",
12986
+ type: "button",
12987
+ variant: "transparent",
12988
+ onClick: onRemove,
12989
+ isLoading: isPending || isLoading,
12990
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
12983
12991
  }
12984
- ) }),
12985
- /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12986
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12987
- "rect",
12988
- {
12989
- width: "12",
12990
- height: "12",
12991
- fill: "white",
12992
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12993
- }
12994
- ) }),
12995
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12996
- "rect",
12997
- {
12998
- width: "12",
12999
- height: "12",
13000
- fill: "white",
13001
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
13002
- }
13003
- ) }),
13004
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13005
- "rect",
13006
- {
13007
- width: "12",
13008
- height: "12",
13009
- fill: "white",
13010
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
13011
- }
13012
- ) }),
13013
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13014
- "rect",
13015
- {
13016
- width: "12",
13017
- height: "12",
13018
- fill: "white",
13019
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
13020
- }
13021
- ) }),
13022
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13023
- "rect",
13024
- {
13025
- width: "12",
13026
- height: "12",
13027
- fill: "white",
13028
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
13029
- }
13030
- ) }),
13031
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13032
- "rect",
13033
- {
13034
- width: "12",
13035
- height: "12",
13036
- fill: "white",
13037
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
13038
- }
13039
- ) })
13040
- ] })
12992
+ )
13041
12993
  ]
13042
- }
12994
+ },
12995
+ promotion.id
13043
12996
  );
13044
12997
  };
13045
- const schema = objectType({
13046
- customer_id: stringType().min(1)
12998
+ function getDisplayValue(promotion) {
12999
+ var _a, _b, _c, _d;
13000
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
13001
+ if (!value) {
13002
+ return null;
13003
+ }
13004
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
13005
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
13006
+ if (!currency) {
13007
+ return null;
13008
+ }
13009
+ return getLocaleAmount(value, currency);
13010
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
13011
+ return formatPercentage(value);
13012
+ }
13013
+ return null;
13014
+ }
13015
+ const formatter = new Intl.NumberFormat([], {
13016
+ style: "percent",
13017
+ minimumFractionDigits: 2
13047
13018
  });
13019
+ const formatPercentage = (value, isPercentageValue = false) => {
13020
+ let val = value || 0;
13021
+ if (!isPercentageValue) {
13022
+ val = val / 100;
13023
+ }
13024
+ return formatter.format(val);
13025
+ };
13026
+ function getPromotionIds(items, shippingMethods) {
13027
+ const promotionIds = /* @__PURE__ */ new Set();
13028
+ for (const item of items) {
13029
+ if (item.adjustments) {
13030
+ for (const adjustment of item.adjustments) {
13031
+ if (adjustment.promotion_id) {
13032
+ promotionIds.add(adjustment.promotion_id);
13033
+ }
13034
+ }
13035
+ }
13036
+ }
13037
+ for (const shippingMethod of shippingMethods) {
13038
+ if (shippingMethod.adjustments) {
13039
+ for (const adjustment of shippingMethod.adjustments) {
13040
+ if (adjustment.promotion_id) {
13041
+ promotionIds.add(adjustment.promotion_id);
13042
+ }
13043
+ }
13044
+ }
13045
+ }
13046
+ return Array.from(promotionIds);
13047
+ }
13048
13048
  const widgetModule = { widgets: [] };
13049
13049
  const routeModule = {
13050
13050
  routes: [
@@ -13073,10 +13073,6 @@ const routeModule = {
13073
13073
  Component: Email,
13074
13074
  path: "/draft-orders/:id/email"
13075
13075
  },
13076
- {
13077
- Component: Items,
13078
- path: "/draft-orders/:id/items"
13079
- },
13080
13076
  {
13081
13077
  Component: CustomItems,
13082
13078
  path: "/draft-orders/:id/custom-items"
@@ -13086,8 +13082,8 @@ const routeModule = {
13086
13082
  path: "/draft-orders/:id/metadata"
13087
13083
  },
13088
13084
  {
13089
- Component: Promotions,
13090
- path: "/draft-orders/:id/promotions"
13085
+ Component: Items,
13086
+ path: "/draft-orders/:id/items"
13091
13087
  },
13092
13088
  {
13093
13089
  Component: SalesChannel,
@@ -13104,6 +13100,10 @@ const routeModule = {
13104
13100
  {
13105
13101
  Component: TransferOwnership,
13106
13102
  path: "/draft-orders/:id/transfer-ownership"
13103
+ },
13104
+ {
13105
+ Component: Promotions,
13106
+ path: "/draft-orders/:id/promotions"
13107
13107
  }
13108
13108
  ]
13109
13109
  }