@medusajs/draft-order 2.11.1-snapshot-20251025141132 → 2.11.1

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