@medusajs/draft-order 2.10.0-snapshot-20250828112236 → 2.10.0-snapshot-20250828112445

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.
@@ -4,7 +4,7 @@ import { Tooltip, DropdownMenu, clx, IconButton, useDataTable, DataTable as Data
4
4
  import { useQuery, useQueryClient, useMutation, keepPreviousData, useInfiniteQuery } from "@tanstack/react-query";
5
5
  import React, { useState, useCallback, useMemo, Fragment, createContext, forwardRef, useId, useContext, useTransition, useRef, useImperativeHandle, useDeferredValue, useEffect, Suspense } from "react";
6
6
  import { useSearchParams, Link, useNavigate, Outlet, useBlocker, useLocation, useParams } from "react-router-dom";
7
- import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, Minus, PencilSquare, EllipsisVertical, ArrowUpMini, ArrowDownMini } from "@medusajs/icons";
7
+ import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, EllipsisVertical, ArrowUpMini, ArrowDownMini, Minus, PencilSquare } from "@medusajs/icons";
8
8
  import Medusa from "@medusajs/js-sdk";
9
9
  import { format, formatDistance, sub, subDays, subMonths } from "date-fns";
10
10
  import { enUS } from "date-fns/locale";
@@ -9765,243 +9765,593 @@ const CustomItemsForm = () => {
9765
9765
  const schema$4 = objectType({
9766
9766
  email: stringType().email()
9767
9767
  });
9768
- const NumberInput = forwardRef(
9769
- ({
9770
- value,
9771
- onChange,
9772
- size = "base",
9773
- min = 0,
9774
- max = 100,
9775
- step = 1,
9776
- className,
9777
- disabled,
9778
- ...props
9779
- }, ref) => {
9780
- const handleChange = (event) => {
9781
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9782
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9783
- onChange(newValue);
9784
- }
9785
- };
9786
- const handleIncrement = () => {
9787
- const newValue = value + step;
9788
- if (max === void 0 || newValue <= max) {
9789
- onChange(newValue);
9790
- }
9791
- };
9792
- const handleDecrement = () => {
9793
- const newValue = value - step;
9794
- if (min === void 0 || newValue >= min) {
9795
- onChange(newValue);
9796
- }
9797
- };
9768
+ const InlineTip = forwardRef(
9769
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9770
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9798
9771
  return /* @__PURE__ */ jsxs(
9799
9772
  "div",
9800
9773
  {
9774
+ ref,
9801
9775
  className: clx(
9802
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9803
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9804
- {
9805
- "h-7": size === "small",
9806
- "h-8": size === "base"
9807
- },
9776
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9808
9777
  className
9809
9778
  ),
9779
+ ...props,
9810
9780
  children: [
9811
9781
  /* @__PURE__ */ jsx(
9812
- "input",
9813
- {
9814
- ref,
9815
- type: "number",
9816
- value,
9817
- onChange: handleChange,
9818
- min,
9819
- max,
9820
- step,
9821
- className: clx(
9822
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9823
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9824
- "placeholder:text-ui-fg-muted"
9825
- ),
9826
- ...props
9827
- }
9828
- ),
9829
- /* @__PURE__ */ jsxs(
9830
- "button",
9782
+ "div",
9831
9783
  {
9832
- className: clx(
9833
- "flex items-center justify-center outline-none transition-fg",
9834
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9835
- "focus:bg-ui-bg-field-component-hover",
9836
- "hover:bg-ui-bg-field-component-hover",
9837
- {
9838
- "size-7": size === "small",
9839
- "size-8": size === "base"
9840
- }
9841
- ),
9842
- type: "button",
9843
- onClick: handleDecrement,
9844
- disabled: min !== void 0 && value <= min || disabled,
9845
- children: [
9846
- /* @__PURE__ */ jsx(Minus, {}),
9847
- /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9848
- ]
9784
+ role: "presentation",
9785
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9786
+ "bg-ui-tag-orange-icon": variant === "warning"
9787
+ })
9849
9788
  }
9850
9789
  ),
9851
- /* @__PURE__ */ jsxs(
9852
- "button",
9853
- {
9854
- className: clx(
9855
- "flex items-center justify-center outline-none transition-fg",
9856
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9857
- "focus:bg-ui-bg-field-hover",
9858
- "hover:bg-ui-bg-field-hover",
9859
- {
9860
- "size-7": size === "small",
9861
- "size-8": size === "base"
9862
- }
9863
- ),
9864
- type: "button",
9865
- onClick: handleIncrement,
9866
- disabled: max !== void 0 && value >= max || disabled,
9867
- children: [
9868
- /* @__PURE__ */ jsx(Plus, {}),
9869
- /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9870
- ]
9871
- }
9872
- )
9790
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9791
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9792
+ labelValue,
9793
+ ":"
9794
+ ] }),
9795
+ " ",
9796
+ children
9797
+ ] })
9873
9798
  ]
9874
9799
  }
9875
9800
  );
9876
9801
  }
9877
9802
  );
9878
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9879
- const productVariantsQueryKeys = {
9880
- list: (query2) => [
9881
- PRODUCT_VARIANTS_QUERY_KEY,
9882
- query2 ? query2 : void 0
9883
- ]
9884
- };
9885
- const useProductVariants = (query2, options) => {
9886
- const { data, ...rest } = useQuery({
9887
- queryKey: productVariantsQueryKeys.list(query2),
9888
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9889
- ...options
9890
- });
9891
- return { ...data, ...rest };
9892
- };
9893
- const useCancelOrderEdit = ({ preview }) => {
9894
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9895
- const onCancel = useCallback(async () => {
9896
- if (!preview) {
9897
- return true;
9898
- }
9899
- let res = false;
9900
- await cancelOrderEdit(void 0, {
9901
- onError: (e) => {
9902
- toast.error(e.message);
9903
- },
9904
- onSuccess: () => {
9905
- res = true;
9906
- }
9907
- });
9908
- return res;
9909
- }, [preview, cancelOrderEdit]);
9910
- return { onCancel };
9911
- };
9912
- let IS_REQUEST_RUNNING = false;
9913
- const useInitiateOrderEdit = ({
9914
- preview
9915
- }) => {
9916
- const navigate = useNavigate();
9917
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
9918
- useEffect(() => {
9919
- async function run() {
9920
- if (IS_REQUEST_RUNNING || !preview) {
9921
- return;
9922
- }
9923
- if (preview.order_change) {
9924
- return;
9925
- }
9926
- IS_REQUEST_RUNNING = true;
9927
- await mutateAsync(void 0, {
9928
- onError: (e) => {
9929
- toast.error(e.message);
9930
- navigate(`/draft-orders/${preview.id}`, { replace: true });
9931
- return;
9932
- }
9933
- });
9934
- IS_REQUEST_RUNNING = false;
9935
- }
9936
- run();
9937
- }, [preview, navigate, mutateAsync]);
9938
- };
9939
- function convertNumber(value) {
9940
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
9941
- }
9942
- const STACKED_MODAL_ID = "items_stacked_modal";
9943
- const Items = () => {
9803
+ InlineTip.displayName = "InlineTip";
9804
+ const MetadataFieldSchema = objectType({
9805
+ key: stringType(),
9806
+ disabled: booleanType().optional(),
9807
+ value: anyType()
9808
+ });
9809
+ const MetadataSchema = objectType({
9810
+ metadata: arrayType(MetadataFieldSchema)
9811
+ });
9812
+ const Metadata = () => {
9944
9813
  const { id } = useParams();
9945
- const {
9946
- order: preview,
9947
- isPending: isPreviewPending,
9948
- isError: isPreviewError,
9949
- error: previewError
9950
- } = useOrderPreview(id, void 0, {
9951
- placeholderData: keepPreviousData
9814
+ const { order, isPending, isError, error } = useOrder(id, {
9815
+ fields: "metadata"
9952
9816
  });
9953
- useInitiateOrderEdit({ preview });
9954
- const { draft_order, isPending, isError, error } = useDraftOrder(
9955
- id,
9956
- {
9957
- fields: "currency_code"
9958
- },
9959
- {
9960
- enabled: !!id
9961
- }
9962
- );
9963
- const { onCancel } = useCancelOrderEdit({ preview });
9964
9817
  if (isError) {
9965
9818
  throw error;
9966
9819
  }
9967
- if (isPreviewError) {
9968
- throw previewError;
9969
- }
9970
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
9971
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
9972
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
9973
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
9974
- ] }) });
9820
+ const isReady = !isPending && !!order;
9821
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9822
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9823
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9824
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9825
+ ] }),
9826
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9827
+ ] });
9975
9828
  };
9976
- const ItemsForm = ({ preview, currencyCode }) => {
9977
- var _a;
9978
- const [isSubmitting, setIsSubmitting] = useState(false);
9979
- const [modalContent, setModalContent] = useState(
9980
- null
9981
- );
9829
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9830
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9831
+ const MetadataForm = ({ orderId, metadata }) => {
9982
9832
  const { handleSuccess } = useRouteModal();
9983
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
9984
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
9985
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
9986
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
9987
- const matches = useMemo(() => {
9988
- return matchSorter(preview.items, query2, {
9989
- keys: ["product_title", "variant_title", "variant_sku", "title"]
9990
- });
9991
- }, [preview.items, query2]);
9992
- const onSubmit = async () => {
9993
- setIsSubmitting(true);
9994
- let requestSucceeded = false;
9995
- await requestOrderEdit(void 0, {
9996
- onError: (e) => {
9997
- toast.error(`Failed to request order edit: ${e.message}`);
9998
- },
9999
- onSuccess: () => {
10000
- requestSucceeded = true;
10001
- }
10002
- });
10003
- if (!requestSucceeded) {
10004
- setIsSubmitting(false);
9833
+ const hasUneditableRows = getHasUneditableRows(metadata);
9834
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9835
+ const form = useForm({
9836
+ defaultValues: {
9837
+ metadata: getDefaultValues(metadata)
9838
+ },
9839
+ resolver: zodResolver(MetadataSchema)
9840
+ });
9841
+ const handleSubmit = form.handleSubmit(async (data) => {
9842
+ const parsedData = parseValues(data);
9843
+ await mutateAsync(
9844
+ {
9845
+ metadata: parsedData
9846
+ },
9847
+ {
9848
+ onSuccess: () => {
9849
+ toast.success("Metadata updated");
9850
+ handleSuccess();
9851
+ },
9852
+ onError: (error) => {
9853
+ toast.error(error.message);
9854
+ }
9855
+ }
9856
+ );
9857
+ });
9858
+ const { fields, insert, remove } = useFieldArray({
9859
+ control: form.control,
9860
+ name: "metadata"
9861
+ });
9862
+ function deleteRow(index) {
9863
+ remove(index);
9864
+ if (fields.length === 1) {
9865
+ insert(0, {
9866
+ key: "",
9867
+ value: "",
9868
+ disabled: false
9869
+ });
9870
+ }
9871
+ }
9872
+ function insertRow(index, position) {
9873
+ insert(index + (position === "above" ? 0 : 1), {
9874
+ key: "",
9875
+ value: "",
9876
+ disabled: false
9877
+ });
9878
+ }
9879
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9880
+ KeyboundForm,
9881
+ {
9882
+ onSubmit: handleSubmit,
9883
+ className: "flex flex-1 flex-col overflow-hidden",
9884
+ children: [
9885
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9886
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9887
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9888
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
9889
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
9890
+ ] }),
9891
+ fields.map((field, index) => {
9892
+ const isDisabled = field.disabled || false;
9893
+ let placeholder = "-";
9894
+ if (typeof field.value === "object") {
9895
+ placeholder = "{ ... }";
9896
+ }
9897
+ if (Array.isArray(field.value)) {
9898
+ placeholder = "[ ... ]";
9899
+ }
9900
+ return /* @__PURE__ */ jsx(
9901
+ ConditionalTooltip,
9902
+ {
9903
+ showTooltip: isDisabled,
9904
+ content: "This row is disabled because it contains non-primitive data.",
9905
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9906
+ /* @__PURE__ */ jsxs(
9907
+ "div",
9908
+ {
9909
+ className: clx("grid grid-cols-2 divide-x", {
9910
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9911
+ }),
9912
+ children: [
9913
+ /* @__PURE__ */ jsx(
9914
+ Form$2.Field,
9915
+ {
9916
+ control: form.control,
9917
+ name: `metadata.${index}.key`,
9918
+ render: ({ field: field2 }) => {
9919
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9920
+ GridInput,
9921
+ {
9922
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
9923
+ ...field2,
9924
+ disabled: isDisabled,
9925
+ placeholder: "Key"
9926
+ }
9927
+ ) }) });
9928
+ }
9929
+ }
9930
+ ),
9931
+ /* @__PURE__ */ jsx(
9932
+ Form$2.Field,
9933
+ {
9934
+ control: form.control,
9935
+ name: `metadata.${index}.value`,
9936
+ render: ({ field: { value, ...field2 } }) => {
9937
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9938
+ GridInput,
9939
+ {
9940
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
9941
+ ...field2,
9942
+ value: isDisabled ? placeholder : value,
9943
+ disabled: isDisabled,
9944
+ placeholder: "Value"
9945
+ }
9946
+ ) }) });
9947
+ }
9948
+ }
9949
+ )
9950
+ ]
9951
+ }
9952
+ ),
9953
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
9954
+ /* @__PURE__ */ jsx(
9955
+ DropdownMenu.Trigger,
9956
+ {
9957
+ className: clx(
9958
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9959
+ {
9960
+ hidden: isDisabled
9961
+ }
9962
+ ),
9963
+ disabled: isDisabled,
9964
+ asChild: true,
9965
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
9966
+ }
9967
+ ),
9968
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
9969
+ /* @__PURE__ */ jsxs(
9970
+ DropdownMenu.Item,
9971
+ {
9972
+ className: "gap-x-2",
9973
+ onClick: () => insertRow(index, "above"),
9974
+ children: [
9975
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
9976
+ "Insert row above"
9977
+ ]
9978
+ }
9979
+ ),
9980
+ /* @__PURE__ */ jsxs(
9981
+ DropdownMenu.Item,
9982
+ {
9983
+ className: "gap-x-2",
9984
+ onClick: () => insertRow(index, "below"),
9985
+ children: [
9986
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
9987
+ "Insert row below"
9988
+ ]
9989
+ }
9990
+ ),
9991
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
9992
+ /* @__PURE__ */ jsxs(
9993
+ DropdownMenu.Item,
9994
+ {
9995
+ className: "gap-x-2",
9996
+ onClick: () => deleteRow(index),
9997
+ children: [
9998
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
9999
+ "Delete row"
10000
+ ]
10001
+ }
10002
+ )
10003
+ ] })
10004
+ ] })
10005
+ ] })
10006
+ },
10007
+ field.id
10008
+ );
10009
+ })
10010
+ ] }),
10011
+ hasUneditableRows && /* @__PURE__ */ 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." })
10012
+ ] }),
10013
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10014
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10015
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10016
+ ] }) })
10017
+ ]
10018
+ }
10019
+ ) });
10020
+ };
10021
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
10022
+ return /* @__PURE__ */ jsx(
10023
+ "input",
10024
+ {
10025
+ ref,
10026
+ ...props,
10027
+ autoComplete: "off",
10028
+ className: clx(
10029
+ "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",
10030
+ className
10031
+ )
10032
+ }
10033
+ );
10034
+ });
10035
+ GridInput.displayName = "MetadataForm.GridInput";
10036
+ const PlaceholderInner = () => {
10037
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10038
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10039
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10040
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
10041
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
10042
+ ] }) })
10043
+ ] });
10044
+ };
10045
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
10046
+ function getDefaultValues(metadata) {
10047
+ if (!metadata || !Object.keys(metadata).length) {
10048
+ return [
10049
+ {
10050
+ key: "",
10051
+ value: "",
10052
+ disabled: false
10053
+ }
10054
+ ];
10055
+ }
10056
+ return Object.entries(metadata).map(([key, value]) => {
10057
+ if (!EDITABLE_TYPES.includes(typeof value)) {
10058
+ return {
10059
+ key,
10060
+ value,
10061
+ disabled: true
10062
+ };
10063
+ }
10064
+ let stringValue = value;
10065
+ if (typeof value !== "string") {
10066
+ stringValue = JSON.stringify(value);
10067
+ }
10068
+ return {
10069
+ key,
10070
+ value: stringValue,
10071
+ original_key: key
10072
+ };
10073
+ });
10074
+ }
10075
+ function parseValues(values) {
10076
+ const metadata = values.metadata;
10077
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10078
+ if (isEmpty) {
10079
+ return null;
10080
+ }
10081
+ const update = {};
10082
+ metadata.forEach((field) => {
10083
+ let key = field.key;
10084
+ let value = field.value;
10085
+ const disabled = field.disabled;
10086
+ if (!key || !value) {
10087
+ return;
10088
+ }
10089
+ if (disabled) {
10090
+ update[key] = value;
10091
+ return;
10092
+ }
10093
+ key = key.trim();
10094
+ value = value.trim();
10095
+ if (value === "true") {
10096
+ update[key] = true;
10097
+ } else if (value === "false") {
10098
+ update[key] = false;
10099
+ } else {
10100
+ const parsedNumber = parseFloat(value);
10101
+ if (!isNaN(parsedNumber)) {
10102
+ update[key] = parsedNumber;
10103
+ } else {
10104
+ update[key] = value;
10105
+ }
10106
+ }
10107
+ });
10108
+ return update;
10109
+ }
10110
+ function getHasUneditableRows(metadata) {
10111
+ if (!metadata) {
10112
+ return false;
10113
+ }
10114
+ return Object.values(metadata).some(
10115
+ (value) => !EDITABLE_TYPES.includes(typeof value)
10116
+ );
10117
+ }
10118
+ const NumberInput = forwardRef(
10119
+ ({
10120
+ value,
10121
+ onChange,
10122
+ size = "base",
10123
+ min = 0,
10124
+ max = 100,
10125
+ step = 1,
10126
+ className,
10127
+ disabled,
10128
+ ...props
10129
+ }, ref) => {
10130
+ const handleChange = (event) => {
10131
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
10132
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
10133
+ onChange(newValue);
10134
+ }
10135
+ };
10136
+ const handleIncrement = () => {
10137
+ const newValue = value + step;
10138
+ if (max === void 0 || newValue <= max) {
10139
+ onChange(newValue);
10140
+ }
10141
+ };
10142
+ const handleDecrement = () => {
10143
+ const newValue = value - step;
10144
+ if (min === void 0 || newValue >= min) {
10145
+ onChange(newValue);
10146
+ }
10147
+ };
10148
+ return /* @__PURE__ */ jsxs(
10149
+ "div",
10150
+ {
10151
+ className: clx(
10152
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
10153
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
10154
+ {
10155
+ "h-7": size === "small",
10156
+ "h-8": size === "base"
10157
+ },
10158
+ className
10159
+ ),
10160
+ children: [
10161
+ /* @__PURE__ */ jsx(
10162
+ "input",
10163
+ {
10164
+ ref,
10165
+ type: "number",
10166
+ value,
10167
+ onChange: handleChange,
10168
+ min,
10169
+ max,
10170
+ step,
10171
+ className: clx(
10172
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
10173
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
10174
+ "placeholder:text-ui-fg-muted"
10175
+ ),
10176
+ ...props
10177
+ }
10178
+ ),
10179
+ /* @__PURE__ */ jsxs(
10180
+ "button",
10181
+ {
10182
+ className: clx(
10183
+ "flex items-center justify-center outline-none transition-fg",
10184
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10185
+ "focus:bg-ui-bg-field-component-hover",
10186
+ "hover:bg-ui-bg-field-component-hover",
10187
+ {
10188
+ "size-7": size === "small",
10189
+ "size-8": size === "base"
10190
+ }
10191
+ ),
10192
+ type: "button",
10193
+ onClick: handleDecrement,
10194
+ disabled: min !== void 0 && value <= min || disabled,
10195
+ children: [
10196
+ /* @__PURE__ */ jsx(Minus, {}),
10197
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
10198
+ ]
10199
+ }
10200
+ ),
10201
+ /* @__PURE__ */ jsxs(
10202
+ "button",
10203
+ {
10204
+ className: clx(
10205
+ "flex items-center justify-center outline-none transition-fg",
10206
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10207
+ "focus:bg-ui-bg-field-hover",
10208
+ "hover:bg-ui-bg-field-hover",
10209
+ {
10210
+ "size-7": size === "small",
10211
+ "size-8": size === "base"
10212
+ }
10213
+ ),
10214
+ type: "button",
10215
+ onClick: handleIncrement,
10216
+ disabled: max !== void 0 && value >= max || disabled,
10217
+ children: [
10218
+ /* @__PURE__ */ jsx(Plus, {}),
10219
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
10220
+ ]
10221
+ }
10222
+ )
10223
+ ]
10224
+ }
10225
+ );
10226
+ }
10227
+ );
10228
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
10229
+ const productVariantsQueryKeys = {
10230
+ list: (query2) => [
10231
+ PRODUCT_VARIANTS_QUERY_KEY,
10232
+ query2 ? query2 : void 0
10233
+ ]
10234
+ };
10235
+ const useProductVariants = (query2, options) => {
10236
+ const { data, ...rest } = useQuery({
10237
+ queryKey: productVariantsQueryKeys.list(query2),
10238
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
10239
+ ...options
10240
+ });
10241
+ return { ...data, ...rest };
10242
+ };
10243
+ const useCancelOrderEdit = ({ preview }) => {
10244
+ const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10245
+ const onCancel = useCallback(async () => {
10246
+ if (!preview) {
10247
+ return true;
10248
+ }
10249
+ let res = false;
10250
+ await cancelOrderEdit(void 0, {
10251
+ onError: (e) => {
10252
+ toast.error(e.message);
10253
+ },
10254
+ onSuccess: () => {
10255
+ res = true;
10256
+ }
10257
+ });
10258
+ return res;
10259
+ }, [preview, cancelOrderEdit]);
10260
+ return { onCancel };
10261
+ };
10262
+ let IS_REQUEST_RUNNING = false;
10263
+ const useInitiateOrderEdit = ({
10264
+ preview
10265
+ }) => {
10266
+ const navigate = useNavigate();
10267
+ const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10268
+ useEffect(() => {
10269
+ async function run() {
10270
+ if (IS_REQUEST_RUNNING || !preview) {
10271
+ return;
10272
+ }
10273
+ if (preview.order_change) {
10274
+ return;
10275
+ }
10276
+ IS_REQUEST_RUNNING = true;
10277
+ await mutateAsync(void 0, {
10278
+ onError: (e) => {
10279
+ toast.error(e.message);
10280
+ navigate(`/draft-orders/${preview.id}`, { replace: true });
10281
+ return;
10282
+ }
10283
+ });
10284
+ IS_REQUEST_RUNNING = false;
10285
+ }
10286
+ run();
10287
+ }, [preview, navigate, mutateAsync]);
10288
+ };
10289
+ function convertNumber(value) {
10290
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10291
+ }
10292
+ const STACKED_MODAL_ID = "items_stacked_modal";
10293
+ const Items = () => {
10294
+ const { id } = useParams();
10295
+ const {
10296
+ order: preview,
10297
+ isPending: isPreviewPending,
10298
+ isError: isPreviewError,
10299
+ error: previewError
10300
+ } = useOrderPreview(id, void 0, {
10301
+ placeholderData: keepPreviousData
10302
+ });
10303
+ useInitiateOrderEdit({ preview });
10304
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10305
+ id,
10306
+ {
10307
+ fields: "currency_code"
10308
+ },
10309
+ {
10310
+ enabled: !!id
10311
+ }
10312
+ );
10313
+ const { onCancel } = useCancelOrderEdit({ preview });
10314
+ if (isError) {
10315
+ throw error;
10316
+ }
10317
+ if (isPreviewError) {
10318
+ throw previewError;
10319
+ }
10320
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10321
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
10322
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10323
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10324
+ ] }) });
10325
+ };
10326
+ const ItemsForm = ({ preview, currencyCode }) => {
10327
+ var _a;
10328
+ const [isSubmitting, setIsSubmitting] = useState(false);
10329
+ const [modalContent, setModalContent] = useState(
10330
+ null
10331
+ );
10332
+ const { handleSuccess } = useRouteModal();
10333
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10334
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10335
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10336
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10337
+ const matches = useMemo(() => {
10338
+ return matchSorter(preview.items, query2, {
10339
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
10340
+ });
10341
+ }, [preview.items, query2]);
10342
+ const onSubmit = async () => {
10343
+ setIsSubmitting(true);
10344
+ let requestSucceeded = false;
10345
+ await requestOrderEdit(void 0, {
10346
+ onError: (e) => {
10347
+ toast.error(`Failed to request order edit: ${e.message}`);
10348
+ },
10349
+ onSuccess: () => {
10350
+ requestSucceeded = true;
10351
+ }
10352
+ });
10353
+ if (!requestSucceeded) {
10354
+ setIsSubmitting(false);
10005
10355
  return;
10006
10356
  }
10007
10357
  await confirmOrderEdit(void 0, {
@@ -10631,532 +10981,114 @@ const CustomItemForm = ({ orderId, currencyCode }) => {
10631
10981
  const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10632
10982
  const form = useForm({
10633
10983
  defaultValues: {
10634
- title: "",
10635
- quantity: 1,
10636
- unit_price: ""
10637
- },
10638
- resolver: zodResolver(customItemSchema)
10639
- });
10640
- const onSubmit = form.handleSubmit(async (data) => {
10641
- await addItems(
10642
- {
10643
- items: [
10644
- {
10645
- title: data.title,
10646
- quantity: data.quantity,
10647
- unit_price: convertNumber(data.unit_price)
10648
- }
10649
- ]
10650
- },
10651
- {
10652
- onSuccess: () => {
10653
- setIsOpen(STACKED_MODAL_ID, false);
10654
- },
10655
- onError: (e) => {
10656
- toast.error(e.message);
10657
- }
10658
- }
10659
- );
10660
- });
10661
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
10662
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
10663
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
10664
- /* @__PURE__ */ jsxs("div", { children: [
10665
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
10666
- /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(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." }) })
10667
- ] }),
10668
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10669
- /* @__PURE__ */ jsx(
10670
- Form$2.Field,
10671
- {
10672
- control: form.control,
10673
- name: "title",
10674
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10675
- /* @__PURE__ */ jsxs("div", { children: [
10676
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
10677
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
10678
- ] }),
10679
- /* @__PURE__ */ jsxs("div", { children: [
10680
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10681
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10682
- ] })
10683
- ] }) })
10684
- }
10685
- ),
10686
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10687
- /* @__PURE__ */ jsx(
10688
- Form$2.Field,
10689
- {
10690
- control: form.control,
10691
- name: "unit_price",
10692
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10693
- /* @__PURE__ */ jsxs("div", { children: [
10694
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
10695
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10696
- ] }),
10697
- /* @__PURE__ */ jsxs("div", { children: [
10698
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10699
- CurrencyInput,
10700
- {
10701
- symbol: getNativeSymbol(currencyCode),
10702
- code: currencyCode,
10703
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10704
- ...field
10705
- }
10706
- ) }),
10707
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10708
- ] })
10709
- ] }) })
10710
- }
10711
- ),
10712
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10713
- /* @__PURE__ */ jsx(
10714
- Form$2.Field,
10715
- {
10716
- control: form.control,
10717
- name: "quantity",
10718
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10719
- /* @__PURE__ */ jsxs("div", { children: [
10720
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
10721
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10722
- ] }),
10723
- /* @__PURE__ */ jsxs("div", { className: "flex-1 w-full", children: [
10724
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10725
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10726
- ] })
10727
- ] }) })
10728
- }
10729
- )
10730
- ] }) }) }),
10731
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
10732
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10733
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10734
- ] }) })
10735
- ] }) }) });
10736
- };
10737
- const customItemSchema = objectType({
10738
- title: stringType().min(1),
10739
- quantity: numberType(),
10740
- unit_price: unionType([numberType(), stringType()])
10741
- });
10742
- const Email = () => {
10743
- const { id } = useParams();
10744
- const { order, isPending, isError, error } = useOrder(id, {
10745
- fields: "+email"
10746
- });
10747
- if (isError) {
10748
- throw error;
10749
- }
10750
- const isReady = !isPending && !!order;
10751
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10752
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10753
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
10754
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
10755
- ] }),
10756
- isReady && /* @__PURE__ */ jsx(EmailForm, { order })
10757
- ] });
10758
- };
10759
- const EmailForm = ({ order }) => {
10760
- const form = useForm({
10761
- defaultValues: {
10762
- email: order.email ?? ""
10763
- },
10764
- resolver: zodResolver(schema$3)
10765
- });
10766
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10767
- const { handleSuccess } = useRouteModal();
10768
- const onSubmit = form.handleSubmit(async (data) => {
10769
- await mutateAsync(
10770
- { email: data.email },
10771
- {
10772
- onSuccess: () => {
10773
- handleSuccess();
10774
- },
10775
- onError: (error) => {
10776
- toast.error(error.message);
10777
- }
10778
- }
10779
- );
10780
- });
10781
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10782
- KeyboundForm,
10783
- {
10784
- className: "flex flex-1 flex-col overflow-hidden",
10785
- onSubmit,
10786
- children: [
10787
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
10788
- Form$2.Field,
10789
- {
10790
- control: form.control,
10791
- name: "email",
10792
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
10793
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
10794
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10795
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10796
- ] })
10797
- }
10798
- ) }),
10799
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10800
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10801
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10802
- ] }) })
10803
- ]
10804
- }
10805
- ) });
10806
- };
10807
- const schema$3 = objectType({
10808
- email: stringType().email()
10809
- });
10810
- const InlineTip = forwardRef(
10811
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10812
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10813
- return /* @__PURE__ */ jsxs(
10814
- "div",
10815
- {
10816
- ref,
10817
- className: clx(
10818
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10819
- className
10820
- ),
10821
- ...props,
10822
- children: [
10823
- /* @__PURE__ */ jsx(
10824
- "div",
10825
- {
10826
- role: "presentation",
10827
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10828
- "bg-ui-tag-orange-icon": variant === "warning"
10829
- })
10830
- }
10831
- ),
10832
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
10833
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10834
- labelValue,
10835
- ":"
10836
- ] }),
10837
- " ",
10838
- children
10839
- ] })
10840
- ]
10841
- }
10842
- );
10843
- }
10844
- );
10845
- InlineTip.displayName = "InlineTip";
10846
- const MetadataFieldSchema = objectType({
10847
- key: stringType(),
10848
- disabled: booleanType().optional(),
10849
- value: anyType()
10850
- });
10851
- const MetadataSchema = objectType({
10852
- metadata: arrayType(MetadataFieldSchema)
10853
- });
10854
- const Metadata = () => {
10855
- const { id } = useParams();
10856
- const { order, isPending, isError, error } = useOrder(id, {
10857
- fields: "metadata"
10858
- });
10859
- if (isError) {
10860
- throw error;
10861
- }
10862
- const isReady = !isPending && !!order;
10863
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10864
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10865
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
10866
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10867
- ] }),
10868
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10869
- ] });
10870
- };
10871
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10872
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10873
- const MetadataForm = ({ orderId, metadata }) => {
10874
- const { handleSuccess } = useRouteModal();
10875
- const hasUneditableRows = getHasUneditableRows(metadata);
10876
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10877
- const form = useForm({
10878
- defaultValues: {
10879
- metadata: getDefaultValues(metadata)
10984
+ title: "",
10985
+ quantity: 1,
10986
+ unit_price: ""
10880
10987
  },
10881
- resolver: zodResolver(MetadataSchema)
10988
+ resolver: zodResolver(customItemSchema)
10882
10989
  });
10883
- const handleSubmit = form.handleSubmit(async (data) => {
10884
- const parsedData = parseValues(data);
10885
- await mutateAsync(
10990
+ const onSubmit = form.handleSubmit(async (data) => {
10991
+ await addItems(
10886
10992
  {
10887
- metadata: parsedData
10993
+ items: [
10994
+ {
10995
+ title: data.title,
10996
+ quantity: data.quantity,
10997
+ unit_price: convertNumber(data.unit_price)
10998
+ }
10999
+ ]
10888
11000
  },
10889
11001
  {
10890
11002
  onSuccess: () => {
10891
- toast.success("Metadata updated");
10892
- handleSuccess();
11003
+ setIsOpen(STACKED_MODAL_ID, false);
10893
11004
  },
10894
- onError: (error) => {
10895
- toast.error(error.message);
11005
+ onError: (e) => {
11006
+ toast.error(e.message);
10896
11007
  }
10897
11008
  }
10898
11009
  );
10899
11010
  });
10900
- const { fields, insert, remove } = useFieldArray({
10901
- control: form.control,
10902
- name: "metadata"
10903
- });
10904
- function deleteRow(index) {
10905
- remove(index);
10906
- if (fields.length === 1) {
10907
- insert(0, {
10908
- key: "",
10909
- value: "",
10910
- disabled: false
10911
- });
10912
- }
10913
- }
10914
- function insertRow(index, position) {
10915
- insert(index + (position === "above" ? 0 : 1), {
10916
- key: "",
10917
- value: "",
10918
- disabled: false
10919
- });
10920
- }
10921
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10922
- KeyboundForm,
10923
- {
10924
- onSubmit: handleSubmit,
10925
- className: "flex flex-1 flex-col overflow-hidden",
10926
- children: [
10927
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10928
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10929
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10930
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
10931
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
11011
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
11012
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
11013
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
11014
+ /* @__PURE__ */ jsxs("div", { children: [
11015
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
11016
+ /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(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." }) })
11017
+ ] }),
11018
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11019
+ /* @__PURE__ */ jsx(
11020
+ Form$2.Field,
11021
+ {
11022
+ control: form.control,
11023
+ name: "title",
11024
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11025
+ /* @__PURE__ */ jsxs("div", { children: [
11026
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
11027
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
10932
11028
  ] }),
10933
- fields.map((field, index) => {
10934
- const isDisabled = field.disabled || false;
10935
- let placeholder = "-";
10936
- if (typeof field.value === "object") {
10937
- placeholder = "{ ... }";
10938
- }
10939
- if (Array.isArray(field.value)) {
10940
- placeholder = "[ ... ]";
10941
- }
10942
- return /* @__PURE__ */ jsx(
10943
- ConditionalTooltip,
11029
+ /* @__PURE__ */ jsxs("div", { children: [
11030
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
11031
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11032
+ ] })
11033
+ ] }) })
11034
+ }
11035
+ ),
11036
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11037
+ /* @__PURE__ */ jsx(
11038
+ Form$2.Field,
11039
+ {
11040
+ control: form.control,
11041
+ name: "unit_price",
11042
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11043
+ /* @__PURE__ */ jsxs("div", { children: [
11044
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
11045
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
11046
+ ] }),
11047
+ /* @__PURE__ */ jsxs("div", { children: [
11048
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11049
+ CurrencyInput,
10944
11050
  {
10945
- showTooltip: isDisabled,
10946
- content: "This row is disabled because it contains non-primitive data.",
10947
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
10948
- /* @__PURE__ */ jsxs(
10949
- "div",
10950
- {
10951
- className: clx("grid grid-cols-2 divide-x", {
10952
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10953
- }),
10954
- children: [
10955
- /* @__PURE__ */ jsx(
10956
- Form$2.Field,
10957
- {
10958
- control: form.control,
10959
- name: `metadata.${index}.key`,
10960
- render: ({ field: field2 }) => {
10961
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10962
- GridInput,
10963
- {
10964
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10965
- ...field2,
10966
- disabled: isDisabled,
10967
- placeholder: "Key"
10968
- }
10969
- ) }) });
10970
- }
10971
- }
10972
- ),
10973
- /* @__PURE__ */ jsx(
10974
- Form$2.Field,
10975
- {
10976
- control: form.control,
10977
- name: `metadata.${index}.value`,
10978
- render: ({ field: { value, ...field2 } }) => {
10979
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10980
- GridInput,
10981
- {
10982
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
10983
- ...field2,
10984
- value: isDisabled ? placeholder : value,
10985
- disabled: isDisabled,
10986
- placeholder: "Value"
10987
- }
10988
- ) }) });
10989
- }
10990
- }
10991
- )
10992
- ]
10993
- }
10994
- ),
10995
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
10996
- /* @__PURE__ */ jsx(
10997
- DropdownMenu.Trigger,
10998
- {
10999
- className: clx(
11000
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11001
- {
11002
- hidden: isDisabled
11003
- }
11004
- ),
11005
- disabled: isDisabled,
11006
- asChild: true,
11007
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
11008
- }
11009
- ),
11010
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
11011
- /* @__PURE__ */ jsxs(
11012
- DropdownMenu.Item,
11013
- {
11014
- className: "gap-x-2",
11015
- onClick: () => insertRow(index, "above"),
11016
- children: [
11017
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
11018
- "Insert row above"
11019
- ]
11020
- }
11021
- ),
11022
- /* @__PURE__ */ jsxs(
11023
- DropdownMenu.Item,
11024
- {
11025
- className: "gap-x-2",
11026
- onClick: () => insertRow(index, "below"),
11027
- children: [
11028
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
11029
- "Insert row below"
11030
- ]
11031
- }
11032
- ),
11033
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
11034
- /* @__PURE__ */ jsxs(
11035
- DropdownMenu.Item,
11036
- {
11037
- className: "gap-x-2",
11038
- onClick: () => deleteRow(index),
11039
- children: [
11040
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
11041
- "Delete row"
11042
- ]
11043
- }
11044
- )
11045
- ] })
11046
- ] })
11047
- ] })
11048
- },
11049
- field.id
11050
- );
11051
- })
11052
- ] }),
11053
- hasUneditableRows && /* @__PURE__ */ 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." })
11054
- ] }),
11055
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11056
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11057
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11058
- ] }) })
11059
- ]
11060
- }
11061
- ) });
11062
- };
11063
- const GridInput = forwardRef(({ className, ...props }, ref) => {
11064
- return /* @__PURE__ */ jsx(
11065
- "input",
11066
- {
11067
- ref,
11068
- ...props,
11069
- autoComplete: "off",
11070
- className: clx(
11071
- "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",
11072
- className
11051
+ symbol: getNativeSymbol(currencyCode),
11052
+ code: currencyCode,
11053
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
11054
+ ...field
11055
+ }
11056
+ ) }),
11057
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11058
+ ] })
11059
+ ] }) })
11060
+ }
11061
+ ),
11062
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11063
+ /* @__PURE__ */ jsx(
11064
+ Form$2.Field,
11065
+ {
11066
+ control: form.control,
11067
+ name: "quantity",
11068
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11069
+ /* @__PURE__ */ jsxs("div", { children: [
11070
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
11071
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
11072
+ ] }),
11073
+ /* @__PURE__ */ jsxs("div", { className: "flex-1 w-full", children: [
11074
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "flex-1 w-full", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
11075
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11076
+ ] })
11077
+ ] }) })
11078
+ }
11073
11079
  )
11074
- }
11075
- );
11076
- });
11077
- GridInput.displayName = "MetadataForm.GridInput";
11078
- const PlaceholderInner = () => {
11079
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11080
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11081
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11082
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11083
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11080
+ ] }) }) }),
11081
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2 justify-end", children: [
11082
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11083
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
11084
11084
  ] }) })
11085
- ] });
11085
+ ] }) }) });
11086
11086
  };
11087
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11088
- function getDefaultValues(metadata) {
11089
- if (!metadata || !Object.keys(metadata).length) {
11090
- return [
11091
- {
11092
- key: "",
11093
- value: "",
11094
- disabled: false
11095
- }
11096
- ];
11097
- }
11098
- return Object.entries(metadata).map(([key, value]) => {
11099
- if (!EDITABLE_TYPES.includes(typeof value)) {
11100
- return {
11101
- key,
11102
- value,
11103
- disabled: true
11104
- };
11105
- }
11106
- let stringValue = value;
11107
- if (typeof value !== "string") {
11108
- stringValue = JSON.stringify(value);
11109
- }
11110
- return {
11111
- key,
11112
- value: stringValue,
11113
- original_key: key
11114
- };
11115
- });
11116
- }
11117
- function parseValues(values) {
11118
- const metadata = values.metadata;
11119
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11120
- if (isEmpty) {
11121
- return null;
11122
- }
11123
- const update = {};
11124
- metadata.forEach((field) => {
11125
- let key = field.key;
11126
- let value = field.value;
11127
- const disabled = field.disabled;
11128
- if (!key || !value) {
11129
- return;
11130
- }
11131
- if (disabled) {
11132
- update[key] = value;
11133
- return;
11134
- }
11135
- key = key.trim();
11136
- value = value.trim();
11137
- if (value === "true") {
11138
- update[key] = true;
11139
- } else if (value === "false") {
11140
- update[key] = false;
11141
- } else {
11142
- const parsedNumber = parseFloat(value);
11143
- if (!isNaN(parsedNumber)) {
11144
- update[key] = parsedNumber;
11145
- } else {
11146
- update[key] = value;
11147
- }
11148
- }
11149
- });
11150
- return update;
11151
- }
11152
- function getHasUneditableRows(metadata) {
11153
- if (!metadata) {
11154
- return false;
11155
- }
11156
- return Object.values(metadata).some(
11157
- (value) => !EDITABLE_TYPES.includes(typeof value)
11158
- );
11159
- }
11087
+ const customItemSchema = objectType({
11088
+ title: stringType().min(1),
11089
+ quantity: numberType(),
11090
+ unit_price: unionType([numberType(), stringType()])
11091
+ });
11160
11092
  const PROMOTION_QUERY_KEY = "promotions";
11161
11093
  const promotionsQueryKeys = {
11162
11094
  list: (query2) => [
@@ -11380,166 +11312,60 @@ const PromotionItem = ({
11380
11312
  }
11381
11313
  )
11382
11314
  ]
11383
- },
11384
- promotion.id
11385
- );
11386
- };
11387
- function getDisplayValue(promotion) {
11388
- var _a, _b, _c, _d;
11389
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11390
- if (!value) {
11391
- return null;
11392
- }
11393
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11394
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11395
- if (!currency) {
11396
- return null;
11397
- }
11398
- return getLocaleAmount(value, currency);
11399
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11400
- return formatPercentage(value);
11401
- }
11402
- return null;
11403
- }
11404
- const formatter = new Intl.NumberFormat([], {
11405
- style: "percent",
11406
- minimumFractionDigits: 2
11407
- });
11408
- const formatPercentage = (value, isPercentageValue = false) => {
11409
- let val = value || 0;
11410
- if (!isPercentageValue) {
11411
- val = val / 100;
11412
- }
11413
- return formatter.format(val);
11414
- };
11415
- function getPromotionCodes(items, shippingMethods) {
11416
- const codes = /* @__PURE__ */ new Set();
11417
- for (const item of items) {
11418
- if (item.adjustments) {
11419
- for (const adjustment of item.adjustments) {
11420
- if (adjustment.code) {
11421
- codes.add(adjustment.code);
11422
- }
11423
- }
11424
- }
11425
- }
11426
- for (const shippingMethod of shippingMethods) {
11427
- if (shippingMethod.adjustments) {
11428
- for (const adjustment of shippingMethod.adjustments) {
11429
- if (adjustment.code) {
11430
- codes.add(adjustment.code);
11431
- }
11432
- }
11433
- }
11434
- }
11435
- return Array.from(codes);
11436
- }
11437
- const SalesChannel = () => {
11438
- const { id } = useParams();
11439
- const { draft_order, isPending, isError, error } = useDraftOrder(
11440
- id,
11441
- {
11442
- fields: "+sales_channel_id"
11443
- },
11444
- {
11445
- enabled: !!id
11446
- }
11447
- );
11448
- if (isError) {
11449
- throw error;
11450
- }
11451
- const ISrEADY = !!draft_order && !isPending;
11452
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11453
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11454
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11455
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11456
- ] }),
11457
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11458
- ] });
11459
- };
11460
- const SalesChannelForm = ({ order }) => {
11461
- const form = useForm({
11462
- defaultValues: {
11463
- sales_channel_id: order.sales_channel_id || ""
11464
- },
11465
- resolver: zodResolver(schema$2)
11466
- });
11467
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11468
- const { handleSuccess } = useRouteModal();
11469
- const onSubmit = form.handleSubmit(async (data) => {
11470
- await mutateAsync(
11471
- {
11472
- sales_channel_id: data.sales_channel_id
11473
- },
11474
- {
11475
- onSuccess: () => {
11476
- toast.success("Sales channel updated");
11477
- handleSuccess();
11478
- },
11479
- onError: (error) => {
11480
- toast.error(error.message);
11481
- }
11482
- }
11483
- );
11484
- });
11485
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11486
- KeyboundForm,
11487
- {
11488
- className: "flex flex-1 flex-col overflow-hidden",
11489
- onSubmit,
11490
- children: [
11491
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11492
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11493
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11494
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11495
- ] }) })
11496
- ]
11497
- }
11498
- ) });
11499
- };
11500
- const SalesChannelField = ({ control, order }) => {
11501
- const salesChannels = useComboboxData({
11502
- queryFn: async (params) => {
11503
- return await sdk.admin.salesChannel.list(params);
11504
- },
11505
- queryKey: ["sales-channels"],
11506
- getOptions: (data) => {
11507
- return data.sales_channels.map((salesChannel) => ({
11508
- label: salesChannel.name,
11509
- value: salesChannel.id
11510
- }));
11511
- },
11512
- defaultValue: order.sales_channel_id || void 0
11513
- });
11514
- return /* @__PURE__ */ jsx(
11515
- Form$2.Field,
11516
- {
11517
- control,
11518
- name: "sales_channel_id",
11519
- render: ({ field }) => {
11520
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11521
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11522
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11523
- Combobox,
11524
- {
11525
- options: salesChannels.options,
11526
- fetchNextPage: salesChannels.fetchNextPage,
11527
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11528
- searchValue: salesChannels.searchValue,
11529
- onSearchValueChange: salesChannels.onSearchValueChange,
11530
- placeholder: "Select sales channel",
11531
- ...field
11532
- }
11533
- ) }),
11534
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11535
- ] });
11536
- }
11537
- }
11315
+ },
11316
+ promotion.id
11538
11317
  );
11539
11318
  };
11540
- const schema$2 = objectType({
11541
- sales_channel_id: stringType().min(1)
11319
+ function getDisplayValue(promotion) {
11320
+ var _a, _b, _c, _d;
11321
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11322
+ if (!value) {
11323
+ return null;
11324
+ }
11325
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11326
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11327
+ if (!currency) {
11328
+ return null;
11329
+ }
11330
+ return getLocaleAmount(value, currency);
11331
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11332
+ return formatPercentage(value);
11333
+ }
11334
+ return null;
11335
+ }
11336
+ const formatter = new Intl.NumberFormat([], {
11337
+ style: "percent",
11338
+ minimumFractionDigits: 2
11542
11339
  });
11340
+ const formatPercentage = (value, isPercentageValue = false) => {
11341
+ let val = value || 0;
11342
+ if (!isPercentageValue) {
11343
+ val = val / 100;
11344
+ }
11345
+ return formatter.format(val);
11346
+ };
11347
+ function getPromotionCodes(items, shippingMethods) {
11348
+ const codes = /* @__PURE__ */ new Set();
11349
+ for (const item of items) {
11350
+ if (item.adjustments) {
11351
+ for (const adjustment of item.adjustments) {
11352
+ if (adjustment.code) {
11353
+ codes.add(adjustment.code);
11354
+ }
11355
+ }
11356
+ }
11357
+ }
11358
+ for (const shippingMethod of shippingMethods) {
11359
+ if (shippingMethod.adjustments) {
11360
+ for (const adjustment of shippingMethod.adjustments) {
11361
+ if (adjustment.code) {
11362
+ codes.add(adjustment.code);
11363
+ }
11364
+ }
11365
+ }
11366
+ }
11367
+ return Array.from(codes);
11368
+ }
11543
11369
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
11544
11370
  const Shipping = () => {
11545
11371
  var _a;
@@ -12379,7 +12205,7 @@ const ShippingAddressForm = ({ order }) => {
12379
12205
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12380
12206
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12381
12207
  },
12382
- resolver: zodResolver(schema$1)
12208
+ resolver: zodResolver(schema$3)
12383
12209
  });
12384
12210
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12385
12211
  const { handleSuccess } = useRouteModal();
@@ -12549,7 +12375,7 @@ const ShippingAddressForm = ({ order }) => {
12549
12375
  }
12550
12376
  ) });
12551
12377
  };
12552
- const schema$1 = addressSchema;
12378
+ const schema$3 = addressSchema;
12553
12379
  const TransferOwnership = () => {
12554
12380
  const { id } = useParams();
12555
12381
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12573,7 +12399,7 @@ const TransferOwnershipForm = ({ order }) => {
12573
12399
  defaultValues: {
12574
12400
  customer_id: order.customer_id || ""
12575
12401
  },
12576
- resolver: zodResolver(schema)
12402
+ resolver: zodResolver(schema$2)
12577
12403
  });
12578
12404
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12579
12405
  const { handleSuccess } = useRouteModal();
@@ -13023,9 +12849,183 @@ const Illustration = () => {
13023
12849
  }
13024
12850
  );
13025
12851
  };
13026
- const schema = objectType({
12852
+ const schema$2 = objectType({
13027
12853
  customer_id: stringType().min(1)
13028
12854
  });
12855
+ const SalesChannel = () => {
12856
+ const { id } = useParams();
12857
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12858
+ id,
12859
+ {
12860
+ fields: "+sales_channel_id"
12861
+ },
12862
+ {
12863
+ enabled: !!id
12864
+ }
12865
+ );
12866
+ if (isError) {
12867
+ throw error;
12868
+ }
12869
+ const ISrEADY = !!draft_order && !isPending;
12870
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12871
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12872
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
12873
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12874
+ ] }),
12875
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
12876
+ ] });
12877
+ };
12878
+ const SalesChannelForm = ({ order }) => {
12879
+ const form = useForm({
12880
+ defaultValues: {
12881
+ sales_channel_id: order.sales_channel_id || ""
12882
+ },
12883
+ resolver: zodResolver(schema$1)
12884
+ });
12885
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12886
+ const { handleSuccess } = useRouteModal();
12887
+ const onSubmit = form.handleSubmit(async (data) => {
12888
+ await mutateAsync(
12889
+ {
12890
+ sales_channel_id: data.sales_channel_id
12891
+ },
12892
+ {
12893
+ onSuccess: () => {
12894
+ toast.success("Sales channel updated");
12895
+ handleSuccess();
12896
+ },
12897
+ onError: (error) => {
12898
+ toast.error(error.message);
12899
+ }
12900
+ }
12901
+ );
12902
+ });
12903
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12904
+ KeyboundForm,
12905
+ {
12906
+ className: "flex flex-1 flex-col overflow-hidden",
12907
+ onSubmit,
12908
+ children: [
12909
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
12910
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12911
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12912
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12913
+ ] }) })
12914
+ ]
12915
+ }
12916
+ ) });
12917
+ };
12918
+ const SalesChannelField = ({ control, order }) => {
12919
+ const salesChannels = useComboboxData({
12920
+ queryFn: async (params) => {
12921
+ return await sdk.admin.salesChannel.list(params);
12922
+ },
12923
+ queryKey: ["sales-channels"],
12924
+ getOptions: (data) => {
12925
+ return data.sales_channels.map((salesChannel) => ({
12926
+ label: salesChannel.name,
12927
+ value: salesChannel.id
12928
+ }));
12929
+ },
12930
+ defaultValue: order.sales_channel_id || void 0
12931
+ });
12932
+ return /* @__PURE__ */ jsx(
12933
+ Form$2.Field,
12934
+ {
12935
+ control,
12936
+ name: "sales_channel_id",
12937
+ render: ({ field }) => {
12938
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12939
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
12940
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12941
+ Combobox,
12942
+ {
12943
+ options: salesChannels.options,
12944
+ fetchNextPage: salesChannels.fetchNextPage,
12945
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
12946
+ searchValue: salesChannels.searchValue,
12947
+ onSearchValueChange: salesChannels.onSearchValueChange,
12948
+ placeholder: "Select sales channel",
12949
+ ...field
12950
+ }
12951
+ ) }),
12952
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12953
+ ] });
12954
+ }
12955
+ }
12956
+ );
12957
+ };
12958
+ const schema$1 = objectType({
12959
+ sales_channel_id: stringType().min(1)
12960
+ });
12961
+ const Email = () => {
12962
+ const { id } = useParams();
12963
+ const { order, isPending, isError, error } = useOrder(id, {
12964
+ fields: "+email"
12965
+ });
12966
+ if (isError) {
12967
+ throw error;
12968
+ }
12969
+ const isReady = !isPending && !!order;
12970
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12971
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12972
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
12973
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
12974
+ ] }),
12975
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
12976
+ ] });
12977
+ };
12978
+ const EmailForm = ({ order }) => {
12979
+ const form = useForm({
12980
+ defaultValues: {
12981
+ email: order.email ?? ""
12982
+ },
12983
+ resolver: zodResolver(schema)
12984
+ });
12985
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12986
+ const { handleSuccess } = useRouteModal();
12987
+ const onSubmit = form.handleSubmit(async (data) => {
12988
+ await mutateAsync(
12989
+ { email: data.email },
12990
+ {
12991
+ onSuccess: () => {
12992
+ handleSuccess();
12993
+ },
12994
+ onError: (error) => {
12995
+ toast.error(error.message);
12996
+ }
12997
+ }
12998
+ );
12999
+ });
13000
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
13001
+ KeyboundForm,
13002
+ {
13003
+ className: "flex flex-1 flex-col overflow-hidden",
13004
+ onSubmit,
13005
+ children: [
13006
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
13007
+ Form$2.Field,
13008
+ {
13009
+ control: form.control,
13010
+ name: "email",
13011
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13012
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
13013
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13014
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13015
+ ] })
13016
+ }
13017
+ ) }),
13018
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13019
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13020
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13021
+ ] }) })
13022
+ ]
13023
+ }
13024
+ ) });
13025
+ };
13026
+ const schema = objectType({
13027
+ email: stringType().email()
13028
+ });
13029
13029
  const widgetModule = { widgets: [] };
13030
13030
  const routeModule = {
13031
13031
  routes: [
@@ -13054,25 +13054,17 @@ const routeModule = {
13054
13054
  Component: CustomItems,
13055
13055
  path: "/draft-orders/:id/custom-items"
13056
13056
  },
13057
- {
13058
- Component: Items,
13059
- path: "/draft-orders/:id/items"
13060
- },
13061
- {
13062
- Component: Email,
13063
- path: "/draft-orders/:id/email"
13064
- },
13065
13057
  {
13066
13058
  Component: Metadata,
13067
13059
  path: "/draft-orders/:id/metadata"
13068
13060
  },
13069
13061
  {
13070
- Component: Promotions,
13071
- path: "/draft-orders/:id/promotions"
13062
+ Component: Items,
13063
+ path: "/draft-orders/:id/items"
13072
13064
  },
13073
13065
  {
13074
- Component: SalesChannel,
13075
- path: "/draft-orders/:id/sales-channel"
13066
+ Component: Promotions,
13067
+ path: "/draft-orders/:id/promotions"
13076
13068
  },
13077
13069
  {
13078
13070
  Component: Shipping,
@@ -13085,6 +13077,14 @@ const routeModule = {
13085
13077
  {
13086
13078
  Component: TransferOwnership,
13087
13079
  path: "/draft-orders/:id/transfer-ownership"
13080
+ },
13081
+ {
13082
+ Component: SalesChannel,
13083
+ path: "/draft-orders/:id/sales-channel"
13084
+ },
13085
+ {
13086
+ Component: Email,
13087
+ path: "/draft-orders/:id/email"
13088
13088
  }
13089
13089
  ]
13090
13090
  }