@medusajs/draft-order 2.10.0-snapshot-20250828141006 → 2.10.0

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