@medusajs/draft-order 2.11.2-snapshot-20251029131556 → 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.
@@ -9761,27 +9761,6 @@ const BillingAddressForm = ({ order }) => {
9761
9761
  ) });
9762
9762
  };
9763
9763
  const schema$5 = addressSchema;
9764
- const CustomItems = () => {
9765
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9766
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9767
- /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9768
- ] });
9769
- };
9770
- const CustomItemsForm = () => {
9771
- const form = reactHookForm.useForm({
9772
- resolver: zod.zodResolver(schema$4)
9773
- });
9774
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9775
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9776
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9777
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9778
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9779
- ] }) })
9780
- ] }) });
9781
- };
9782
- const schema$4 = objectType({
9783
- email: stringType().email()
9784
- });
9785
9764
  const Email = () => {
9786
9765
  const { id } = reactRouterDom.useParams();
9787
9766
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9804,7 +9783,7 @@ const EmailForm = ({ order }) => {
9804
9783
  defaultValues: {
9805
9784
  email: order.email ?? ""
9806
9785
  },
9807
- resolver: zod.zodResolver(schema$3)
9786
+ resolver: zod.zodResolver(schema$4)
9808
9787
  });
9809
9788
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9810
9789
  const { handleSuccess } = useRouteModal();
@@ -9847,1432 +9826,862 @@ const EmailForm = ({ order }) => {
9847
9826
  }
9848
9827
  ) });
9849
9828
  };
9850
- const schema$3 = objectType({
9829
+ const schema$4 = objectType({
9851
9830
  email: stringType().email()
9852
9831
  });
9853
- const NumberInput = React.forwardRef(
9854
- ({
9855
- value,
9856
- onChange,
9857
- size = "base",
9858
- min = 0,
9859
- max = 100,
9860
- step = 1,
9861
- className,
9862
- disabled,
9863
- ...props
9864
- }, ref) => {
9865
- const handleChange = (event) => {
9866
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9867
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9868
- onChange(newValue);
9869
- }
9870
- };
9871
- const handleIncrement = () => {
9872
- const newValue = value + step;
9873
- if (max === void 0 || newValue <= max) {
9874
- onChange(newValue);
9875
- }
9876
- };
9877
- const handleDecrement = () => {
9878
- const newValue = value - step;
9879
- if (min === void 0 || newValue >= min) {
9880
- onChange(newValue);
9881
- }
9882
- };
9832
+ const InlineTip = React.forwardRef(
9833
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9834
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9883
9835
  return /* @__PURE__ */ jsxRuntime.jsxs(
9884
9836
  "div",
9885
9837
  {
9838
+ ref,
9886
9839
  className: ui.clx(
9887
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9888
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9889
- {
9890
- "h-7": size === "small",
9891
- "h-8": size === "base"
9892
- },
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",
9893
9841
  className
9894
9842
  ),
9843
+ ...props,
9895
9844
  children: [
9896
9845
  /* @__PURE__ */ jsxRuntime.jsx(
9897
- "input",
9898
- {
9899
- ref,
9900
- type: "number",
9901
- value,
9902
- onChange: handleChange,
9903
- min,
9904
- max,
9905
- step,
9906
- className: ui.clx(
9907
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9908
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9909
- "placeholder:text-ui-fg-muted"
9910
- ),
9911
- ...props
9912
- }
9913
- ),
9914
- /* @__PURE__ */ jsxRuntime.jsxs(
9915
- "button",
9846
+ "div",
9916
9847
  {
9917
- className: ui.clx(
9918
- "flex items-center justify-center outline-none transition-fg",
9919
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9920
- "focus:bg-ui-bg-field-component-hover",
9921
- "hover:bg-ui-bg-field-component-hover",
9922
- {
9923
- "size-7": size === "small",
9924
- "size-8": size === "base"
9925
- }
9926
- ),
9927
- type: "button",
9928
- onClick: handleDecrement,
9929
- disabled: min !== void 0 && value <= min || disabled,
9930
- children: [
9931
- /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
9932
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9933
- ]
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
+ })
9934
9852
  }
9935
9853
  ),
9936
- /* @__PURE__ */ jsxRuntime.jsxs(
9937
- "button",
9938
- {
9939
- className: ui.clx(
9940
- "flex items-center justify-center outline-none transition-fg",
9941
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9942
- "focus:bg-ui-bg-field-hover",
9943
- "hover:bg-ui-bg-field-hover",
9944
- {
9945
- "size-7": size === "small",
9946
- "size-8": size === "base"
9947
- }
9948
- ),
9949
- type: "button",
9950
- onClick: handleIncrement,
9951
- disabled: max !== void 0 && value >= max || disabled,
9952
- children: [
9953
- /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
9954
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9955
- ]
9956
- }
9957
- )
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
+ ] })
9958
9862
  ]
9959
9863
  }
9960
9864
  );
9961
9865
  }
9962
9866
  );
9963
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9964
- const productVariantsQueryKeys = {
9965
- list: (query2) => [
9966
- PRODUCT_VARIANTS_QUERY_KEY,
9967
- query2 ? query2 : void 0
9968
- ]
9969
- };
9970
- const useProductVariants = (query2, options) => {
9971
- const { data, ...rest } = reactQuery.useQuery({
9972
- queryKey: productVariantsQueryKeys.list(query2),
9973
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9974
- ...options
9975
- });
9976
- return { ...data, ...rest };
9977
- };
9978
- const useCancelOrderEdit = ({ preview }) => {
9979
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9980
- const onCancel = React.useCallback(async () => {
9981
- if (!preview) {
9982
- return true;
9983
- }
9984
- let res = false;
9985
- await cancelOrderEdit(void 0, {
9986
- onError: (e) => {
9987
- ui.toast.error(e.message);
9988
- },
9989
- onSuccess: () => {
9990
- res = true;
9991
- }
9992
- });
9993
- return res;
9994
- }, [preview, cancelOrderEdit]);
9995
- return { onCancel };
9996
- };
9997
- let IS_REQUEST_RUNNING = false;
9998
- const useInitiateOrderEdit = ({
9999
- preview
10000
- }) => {
10001
- const navigate = reactRouterDom.useNavigate();
10002
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10003
- React.useEffect(() => {
10004
- async function run() {
10005
- if (IS_REQUEST_RUNNING || !preview) {
10006
- return;
10007
- }
10008
- if (preview.order_change) {
10009
- return;
10010
- }
10011
- IS_REQUEST_RUNNING = true;
10012
- await mutateAsync(void 0, {
10013
- onError: (e) => {
10014
- ui.toast.error(e.message);
10015
- navigate(`/draft-orders/${preview.id}`, { replace: true });
10016
- return;
10017
- }
10018
- });
10019
- IS_REQUEST_RUNNING = false;
10020
- }
10021
- run();
10022
- }, [preview, navigate, mutateAsync]);
10023
- };
10024
- function convertNumber(value) {
10025
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10026
- }
10027
- const STACKED_MODAL_ID = "items_stacked_modal";
10028
- 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 = () => {
10029
9877
  const { id } = reactRouterDom.useParams();
10030
- const {
10031
- order: preview,
10032
- isPending: isPreviewPending,
10033
- isError: isPreviewError,
10034
- error: previewError
10035
- } = useOrderPreview(id, void 0, {
10036
- placeholderData: reactQuery.keepPreviousData
9878
+ const { order, isPending, isError, error } = useOrder(id, {
9879
+ fields: "metadata"
10037
9880
  });
10038
- useInitiateOrderEdit({ preview });
10039
- const { draft_order, isPending, isError, error } = useDraftOrder(
10040
- id,
10041
- {
10042
- fields: "currency_code"
10043
- },
10044
- {
10045
- enabled: !!id
10046
- }
10047
- );
10048
- const { onCancel } = useCancelOrderEdit({ preview });
10049
9881
  if (isError) {
10050
9882
  throw error;
10051
9883
  }
10052
- if (isPreviewError) {
10053
- throw previewError;
10054
- }
10055
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10056
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10057
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10058
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10059
- ] }) });
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
+ ] });
10060
9892
  };
10061
- const ItemsForm = ({ preview, currencyCode }) => {
10062
- var _a;
10063
- const [isSubmitting, setIsSubmitting] = React.useState(false);
10064
- const [modalContent, setModalContent] = React.useState(
10065
- null
10066
- );
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 }) => {
10067
9896
  const { handleSuccess } = useRouteModal();
10068
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10069
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10070
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10071
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10072
- const matches = React.useMemo(() => {
10073
- return matchSorter.matchSorter(preview.items, query2, {
10074
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10075
- });
10076
- }, [preview.items, query2]);
10077
- const onSubmit = async () => {
10078
- setIsSubmitting(true);
10079
- let requestSucceeded = false;
10080
- await requestOrderEdit(void 0, {
10081
- onError: (e) => {
10082
- ui.toast.error(`Failed to request order edit: ${e.message}`);
10083
- },
10084
- onSuccess: () => {
10085
- requestSucceeded = true;
10086
- }
10087
- });
10088
- if (!requestSucceeded) {
10089
- setIsSubmitting(false);
10090
- return;
10091
- }
10092
- await confirmOrderEdit(void 0, {
10093
- onError: (e) => {
10094
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10095
- },
10096
- onSuccess: () => {
10097
- handleSuccess();
10098
- },
10099
- onSettled: () => {
10100
- setIsSubmitting(false);
10101
- }
10102
- });
10103
- };
10104
- const onKeyDown = React.useCallback(
10105
- (e) => {
10106
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10107
- if (modalContent || isSubmitting) {
10108
- return;
10109
- }
10110
- onSubmit();
10111
- }
10112
- },
10113
- [modalContent, isSubmitting, onSubmit]
10114
- );
10115
- React.useEffect(() => {
10116
- document.addEventListener("keydown", onKeyDown);
10117
- return () => {
10118
- document.removeEventListener("keydown", onKeyDown);
10119
- };
10120
- }, [onKeyDown]);
10121
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10122
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10123
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10124
- StackedFocusModal,
10125
- {
10126
- id: STACKED_MODAL_ID,
10127
- onOpenChangeCallback: (open) => {
10128
- if (!open) {
10129
- setModalContent(null);
10130
- }
10131
- },
10132
- children: [
10133
- /* @__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: [
10134
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10135
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10136
- /* @__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" }) })
10137
- ] }),
10138
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10139
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10140
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10141
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10142
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10143
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10144
- ] }),
10145
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10146
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10147
- ui.Input,
10148
- {
10149
- type: "search",
10150
- placeholder: "Search items",
10151
- value: searchValue,
10152
- onChange: (e) => onSearchValueChange(e.target.value)
10153
- }
10154
- ) }),
10155
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10156
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10157
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10158
- /* @__PURE__ */ jsxRuntime.jsx(
10159
- StackedModalTrigger$1,
10160
- {
10161
- type: "add-items",
10162
- setModalContent
10163
- }
10164
- ),
10165
- /* @__PURE__ */ jsxRuntime.jsx(
10166
- StackedModalTrigger$1,
10167
- {
10168
- type: "add-custom-item",
10169
- setModalContent
10170
- }
10171
- )
10172
- ] })
10173
- ] })
10174
- ] })
10175
- ] }),
10176
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10177
- /* @__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: [
10178
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10179
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10180
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10181
- /* @__PURE__ */ jsxRuntime.jsx("div", {})
10182
- ] }) }),
10183
- /* @__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: [
10184
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10185
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10186
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10187
- Item,
10188
- {
10189
- item,
10190
- preview,
10191
- currencyCode
10192
- },
10193
- item.id
10194
- )) : /* @__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: [
10195
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10196
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10197
- 'No items found for "',
10198
- query2,
10199
- '".'
10200
- ] })
10201
- ] }) })
10202
- ] })
10203
- ] }),
10204
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10205
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10206
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10207
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10208
- ui.Text,
10209
- {
10210
- size: "small",
10211
- leading: "compact",
10212
- className: "text-ui-fg-subtle",
10213
- children: [
10214
- itemCount,
10215
- " ",
10216
- itemCount === 1 ? "item" : "items"
10217
- ]
10218
- }
10219
- ) }),
10220
- /* @__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) }) })
10221
- ] })
10222
- ] }) }),
10223
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10224
- CustomItemForm,
10225
- {
10226
- orderId: preview.id,
10227
- currencyCode
10228
- }
10229
- ) : null)
10230
- ]
10231
- }
10232
- ) }),
10233
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10234
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10235
- /* @__PURE__ */ jsxRuntime.jsx(
10236
- ui.Button,
10237
- {
10238
- size: "small",
10239
- type: "button",
10240
- onClick: onSubmit,
10241
- isLoading: isSubmitting,
10242
- children: "Save"
10243
- }
10244
- )
10245
- ] }) })
10246
- ] });
10247
- };
10248
- const Item = ({ item, preview, currencyCode }) => {
10249
- if (item.variant_id) {
10250
- return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10251
- }
10252
- return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10253
- };
10254
- const VariantItem = ({ item, preview, currencyCode }) => {
10255
- const [editing, setEditing] = React.useState(false);
9897
+ const hasUneditableRows = getHasUneditableRows(metadata);
9898
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10256
9899
  const form = reactHookForm.useForm({
10257
9900
  defaultValues: {
10258
- quantity: item.quantity,
10259
- unit_price: item.unit_price
9901
+ metadata: getDefaultValues(metadata)
10260
9902
  },
10261
- resolver: zod.zodResolver(variantItemSchema)
9903
+ resolver: zod.zodResolver(MetadataSchema)
10262
9904
  });
10263
- const actionId = React.useMemo(() => {
10264
- var _a, _b;
10265
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10266
- }, [item]);
10267
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10268
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10269
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10270
- const onSubmit = form.handleSubmit(async (data) => {
10271
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10272
- setEditing(false);
10273
- return;
10274
- }
10275
- if (!actionId) {
10276
- await updateOriginalItem(
10277
- {
10278
- item_id: item.id,
10279
- quantity: data.quantity,
10280
- unit_price: convertNumber(data.unit_price)
10281
- },
10282
- {
10283
- onSuccess: () => {
10284
- setEditing(false);
10285
- },
10286
- onError: (e) => {
10287
- ui.toast.error(e.message);
10288
- }
10289
- }
10290
- );
10291
- return;
10292
- }
10293
- await updateActionItem(
9905
+ const handleSubmit = form.handleSubmit(async (data) => {
9906
+ const parsedData = parseValues(data);
9907
+ await mutateAsync(
10294
9908
  {
10295
- action_id: actionId,
10296
- quantity: data.quantity,
10297
- unit_price: convertNumber(data.unit_price)
9909
+ metadata: parsedData
10298
9910
  },
10299
9911
  {
10300
9912
  onSuccess: () => {
10301
- setEditing(false);
9913
+ ui.toast.success("Metadata updated");
9914
+ handleSuccess();
10302
9915
  },
10303
- onError: (e) => {
10304
- ui.toast.error(e.message);
9916
+ onError: (error) => {
9917
+ ui.toast.error(error.message);
10305
9918
  }
10306
9919
  }
10307
9920
  );
10308
9921
  });
10309
- 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: [
10310
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10311
- /* @__PURE__ */ jsxRuntime.jsx(
10312
- Thumbnail,
10313
- {
10314
- thumbnail: item.thumbnail,
10315
- alt: item.product_title ?? void 0
10316
- }
10317
- ),
10318
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10319
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10320
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10321
- /* @__PURE__ */ jsxRuntime.jsxs(
10322
- ui.Text,
10323
- {
10324
- size: "small",
10325
- leading: "compact",
10326
- className: "text-ui-fg-subtle",
10327
- children: [
10328
- "(",
10329
- item.variant_title,
10330
- ")"
10331
- ]
10332
- }
10333
- )
10334
- ] }),
10335
- /* @__PURE__ */ jsxRuntime.jsx(
10336
- ui.Text,
10337
- {
10338
- size: "small",
10339
- leading: "compact",
10340
- className: "text-ui-fg-subtle",
10341
- children: item.variant_sku
10342
- }
10343
- )
10344
- ] })
10345
- ] }),
10346
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10347
- Form$2.Field,
10348
- {
10349
- control: form.control,
10350
- name: "quantity",
10351
- render: ({ field }) => {
10352
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10353
- }
10354
- }
10355
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10356
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10357
- Form$2.Field,
10358
- {
10359
- control: form.control,
10360
- name: "unit_price",
10361
- render: ({ field: { onChange, ...field } }) => {
10362
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10363
- ui.CurrencyInput,
10364
- {
10365
- ...field,
10366
- symbol: getNativeSymbol(currencyCode),
10367
- code: currencyCode,
10368
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10369
- }
10370
- ) }) });
10371
- }
10372
- }
10373
- ) }) : /* @__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) }) }),
10374
- /* @__PURE__ */ jsxRuntime.jsx(
10375
- ui.IconButton,
10376
- {
10377
- type: "button",
10378
- size: "small",
10379
- onClick: editing ? onSubmit : () => {
10380
- setEditing(true);
10381
- },
10382
- disabled: isPending,
10383
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10384
- }
10385
- )
10386
- ] }) }) });
10387
- };
10388
- const variantItemSchema = objectType({
10389
- quantity: numberType(),
10390
- unit_price: unionType([numberType(), stringType()])
10391
- });
10392
- const CustomItem = ({ item, preview, currencyCode }) => {
10393
- const [editing, setEditing] = React.useState(false);
10394
- const { quantity, unit_price, title } = item;
10395
- const form = reactHookForm.useForm({
10396
- defaultValues: {
10397
- title,
10398
- quantity,
10399
- unit_price
10400
- },
10401
- resolver: zod.zodResolver(customItemSchema)
9922
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
9923
+ control: form.control,
9924
+ name: "metadata"
10402
9925
  });
10403
- React.useEffect(() => {
10404
- form.reset({
10405
- title,
10406
- quantity,
10407
- unit_price
10408
- });
10409
- }, [form, title, quantity, unit_price]);
10410
- const actionId = React.useMemo(() => {
10411
- var _a, _b;
10412
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10413
- }, [item]);
10414
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10415
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10416
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10417
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10418
- const onSubmit = form.handleSubmit(async (data) => {
10419
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10420
- setEditing(false);
10421
- return;
10422
- }
10423
- if (!actionId) {
10424
- await updateOriginalItem(
10425
- {
10426
- item_id: item.id,
10427
- quantity: data.quantity,
10428
- unit_price: convertNumber(data.unit_price)
10429
- },
10430
- {
10431
- onSuccess: () => {
10432
- setEditing(false);
10433
- },
10434
- onError: (e) => {
10435
- ui.toast.error(e.message);
10436
- }
10437
- }
10438
- );
10439
- return;
10440
- }
10441
- if (data.quantity === 0) {
10442
- await removeActionItem(actionId, {
10443
- onSuccess: () => {
10444
- setEditing(false);
10445
- },
10446
- onError: (e) => {
10447
- ui.toast.error(e.message);
10448
- }
9926
+ function deleteRow(index) {
9927
+ remove(index);
9928
+ if (fields.length === 1) {
9929
+ insert(0, {
9930
+ key: "",
9931
+ value: "",
9932
+ disabled: false
10449
9933
  });
10450
- return;
10451
9934
  }
10452
- await updateActionItem(
10453
- {
10454
- action_id: actionId,
10455
- quantity: data.quantity,
10456
- unit_price: convertNumber(data.unit_price)
10457
- },
10458
- {
10459
- onSuccess: () => {
10460
- setEditing(false);
10461
- },
10462
- onError: (e) => {
10463
- ui.toast.error(e.message);
10464
- }
10465
- }
10466
- );
10467
- });
10468
- 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: [
10469
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10470
- /* @__PURE__ */ jsxRuntime.jsx(
10471
- Thumbnail,
10472
- {
10473
- thumbnail: item.thumbnail,
10474
- alt: item.title ?? void 0
10475
- }
10476
- ),
10477
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10478
- Form$2.Field,
10479
- {
10480
- control: form.control,
10481
- name: "title",
10482
- render: ({ field }) => {
10483
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
10484
- }
10485
- }
10486
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
10487
- ] }),
10488
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10489
- Form$2.Field,
10490
- {
10491
- control: form.control,
10492
- name: "quantity",
10493
- render: ({ field }) => {
10494
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10495
- }
10496
- }
10497
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
10498
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10499
- Form$2.Field,
10500
- {
10501
- control: form.control,
10502
- name: "unit_price",
10503
- render: ({ field: { onChange, ...field } }) => {
10504
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10505
- ui.CurrencyInput,
10506
- {
10507
- ...field,
10508
- symbol: getNativeSymbol(currencyCode),
10509
- code: currencyCode,
10510
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10511
- }
10512
- ) }) });
10513
- }
10514
- }
10515
- ) : /* @__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) }) }),
10516
- /* @__PURE__ */ jsxRuntime.jsx(
10517
- ui.IconButton,
10518
- {
10519
- type: "button",
10520
- size: "small",
10521
- onClick: editing ? onSubmit : () => {
10522
- setEditing(true);
10523
- },
10524
- disabled: isPending,
10525
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10526
- }
10527
- )
10528
- ] }) }) });
10529
- };
10530
- const StackedModalTrigger$1 = ({
10531
- type,
10532
- setModalContent
10533
- }) => {
10534
- const { setIsOpen } = useStackedModal();
10535
- const onClick = React.useCallback(() => {
10536
- setModalContent(type);
10537
- setIsOpen(STACKED_MODAL_ID, true);
10538
- }, [setModalContent, setIsOpen, type]);
10539
- 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" }) });
10540
- };
10541
- const VARIANT_PREFIX = "items";
10542
- const LIMIT = 50;
10543
- const ExistingItemsForm = ({ orderId, items }) => {
10544
- const { setIsOpen } = useStackedModal();
10545
- const [rowSelection, setRowSelection] = React.useState(
10546
- items.reduce((acc, item) => {
10547
- acc[item.variant_id] = true;
10548
- return acc;
10549
- }, {})
10550
- );
10551
- React.useEffect(() => {
10552
- setRowSelection(
10553
- items.reduce((acc, item) => {
10554
- if (item.variant_id) {
10555
- acc[item.variant_id] = true;
10556
- }
10557
- return acc;
10558
- }, {})
10559
- );
10560
- }, [items]);
10561
- const { q, order, offset } = useQueryParams(
10562
- ["q", "order", "offset"],
10563
- VARIANT_PREFIX
10564
- );
10565
- const { variants, count, isPending, isError, error } = useProductVariants(
10566
- {
10567
- q,
10568
- order,
10569
- offset: offset ? parseInt(offset) : void 0,
10570
- limit: LIMIT
10571
- },
10572
- {
10573
- placeholderData: reactQuery.keepPreviousData
10574
- }
10575
- );
10576
- const columns = useColumns();
10577
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10578
- const onSubmit = async () => {
10579
- const ids = Object.keys(rowSelection).filter(
10580
- (id) => !items.find((i) => i.variant_id === id)
10581
- );
10582
- await mutateAsync(
10583
- {
10584
- items: ids.map((id) => ({
10585
- variant_id: id,
10586
- quantity: 1
10587
- }))
10588
- },
10589
- {
10590
- onSuccess: () => {
10591
- setRowSelection({});
10592
- setIsOpen(STACKED_MODAL_ID, false);
10593
- },
10594
- onError: (e) => {
10595
- ui.toast.error(e.message);
10596
- }
10597
- }
10598
- );
10599
- };
10600
- if (isError) {
10601
- throw error;
10602
9935
  }
10603
- return /* @__PURE__ */ jsxRuntime.jsxs(
10604
- StackedFocusModal.Content,
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,
10605
9945
  {
10606
- onOpenAutoFocus: (e) => {
10607
- e.preventDefault();
10608
- const searchInput = document.querySelector(
10609
- "[data-modal-id='modal-search-input']"
10610
- );
10611
- if (searchInput) {
10612
- searchInput.focus();
10613
- }
10614
- },
9946
+ onSubmit: handleSubmit,
9947
+ className: "flex flex-1 flex-col overflow-hidden",
10615
9948
  children: [
10616
- /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
10617
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10618
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10619
- ] }),
10620
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
10621
- DataTable,
10622
- {
10623
- data: variants,
10624
- columns,
10625
- isLoading: isPending,
10626
- getRowId: (row) => row.id,
10627
- rowCount: count,
10628
- prefix: VARIANT_PREFIX,
10629
- layout: "fill",
10630
- rowSelection: {
10631
- state: rowSelection,
10632
- onRowSelectionChange: setRowSelection,
10633
- enableRowSelection: (row) => {
10634
- 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 = "{ ... }";
10635
9960
  }
10636
- },
10637
- autoFocusSearch: true
10638
- }
10639
- ) }),
10640
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10641
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10642
- /* @__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" })
10643
10080
  ] }) })
10644
10081
  ]
10645
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
+ }
10646
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
+ ] });
10647
10108
  };
10648
- const columnHelper = ui.createDataTableColumnHelper();
10649
- const useColumns = () => {
10650
- return React.useMemo(() => {
10109
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
10110
+ function getDefaultValues(metadata) {
10111
+ if (!metadata || !Object.keys(metadata).length) {
10651
10112
  return [
10652
- columnHelper.select(),
10653
- columnHelper.accessor("product.title", {
10654
- header: "Product",
10655
- cell: ({ row }) => {
10656
- var _a, _b, _c;
10657
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
10658
- /* @__PURE__ */ jsxRuntime.jsx(
10659
- Thumbnail,
10660
- {
10661
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10662
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10663
- }
10664
- ),
10665
- /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10666
- ] });
10667
- },
10668
- enableSorting: true
10669
- }),
10670
- columnHelper.accessor("title", {
10671
- header: "Variant",
10672
- enableSorting: true
10673
- }),
10674
- columnHelper.accessor("sku", {
10675
- header: "SKU",
10676
- cell: ({ getValue }) => {
10677
- return getValue() ?? "-";
10678
- },
10679
- enableSorting: true
10680
- }),
10681
- columnHelper.accessor("updated_at", {
10682
- header: "Updated",
10683
- cell: ({ getValue }) => {
10684
- return /* @__PURE__ */ jsxRuntime.jsx(
10685
- ui.Tooltip,
10686
- {
10687
- content: getFullDate({ date: getValue(), includeTime: true }),
10688
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10689
- }
10690
- );
10691
- },
10692
- enableSorting: true,
10693
- sortAscLabel: "Oldest first",
10694
- sortDescLabel: "Newest first"
10695
- }),
10696
- columnHelper.accessor("created_at", {
10697
- header: "Created",
10698
- cell: ({ getValue }) => {
10699
- return /* @__PURE__ */ jsxRuntime.jsx(
10700
- ui.Tooltip,
10701
- {
10702
- content: getFullDate({ date: getValue(), includeTime: true }),
10703
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10704
- }
10705
- );
10706
- },
10707
- enableSorting: true,
10708
- sortAscLabel: "Oldest first",
10709
- sortDescLabel: "Newest first"
10710
- })
10113
+ {
10114
+ key: "",
10115
+ value: "",
10116
+ disabled: false
10117
+ }
10711
10118
  ];
10712
- }, []);
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
+ ]
10713
10193
  };
10714
- const CustomItemForm = ({ orderId, currencyCode }) => {
10715
- const { setIsOpen } = useStackedModal();
10716
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10717
- const form = reactHookForm.useForm({
10718
- defaultValues: {
10719
- title: "",
10720
- quantity: 1,
10721
- 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
10722
10276
  },
10723
- 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
+ }
10724
10297
  });
10725
- const onSubmit = form.handleSubmit(async (data) => {
10726
- await addItems(
10298
+ const add = async (value) => {
10299
+ if (!value) {
10300
+ return;
10301
+ }
10302
+ addPromotions(
10727
10303
  {
10728
- items: [
10729
- {
10730
- title: data.title,
10731
- quantity: data.quantity,
10732
- unit_price: convertNumber(data.unit_price)
10733
- }
10734
- ]
10304
+ promo_codes: [value]
10735
10305
  },
10736
10306
  {
10737
- onSuccess: () => {
10738
- setIsOpen(STACKED_MODAL_ID, false);
10739
- },
10740
10307
  onError: (e) => {
10741
10308
  ui.toast.error(e.message);
10309
+ comboboxData.onSearchValueChange("");
10310
+ setComboboxValue("");
10311
+ },
10312
+ onSuccess: () => {
10313
+ comboboxData.onSearchValueChange("");
10314
+ setComboboxValue("");
10742
10315
  }
10743
10316
  }
10744
10317
  );
10745
- });
10746
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
10747
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
10748
- /* @__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: [
10749
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10750
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
10751
- /* @__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." }) })
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
+ )
10752
10373
  ] }),
10753
10374
  /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10754
- /* @__PURE__ */ jsxRuntime.jsx(
10755
- Form$2.Field,
10756
- {
10757
- control: form.control,
10758
- name: "title",
10759
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10760
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10761
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
10762
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
10763
- ] }),
10764
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10765
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10766
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10767
- ] })
10768
- ] }) })
10769
- }
10770
- ),
10771
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10772
- /* @__PURE__ */ jsxRuntime.jsx(
10773
- 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,
10774
10377
  {
10775
- control: form.control,
10776
- name: "unit_price",
10777
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10778
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10779
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
10780
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10781
- ] }),
10782
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10783
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10784
- ui.CurrencyInput,
10785
- {
10786
- symbol: getNativeSymbol(currencyCode),
10787
- code: currencyCode,
10788
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10789
- ...field
10790
- }
10791
- ) }),
10792
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10793
- ] })
10794
- ] }) })
10795
- }
10796
- ),
10797
- /* @__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" }) }),
10798
10387
  /* @__PURE__ */ jsxRuntime.jsx(
10799
- Form$2.Field,
10388
+ ui.Button,
10800
10389
  {
10801
- control: form.control,
10802
- name: "quantity",
10803
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10804
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10805
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
10806
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10807
- ] }),
10808
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
10809
- /* @__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" }) }) }),
10810
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10811
- ] })
10812
- ] }) })
10390
+ size: "small",
10391
+ type: "submit",
10392
+ isLoading: isSubmitting || isAddingPromotions,
10393
+ children: "Save"
10813
10394
  }
10814
10395
  )
10815
- ] }) }) }),
10816
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10817
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10818
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10819
10396
  ] }) })
10820
- ] }) }) });
10821
- };
10822
- const customItemSchema = objectType({
10823
- title: stringType().min(1),
10824
- quantity: numberType(),
10825
- unit_price: unionType([numberType(), stringType()])
10826
- });
10827
- const InlineTip = React.forwardRef(
10828
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10829
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10830
- return /* @__PURE__ */ jsxRuntime.jsxs(
10831
- "div",
10832
- {
10833
- ref,
10834
- className: ui.clx(
10835
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10836
- className
10837
- ),
10838
- ...props,
10839
- children: [
10840
- /* @__PURE__ */ jsxRuntime.jsx(
10841
- "div",
10842
- {
10843
- role: "presentation",
10844
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10845
- "bg-ui-tag-orange-icon": variant === "warning"
10846
- })
10847
- }
10848
- ),
10849
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10850
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10851
- labelValue,
10852
- ":"
10853
- ] }),
10854
- " ",
10855
- children
10856
- ] })
10857
- ]
10858
- }
10859
- );
10860
- }
10861
- );
10862
- InlineTip.displayName = "InlineTip";
10863
- const MetadataFieldSchema = objectType({
10864
- key: stringType(),
10865
- disabled: booleanType().optional(),
10866
- value: anyType()
10867
- });
10868
- const MetadataSchema = objectType({
10869
- metadata: arrayType(MetadataFieldSchema)
10870
- });
10871
- const Metadata = () => {
10872
- const { id } = reactRouterDom.useParams();
10873
- const { order, isPending, isError, error } = useOrder(id, {
10874
- fields: "metadata"
10875
- });
10876
- if (isError) {
10877
- throw error;
10878
- }
10879
- const isReady = !isPending && !!order;
10880
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10881
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10882
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10883
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10884
- ] }),
10885
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10886
10397
  ] });
10887
10398
  };
10888
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10889
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10890
- const MetadataForm = ({ orderId, metadata }) => {
10891
- const { handleSuccess } = useRouteModal();
10892
- const hasUneditableRows = getHasUneditableRows(metadata);
10893
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10894
- const form = reactHookForm.useForm({
10895
- defaultValues: {
10896
- metadata: getDefaultValues(metadata)
10897
- },
10898
- resolver: zod.zodResolver(MetadataSchema)
10899
- });
10900
- const handleSubmit = form.handleSubmit(async (data) => {
10901
- const parsedData = parseValues(data);
10902
- 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(
10903
10408
  {
10904
- metadata: parsedData
10409
+ promo_codes: [promotion.code]
10905
10410
  },
10906
10411
  {
10907
- onSuccess: () => {
10908
- ui.toast.success("Metadata updated");
10909
- handleSuccess();
10910
- },
10911
- onError: (error) => {
10912
- ui.toast.error(error.message);
10412
+ onError: (e) => {
10413
+ ui.toast.error(e.message);
10913
10414
  }
10914
10415
  }
10915
10416
  );
10916
- });
10917
- const { fields, insert, remove } = reactHookForm.useFieldArray({
10918
- control: form.control,
10919
- name: "metadata"
10920
- });
10921
- function deleteRow(index) {
10922
- remove(index);
10923
- if (fields.length === 1) {
10924
- insert(0, {
10925
- key: "",
10926
- value: "",
10927
- disabled: false
10928
- });
10929
- }
10930
- }
10931
- function insertRow(index, position) {
10932
- insert(index + (position === "above" ? 0 : 1), {
10933
- key: "",
10934
- value: "",
10935
- disabled: false
10936
- });
10937
- }
10938
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10939
- KeyboundForm,
10417
+ };
10418
+ const displayValue = getDisplayValue(promotion);
10419
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10420
+ "div",
10940
10421
  {
10941
- onSubmit: handleSubmit,
10942
- 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
+ ),
10943
10428
  children: [
10944
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10945
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10946
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10947
- /* @__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" }) }),
10948
- /* @__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: "·" })
10949
10435
  ] }),
10950
- fields.map((field, index) => {
10951
- const isDisabled = field.disabled || false;
10952
- let placeholder = "-";
10953
- if (typeof field.value === "object") {
10954
- placeholder = "{ ... }";
10955
- }
10956
- if (Array.isArray(field.value)) {
10957
- placeholder = "[ ... ]";
10958
- }
10959
- return /* @__PURE__ */ jsxRuntime.jsx(
10960
- ConditionalTooltip,
10961
- {
10962
- showTooltip: isDisabled,
10963
- content: "This row is disabled because it contains non-primitive data.",
10964
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
10965
- /* @__PURE__ */ jsxRuntime.jsxs(
10966
- "div",
10967
- {
10968
- className: ui.clx("grid grid-cols-2 divide-x", {
10969
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10970
- }),
10971
- children: [
10972
- /* @__PURE__ */ jsxRuntime.jsx(
10973
- Form$2.Field,
10974
- {
10975
- control: form.control,
10976
- name: `metadata.${index}.key`,
10977
- render: ({ field: field2 }) => {
10978
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10979
- GridInput,
10980
- {
10981
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10982
- ...field2,
10983
- disabled: isDisabled,
10984
- placeholder: "Key"
10985
- }
10986
- ) }) });
10987
- }
10988
- }
10989
- ),
10990
- /* @__PURE__ */ jsxRuntime.jsx(
10991
- Form$2.Field,
10992
- {
10993
- control: form.control,
10994
- name: `metadata.${index}.value`,
10995
- render: ({ field: { value, ...field2 } }) => {
10996
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10997
- GridInput,
10998
- {
10999
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
11000
- ...field2,
11001
- value: isDisabled ? placeholder : value,
11002
- disabled: isDisabled,
11003
- placeholder: "Value"
11004
- }
11005
- ) }) });
11006
- }
11007
- }
11008
- )
11009
- ]
11010
- }
11011
- ),
11012
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
11013
- /* @__PURE__ */ jsxRuntime.jsx(
11014
- ui.DropdownMenu.Trigger,
11015
- {
11016
- className: ui.clx(
11017
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11018
- {
11019
- hidden: isDisabled
11020
- }
11021
- ),
11022
- disabled: isDisabled,
11023
- asChild: true,
11024
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
11025
- }
11026
- ),
11027
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11028
- /* @__PURE__ */ jsxRuntime.jsxs(
11029
- ui.DropdownMenu.Item,
11030
- {
11031
- className: "gap-x-2",
11032
- onClick: () => insertRow(index, "above"),
11033
- children: [
11034
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11035
- "Insert row above"
11036
- ]
11037
- }
11038
- ),
11039
- /* @__PURE__ */ jsxRuntime.jsxs(
11040
- ui.DropdownMenu.Item,
11041
- {
11042
- className: "gap-x-2",
11043
- onClick: () => insertRow(index, "below"),
11044
- children: [
11045
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11046
- "Insert row below"
11047
- ]
11048
- }
11049
- ),
11050
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11051
- /* @__PURE__ */ jsxRuntime.jsxs(
11052
- ui.DropdownMenu.Item,
11053
- {
11054
- className: "gap-x-2",
11055
- onClick: () => deleteRow(index),
11056
- children: [
11057
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11058
- "Delete row"
11059
- ]
11060
- }
11061
- )
11062
- ] })
11063
- ] })
11064
- ] })
11065
- },
11066
- field.id
11067
- );
11068
- })
11069
- ] }),
11070
- 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
+ ] })
11071
10438
  ] }),
11072
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11073
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11074
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11075
- ] }) })
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
+ )
11076
10450
  ]
11077
- }
11078
- ) });
10451
+ },
10452
+ promotion.id
10453
+ );
11079
10454
  };
11080
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11081
- return /* @__PURE__ */ jsxRuntime.jsx(
11082
- "input",
11083
- {
11084
- ref,
11085
- ...props,
11086
- autoComplete: "off",
11087
- className: ui.clx(
11088
- "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",
11089
- className
11090
- )
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;
10460
+ }
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;
11091
10465
  }
11092
- );
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;
10471
+ }
10472
+ const formatter = new Intl.NumberFormat([], {
10473
+ style: "percent",
10474
+ minimumFractionDigits: 2
11093
10475
  });
11094
- GridInput.displayName = "MetadataForm.GridInput";
11095
- const PlaceholderInner = () => {
11096
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11097
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11098
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11099
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11100
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11101
- ] }) })
11102
- ] });
10476
+ const formatPercentage = (value, isPercentageValue = false) => {
10477
+ let val = value || 0;
10478
+ if (!isPercentageValue) {
10479
+ val = val / 100;
10480
+ }
10481
+ return formatter.format(val);
11103
10482
  };
11104
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11105
- function getDefaultValues(metadata) {
11106
- if (!metadata || !Object.keys(metadata).length) {
11107
- return [
11108
- {
11109
- key: "",
11110
- value: "",
11111
- disabled: false
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
+ }
11112
10491
  }
11113
- ];
11114
- }
11115
- return Object.entries(metadata).map(([key, value]) => {
11116
- if (!EDITABLE_TYPES.includes(typeof value)) {
11117
- return {
11118
- key,
11119
- value,
11120
- disabled: true
11121
- };
11122
10492
  }
11123
- let stringValue = value;
11124
- if (typeof value !== "string") {
11125
- stringValue = JSON.stringify(value);
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
+ }
11126
10501
  }
11127
- return {
11128
- key,
11129
- value: stringValue,
11130
- original_key: key
11131
- };
11132
- });
11133
- }
11134
- function parseValues(values) {
11135
- const metadata = values.metadata;
11136
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11137
- if (isEmpty) {
11138
- return null;
11139
10502
  }
11140
- const update = {};
11141
- metadata.forEach((field) => {
11142
- let key = field.key;
11143
- let value = field.value;
11144
- const disabled = field.disabled;
11145
- if (!key || !value) {
11146
- return;
11147
- }
11148
- if (disabled) {
11149
- update[key] = value;
11150
- return;
10503
+ return Array.from(promotionIds);
10504
+ }
10505
+ const SalesChannel = () => {
10506
+ const { id } = reactRouterDom.useParams();
10507
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10508
+ id,
10509
+ {
10510
+ fields: "+sales_channel_id"
10511
+ },
10512
+ {
10513
+ enabled: !!id
11151
10514
  }
11152
- key = key.trim();
11153
- value = value.trim();
11154
- if (value === "true") {
11155
- update[key] = true;
11156
- } else if (value === "false") {
11157
- update[key] = false;
11158
- } else {
11159
- const parsedNumber = parseFloat(value);
11160
- if (!isNaN(parsedNumber)) {
11161
- update[key] = parsedNumber;
11162
- } else {
11163
- update[key] = value;
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
+ ] });
10527
+ };
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)
10534
+ });
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
+ }
11164
10550
  }
11165
- }
10551
+ );
11166
10552
  });
11167
- return update;
11168
- }
11169
- function getHasUneditableRows(metadata) {
11170
- if (!metadata) {
11171
- return false;
11172
- }
11173
- return Object.values(metadata).some(
11174
- (value) => !EDITABLE_TYPES.includes(typeof value)
11175
- );
11176
- }
11177
- const PROMOTION_QUERY_KEY = "promotions";
11178
- const promotionsQueryKeys = {
11179
- list: (query2) => [
11180
- PROMOTION_QUERY_KEY,
11181
- query2 ? query2 : void 0
11182
- ],
11183
- detail: (id, query2) => [
11184
- PROMOTION_QUERY_KEY,
11185
- id,
11186
- query2 ? query2 : void 0
11187
- ]
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
+ ) });
11188
10567
  };
11189
- const usePromotions = (query2, options) => {
11190
- const { data, ...rest } = reactQuery.useQuery({
11191
- queryKey: promotionsQueryKeys.list(query2),
11192
- queryFn: async () => sdk.admin.promotion.list(query2),
11193
- ...options
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
11194
10581
  });
11195
- return { ...data, ...rest };
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
+ );
11196
10607
  };
11197
- const Promotions = () => {
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;
11198
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
+ });
11199
10621
  const {
11200
10622
  order: preview,
10623
+ isPending: isPreviewPending,
11201
10624
  isError: isPreviewError,
11202
10625
  error: previewError
11203
- } = useOrderPreview(id, void 0);
10626
+ } = useOrderPreview(id);
11204
10627
  useInitiateOrderEdit({ preview });
11205
10628
  const { onCancel } = useCancelOrderEdit({ preview });
10629
+ if (isError) {
10630
+ throw error;
10631
+ }
11206
10632
  if (isPreviewError) {
11207
10633
  throw previewError;
11208
10634
  }
11209
- const isReady = !!preview;
11210
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11211
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11212
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11213
- ] });
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
+ ] }) });
11214
10648
  };
11215
- const PromotionForm = ({ preview }) => {
11216
- const { items, shipping_methods } = preview;
10649
+ const ShippingForm = ({ preview, order }) => {
10650
+ var _a;
10651
+ const { setIsOpen } = useStackedModal();
11217
10652
  const [isSubmitting, setIsSubmitting] = React.useState(false);
11218
- const [comboboxValue, setComboboxValue] = React.useState("");
11219
- const { handleSuccess } = useRouteModal();
11220
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11221
- const promoIds = getPromotionIds(items, shipping_methods);
11222
- 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(
11223
10656
  {
11224
- id: promoIds
10657
+ id: appliedShippingOptionIds,
10658
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11225
10659
  },
11226
10660
  {
11227
- enabled: !!promoIds.length
10661
+ enabled: appliedShippingOptionIds.length > 0
11228
10662
  }
11229
10663
  );
11230
- const comboboxData = useComboboxData({
11231
- queryKey: ["promotions", "combobox", promoIds],
11232
- queryFn: async (params) => {
11233
- return await sdk.admin.promotion.list({
11234
- ...params,
11235
- id: {
11236
- $nin: promoIds
11237
- }
11238
- });
11239
- },
11240
- getOptions: (data) => {
11241
- return data.promotions.map((promotion) => ({
11242
- label: promotion.code,
11243
- value: promotion.code
11244
- }));
11245
- }
11246
- });
11247
- const add = async (value) => {
11248
- if (!value) {
11249
- return;
11250
- }
11251
- addPromotions(
11252
- {
11253
- promo_codes: [value]
11254
- },
11255
- {
11256
- onError: (e) => {
11257
- ui.toast.error(e.message);
11258
- comboboxData.onSearchValueChange("");
11259
- setComboboxValue("");
11260
- },
11261
- onSuccess: () => {
11262
- comboboxData.onSearchValueChange("");
11263
- setComboboxValue("");
11264
- }
11265
- }
11266
- );
11267
- };
11268
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11269
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11270
- const onSubmit = async () => {
11271
- setIsSubmitting(true);
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();
10675
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10676
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10677
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
10678
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
10679
+ const onSubmit = async () => {
10680
+ setIsSubmitting(true);
11272
10681
  let requestSucceeded = false;
11273
10682
  await requestOrderEdit(void 0, {
11274
10683
  onError: (e) => {
11275
- ui.toast.error(e.message);
10684
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
11276
10685
  },
11277
10686
  onSuccess: () => {
11278
10687
  requestSucceeded = true;
@@ -11284,7 +10693,7 @@ const PromotionForm = ({ preview }) => {
11284
10693
  }
11285
10694
  await confirmOrderEdit(void 0, {
11286
10695
  onError: (e) => {
11287
- ui.toast.error(e.message);
10696
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
11288
10697
  },
11289
10698
  onSuccess: () => {
11290
10699
  handleSuccess();
@@ -11294,371 +10703,16 @@ const PromotionForm = ({ preview }) => {
11294
10703
  }
11295
10704
  });
11296
10705
  };
11297
- if (isError) {
11298
- throw error;
11299
- }
11300
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11301
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11302
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11303
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11304
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11305
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11306
- ] }),
11307
- /* @__PURE__ */ jsxRuntime.jsx(
11308
- Combobox,
11309
- {
11310
- id: "promotion-combobox",
11311
- "aria-describedby": "promotion-combobox-hint",
11312
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11313
- fetchNextPage: comboboxData.fetchNextPage,
11314
- options: comboboxData.options,
11315
- onSearchValueChange: comboboxData.onSearchValueChange,
11316
- searchValue: comboboxData.searchValue,
11317
- disabled: comboboxData.disabled || isAddingPromotions,
11318
- onChange: add,
11319
- value: comboboxValue
11320
- }
11321
- )
11322
- ] }),
11323
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11324
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11325
- PromotionItem,
11326
- {
11327
- promotion,
11328
- orderId: preview.id,
11329
- isLoading: isPending
11330
- },
11331
- promotion.id
11332
- )) })
11333
- ] }) }),
11334
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11335
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11336
- /* @__PURE__ */ jsxRuntime.jsx(
11337
- ui.Button,
11338
- {
11339
- size: "small",
11340
- type: "submit",
11341
- isLoading: isSubmitting || isAddingPromotions,
11342
- children: "Save"
11343
- }
11344
- )
11345
- ] }) })
11346
- ] });
11347
- };
11348
- const PromotionItem = ({
11349
- promotion,
11350
- orderId,
11351
- isLoading
11352
- }) => {
11353
- var _a;
11354
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11355
- const onRemove = async () => {
11356
- removePromotions(
11357
- {
11358
- promo_codes: [promotion.code]
11359
- },
11360
- {
11361
- onError: (e) => {
11362
- ui.toast.error(e.message);
11363
- }
11364
- }
11365
- );
11366
- };
11367
- const displayValue = getDisplayValue(promotion);
11368
- return /* @__PURE__ */ jsxRuntime.jsxs(
11369
- "div",
11370
- {
11371
- className: ui.clx(
11372
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11373
- {
11374
- "animate-pulse": isLoading
11375
- }
11376
- ),
11377
- children: [
11378
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11379
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11380
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11381
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11382
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11383
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11384
- ] }),
11385
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11386
- ] })
11387
- ] }),
11388
- /* @__PURE__ */ jsxRuntime.jsx(
11389
- ui.IconButton,
11390
- {
11391
- size: "small",
11392
- type: "button",
11393
- variant: "transparent",
11394
- onClick: onRemove,
11395
- isLoading: isPending || isLoading,
11396
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11397
- }
11398
- )
11399
- ]
11400
- },
11401
- promotion.id
11402
- );
11403
- };
11404
- function getDisplayValue(promotion) {
11405
- var _a, _b, _c, _d;
11406
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11407
- if (!value) {
11408
- return null;
11409
- }
11410
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11411
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11412
- if (!currency) {
11413
- return null;
11414
- }
11415
- return getLocaleAmount(value, currency);
11416
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11417
- return formatPercentage(value);
11418
- }
11419
- return null;
11420
- }
11421
- const formatter = new Intl.NumberFormat([], {
11422
- style: "percent",
11423
- minimumFractionDigits: 2
11424
- });
11425
- const formatPercentage = (value, isPercentageValue = false) => {
11426
- let val = value || 0;
11427
- if (!isPercentageValue) {
11428
- val = val / 100;
11429
- }
11430
- return formatter.format(val);
11431
- };
11432
- function getPromotionIds(items, shippingMethods) {
11433
- const promotionIds = /* @__PURE__ */ new Set();
11434
- for (const item of items) {
11435
- if (item.adjustments) {
11436
- for (const adjustment of item.adjustments) {
11437
- if (adjustment.promotion_id) {
11438
- promotionIds.add(adjustment.promotion_id);
11439
- }
11440
- }
11441
- }
11442
- }
11443
- for (const shippingMethod of shippingMethods) {
11444
- if (shippingMethod.adjustments) {
11445
- for (const adjustment of shippingMethod.adjustments) {
11446
- if (adjustment.promotion_id) {
11447
- promotionIds.add(adjustment.promotion_id);
10706
+ const onKeydown = React.useCallback(
10707
+ (e) => {
10708
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10709
+ if (data || isSubmitting) {
10710
+ return;
11448
10711
  }
10712
+ onSubmit();
11449
10713
  }
11450
- }
11451
- }
11452
- return Array.from(promotionIds);
11453
- }
11454
- const SalesChannel = () => {
11455
- const { id } = reactRouterDom.useParams();
11456
- const { draft_order, isPending, isError, error } = useDraftOrder(
11457
- id,
11458
- {
11459
- fields: "+sales_channel_id"
11460
10714
  },
11461
- {
11462
- enabled: !!id
11463
- }
11464
- );
11465
- if (isError) {
11466
- throw error;
11467
- }
11468
- const ISrEADY = !!draft_order && !isPending;
11469
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11470
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11471
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11472
- /* @__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" }) })
11473
- ] }),
11474
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11475
- ] });
11476
- };
11477
- const SalesChannelForm = ({ order }) => {
11478
- const form = reactHookForm.useForm({
11479
- defaultValues: {
11480
- sales_channel_id: order.sales_channel_id || ""
11481
- },
11482
- resolver: zod.zodResolver(schema$2)
11483
- });
11484
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11485
- const { handleSuccess } = useRouteModal();
11486
- const onSubmit = form.handleSubmit(async (data) => {
11487
- await mutateAsync(
11488
- {
11489
- sales_channel_id: data.sales_channel_id
11490
- },
11491
- {
11492
- onSuccess: () => {
11493
- ui.toast.success("Sales channel updated");
11494
- handleSuccess();
11495
- },
11496
- onError: (error) => {
11497
- ui.toast.error(error.message);
11498
- }
11499
- }
11500
- );
11501
- });
11502
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11503
- KeyboundForm,
11504
- {
11505
- className: "flex flex-1 flex-col overflow-hidden",
11506
- onSubmit,
11507
- children: [
11508
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11509
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11510
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11511
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11512
- ] }) })
11513
- ]
11514
- }
11515
- ) });
11516
- };
11517
- const SalesChannelField = ({ control, order }) => {
11518
- const salesChannels = useComboboxData({
11519
- queryFn: async (params) => {
11520
- return await sdk.admin.salesChannel.list(params);
11521
- },
11522
- queryKey: ["sales-channels"],
11523
- getOptions: (data) => {
11524
- return data.sales_channels.map((salesChannel) => ({
11525
- label: salesChannel.name,
11526
- value: salesChannel.id
11527
- }));
11528
- },
11529
- defaultValue: order.sales_channel_id || void 0
11530
- });
11531
- return /* @__PURE__ */ jsxRuntime.jsx(
11532
- Form$2.Field,
11533
- {
11534
- control,
11535
- name: "sales_channel_id",
11536
- render: ({ field }) => {
11537
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11538
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11539
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11540
- Combobox,
11541
- {
11542
- options: salesChannels.options,
11543
- fetchNextPage: salesChannels.fetchNextPage,
11544
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11545
- searchValue: salesChannels.searchValue,
11546
- onSearchValueChange: salesChannels.onSearchValueChange,
11547
- placeholder: "Select sales channel",
11548
- ...field
11549
- }
11550
- ) }),
11551
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11552
- ] });
11553
- }
11554
- }
11555
- );
11556
- };
11557
- const schema$2 = objectType({
11558
- sales_channel_id: stringType().min(1)
11559
- });
11560
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11561
- const Shipping = () => {
11562
- var _a;
11563
- const { id } = reactRouterDom.useParams();
11564
- const { order, isPending, isError, error } = useOrder(id, {
11565
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11566
- });
11567
- const {
11568
- order: preview,
11569
- isPending: isPreviewPending,
11570
- isError: isPreviewError,
11571
- error: previewError
11572
- } = useOrderPreview(id);
11573
- useInitiateOrderEdit({ preview });
11574
- const { onCancel } = useCancelOrderEdit({ preview });
11575
- if (isError) {
11576
- throw error;
11577
- }
11578
- if (isPreviewError) {
11579
- throw previewError;
11580
- }
11581
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11582
- const isReady = preview && !isPreviewPending && order && !isPending;
11583
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11584
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11585
- /* @__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: [
11586
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11587
- /* @__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." }) })
11588
- ] }) }) }),
11589
- /* @__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" }) }) })
11590
- ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11591
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11592
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11593
- ] }) });
11594
- };
11595
- const ShippingForm = ({ preview, order }) => {
11596
- var _a;
11597
- const { setIsOpen } = useStackedModal();
11598
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11599
- const [data, setData] = React.useState(null);
11600
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11601
- const { shipping_options } = useShippingOptions(
11602
- {
11603
- id: appliedShippingOptionIds,
11604
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11605
- },
11606
- {
11607
- enabled: appliedShippingOptionIds.length > 0
11608
- }
11609
- );
11610
- const uniqueShippingProfiles = React.useMemo(() => {
11611
- const profiles = /* @__PURE__ */ new Map();
11612
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11613
- profiles.set(profile.id, profile);
11614
- });
11615
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11616
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11617
- });
11618
- return Array.from(profiles.values());
11619
- }, [order.items, shipping_options]);
11620
- const { handleSuccess } = useRouteModal();
11621
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11622
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11623
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11624
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11625
- const onSubmit = async () => {
11626
- setIsSubmitting(true);
11627
- let requestSucceeded = false;
11628
- await requestOrderEdit(void 0, {
11629
- onError: (e) => {
11630
- ui.toast.error(`Failed to request order edit: ${e.message}`);
11631
- },
11632
- onSuccess: () => {
11633
- requestSucceeded = true;
11634
- }
11635
- });
11636
- if (!requestSucceeded) {
11637
- setIsSubmitting(false);
11638
- return;
11639
- }
11640
- await confirmOrderEdit(void 0, {
11641
- onError: (e) => {
11642
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
11643
- },
11644
- onSuccess: () => {
11645
- handleSuccess();
11646
- },
11647
- onSettled: () => {
11648
- setIsSubmitting(false);
11649
- }
11650
- });
11651
- };
11652
- const onKeydown = React.useCallback(
11653
- (e) => {
11654
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11655
- if (data || isSubmitting) {
11656
- return;
11657
- }
11658
- onSubmit();
11659
- }
11660
- },
11661
- [data, isSubmitting, onSubmit]
10715
+ [data, isSubmitting, onSubmit]
11662
10716
  );
11663
10717
  React.useEffect(() => {
11664
10718
  document.addEventListener("keydown", onKeydown);
@@ -11864,7 +10918,7 @@ const ShippingForm = ({ preview, order }) => {
11864
10918
  ]
11865
10919
  }
11866
10920
  ) : /* @__PURE__ */ jsxRuntime.jsx(
11867
- StackedModalTrigger,
10921
+ StackedModalTrigger$1,
11868
10922
  {
11869
10923
  shippingProfileId: profile.id,
11870
10924
  shippingOption,
@@ -11975,7 +11029,7 @@ const ShippingForm = ({ preview, order }) => {
11975
11029
  ] }) })
11976
11030
  ] });
11977
11031
  };
11978
- const StackedModalTrigger = ({
11032
+ const StackedModalTrigger$1 = ({
11979
11033
  shippingProfileId,
11980
11034
  shippingOption,
11981
11035
  shippingMethod,
@@ -12396,7 +11450,7 @@ const ShippingAddressForm = ({ order }) => {
12396
11450
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12397
11451
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12398
11452
  },
12399
- resolver: zod.zodResolver(schema$1)
11453
+ resolver: zod.zodResolver(schema$2)
12400
11454
  });
12401
11455
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12402
11456
  const { handleSuccess } = useRouteModal();
@@ -12566,7 +11620,7 @@ const ShippingAddressForm = ({ order }) => {
12566
11620
  }
12567
11621
  ) });
12568
11622
  };
12569
- const schema$1 = addressSchema;
11623
+ const schema$2 = addressSchema;
12570
11624
  const TransferOwnership = () => {
12571
11625
  const { id } = reactRouterDom.useParams();
12572
11626
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12590,7 +11644,7 @@ const TransferOwnershipForm = ({ order }) => {
12590
11644
  defaultValues: {
12591
11645
  customer_id: order.customer_id || ""
12592
11646
  },
12593
- resolver: zod.zodResolver(schema)
11647
+ resolver: zod.zodResolver(schema$1)
12594
11648
  });
12595
11649
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12596
11650
  const { handleSuccess } = useRouteModal();
@@ -12912,136 +11966,1082 @@ const Illustration = () => {
12912
11966
  transform: "matrix(0.865865 0.500278 -0.871576 0.490261 105.4 62.5457)",
12913
11967
  fill: "#A1A1AA"
12914
11968
  }
12915
- ),
11969
+ ),
11970
+ /* @__PURE__ */ jsxRuntime.jsx(
11971
+ "path",
11972
+ {
11973
+ d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
11974
+ fill: "#52525B"
11975
+ }
11976
+ ),
11977
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11978
+ "path",
11979
+ {
11980
+ d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
11981
+ stroke: "#A1A1AA",
11982
+ strokeWidth: "1.5",
11983
+ strokeLinecap: "round",
11984
+ strokeLinejoin: "round"
11985
+ }
11986
+ ) }),
11987
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11988
+ "path",
11989
+ {
11990
+ d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
11991
+ stroke: "#A1A1AA",
11992
+ strokeWidth: "1.5",
11993
+ strokeLinecap: "round",
11994
+ strokeLinejoin: "round"
11995
+ }
11996
+ ) }),
11997
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11998
+ "path",
11999
+ {
12000
+ d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12001
+ stroke: "#A1A1AA",
12002
+ strokeWidth: "1.5",
12003
+ strokeLinecap: "round",
12004
+ strokeLinejoin: "round"
12005
+ }
12006
+ ) }),
12007
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12008
+ "path",
12009
+ {
12010
+ d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12011
+ stroke: "#A1A1AA",
12012
+ strokeWidth: "1.5",
12013
+ strokeLinecap: "round",
12014
+ strokeLinejoin: "round"
12015
+ }
12016
+ ) }),
12017
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12018
+ "path",
12019
+ {
12020
+ d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12021
+ stroke: "#A1A1AA",
12022
+ strokeWidth: "1.5",
12023
+ strokeLinecap: "round",
12024
+ strokeLinejoin: "round"
12025
+ }
12026
+ ) }),
12027
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12028
+ "path",
12029
+ {
12030
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12031
+ stroke: "#A1A1AA",
12032
+ strokeWidth: "1.5",
12033
+ strokeLinecap: "round",
12034
+ strokeLinejoin: "round"
12035
+ }
12036
+ ) }),
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
+ ] }),
12916
12533
  /* @__PURE__ */ jsxRuntime.jsx(
12917
- "path",
12918
- {
12919
- d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
12920
- fill: "#52525B"
12921
- }
12922
- ),
12923
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12924
- "path",
12925
- {
12926
- d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
12927
- stroke: "#A1A1AA",
12928
- strokeWidth: "1.5",
12929
- strokeLinecap: "round",
12930
- strokeLinejoin: "round"
12931
- }
12932
- ) }),
12933
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12934
- "path",
12935
- {
12936
- d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
12937
- stroke: "#A1A1AA",
12938
- strokeWidth: "1.5",
12939
- strokeLinecap: "round",
12940
- strokeLinejoin: "round"
12941
- }
12942
- ) }),
12943
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12944
- "path",
12945
- {
12946
- d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12947
- stroke: "#A1A1AA",
12948
- strokeWidth: "1.5",
12949
- strokeLinecap: "round",
12950
- strokeLinejoin: "round"
12951
- }
12952
- ) }),
12953
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12954
- "path",
12534
+ ui.Text,
12955
12535
  {
12956
- d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12957
- stroke: "#A1A1AA",
12958
- strokeWidth: "1.5",
12959
- strokeLinecap: "round",
12960
- strokeLinejoin: "round"
12536
+ size: "small",
12537
+ leading: "compact",
12538
+ className: "text-ui-fg-subtle",
12539
+ children: item.variant_sku
12961
12540
  }
12962
- ) }),
12963
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12964
- "path",
12965
- {
12966
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12967
- stroke: "#A1A1AA",
12968
- strokeWidth: "1.5",
12969
- strokeLinecap: "round",
12970
- strokeLinejoin: "round"
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);
12971
12634
  }
12972
- ) }),
12973
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12974
- "path",
12975
- {
12976
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12977
- stroke: "#A1A1AA",
12978
- strokeWidth: "1.5",
12979
- strokeLinecap: "round",
12980
- strokeLinejoin: "round"
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 }) }) });
12981
12682
  }
12982
- ) }),
12983
- /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12984
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12985
- "rect",
12986
- {
12987
- width: "12",
12988
- height: "12",
12989
- fill: "white",
12990
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12991
- }
12992
- ) }),
12993
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12994
- "rect",
12995
- {
12996
- width: "12",
12997
- height: "12",
12998
- fill: "white",
12999
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
13000
- }
13001
- ) }),
13002
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13003
- "rect",
13004
- {
13005
- width: "12",
13006
- height: "12",
13007
- fill: "white",
13008
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
13009
- }
13010
- ) }),
13011
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13012
- "rect",
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,
13013
12704
  {
13014
- width: "12",
13015
- height: "12",
13016
- fill: "white",
13017
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12705
+ ...field,
12706
+ symbol: getNativeSymbol(currencyCode),
12707
+ code: currencyCode,
12708
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
13018
12709
  }
13019
- ) }),
13020
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13021
- "rect",
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,
13022
12884
  {
13023
- width: "12",
13024
- height: "12",
13025
- fill: "white",
13026
- 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() }) })
13027
12887
  }
13028
- ) }),
13029
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13030
- "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,
13031
12899
  {
13032
- width: "12",
13033
- height: "12",
13034
- fill: "white",
13035
- 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() }) })
13036
12902
  }
13037
- ) })
13038
- ] })
13039
- ]
13040
- }
13041
- );
12903
+ );
12904
+ },
12905
+ enableSorting: true,
12906
+ sortAscLabel: "Oldest first",
12907
+ sortDescLabel: "Newest first"
12908
+ })
12909
+ ];
12910
+ }, []);
12911
+ };
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
+ });
13025
+ const CustomItems = () => {
13026
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
13027
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
13028
+ /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
13029
+ ] });
13030
+ };
13031
+ const CustomItemsForm = () => {
13032
+ const form = reactHookForm.useForm({
13033
+ resolver: zod.zodResolver(schema)
13034
+ });
13035
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
13036
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
13037
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
13038
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13039
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
13040
+ ] }) })
13041
+ ] }) });
13042
13042
  };
13043
13043
  const schema = objectType({
13044
- customer_id: stringType().min(1)
13044
+ email: stringType().email()
13045
13045
  });
13046
13046
  const widgetModule = { widgets: [] };
13047
13047
  const routeModule = {
@@ -13067,18 +13067,10 @@ const routeModule = {
13067
13067
  Component: BillingAddress,
13068
13068
  path: "/draft-orders/:id/billing-address"
13069
13069
  },
13070
- {
13071
- Component: CustomItems,
13072
- path: "/draft-orders/:id/custom-items"
13073
- },
13074
13070
  {
13075
13071
  Component: Email,
13076
13072
  path: "/draft-orders/:id/email"
13077
13073
  },
13078
- {
13079
- Component: Items,
13080
- path: "/draft-orders/:id/items"
13081
- },
13082
13074
  {
13083
13075
  Component: Metadata,
13084
13076
  path: "/draft-orders/:id/metadata"
@@ -13102,6 +13094,14 @@ const routeModule = {
13102
13094
  {
13103
13095
  Component: TransferOwnership,
13104
13096
  path: "/draft-orders/:id/transfer-ownership"
13097
+ },
13098
+ {
13099
+ Component: Items,
13100
+ path: "/draft-orders/:id/items"
13101
+ },
13102
+ {
13103
+ Component: CustomItems,
13104
+ path: "/draft-orders/:id/custom-items"
13105
13105
  }
13106
13106
  ]
13107
13107
  }