@medusajs/draft-order 2.11.4-snapshot-20251106121614 → 2.11.4-snapshot-20251106130942

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.
@@ -9568,6 +9568,27 @@ const ID = () => {
9568
9568
  /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Outlet, {})
9569
9569
  ] });
9570
9570
  };
9571
+ const CustomItems = () => {
9572
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9573
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9574
+ /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9575
+ ] });
9576
+ };
9577
+ const CustomItemsForm = () => {
9578
+ const form = reactHookForm.useForm({
9579
+ resolver: zod.zodResolver(schema$5)
9580
+ });
9581
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9582
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9583
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9584
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9585
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9586
+ ] }) })
9587
+ ] }) });
9588
+ };
9589
+ const schema$5 = objectType({
9590
+ email: stringType().email()
9591
+ });
9571
9592
  const BillingAddress = () => {
9572
9593
  const { id } = reactRouterDom.useParams();
9573
9594
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9600,7 +9621,7 @@ const BillingAddressForm = ({ order }) => {
9600
9621
  postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9601
9622
  phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9602
9623
  },
9603
- resolver: zod.zodResolver(schema$5)
9624
+ resolver: zod.zodResolver(schema$4)
9604
9625
  });
9605
9626
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9606
9627
  const { handleSuccess } = useRouteModal();
@@ -9757,895 +9778,893 @@ const BillingAddressForm = ({ order }) => {
9757
9778
  }
9758
9779
  ) });
9759
9780
  };
9760
- const schema$5 = addressSchema;
9761
- const CustomItems = () => {
9762
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9763
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9764
- /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9765
- ] });
9766
- };
9767
- const CustomItemsForm = () => {
9768
- const form = reactHookForm.useForm({
9769
- resolver: zod.zodResolver(schema$4)
9770
- });
9771
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9772
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9773
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9774
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9775
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9776
- ] }) })
9777
- ] }) });
9778
- };
9779
- const schema$4 = objectType({
9780
- email: stringType().email()
9781
- });
9782
- const InlineTip = React.forwardRef(
9783
- ({ variant = "tip", label, className, children, ...props }, ref) => {
9784
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9781
+ const schema$4 = addressSchema;
9782
+ const NumberInput = React.forwardRef(
9783
+ ({
9784
+ value,
9785
+ onChange,
9786
+ size = "base",
9787
+ min = 0,
9788
+ max = 100,
9789
+ step = 1,
9790
+ className,
9791
+ disabled,
9792
+ ...props
9793
+ }, ref) => {
9794
+ const handleChange = (event) => {
9795
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
9796
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9797
+ onChange(newValue);
9798
+ }
9799
+ };
9800
+ const handleIncrement = () => {
9801
+ const newValue = value + step;
9802
+ if (max === void 0 || newValue <= max) {
9803
+ onChange(newValue);
9804
+ }
9805
+ };
9806
+ const handleDecrement = () => {
9807
+ const newValue = value - step;
9808
+ if (min === void 0 || newValue >= min) {
9809
+ onChange(newValue);
9810
+ }
9811
+ };
9785
9812
  return /* @__PURE__ */ jsxRuntime.jsxs(
9786
9813
  "div",
9787
9814
  {
9788
- ref,
9789
9815
  className: ui.clx(
9790
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9816
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9817
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9818
+ {
9819
+ "h-7": size === "small",
9820
+ "h-8": size === "base"
9821
+ },
9791
9822
  className
9792
9823
  ),
9793
- ...props,
9794
9824
  children: [
9795
9825
  /* @__PURE__ */ jsxRuntime.jsx(
9796
- "div",
9826
+ "input",
9797
9827
  {
9798
- role: "presentation",
9799
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9800
- "bg-ui-tag-orange-icon": variant === "warning"
9801
- })
9828
+ ref,
9829
+ type: "number",
9830
+ value,
9831
+ onChange: handleChange,
9832
+ min,
9833
+ max,
9834
+ step,
9835
+ className: ui.clx(
9836
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9837
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9838
+ "placeholder:text-ui-fg-muted"
9839
+ ),
9840
+ ...props
9802
9841
  }
9803
9842
  ),
9804
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
9805
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9806
- labelValue,
9807
- ":"
9808
- ] }),
9809
- " ",
9810
- children
9811
- ] })
9843
+ /* @__PURE__ */ jsxRuntime.jsxs(
9844
+ "button",
9845
+ {
9846
+ className: ui.clx(
9847
+ "flex items-center justify-center outline-none transition-fg",
9848
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9849
+ "focus:bg-ui-bg-field-component-hover",
9850
+ "hover:bg-ui-bg-field-component-hover",
9851
+ {
9852
+ "size-7": size === "small",
9853
+ "size-8": size === "base"
9854
+ }
9855
+ ),
9856
+ type: "button",
9857
+ onClick: handleDecrement,
9858
+ disabled: min !== void 0 && value <= min || disabled,
9859
+ children: [
9860
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
9861
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9862
+ ]
9863
+ }
9864
+ ),
9865
+ /* @__PURE__ */ jsxRuntime.jsxs(
9866
+ "button",
9867
+ {
9868
+ className: ui.clx(
9869
+ "flex items-center justify-center outline-none transition-fg",
9870
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9871
+ "focus:bg-ui-bg-field-hover",
9872
+ "hover:bg-ui-bg-field-hover",
9873
+ {
9874
+ "size-7": size === "small",
9875
+ "size-8": size === "base"
9876
+ }
9877
+ ),
9878
+ type: "button",
9879
+ onClick: handleIncrement,
9880
+ disabled: max !== void 0 && value >= max || disabled,
9881
+ children: [
9882
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
9883
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9884
+ ]
9885
+ }
9886
+ )
9812
9887
  ]
9813
9888
  }
9814
9889
  );
9815
9890
  }
9816
9891
  );
9817
- InlineTip.displayName = "InlineTip";
9818
- const MetadataFieldSchema = objectType({
9819
- key: stringType(),
9820
- disabled: booleanType().optional(),
9821
- value: anyType()
9822
- });
9823
- const MetadataSchema = objectType({
9824
- metadata: arrayType(MetadataFieldSchema)
9825
- });
9826
- const Metadata = () => {
9827
- const { id } = reactRouterDom.useParams();
9828
- const { order, isPending, isError, error } = useOrder(id, {
9829
- fields: "metadata"
9830
- });
9831
- if (isError) {
9832
- throw error;
9833
- }
9834
- const isReady = !isPending && !!order;
9835
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9836
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9837
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
9838
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9839
- ] }),
9840
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9841
- ] });
9892
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9893
+ const productVariantsQueryKeys = {
9894
+ list: (query2) => [
9895
+ PRODUCT_VARIANTS_QUERY_KEY,
9896
+ query2 ? query2 : void 0
9897
+ ]
9842
9898
  };
9843
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9844
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9845
- const MetadataForm = ({ orderId, metadata }) => {
9846
- const { handleSuccess } = useRouteModal();
9847
- const hasUneditableRows = getHasUneditableRows(metadata);
9848
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9849
- const form = reactHookForm.useForm({
9850
- defaultValues: {
9851
- metadata: getDefaultValues(metadata)
9852
- },
9853
- resolver: zod.zodResolver(MetadataSchema)
9899
+ const useProductVariants = (query2, options) => {
9900
+ const { data, ...rest } = reactQuery.useQuery({
9901
+ queryKey: productVariantsQueryKeys.list(query2),
9902
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
9903
+ ...options
9854
9904
  });
9855
- const handleSubmit = form.handleSubmit(async (data) => {
9856
- const parsedData = parseValues(data);
9857
- await mutateAsync(
9858
- {
9859
- metadata: parsedData
9905
+ return { ...data, ...rest };
9906
+ };
9907
+ const useCancelOrderEdit = ({ preview }) => {
9908
+ const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9909
+ const onCancel = React.useCallback(async () => {
9910
+ if (!preview) {
9911
+ return true;
9912
+ }
9913
+ let res = false;
9914
+ await cancelOrderEdit(void 0, {
9915
+ onError: (e) => {
9916
+ ui.toast.error(e.message);
9860
9917
  },
9861
- {
9862
- onSuccess: () => {
9863
- ui.toast.success("Metadata updated");
9864
- handleSuccess();
9865
- },
9866
- onError: (error) => {
9867
- ui.toast.error(error.message);
9868
- }
9918
+ onSuccess: () => {
9919
+ res = true;
9869
9920
  }
9870
- );
9871
- });
9872
- const { fields, insert, remove } = reactHookForm.useFieldArray({
9873
- control: form.control,
9874
- name: "metadata"
9875
- });
9876
- function deleteRow(index) {
9877
- remove(index);
9878
- if (fields.length === 1) {
9879
- insert(0, {
9880
- key: "",
9881
- value: "",
9882
- disabled: false
9883
- });
9884
- }
9885
- }
9886
- function insertRow(index, position) {
9887
- insert(index + (position === "above" ? 0 : 1), {
9888
- key: "",
9889
- value: "",
9890
- disabled: false
9891
9921
  });
9892
- }
9893
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9894
- KeyboundForm,
9895
- {
9896
- onSubmit: handleSubmit,
9897
- className: "flex flex-1 flex-col overflow-hidden",
9898
- children: [
9899
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9900
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9901
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9902
- /* @__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" }) }),
9903
- /* @__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" }) })
9904
- ] }),
9905
- fields.map((field, index) => {
9906
- const isDisabled = field.disabled || false;
9907
- let placeholder = "-";
9908
- if (typeof field.value === "object") {
9909
- placeholder = "{ ... }";
9910
- }
9911
- if (Array.isArray(field.value)) {
9912
- placeholder = "[ ... ]";
9913
- }
9914
- return /* @__PURE__ */ jsxRuntime.jsx(
9915
- ConditionalTooltip,
9916
- {
9917
- showTooltip: isDisabled,
9918
- content: "This row is disabled because it contains non-primitive data.",
9919
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
9920
- /* @__PURE__ */ jsxRuntime.jsxs(
9921
- "div",
9922
- {
9923
- className: ui.clx("grid grid-cols-2 divide-x", {
9924
- "overflow-hidden rounded-b-lg": index === fields.length - 1
9925
- }),
9926
- children: [
9927
- /* @__PURE__ */ jsxRuntime.jsx(
9928
- Form$2.Field,
9929
- {
9930
- control: form.control,
9931
- name: `metadata.${index}.key`,
9932
- render: ({ field: field2 }) => {
9933
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
9934
- GridInput,
9935
- {
9936
- "aria-labelledby": METADATA_KEY_LABEL_ID,
9937
- ...field2,
9938
- disabled: isDisabled,
9939
- placeholder: "Key"
9940
- }
9941
- ) }) });
9942
- }
9943
- }
9944
- ),
9945
- /* @__PURE__ */ jsxRuntime.jsx(
9946
- Form$2.Field,
9947
- {
9948
- control: form.control,
9949
- name: `metadata.${index}.value`,
9950
- render: ({ field: { value, ...field2 } }) => {
9951
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
9952
- GridInput,
9953
- {
9954
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
9955
- ...field2,
9956
- value: isDisabled ? placeholder : value,
9957
- disabled: isDisabled,
9958
- placeholder: "Value"
9959
- }
9960
- ) }) });
9961
- }
9962
- }
9963
- )
9964
- ]
9965
- }
9966
- ),
9967
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
9968
- /* @__PURE__ */ jsxRuntime.jsx(
9969
- ui.DropdownMenu.Trigger,
9970
- {
9971
- className: ui.clx(
9972
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9973
- {
9974
- hidden: isDisabled
9975
- }
9976
- ),
9977
- disabled: isDisabled,
9978
- asChild: true,
9979
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
9980
- }
9981
- ),
9982
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
9983
- /* @__PURE__ */ jsxRuntime.jsxs(
9984
- ui.DropdownMenu.Item,
9985
- {
9986
- className: "gap-x-2",
9987
- onClick: () => insertRow(index, "above"),
9988
- children: [
9989
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
9990
- "Insert row above"
9991
- ]
9992
- }
9993
- ),
9994
- /* @__PURE__ */ jsxRuntime.jsxs(
9995
- ui.DropdownMenu.Item,
9996
- {
9997
- className: "gap-x-2",
9998
- onClick: () => insertRow(index, "below"),
9999
- children: [
10000
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
10001
- "Insert row below"
10002
- ]
10003
- }
10004
- ),
10005
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
10006
- /* @__PURE__ */ jsxRuntime.jsxs(
10007
- ui.DropdownMenu.Item,
10008
- {
10009
- className: "gap-x-2",
10010
- onClick: () => deleteRow(index),
10011
- children: [
10012
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
10013
- "Delete row"
10014
- ]
10015
- }
10016
- )
10017
- ] })
10018
- ] })
10019
- ] })
10020
- },
10021
- field.id
10022
- );
10023
- })
10024
- ] }),
10025
- 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." })
10026
- ] }),
10027
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10028
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10029
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10030
- ] }) })
10031
- ]
9922
+ return res;
9923
+ }, [preview, cancelOrderEdit]);
9924
+ return { onCancel };
9925
+ };
9926
+ let IS_REQUEST_RUNNING = false;
9927
+ const useInitiateOrderEdit = ({
9928
+ preview
9929
+ }) => {
9930
+ const navigate = reactRouterDom.useNavigate();
9931
+ const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
9932
+ React.useEffect(() => {
9933
+ async function run() {
9934
+ if (IS_REQUEST_RUNNING || !preview) {
9935
+ return;
9936
+ }
9937
+ if (preview.order_change) {
9938
+ return;
9939
+ }
9940
+ IS_REQUEST_RUNNING = true;
9941
+ await mutateAsync(void 0, {
9942
+ onError: (e) => {
9943
+ ui.toast.error(e.message);
9944
+ navigate(`/draft-orders/${preview.id}`, { replace: true });
9945
+ return;
9946
+ }
9947
+ });
9948
+ IS_REQUEST_RUNNING = false;
10032
9949
  }
10033
- ) });
9950
+ run();
9951
+ }, [preview, navigate, mutateAsync]);
10034
9952
  };
10035
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
10036
- return /* @__PURE__ */ jsxRuntime.jsx(
10037
- "input",
9953
+ function convertNumber(value) {
9954
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
9955
+ }
9956
+ const STACKED_MODAL_ID = "items_stacked_modal";
9957
+ const Items = () => {
9958
+ const { id } = reactRouterDom.useParams();
9959
+ const {
9960
+ order: preview,
9961
+ isPending: isPreviewPending,
9962
+ isError: isPreviewError,
9963
+ error: previewError
9964
+ } = useOrderPreview(id, void 0, {
9965
+ placeholderData: reactQuery.keepPreviousData
9966
+ });
9967
+ useInitiateOrderEdit({ preview });
9968
+ const { draft_order, isPending, isError, error } = useDraftOrder(
9969
+ id,
10038
9970
  {
10039
- ref,
10040
- ...props,
10041
- autoComplete: "off",
10042
- className: ui.clx(
10043
- "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",
10044
- className
10045
- )
9971
+ fields: "currency_code"
9972
+ },
9973
+ {
9974
+ enabled: !!id
10046
9975
  }
10047
9976
  );
10048
- });
10049
- GridInput.displayName = "MetadataForm.GridInput";
10050
- const PlaceholderInner = () => {
10051
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10052
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10053
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10054
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
10055
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
10056
- ] }) })
10057
- ] });
10058
- };
10059
- const EDITABLE_TYPES = ["string", "number", "boolean"];
10060
- function getDefaultValues(metadata) {
10061
- if (!metadata || !Object.keys(metadata).length) {
10062
- return [
10063
- {
10064
- key: "",
10065
- value: "",
10066
- disabled: false
10067
- }
10068
- ];
9977
+ const { onCancel } = useCancelOrderEdit({ preview });
9978
+ if (isError) {
9979
+ throw error;
10069
9980
  }
10070
- return Object.entries(metadata).map(([key, value]) => {
10071
- if (!EDITABLE_TYPES.includes(typeof value)) {
10072
- return {
10073
- key,
10074
- value,
10075
- disabled: true
10076
- };
10077
- }
10078
- let stringValue = value;
10079
- if (typeof value !== "string") {
10080
- stringValue = JSON.stringify(value);
10081
- }
10082
- return {
10083
- key,
10084
- value: stringValue,
10085
- original_key: key
10086
- };
10087
- });
10088
- }
10089
- function parseValues(values) {
10090
- const metadata = values.metadata;
10091
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10092
- if (isEmpty) {
10093
- return null;
9981
+ if (isPreviewError) {
9982
+ throw previewError;
10094
9983
  }
10095
- const update = {};
10096
- metadata.forEach((field) => {
10097
- let key = field.key;
10098
- let value = field.value;
10099
- const disabled = field.disabled;
10100
- if (!key || !value) {
10101
- return;
10102
- }
10103
- if (disabled) {
10104
- update[key] = value;
9984
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
9985
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
9986
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
9987
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
9988
+ ] }) });
9989
+ };
9990
+ const ItemsForm = ({ preview, currencyCode }) => {
9991
+ var _a;
9992
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
9993
+ const [modalContent, setModalContent] = React.useState(
9994
+ null
9995
+ );
9996
+ const { handleSuccess } = useRouteModal();
9997
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
9998
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
9999
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10000
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10001
+ const matches = React.useMemo(() => {
10002
+ return matchSorter.matchSorter(preview.items, query2, {
10003
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
10004
+ });
10005
+ }, [preview.items, query2]);
10006
+ const onSubmit = async () => {
10007
+ setIsSubmitting(true);
10008
+ let requestSucceeded = false;
10009
+ await requestOrderEdit(void 0, {
10010
+ onError: (e) => {
10011
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
10012
+ },
10013
+ onSuccess: () => {
10014
+ requestSucceeded = true;
10015
+ }
10016
+ });
10017
+ if (!requestSucceeded) {
10018
+ setIsSubmitting(false);
10105
10019
  return;
10106
10020
  }
10107
- key = key.trim();
10108
- value = value.trim();
10109
- if (value === "true") {
10110
- update[key] = true;
10111
- } else if (value === "false") {
10112
- update[key] = false;
10113
- } else {
10114
- const parsedNumber = parseFloat(value);
10115
- if (!isNaN(parsedNumber)) {
10116
- update[key] = parsedNumber;
10117
- } else {
10118
- update[key] = value;
10021
+ await confirmOrderEdit(void 0, {
10022
+ onError: (e) => {
10023
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10024
+ },
10025
+ onSuccess: () => {
10026
+ handleSuccess();
10027
+ },
10028
+ onSettled: () => {
10029
+ setIsSubmitting(false);
10119
10030
  }
10120
- }
10121
- });
10122
- return update;
10123
- }
10124
- function getHasUneditableRows(metadata) {
10125
- if (!metadata) {
10126
- return false;
10127
- }
10128
- return Object.values(metadata).some(
10129
- (value) => !EDITABLE_TYPES.includes(typeof value)
10031
+ });
10032
+ };
10033
+ const onKeyDown = React.useCallback(
10034
+ (e) => {
10035
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10036
+ if (modalContent || isSubmitting) {
10037
+ return;
10038
+ }
10039
+ onSubmit();
10040
+ }
10041
+ },
10042
+ [modalContent, isSubmitting, onSubmit]
10130
10043
  );
10131
- }
10132
- const Email = () => {
10133
- const { id } = reactRouterDom.useParams();
10134
- const { order, isPending, isError, error } = useOrder(id, {
10135
- fields: "+email"
10136
- });
10137
- if (isError) {
10138
- throw error;
10139
- }
10140
- const isReady = !isPending && !!order;
10141
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10142
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10143
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
10144
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
10145
- ] }),
10146
- isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
10044
+ React.useEffect(() => {
10045
+ document.addEventListener("keydown", onKeyDown);
10046
+ return () => {
10047
+ document.removeEventListener("keydown", onKeyDown);
10048
+ };
10049
+ }, [onKeyDown]);
10050
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10051
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10052
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10053
+ StackedFocusModal,
10054
+ {
10055
+ id: STACKED_MODAL_ID,
10056
+ onOpenChangeCallback: (open) => {
10057
+ if (!open) {
10058
+ setModalContent(null);
10059
+ }
10060
+ },
10061
+ children: [
10062
+ /* @__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: [
10063
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10064
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10065
+ /* @__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" }) })
10066
+ ] }),
10067
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10068
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10069
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10070
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10071
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10072
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10073
+ ] }),
10074
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10075
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10076
+ ui.Input,
10077
+ {
10078
+ type: "search",
10079
+ placeholder: "Search items",
10080
+ value: searchValue,
10081
+ onChange: (e) => onSearchValueChange(e.target.value)
10082
+ }
10083
+ ) }),
10084
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10085
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10086
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10087
+ /* @__PURE__ */ jsxRuntime.jsx(
10088
+ StackedModalTrigger$1,
10089
+ {
10090
+ type: "add-items",
10091
+ setModalContent
10092
+ }
10093
+ ),
10094
+ /* @__PURE__ */ jsxRuntime.jsx(
10095
+ StackedModalTrigger$1,
10096
+ {
10097
+ type: "add-custom-item",
10098
+ setModalContent
10099
+ }
10100
+ )
10101
+ ] })
10102
+ ] })
10103
+ ] })
10104
+ ] }),
10105
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10106
+ /* @__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: [
10107
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10108
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10109
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10110
+ /* @__PURE__ */ jsxRuntime.jsx("div", {})
10111
+ ] }) }),
10112
+ /* @__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: [
10113
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10114
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10115
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10116
+ Item,
10117
+ {
10118
+ item,
10119
+ preview,
10120
+ currencyCode
10121
+ },
10122
+ item.id
10123
+ )) : /* @__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: [
10124
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10125
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10126
+ 'No items found for "',
10127
+ query2,
10128
+ '".'
10129
+ ] })
10130
+ ] }) })
10131
+ ] })
10132
+ ] }),
10133
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10134
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10135
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10136
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10137
+ ui.Text,
10138
+ {
10139
+ size: "small",
10140
+ leading: "compact",
10141
+ className: "text-ui-fg-subtle",
10142
+ children: [
10143
+ itemCount,
10144
+ " ",
10145
+ itemCount === 1 ? "item" : "items"
10146
+ ]
10147
+ }
10148
+ ) }),
10149
+ /* @__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) }) })
10150
+ ] })
10151
+ ] }) }),
10152
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10153
+ CustomItemForm,
10154
+ {
10155
+ orderId: preview.id,
10156
+ currencyCode
10157
+ }
10158
+ ) : null)
10159
+ ]
10160
+ }
10161
+ ) }),
10162
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10163
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10164
+ /* @__PURE__ */ jsxRuntime.jsx(
10165
+ ui.Button,
10166
+ {
10167
+ size: "small",
10168
+ type: "button",
10169
+ onClick: onSubmit,
10170
+ isLoading: isSubmitting,
10171
+ children: "Save"
10172
+ }
10173
+ )
10174
+ ] }) })
10147
10175
  ] });
10148
10176
  };
10149
- const EmailForm = ({ order }) => {
10177
+ const Item = ({ item, preview, currencyCode }) => {
10178
+ if (item.variant_id) {
10179
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10180
+ }
10181
+ return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10182
+ };
10183
+ const VariantItem = ({ item, preview, currencyCode }) => {
10184
+ const [editing, setEditing] = React.useState(false);
10150
10185
  const form = reactHookForm.useForm({
10151
10186
  defaultValues: {
10152
- email: order.email ?? ""
10187
+ quantity: item.quantity,
10188
+ unit_price: item.unit_price
10153
10189
  },
10154
- resolver: zod.zodResolver(schema$3)
10190
+ resolver: zod.zodResolver(variantItemSchema)
10155
10191
  });
10156
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10157
- const { handleSuccess } = useRouteModal();
10192
+ const actionId = React.useMemo(() => {
10193
+ var _a, _b;
10194
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10195
+ }, [item]);
10196
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10197
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10198
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10158
10199
  const onSubmit = form.handleSubmit(async (data) => {
10159
- await mutateAsync(
10160
- { email: data.email },
10200
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10201
+ setEditing(false);
10202
+ return;
10203
+ }
10204
+ if (!actionId) {
10205
+ await updateOriginalItem(
10206
+ {
10207
+ item_id: item.id,
10208
+ quantity: data.quantity,
10209
+ unit_price: convertNumber(data.unit_price)
10210
+ },
10211
+ {
10212
+ onSuccess: () => {
10213
+ setEditing(false);
10214
+ },
10215
+ onError: (e) => {
10216
+ ui.toast.error(e.message);
10217
+ }
10218
+ }
10219
+ );
10220
+ return;
10221
+ }
10222
+ await updateActionItem(
10223
+ {
10224
+ action_id: actionId,
10225
+ quantity: data.quantity,
10226
+ unit_price: convertNumber(data.unit_price)
10227
+ },
10161
10228
  {
10162
10229
  onSuccess: () => {
10163
- handleSuccess();
10230
+ setEditing(false);
10164
10231
  },
10165
- onError: (error) => {
10166
- ui.toast.error(error.message);
10232
+ onError: (e) => {
10233
+ ui.toast.error(e.message);
10167
10234
  }
10168
10235
  }
10169
10236
  );
10170
10237
  });
10171
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10172
- KeyboundForm,
10173
- {
10174
- className: "flex flex-1 flex-col overflow-hidden",
10175
- onSubmit,
10176
- children: [
10177
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
10178
- Form$2.Field,
10179
- {
10180
- control: form.control,
10181
- name: "email",
10182
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10183
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
10184
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10185
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10186
- ] })
10187
- }
10188
- ) }),
10189
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10190
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10191
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10192
- ] }) })
10193
- ]
10194
- }
10195
- ) });
10196
- };
10197
- const schema$3 = objectType({
10198
- email: stringType().email()
10199
- });
10200
- const NumberInput = React.forwardRef(
10201
- ({
10202
- value,
10203
- onChange,
10204
- size = "base",
10205
- min = 0,
10206
- max = 100,
10207
- step = 1,
10208
- className,
10209
- disabled,
10210
- ...props
10211
- }, ref) => {
10212
- const handleChange = (event) => {
10213
- const newValue = event.target.value === "" ? min : Number(event.target.value);
10214
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
10215
- onChange(newValue);
10216
- }
10217
- };
10218
- const handleIncrement = () => {
10219
- const newValue = value + step;
10220
- if (max === void 0 || newValue <= max) {
10221
- onChange(newValue);
10222
- }
10223
- };
10224
- const handleDecrement = () => {
10225
- const newValue = value - step;
10226
- if (min === void 0 || newValue >= min) {
10227
- onChange(newValue);
10228
- }
10229
- };
10230
- return /* @__PURE__ */ jsxRuntime.jsxs(
10231
- "div",
10232
- {
10233
- className: ui.clx(
10234
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
10235
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
10236
- {
10237
- "h-7": size === "small",
10238
- "h-8": size === "base"
10239
- },
10240
- className
10241
- ),
10242
- children: [
10243
- /* @__PURE__ */ jsxRuntime.jsx(
10244
- "input",
10245
- {
10246
- ref,
10247
- type: "number",
10248
- value,
10249
- onChange: handleChange,
10250
- min,
10251
- max,
10252
- step,
10253
- className: ui.clx(
10254
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
10255
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
10256
- "placeholder:text-ui-fg-muted"
10257
- ),
10258
- ...props
10259
- }
10260
- ),
10238
+ 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: [
10239
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10240
+ /* @__PURE__ */ jsxRuntime.jsx(
10241
+ Thumbnail,
10242
+ {
10243
+ thumbnail: item.thumbnail,
10244
+ alt: item.product_title ?? void 0
10245
+ }
10246
+ ),
10247
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10248
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10249
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10261
10250
  /* @__PURE__ */ jsxRuntime.jsxs(
10262
- "button",
10251
+ ui.Text,
10263
10252
  {
10264
- className: ui.clx(
10265
- "flex items-center justify-center outline-none transition-fg",
10266
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10267
- "focus:bg-ui-bg-field-component-hover",
10268
- "hover:bg-ui-bg-field-component-hover",
10269
- {
10270
- "size-7": size === "small",
10271
- "size-8": size === "base"
10272
- }
10273
- ),
10274
- type: "button",
10275
- onClick: handleDecrement,
10276
- disabled: min !== void 0 && value <= min || disabled,
10253
+ size: "small",
10254
+ leading: "compact",
10255
+ className: "text-ui-fg-subtle",
10277
10256
  children: [
10278
- /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
10279
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
10257
+ "(",
10258
+ item.variant_title,
10259
+ ")"
10280
10260
  ]
10281
10261
  }
10282
- ),
10283
- /* @__PURE__ */ jsxRuntime.jsxs(
10284
- "button",
10262
+ )
10263
+ ] }),
10264
+ /* @__PURE__ */ jsxRuntime.jsx(
10265
+ ui.Text,
10266
+ {
10267
+ size: "small",
10268
+ leading: "compact",
10269
+ className: "text-ui-fg-subtle",
10270
+ children: item.variant_sku
10271
+ }
10272
+ )
10273
+ ] })
10274
+ ] }),
10275
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10276
+ Form$2.Field,
10277
+ {
10278
+ control: form.control,
10279
+ name: "quantity",
10280
+ render: ({ field }) => {
10281
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10282
+ }
10283
+ }
10284
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10285
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10286
+ Form$2.Field,
10287
+ {
10288
+ control: form.control,
10289
+ name: "unit_price",
10290
+ render: ({ field: { onChange, ...field } }) => {
10291
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10292
+ ui.CurrencyInput,
10285
10293
  {
10286
- className: ui.clx(
10287
- "flex items-center justify-center outline-none transition-fg",
10288
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10289
- "focus:bg-ui-bg-field-hover",
10290
- "hover:bg-ui-bg-field-hover",
10291
- {
10292
- "size-7": size === "small",
10293
- "size-8": size === "base"
10294
- }
10295
- ),
10296
- type: "button",
10297
- onClick: handleIncrement,
10298
- disabled: max !== void 0 && value >= max || disabled,
10299
- children: [
10300
- /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
10301
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
10302
- ]
10294
+ ...field,
10295
+ symbol: getNativeSymbol(currencyCode),
10296
+ code: currencyCode,
10297
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10303
10298
  }
10304
- )
10305
- ]
10299
+ ) }) });
10300
+ }
10306
10301
  }
10307
- );
10308
- }
10309
- );
10310
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
10311
- const productVariantsQueryKeys = {
10312
- list: (query2) => [
10313
- PRODUCT_VARIANTS_QUERY_KEY,
10314
- query2 ? query2 : void 0
10315
- ]
10316
- };
10317
- const useProductVariants = (query2, options) => {
10318
- const { data, ...rest } = reactQuery.useQuery({
10319
- queryKey: productVariantsQueryKeys.list(query2),
10320
- queryFn: async () => await sdk.admin.productVariant.list(query2),
10321
- ...options
10322
- });
10323
- return { ...data, ...rest };
10324
- };
10325
- const useCancelOrderEdit = ({ preview }) => {
10326
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10327
- const onCancel = React.useCallback(async () => {
10328
- if (!preview) {
10329
- return true;
10330
- }
10331
- let res = false;
10332
- await cancelOrderEdit(void 0, {
10333
- onError: (e) => {
10334
- ui.toast.error(e.message);
10335
- },
10336
- onSuccess: () => {
10337
- res = true;
10302
+ ) }) : /* @__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) }) }),
10303
+ /* @__PURE__ */ jsxRuntime.jsx(
10304
+ ui.IconButton,
10305
+ {
10306
+ type: "button",
10307
+ size: "small",
10308
+ onClick: editing ? onSubmit : () => {
10309
+ setEditing(true);
10310
+ },
10311
+ disabled: isPending,
10312
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10338
10313
  }
10339
- });
10340
- return res;
10341
- }, [preview, cancelOrderEdit]);
10342
- return { onCancel };
10314
+ )
10315
+ ] }) }) });
10343
10316
  };
10344
- let IS_REQUEST_RUNNING = false;
10345
- const useInitiateOrderEdit = ({
10346
- preview
10347
- }) => {
10348
- const navigate = reactRouterDom.useNavigate();
10349
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10317
+ const variantItemSchema = objectType({
10318
+ quantity: numberType(),
10319
+ unit_price: unionType([numberType(), stringType()])
10320
+ });
10321
+ const CustomItem = ({ item, preview, currencyCode }) => {
10322
+ const [editing, setEditing] = React.useState(false);
10323
+ const { quantity, unit_price, title } = item;
10324
+ const form = reactHookForm.useForm({
10325
+ defaultValues: {
10326
+ title,
10327
+ quantity,
10328
+ unit_price
10329
+ },
10330
+ resolver: zod.zodResolver(customItemSchema)
10331
+ });
10350
10332
  React.useEffect(() => {
10351
- async function run() {
10352
- if (IS_REQUEST_RUNNING || !preview) {
10353
- return;
10354
- }
10355
- if (preview.order_change) {
10356
- return;
10357
- }
10358
- IS_REQUEST_RUNNING = true;
10359
- await mutateAsync(void 0, {
10333
+ form.reset({
10334
+ title,
10335
+ quantity,
10336
+ unit_price
10337
+ });
10338
+ }, [form, title, quantity, unit_price]);
10339
+ const actionId = React.useMemo(() => {
10340
+ var _a, _b;
10341
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10342
+ }, [item]);
10343
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10344
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10345
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10346
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10347
+ const onSubmit = form.handleSubmit(async (data) => {
10348
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10349
+ setEditing(false);
10350
+ return;
10351
+ }
10352
+ if (!actionId) {
10353
+ await updateOriginalItem(
10354
+ {
10355
+ item_id: item.id,
10356
+ quantity: data.quantity,
10357
+ unit_price: convertNumber(data.unit_price)
10358
+ },
10359
+ {
10360
+ onSuccess: () => {
10361
+ setEditing(false);
10362
+ },
10363
+ onError: (e) => {
10364
+ ui.toast.error(e.message);
10365
+ }
10366
+ }
10367
+ );
10368
+ return;
10369
+ }
10370
+ if (data.quantity === 0) {
10371
+ await removeActionItem(actionId, {
10372
+ onSuccess: () => {
10373
+ setEditing(false);
10374
+ },
10360
10375
  onError: (e) => {
10361
10376
  ui.toast.error(e.message);
10362
- navigate(`/draft-orders/${preview.id}`, { replace: true });
10363
- return;
10364
10377
  }
10365
10378
  });
10366
- IS_REQUEST_RUNNING = false;
10379
+ return;
10367
10380
  }
10368
- run();
10369
- }, [preview, navigate, mutateAsync]);
10370
- };
10371
- function convertNumber(value) {
10372
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10373
- }
10374
- const STACKED_MODAL_ID = "items_stacked_modal";
10375
- const Items = () => {
10376
- const { id } = reactRouterDom.useParams();
10377
- const {
10378
- order: preview,
10379
- isPending: isPreviewPending,
10380
- isError: isPreviewError,
10381
- error: previewError
10382
- } = useOrderPreview(id, void 0, {
10383
- placeholderData: reactQuery.keepPreviousData
10381
+ await updateActionItem(
10382
+ {
10383
+ action_id: actionId,
10384
+ quantity: data.quantity,
10385
+ unit_price: convertNumber(data.unit_price)
10386
+ },
10387
+ {
10388
+ onSuccess: () => {
10389
+ setEditing(false);
10390
+ },
10391
+ onError: (e) => {
10392
+ ui.toast.error(e.message);
10393
+ }
10394
+ }
10395
+ );
10384
10396
  });
10385
- useInitiateOrderEdit({ preview });
10386
- const { draft_order, isPending, isError, error } = useDraftOrder(
10387
- id,
10388
- {
10389
- fields: "currency_code"
10390
- },
10391
- {
10392
- enabled: !!id
10393
- }
10394
- );
10395
- const { onCancel } = useCancelOrderEdit({ preview });
10396
- if (isError) {
10397
- throw error;
10398
- }
10399
- if (isPreviewError) {
10400
- throw previewError;
10401
- }
10402
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10403
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10404
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10405
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10406
- ] }) });
10397
+ 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: [
10398
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10399
+ /* @__PURE__ */ jsxRuntime.jsx(
10400
+ Thumbnail,
10401
+ {
10402
+ thumbnail: item.thumbnail,
10403
+ alt: item.title ?? void 0
10404
+ }
10405
+ ),
10406
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
10407
+ Form$2.Field,
10408
+ {
10409
+ control: form.control,
10410
+ name: "title",
10411
+ render: ({ field }) => {
10412
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
10413
+ }
10414
+ }
10415
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
10416
+ ] }),
10417
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
10418
+ Form$2.Field,
10419
+ {
10420
+ control: form.control,
10421
+ name: "quantity",
10422
+ render: ({ field }) => {
10423
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10424
+ }
10425
+ }
10426
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
10427
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
10428
+ Form$2.Field,
10429
+ {
10430
+ control: form.control,
10431
+ name: "unit_price",
10432
+ render: ({ field: { onChange, ...field } }) => {
10433
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10434
+ ui.CurrencyInput,
10435
+ {
10436
+ ...field,
10437
+ symbol: getNativeSymbol(currencyCode),
10438
+ code: currencyCode,
10439
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10440
+ }
10441
+ ) }) });
10442
+ }
10443
+ }
10444
+ ) : /* @__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) }) }),
10445
+ /* @__PURE__ */ jsxRuntime.jsx(
10446
+ ui.IconButton,
10447
+ {
10448
+ type: "button",
10449
+ size: "small",
10450
+ onClick: editing ? onSubmit : () => {
10451
+ setEditing(true);
10452
+ },
10453
+ disabled: isPending,
10454
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10455
+ }
10456
+ )
10457
+ ] }) }) });
10407
10458
  };
10408
- const ItemsForm = ({ preview, currencyCode }) => {
10409
- var _a;
10410
- const [isSubmitting, setIsSubmitting] = React.useState(false);
10411
- const [modalContent, setModalContent] = React.useState(
10412
- null
10459
+ const StackedModalTrigger$1 = ({
10460
+ type,
10461
+ setModalContent
10462
+ }) => {
10463
+ const { setIsOpen } = useStackedModal();
10464
+ const onClick = React.useCallback(() => {
10465
+ setModalContent(type);
10466
+ setIsOpen(STACKED_MODAL_ID, true);
10467
+ }, [setModalContent, setIsOpen, type]);
10468
+ 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" }) });
10469
+ };
10470
+ const VARIANT_PREFIX = "items";
10471
+ const LIMIT = 50;
10472
+ const ExistingItemsForm = ({ orderId, items }) => {
10473
+ const { setIsOpen } = useStackedModal();
10474
+ const [rowSelection, setRowSelection] = React.useState(
10475
+ items.reduce((acc, item) => {
10476
+ acc[item.variant_id] = true;
10477
+ return acc;
10478
+ }, {})
10413
10479
  );
10414
- const { handleSuccess } = useRouteModal();
10415
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10416
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10417
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10418
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10419
- const matches = React.useMemo(() => {
10420
- return matchSorter.matchSorter(preview.items, query2, {
10421
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10422
- });
10423
- }, [preview.items, query2]);
10424
- const onSubmit = async () => {
10425
- setIsSubmitting(true);
10426
- let requestSucceeded = false;
10427
- await requestOrderEdit(void 0, {
10428
- onError: (e) => {
10429
- ui.toast.error(`Failed to request order edit: ${e.message}`);
10430
- },
10431
- onSuccess: () => {
10432
- requestSucceeded = true;
10433
- }
10434
- });
10435
- if (!requestSucceeded) {
10436
- setIsSubmitting(false);
10437
- return;
10480
+ React.useEffect(() => {
10481
+ setRowSelection(
10482
+ items.reduce((acc, item) => {
10483
+ if (item.variant_id) {
10484
+ acc[item.variant_id] = true;
10485
+ }
10486
+ return acc;
10487
+ }, {})
10488
+ );
10489
+ }, [items]);
10490
+ const { q, order, offset } = useQueryParams(
10491
+ ["q", "order", "offset"],
10492
+ VARIANT_PREFIX
10493
+ );
10494
+ const { variants, count, isPending, isError, error } = useProductVariants(
10495
+ {
10496
+ q,
10497
+ order,
10498
+ offset: offset ? parseInt(offset) : void 0,
10499
+ limit: LIMIT
10500
+ },
10501
+ {
10502
+ placeholderData: reactQuery.keepPreviousData
10438
10503
  }
10439
- await confirmOrderEdit(void 0, {
10440
- onError: (e) => {
10441
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10442
- },
10443
- onSuccess: () => {
10444
- handleSuccess();
10504
+ );
10505
+ const columns = useColumns();
10506
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
10507
+ const onSubmit = async () => {
10508
+ const ids = Object.keys(rowSelection).filter(
10509
+ (id) => !items.find((i) => i.variant_id === id)
10510
+ );
10511
+ await mutateAsync(
10512
+ {
10513
+ items: ids.map((id) => ({
10514
+ variant_id: id,
10515
+ quantity: 1
10516
+ }))
10445
10517
  },
10446
- onSettled: () => {
10447
- setIsSubmitting(false);
10518
+ {
10519
+ onSuccess: () => {
10520
+ setRowSelection({});
10521
+ setIsOpen(STACKED_MODAL_ID, false);
10522
+ },
10523
+ onError: (e) => {
10524
+ ui.toast.error(e.message);
10525
+ }
10448
10526
  }
10449
- });
10527
+ );
10450
10528
  };
10451
- const onKeyDown = React.useCallback(
10452
- (e) => {
10453
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10454
- if (modalContent || isSubmitting) {
10455
- return;
10529
+ if (isError) {
10530
+ throw error;
10531
+ }
10532
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10533
+ StackedFocusModal.Content,
10534
+ {
10535
+ onOpenAutoFocus: (e) => {
10536
+ e.preventDefault();
10537
+ const searchInput = document.querySelector(
10538
+ "[data-modal-id='modal-search-input']"
10539
+ );
10540
+ if (searchInput) {
10541
+ searchInput.focus();
10456
10542
  }
10457
- onSubmit();
10458
- }
10459
- },
10460
- [modalContent, isSubmitting, onSubmit]
10461
- );
10462
- React.useEffect(() => {
10463
- document.addEventListener("keydown", onKeyDown);
10464
- return () => {
10465
- document.removeEventListener("keydown", onKeyDown);
10466
- };
10467
- }, [onKeyDown]);
10468
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10469
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10470
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10471
- StackedFocusModal,
10472
- {
10473
- id: STACKED_MODAL_ID,
10474
- onOpenChangeCallback: (open) => {
10475
- if (!open) {
10476
- setModalContent(null);
10543
+ },
10544
+ children: [
10545
+ /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
10546
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10547
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10548
+ ] }),
10549
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
10550
+ DataTable,
10551
+ {
10552
+ data: variants,
10553
+ columns,
10554
+ isLoading: isPending,
10555
+ getRowId: (row) => row.id,
10556
+ rowCount: count,
10557
+ prefix: VARIANT_PREFIX,
10558
+ layout: "fill",
10559
+ rowSelection: {
10560
+ state: rowSelection,
10561
+ onRowSelectionChange: setRowSelection,
10562
+ enableRowSelection: (row) => {
10563
+ return !items.find((i) => i.variant_id === row.original.id);
10564
+ }
10565
+ },
10566
+ autoFocusSearch: true
10477
10567
  }
10568
+ ) }),
10569
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10570
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10571
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10572
+ ] }) })
10573
+ ]
10574
+ }
10575
+ );
10576
+ };
10577
+ const columnHelper = ui.createDataTableColumnHelper();
10578
+ const useColumns = () => {
10579
+ return React.useMemo(() => {
10580
+ return [
10581
+ columnHelper.select(),
10582
+ columnHelper.accessor("product.title", {
10583
+ header: "Product",
10584
+ cell: ({ row }) => {
10585
+ var _a, _b, _c;
10586
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
10587
+ /* @__PURE__ */ jsxRuntime.jsx(
10588
+ Thumbnail,
10589
+ {
10590
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10591
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
10592
+ }
10593
+ ),
10594
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10595
+ ] });
10478
10596
  },
10479
- children: [
10480
- /* @__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: [
10481
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10482
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10483
- /* @__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" }) })
10484
- ] }),
10485
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10486
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10487
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10488
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10489
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10490
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10491
- ] }),
10492
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10493
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10494
- ui.Input,
10495
- {
10496
- type: "search",
10497
- placeholder: "Search items",
10498
- value: searchValue,
10499
- onChange: (e) => onSearchValueChange(e.target.value)
10500
- }
10501
- ) }),
10502
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10503
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10504
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10505
- /* @__PURE__ */ jsxRuntime.jsx(
10506
- StackedModalTrigger$1,
10507
- {
10508
- type: "add-items",
10509
- setModalContent
10510
- }
10511
- ),
10512
- /* @__PURE__ */ jsxRuntime.jsx(
10513
- StackedModalTrigger$1,
10514
- {
10515
- type: "add-custom-item",
10516
- setModalContent
10517
- }
10518
- )
10519
- ] })
10520
- ] })
10521
- ] })
10522
- ] }),
10523
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10524
- /* @__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: [
10525
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10526
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10527
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10528
- /* @__PURE__ */ jsxRuntime.jsx("div", {})
10529
- ] }) }),
10530
- /* @__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: [
10531
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10532
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10533
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10534
- Item,
10535
- {
10536
- item,
10537
- preview,
10538
- currencyCode
10539
- },
10540
- item.id
10541
- )) : /* @__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: [
10542
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10543
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10544
- 'No items found for "',
10545
- query2,
10546
- '".'
10547
- ] })
10548
- ] }) })
10549
- ] })
10550
- ] }),
10551
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10552
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10553
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10554
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10555
- ui.Text,
10556
- {
10557
- size: "small",
10558
- leading: "compact",
10559
- className: "text-ui-fg-subtle",
10560
- children: [
10561
- itemCount,
10562
- " ",
10563
- itemCount === 1 ? "item" : "items"
10564
- ]
10565
- }
10566
- ) }),
10567
- /* @__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) }) })
10568
- ] })
10569
- ] }) }),
10570
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10571
- CustomItemForm,
10597
+ enableSorting: true
10598
+ }),
10599
+ columnHelper.accessor("title", {
10600
+ header: "Variant",
10601
+ enableSorting: true
10602
+ }),
10603
+ columnHelper.accessor("sku", {
10604
+ header: "SKU",
10605
+ cell: ({ getValue }) => {
10606
+ return getValue() ?? "-";
10607
+ },
10608
+ enableSorting: true
10609
+ }),
10610
+ columnHelper.accessor("updated_at", {
10611
+ header: "Updated",
10612
+ cell: ({ getValue }) => {
10613
+ return /* @__PURE__ */ jsxRuntime.jsx(
10614
+ ui.Tooltip,
10615
+ {
10616
+ content: getFullDate({ date: getValue(), includeTime: true }),
10617
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10618
+ }
10619
+ );
10620
+ },
10621
+ enableSorting: true,
10622
+ sortAscLabel: "Oldest first",
10623
+ sortDescLabel: "Newest first"
10624
+ }),
10625
+ columnHelper.accessor("created_at", {
10626
+ header: "Created",
10627
+ cell: ({ getValue }) => {
10628
+ return /* @__PURE__ */ jsxRuntime.jsx(
10629
+ ui.Tooltip,
10572
10630
  {
10573
- orderId: preview.id,
10574
- currencyCode
10631
+ content: getFullDate({ date: getValue(), includeTime: true }),
10632
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10575
10633
  }
10576
- ) : null)
10577
- ]
10578
- }
10579
- ) }),
10580
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10581
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10582
- /* @__PURE__ */ jsxRuntime.jsx(
10583
- ui.Button,
10584
- {
10585
- size: "small",
10586
- type: "button",
10587
- onClick: onSubmit,
10588
- isLoading: isSubmitting,
10589
- children: "Save"
10590
- }
10591
- )
10592
- ] }) })
10593
- ] });
10594
- };
10595
- const Item = ({ item, preview, currencyCode }) => {
10596
- if (item.variant_id) {
10597
- return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10598
- }
10599
- return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10634
+ );
10635
+ },
10636
+ enableSorting: true,
10637
+ sortAscLabel: "Oldest first",
10638
+ sortDescLabel: "Newest first"
10639
+ })
10640
+ ];
10641
+ }, []);
10600
10642
  };
10601
- const VariantItem = ({ item, preview, currencyCode }) => {
10602
- const [editing, setEditing] = React.useState(false);
10643
+ const CustomItemForm = ({ orderId, currencyCode }) => {
10644
+ const { setIsOpen } = useStackedModal();
10645
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10603
10646
  const form = reactHookForm.useForm({
10604
10647
  defaultValues: {
10605
- quantity: item.quantity,
10606
- unit_price: item.unit_price
10648
+ title: "",
10649
+ quantity: 1,
10650
+ unit_price: ""
10607
10651
  },
10608
- resolver: zod.zodResolver(variantItemSchema)
10652
+ resolver: zod.zodResolver(customItemSchema)
10609
10653
  });
10610
- const actionId = React.useMemo(() => {
10611
- var _a, _b;
10612
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10613
- }, [item]);
10614
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10615
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10616
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10617
10654
  const onSubmit = form.handleSubmit(async (data) => {
10618
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10619
- setEditing(false);
10620
- return;
10621
- }
10622
- if (!actionId) {
10623
- await updateOriginalItem(
10624
- {
10625
- item_id: item.id,
10626
- quantity: data.quantity,
10627
- unit_price: convertNumber(data.unit_price)
10628
- },
10629
- {
10630
- onSuccess: () => {
10631
- setEditing(false);
10632
- },
10633
- onError: (e) => {
10634
- ui.toast.error(e.message);
10635
- }
10636
- }
10637
- );
10638
- return;
10639
- }
10640
- await updateActionItem(
10655
+ await addItems(
10641
10656
  {
10642
- action_id: actionId,
10643
- quantity: data.quantity,
10644
- unit_price: convertNumber(data.unit_price)
10657
+ items: [
10658
+ {
10659
+ title: data.title,
10660
+ quantity: data.quantity,
10661
+ unit_price: convertNumber(data.unit_price)
10662
+ }
10663
+ ]
10645
10664
  },
10646
10665
  {
10647
10666
  onSuccess: () => {
10648
- setEditing(false);
10667
+ setIsOpen(STACKED_MODAL_ID, false);
10649
10668
  },
10650
10669
  onError: (e) => {
10651
10670
  ui.toast.error(e.message);
@@ -10653,524 +10672,505 @@ const VariantItem = ({ item, preview, currencyCode }) => {
10653
10672
  }
10654
10673
  );
10655
10674
  });
10656
- 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: [
10657
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10675
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
10676
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
10677
+ /* @__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: [
10678
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10679
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
10680
+ /* @__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." }) })
10681
+ ] }),
10682
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10658
10683
  /* @__PURE__ */ jsxRuntime.jsx(
10659
- Thumbnail,
10684
+ Form$2.Field,
10660
10685
  {
10661
- thumbnail: item.thumbnail,
10662
- alt: item.product_title ?? void 0
10686
+ control: form.control,
10687
+ name: "title",
10688
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10689
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10690
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
10691
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
10692
+ ] }),
10693
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10694
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10695
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10696
+ ] })
10697
+ ] }) })
10663
10698
  }
10664
10699
  ),
10665
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10666
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10667
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10668
- /* @__PURE__ */ jsxRuntime.jsxs(
10669
- ui.Text,
10670
- {
10671
- size: "small",
10672
- leading: "compact",
10673
- className: "text-ui-fg-subtle",
10674
- children: [
10675
- "(",
10676
- item.variant_title,
10677
- ")"
10678
- ]
10679
- }
10680
- )
10681
- ] }),
10682
- /* @__PURE__ */ jsxRuntime.jsx(
10683
- ui.Text,
10684
- {
10685
- size: "small",
10686
- leading: "compact",
10687
- className: "text-ui-fg-subtle",
10688
- children: item.variant_sku
10689
- }
10690
- )
10691
- ] })
10692
- ] }),
10693
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10694
- Form$2.Field,
10695
- {
10696
- control: form.control,
10697
- name: "quantity",
10698
- render: ({ field }) => {
10699
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10700
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10701
+ /* @__PURE__ */ jsxRuntime.jsx(
10702
+ Form$2.Field,
10703
+ {
10704
+ control: form.control,
10705
+ name: "unit_price",
10706
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10707
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10708
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
10709
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10710
+ ] }),
10711
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10712
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10713
+ ui.CurrencyInput,
10714
+ {
10715
+ symbol: getNativeSymbol(currencyCode),
10716
+ code: currencyCode,
10717
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10718
+ ...field
10719
+ }
10720
+ ) }),
10721
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10722
+ ] })
10723
+ ] }) })
10700
10724
  }
10701
- }
10702
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10703
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10704
- Form$2.Field,
10705
- {
10706
- control: form.control,
10707
- name: "unit_price",
10708
- render: ({ field: { onChange, ...field } }) => {
10709
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10710
- ui.CurrencyInput,
10711
- {
10712
- ...field,
10713
- symbol: getNativeSymbol(currencyCode),
10714
- code: currencyCode,
10715
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10716
- }
10717
- ) }) });
10725
+ ),
10726
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10727
+ /* @__PURE__ */ jsxRuntime.jsx(
10728
+ Form$2.Field,
10729
+ {
10730
+ control: form.control,
10731
+ name: "quantity",
10732
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10733
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10734
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
10735
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10736
+ ] }),
10737
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
10738
+ /* @__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" }) }) }),
10739
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10740
+ ] })
10741
+ ] }) })
10718
10742
  }
10719
- }
10720
- ) }) : /* @__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) }) }),
10721
- /* @__PURE__ */ jsxRuntime.jsx(
10722
- ui.IconButton,
10723
- {
10724
- type: "button",
10725
- size: "small",
10726
- onClick: editing ? onSubmit : () => {
10727
- setEditing(true);
10728
- },
10729
- disabled: isPending,
10730
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10731
- }
10732
- )
10743
+ )
10744
+ ] }) }) }),
10745
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10746
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10747
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10748
+ ] }) })
10733
10749
  ] }) }) });
10734
10750
  };
10735
- const variantItemSchema = objectType({
10751
+ const customItemSchema = objectType({
10752
+ title: stringType().min(1),
10736
10753
  quantity: numberType(),
10737
10754
  unit_price: unionType([numberType(), stringType()])
10738
10755
  });
10739
- const CustomItem = ({ item, preview, currencyCode }) => {
10740
- const [editing, setEditing] = React.useState(false);
10741
- const { quantity, unit_price, title } = item;
10756
+ const Email = () => {
10757
+ const { id } = reactRouterDom.useParams();
10758
+ const { order, isPending, isError, error } = useOrder(id, {
10759
+ fields: "+email"
10760
+ });
10761
+ if (isError) {
10762
+ throw error;
10763
+ }
10764
+ const isReady = !isPending && !!order;
10765
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10766
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10767
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Email" }) }),
10768
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
10769
+ ] }),
10770
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(EmailForm, { order })
10771
+ ] });
10772
+ };
10773
+ const EmailForm = ({ order }) => {
10742
10774
  const form = reactHookForm.useForm({
10743
10775
  defaultValues: {
10744
- title,
10745
- quantity,
10746
- unit_price
10776
+ email: order.email ?? ""
10747
10777
  },
10748
- resolver: zod.zodResolver(customItemSchema)
10778
+ resolver: zod.zodResolver(schema$3)
10749
10779
  });
10750
- React.useEffect(() => {
10751
- form.reset({
10752
- title,
10753
- quantity,
10754
- unit_price
10755
- });
10756
- }, [form, title, quantity, unit_price]);
10757
- const actionId = React.useMemo(() => {
10758
- var _a, _b;
10759
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10760
- }, [item]);
10761
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10762
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10763
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10764
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10780
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10781
+ const { handleSuccess } = useRouteModal();
10765
10782
  const onSubmit = form.handleSubmit(async (data) => {
10766
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10767
- setEditing(false);
10768
- return;
10769
- }
10770
- if (!actionId) {
10771
- await updateOriginalItem(
10772
- {
10773
- item_id: item.id,
10774
- quantity: data.quantity,
10775
- unit_price: convertNumber(data.unit_price)
10776
- },
10777
- {
10778
- onSuccess: () => {
10779
- setEditing(false);
10780
- },
10781
- onError: (e) => {
10782
- ui.toast.error(e.message);
10783
- }
10784
- }
10785
- );
10786
- return;
10787
- }
10788
- if (data.quantity === 0) {
10789
- await removeActionItem(actionId, {
10790
- onSuccess: () => {
10791
- setEditing(false);
10792
- },
10793
- onError: (e) => {
10794
- ui.toast.error(e.message);
10795
- }
10796
- });
10797
- return;
10798
- }
10799
- await updateActionItem(
10800
- {
10801
- action_id: actionId,
10802
- quantity: data.quantity,
10803
- unit_price: convertNumber(data.unit_price)
10804
- },
10783
+ await mutateAsync(
10784
+ { email: data.email },
10805
10785
  {
10806
10786
  onSuccess: () => {
10807
- setEditing(false);
10787
+ handleSuccess();
10808
10788
  },
10809
- onError: (e) => {
10810
- ui.toast.error(e.message);
10789
+ onError: (error) => {
10790
+ ui.toast.error(error.message);
10811
10791
  }
10812
10792
  }
10813
10793
  );
10814
10794
  });
10815
- 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: [
10816
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10817
- /* @__PURE__ */ jsxRuntime.jsx(
10818
- Thumbnail,
10819
- {
10820
- thumbnail: item.thumbnail,
10821
- alt: item.title ?? void 0
10822
- }
10823
- ),
10824
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10825
- Form$2.Field,
10826
- {
10827
- control: form.control,
10828
- name: "title",
10829
- render: ({ field }) => {
10830
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
10795
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10796
+ KeyboundForm,
10797
+ {
10798
+ className: "flex flex-1 flex-col overflow-hidden",
10799
+ onSubmit,
10800
+ children: [
10801
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(
10802
+ Form$2.Field,
10803
+ {
10804
+ control: form.control,
10805
+ name: "email",
10806
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10807
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Email" }),
10808
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10809
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10810
+ ] })
10831
10811
  }
10832
- }
10833
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
10834
- ] }),
10835
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10836
- Form$2.Field,
10837
- {
10838
- control: form.control,
10839
- name: "quantity",
10840
- render: ({ field }) => {
10841
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10842
- }
10843
- }
10844
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
10845
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10846
- Form$2.Field,
10812
+ ) }),
10813
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10814
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10815
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10816
+ ] }) })
10817
+ ]
10818
+ }
10819
+ ) });
10820
+ };
10821
+ const schema$3 = objectType({
10822
+ email: stringType().email()
10823
+ });
10824
+ const InlineTip = React.forwardRef(
10825
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
10826
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10827
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10828
+ "div",
10847
10829
  {
10848
- control: form.control,
10849
- name: "unit_price",
10850
- render: ({ field: { onChange, ...field } }) => {
10851
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10852
- ui.CurrencyInput,
10830
+ ref,
10831
+ className: ui.clx(
10832
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10833
+ className
10834
+ ),
10835
+ ...props,
10836
+ children: [
10837
+ /* @__PURE__ */ jsxRuntime.jsx(
10838
+ "div",
10853
10839
  {
10854
- ...field,
10855
- symbol: getNativeSymbol(currencyCode),
10856
- code: currencyCode,
10857
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10840
+ role: "presentation",
10841
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10842
+ "bg-ui-tag-orange-icon": variant === "warning"
10843
+ })
10858
10844
  }
10859
- ) }) });
10860
- }
10861
- }
10862
- ) : /* @__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) }) }),
10863
- /* @__PURE__ */ jsxRuntime.jsx(
10864
- ui.IconButton,
10865
- {
10866
- type: "button",
10867
- size: "small",
10868
- onClick: editing ? onSubmit : () => {
10869
- setEditing(true);
10870
- },
10871
- disabled: isPending,
10872
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10845
+ ),
10846
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10847
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10848
+ labelValue,
10849
+ ":"
10850
+ ] }),
10851
+ " ",
10852
+ children
10853
+ ] })
10854
+ ]
10873
10855
  }
10874
- )
10875
- ] }) }) });
10876
- };
10877
- const StackedModalTrigger$1 = ({
10878
- type,
10879
- setModalContent
10880
- }) => {
10881
- const { setIsOpen } = useStackedModal();
10882
- const onClick = React.useCallback(() => {
10883
- setModalContent(type);
10884
- setIsOpen(STACKED_MODAL_ID, true);
10885
- }, [setModalContent, setIsOpen, type]);
10886
- 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" }) });
10887
- };
10888
- const VARIANT_PREFIX = "items";
10889
- const LIMIT = 50;
10890
- const ExistingItemsForm = ({ orderId, items }) => {
10891
- const { setIsOpen } = useStackedModal();
10892
- const [rowSelection, setRowSelection] = React.useState(
10893
- items.reduce((acc, item) => {
10894
- acc[item.variant_id] = true;
10895
- return acc;
10896
- }, {})
10897
- );
10898
- React.useEffect(() => {
10899
- setRowSelection(
10900
- items.reduce((acc, item) => {
10901
- if (item.variant_id) {
10902
- acc[item.variant_id] = true;
10903
- }
10904
- return acc;
10905
- }, {})
10906
10856
  );
10907
- }, [items]);
10908
- const { q, order, offset } = useQueryParams(
10909
- ["q", "order", "offset"],
10910
- VARIANT_PREFIX
10911
- );
10912
- const { variants, count, isPending, isError, error } = useProductVariants(
10913
- {
10914
- q,
10915
- order,
10916
- offset: offset ? parseInt(offset) : void 0,
10917
- limit: LIMIT
10857
+ }
10858
+ );
10859
+ InlineTip.displayName = "InlineTip";
10860
+ const MetadataFieldSchema = objectType({
10861
+ key: stringType(),
10862
+ disabled: booleanType().optional(),
10863
+ value: anyType()
10864
+ });
10865
+ const MetadataSchema = objectType({
10866
+ metadata: arrayType(MetadataFieldSchema)
10867
+ });
10868
+ const Metadata = () => {
10869
+ const { id } = reactRouterDom.useParams();
10870
+ const { order, isPending, isError, error } = useOrder(id, {
10871
+ fields: "metadata"
10872
+ });
10873
+ if (isError) {
10874
+ throw error;
10875
+ }
10876
+ const isReady = !isPending && !!order;
10877
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10878
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10879
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10880
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10881
+ ] }),
10882
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10883
+ ] });
10884
+ };
10885
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10886
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10887
+ const MetadataForm = ({ orderId, metadata }) => {
10888
+ const { handleSuccess } = useRouteModal();
10889
+ const hasUneditableRows = getHasUneditableRows(metadata);
10890
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10891
+ const form = reactHookForm.useForm({
10892
+ defaultValues: {
10893
+ metadata: getDefaultValues(metadata)
10918
10894
  },
10919
- {
10920
- placeholderData: reactQuery.keepPreviousData
10921
- }
10922
- );
10923
- const columns = useColumns();
10924
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10925
- const onSubmit = async () => {
10926
- const ids = Object.keys(rowSelection).filter(
10927
- (id) => !items.find((i) => i.variant_id === id)
10928
- );
10895
+ resolver: zod.zodResolver(MetadataSchema)
10896
+ });
10897
+ const handleSubmit = form.handleSubmit(async (data) => {
10898
+ const parsedData = parseValues(data);
10929
10899
  await mutateAsync(
10930
10900
  {
10931
- items: ids.map((id) => ({
10932
- variant_id: id,
10933
- quantity: 1
10934
- }))
10901
+ metadata: parsedData
10935
10902
  },
10936
10903
  {
10937
10904
  onSuccess: () => {
10938
- setRowSelection({});
10939
- setIsOpen(STACKED_MODAL_ID, false);
10905
+ ui.toast.success("Metadata updated");
10906
+ handleSuccess();
10940
10907
  },
10941
- onError: (e) => {
10942
- ui.toast.error(e.message);
10908
+ onError: (error) => {
10909
+ ui.toast.error(error.message);
10943
10910
  }
10944
10911
  }
10945
10912
  );
10946
- };
10947
- if (isError) {
10948
- throw error;
10913
+ });
10914
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
10915
+ control: form.control,
10916
+ name: "metadata"
10917
+ });
10918
+ function deleteRow(index) {
10919
+ remove(index);
10920
+ if (fields.length === 1) {
10921
+ insert(0, {
10922
+ key: "",
10923
+ value: "",
10924
+ disabled: false
10925
+ });
10926
+ }
10949
10927
  }
10950
- return /* @__PURE__ */ jsxRuntime.jsxs(
10951
- StackedFocusModal.Content,
10928
+ function insertRow(index, position) {
10929
+ insert(index + (position === "above" ? 0 : 1), {
10930
+ key: "",
10931
+ value: "",
10932
+ disabled: false
10933
+ });
10934
+ }
10935
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10936
+ KeyboundForm,
10952
10937
  {
10953
- onOpenAutoFocus: (e) => {
10954
- e.preventDefault();
10955
- const searchInput = document.querySelector(
10956
- "[data-modal-id='modal-search-input']"
10957
- );
10958
- if (searchInput) {
10959
- searchInput.focus();
10960
- }
10961
- },
10938
+ onSubmit: handleSubmit,
10939
+ className: "flex flex-1 flex-col overflow-hidden",
10962
10940
  children: [
10963
- /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
10964
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10965
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10966
- ] }),
10967
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
10968
- DataTable,
10969
- {
10970
- data: variants,
10971
- columns,
10972
- isLoading: isPending,
10973
- getRowId: (row) => row.id,
10974
- rowCount: count,
10975
- prefix: VARIANT_PREFIX,
10976
- layout: "fill",
10977
- rowSelection: {
10978
- state: rowSelection,
10979
- onRowSelectionChange: setRowSelection,
10980
- enableRowSelection: (row) => {
10981
- return !items.find((i) => i.variant_id === row.original.id);
10941
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10942
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10943
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10944
+ /* @__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" }) }),
10945
+ /* @__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" }) })
10946
+ ] }),
10947
+ fields.map((field, index) => {
10948
+ const isDisabled = field.disabled || false;
10949
+ let placeholder = "-";
10950
+ if (typeof field.value === "object") {
10951
+ placeholder = "{ ... }";
10982
10952
  }
10983
- },
10984
- autoFocusSearch: true
10985
- }
10986
- ) }),
10987
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10988
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10989
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10953
+ if (Array.isArray(field.value)) {
10954
+ placeholder = "[ ... ]";
10955
+ }
10956
+ return /* @__PURE__ */ jsxRuntime.jsx(
10957
+ ConditionalTooltip,
10958
+ {
10959
+ showTooltip: isDisabled,
10960
+ content: "This row is disabled because it contains non-primitive data.",
10961
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
10962
+ /* @__PURE__ */ jsxRuntime.jsxs(
10963
+ "div",
10964
+ {
10965
+ className: ui.clx("grid grid-cols-2 divide-x", {
10966
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
10967
+ }),
10968
+ children: [
10969
+ /* @__PURE__ */ jsxRuntime.jsx(
10970
+ Form$2.Field,
10971
+ {
10972
+ control: form.control,
10973
+ name: `metadata.${index}.key`,
10974
+ render: ({ field: field2 }) => {
10975
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10976
+ GridInput,
10977
+ {
10978
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
10979
+ ...field2,
10980
+ disabled: isDisabled,
10981
+ placeholder: "Key"
10982
+ }
10983
+ ) }) });
10984
+ }
10985
+ }
10986
+ ),
10987
+ /* @__PURE__ */ jsxRuntime.jsx(
10988
+ Form$2.Field,
10989
+ {
10990
+ control: form.control,
10991
+ name: `metadata.${index}.value`,
10992
+ render: ({ field: { value, ...field2 } }) => {
10993
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10994
+ GridInput,
10995
+ {
10996
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
10997
+ ...field2,
10998
+ value: isDisabled ? placeholder : value,
10999
+ disabled: isDisabled,
11000
+ placeholder: "Value"
11001
+ }
11002
+ ) }) });
11003
+ }
11004
+ }
11005
+ )
11006
+ ]
11007
+ }
11008
+ ),
11009
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
11010
+ /* @__PURE__ */ jsxRuntime.jsx(
11011
+ ui.DropdownMenu.Trigger,
11012
+ {
11013
+ className: ui.clx(
11014
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11015
+ {
11016
+ hidden: isDisabled
11017
+ }
11018
+ ),
11019
+ disabled: isDisabled,
11020
+ asChild: true,
11021
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
11022
+ }
11023
+ ),
11024
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11025
+ /* @__PURE__ */ jsxRuntime.jsxs(
11026
+ ui.DropdownMenu.Item,
11027
+ {
11028
+ className: "gap-x-2",
11029
+ onClick: () => insertRow(index, "above"),
11030
+ children: [
11031
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11032
+ "Insert row above"
11033
+ ]
11034
+ }
11035
+ ),
11036
+ /* @__PURE__ */ jsxRuntime.jsxs(
11037
+ ui.DropdownMenu.Item,
11038
+ {
11039
+ className: "gap-x-2",
11040
+ onClick: () => insertRow(index, "below"),
11041
+ children: [
11042
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11043
+ "Insert row below"
11044
+ ]
11045
+ }
11046
+ ),
11047
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11048
+ /* @__PURE__ */ jsxRuntime.jsxs(
11049
+ ui.DropdownMenu.Item,
11050
+ {
11051
+ className: "gap-x-2",
11052
+ onClick: () => deleteRow(index),
11053
+ children: [
11054
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11055
+ "Delete row"
11056
+ ]
11057
+ }
11058
+ )
11059
+ ] })
11060
+ ] })
11061
+ ] })
11062
+ },
11063
+ field.id
11064
+ );
11065
+ })
11066
+ ] }),
11067
+ 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." })
11068
+ ] }),
11069
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11070
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11071
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10990
11072
  ] }) })
10991
11073
  ]
10992
11074
  }
10993
- );
11075
+ ) });
10994
11076
  };
10995
- const columnHelper = ui.createDataTableColumnHelper();
10996
- const useColumns = () => {
10997
- return React.useMemo(() => {
11077
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11078
+ return /* @__PURE__ */ jsxRuntime.jsx(
11079
+ "input",
11080
+ {
11081
+ ref,
11082
+ ...props,
11083
+ autoComplete: "off",
11084
+ className: ui.clx(
11085
+ "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",
11086
+ className
11087
+ )
11088
+ }
11089
+ );
11090
+ });
11091
+ GridInput.displayName = "MetadataForm.GridInput";
11092
+ const PlaceholderInner = () => {
11093
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11094
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11095
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11096
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11097
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11098
+ ] }) })
11099
+ ] });
11100
+ };
11101
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
11102
+ function getDefaultValues(metadata) {
11103
+ if (!metadata || !Object.keys(metadata).length) {
10998
11104
  return [
10999
- columnHelper.select(),
11000
- columnHelper.accessor("product.title", {
11001
- header: "Product",
11002
- cell: ({ row }) => {
11003
- var _a, _b, _c;
11004
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
11005
- /* @__PURE__ */ jsxRuntime.jsx(
11006
- Thumbnail,
11007
- {
11008
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
11009
- alt: (_b = row.original.product) == null ? void 0 : _b.title
11010
- }
11011
- ),
11012
- /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
11013
- ] });
11014
- },
11015
- enableSorting: true
11016
- }),
11017
- columnHelper.accessor("title", {
11018
- header: "Variant",
11019
- enableSorting: true
11020
- }),
11021
- columnHelper.accessor("sku", {
11022
- header: "SKU",
11023
- cell: ({ getValue }) => {
11024
- return getValue() ?? "-";
11025
- },
11026
- enableSorting: true
11027
- }),
11028
- columnHelper.accessor("updated_at", {
11029
- header: "Updated",
11030
- cell: ({ getValue }) => {
11031
- return /* @__PURE__ */ jsxRuntime.jsx(
11032
- ui.Tooltip,
11033
- {
11034
- content: getFullDate({ date: getValue(), includeTime: true }),
11035
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
11036
- }
11037
- );
11038
- },
11039
- enableSorting: true,
11040
- sortAscLabel: "Oldest first",
11041
- sortDescLabel: "Newest first"
11042
- }),
11043
- columnHelper.accessor("created_at", {
11044
- header: "Created",
11045
- cell: ({ getValue }) => {
11046
- return /* @__PURE__ */ jsxRuntime.jsx(
11047
- ui.Tooltip,
11048
- {
11049
- content: getFullDate({ date: getValue(), includeTime: true }),
11050
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
11051
- }
11052
- );
11053
- },
11054
- enableSorting: true,
11055
- sortAscLabel: "Oldest first",
11056
- sortDescLabel: "Newest first"
11057
- })
11105
+ {
11106
+ key: "",
11107
+ value: "",
11108
+ disabled: false
11109
+ }
11058
11110
  ];
11059
- }, []);
11060
- };
11061
- const CustomItemForm = ({ orderId, currencyCode }) => {
11062
- const { setIsOpen } = useStackedModal();
11063
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
11064
- const form = reactHookForm.useForm({
11065
- defaultValues: {
11066
- title: "",
11067
- quantity: 1,
11068
- unit_price: ""
11069
- },
11070
- resolver: zod.zodResolver(customItemSchema)
11111
+ }
11112
+ return Object.entries(metadata).map(([key, value]) => {
11113
+ if (!EDITABLE_TYPES.includes(typeof value)) {
11114
+ return {
11115
+ key,
11116
+ value,
11117
+ disabled: true
11118
+ };
11119
+ }
11120
+ let stringValue = value;
11121
+ if (typeof value !== "string") {
11122
+ stringValue = JSON.stringify(value);
11123
+ }
11124
+ return {
11125
+ key,
11126
+ value: stringValue,
11127
+ original_key: key
11128
+ };
11071
11129
  });
11072
- const onSubmit = form.handleSubmit(async (data) => {
11073
- await addItems(
11074
- {
11075
- items: [
11076
- {
11077
- title: data.title,
11078
- quantity: data.quantity,
11079
- unit_price: convertNumber(data.unit_price)
11080
- }
11081
- ]
11082
- },
11083
- {
11084
- onSuccess: () => {
11085
- setIsOpen(STACKED_MODAL_ID, false);
11086
- },
11087
- onError: (e) => {
11088
- ui.toast.error(e.message);
11089
- }
11130
+ }
11131
+ function parseValues(values) {
11132
+ const metadata = values.metadata;
11133
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11134
+ if (isEmpty) {
11135
+ return null;
11136
+ }
11137
+ const update = {};
11138
+ metadata.forEach((field) => {
11139
+ let key = field.key;
11140
+ let value = field.value;
11141
+ const disabled = field.disabled;
11142
+ if (!key || !value) {
11143
+ return;
11144
+ }
11145
+ if (disabled) {
11146
+ update[key] = value;
11147
+ return;
11148
+ }
11149
+ key = key.trim();
11150
+ value = value.trim();
11151
+ if (value === "true") {
11152
+ update[key] = true;
11153
+ } else if (value === "false") {
11154
+ update[key] = false;
11155
+ } else {
11156
+ const parsedNumber = parseFloat(value);
11157
+ if (!isNaN(parsedNumber)) {
11158
+ update[key] = parsedNumber;
11159
+ } else {
11160
+ update[key] = value;
11090
11161
  }
11091
- );
11162
+ }
11092
11163
  });
11093
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
11094
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
11095
- /* @__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: [
11096
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11097
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
11098
- /* @__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." }) })
11099
- ] }),
11100
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11101
- /* @__PURE__ */ jsxRuntime.jsx(
11102
- Form$2.Field,
11103
- {
11104
- control: form.control,
11105
- name: "title",
11106
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11107
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11108
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
11109
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
11110
- ] }),
11111
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11112
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
11113
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11114
- ] })
11115
- ] }) })
11116
- }
11117
- ),
11118
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11119
- /* @__PURE__ */ jsxRuntime.jsx(
11120
- Form$2.Field,
11121
- {
11122
- control: form.control,
11123
- name: "unit_price",
11124
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11125
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11126
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
11127
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
11128
- ] }),
11129
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11130
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11131
- ui.CurrencyInput,
11132
- {
11133
- symbol: getNativeSymbol(currencyCode),
11134
- code: currencyCode,
11135
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
11136
- ...field
11137
- }
11138
- ) }),
11139
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11140
- ] })
11141
- ] }) })
11142
- }
11143
- ),
11144
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11145
- /* @__PURE__ */ jsxRuntime.jsx(
11146
- Form$2.Field,
11147
- {
11148
- control: form.control,
11149
- name: "quantity",
11150
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11151
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11152
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
11153
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
11154
- ] }),
11155
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
11156
- /* @__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" }) }) }),
11157
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11158
- ] })
11159
- ] }) })
11160
- }
11161
- )
11162
- ] }) }) }),
11163
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11164
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11165
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
11166
- ] }) })
11167
- ] }) }) });
11168
- };
11169
- const customItemSchema = objectType({
11170
- title: stringType().min(1),
11171
- quantity: numberType(),
11172
- unit_price: unionType([numberType(), stringType()])
11173
- });
11164
+ return update;
11165
+ }
11166
+ function getHasUneditableRows(metadata) {
11167
+ if (!metadata) {
11168
+ return false;
11169
+ }
11170
+ return Object.values(metadata).some(
11171
+ (value) => !EDITABLE_TYPES.includes(typeof value)
11172
+ );
11173
+ }
11174
11174
  const PROMOTION_QUERY_KEY = "promotions";
11175
11175
  const promotionsQueryKeys = {
11176
11176
  list: (query2) => [
@@ -13060,25 +13060,25 @@ const routeModule = {
13060
13060
  handle,
13061
13061
  loader,
13062
13062
  children: [
13063
- {
13064
- Component: BillingAddress,
13065
- path: "/draft-orders/:id/billing-address"
13066
- },
13067
13063
  {
13068
13064
  Component: CustomItems,
13069
13065
  path: "/draft-orders/:id/custom-items"
13070
13066
  },
13071
13067
  {
13072
- Component: Metadata,
13073
- path: "/draft-orders/:id/metadata"
13068
+ Component: BillingAddress,
13069
+ path: "/draft-orders/:id/billing-address"
13070
+ },
13071
+ {
13072
+ Component: Items,
13073
+ path: "/draft-orders/:id/items"
13074
13074
  },
13075
13075
  {
13076
13076
  Component: Email,
13077
13077
  path: "/draft-orders/:id/email"
13078
13078
  },
13079
13079
  {
13080
- Component: Items,
13081
- path: "/draft-orders/:id/items"
13080
+ Component: Metadata,
13081
+ path: "/draft-orders/:id/metadata"
13082
13082
  },
13083
13083
  {
13084
13084
  Component: Promotions,
@@ -13110,7 +13110,8 @@ const menuItemModule = {
13110
13110
  label: config.label,
13111
13111
  icon: void 0,
13112
13112
  path: "/draft-orders",
13113
- nested: "/orders"
13113
+ nested: "/orders",
13114
+ translationNs: void 0
13114
13115
  }
13115
13116
  ]
13116
13117
  };