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

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