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

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