@medusajs/draft-order 2.11.0-preview-20251015120203 → 2.11.0-preview-20251015180203

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.
@@ -9852,795 +9852,604 @@ const EmailForm = ({ order }) => {
9852
9852
  const schema$3 = objectType({
9853
9853
  email: stringType().email()
9854
9854
  });
9855
- const InlineTip = React.forwardRef(
9856
- ({ variant = "tip", label, className, children, ...props }, ref) => {
9857
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9855
+ const NumberInput = React.forwardRef(
9856
+ ({
9857
+ value,
9858
+ onChange,
9859
+ size = "base",
9860
+ min = 0,
9861
+ max = 100,
9862
+ step = 1,
9863
+ className,
9864
+ disabled,
9865
+ ...props
9866
+ }, ref) => {
9867
+ const handleChange = (event) => {
9868
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
9869
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9870
+ onChange(newValue);
9871
+ }
9872
+ };
9873
+ const handleIncrement = () => {
9874
+ const newValue = value + step;
9875
+ if (max === void 0 || newValue <= max) {
9876
+ onChange(newValue);
9877
+ }
9878
+ };
9879
+ const handleDecrement = () => {
9880
+ const newValue = value - step;
9881
+ if (min === void 0 || newValue >= min) {
9882
+ onChange(newValue);
9883
+ }
9884
+ };
9858
9885
  return /* @__PURE__ */ jsxRuntime.jsxs(
9859
9886
  "div",
9860
9887
  {
9861
- ref,
9862
9888
  className: ui.clx(
9863
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9889
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9890
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9891
+ {
9892
+ "h-7": size === "small",
9893
+ "h-8": size === "base"
9894
+ },
9864
9895
  className
9865
9896
  ),
9866
- ...props,
9867
9897
  children: [
9868
9898
  /* @__PURE__ */ jsxRuntime.jsx(
9869
- "div",
9899
+ "input",
9870
9900
  {
9871
- role: "presentation",
9872
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9873
- "bg-ui-tag-orange-icon": variant === "warning"
9874
- })
9901
+ ref,
9902
+ type: "number",
9903
+ value,
9904
+ onChange: handleChange,
9905
+ min,
9906
+ max,
9907
+ step,
9908
+ className: ui.clx(
9909
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9910
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9911
+ "placeholder:text-ui-fg-muted"
9912
+ ),
9913
+ ...props
9875
9914
  }
9876
9915
  ),
9877
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
9878
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9879
- labelValue,
9880
- ":"
9881
- ] }),
9882
- " ",
9883
- children
9884
- ] })
9916
+ /* @__PURE__ */ jsxRuntime.jsxs(
9917
+ "button",
9918
+ {
9919
+ className: ui.clx(
9920
+ "flex items-center justify-center outline-none transition-fg",
9921
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9922
+ "focus:bg-ui-bg-field-component-hover",
9923
+ "hover:bg-ui-bg-field-component-hover",
9924
+ {
9925
+ "size-7": size === "small",
9926
+ "size-8": size === "base"
9927
+ }
9928
+ ),
9929
+ type: "button",
9930
+ onClick: handleDecrement,
9931
+ disabled: min !== void 0 && value <= min || disabled,
9932
+ children: [
9933
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
9934
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9935
+ ]
9936
+ }
9937
+ ),
9938
+ /* @__PURE__ */ jsxRuntime.jsxs(
9939
+ "button",
9940
+ {
9941
+ className: ui.clx(
9942
+ "flex items-center justify-center outline-none transition-fg",
9943
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9944
+ "focus:bg-ui-bg-field-hover",
9945
+ "hover:bg-ui-bg-field-hover",
9946
+ {
9947
+ "size-7": size === "small",
9948
+ "size-8": size === "base"
9949
+ }
9950
+ ),
9951
+ type: "button",
9952
+ onClick: handleIncrement,
9953
+ disabled: max !== void 0 && value >= max || disabled,
9954
+ children: [
9955
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
9956
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9957
+ ]
9958
+ }
9959
+ )
9885
9960
  ]
9886
9961
  }
9887
9962
  );
9888
9963
  }
9889
9964
  );
9890
- InlineTip.displayName = "InlineTip";
9891
- const MetadataFieldSchema = objectType({
9892
- key: stringType(),
9893
- disabled: booleanType().optional(),
9894
- value: anyType()
9895
- });
9896
- const MetadataSchema = objectType({
9897
- metadata: arrayType(MetadataFieldSchema)
9898
- });
9899
- const Metadata = () => {
9965
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9966
+ const productVariantsQueryKeys = {
9967
+ list: (query2) => [
9968
+ PRODUCT_VARIANTS_QUERY_KEY,
9969
+ query2 ? query2 : void 0
9970
+ ]
9971
+ };
9972
+ const useProductVariants = (query2, options) => {
9973
+ const { data, ...rest } = reactQuery.useQuery({
9974
+ queryKey: productVariantsQueryKeys.list(query2),
9975
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
9976
+ ...options
9977
+ });
9978
+ return { ...data, ...rest };
9979
+ };
9980
+ const useCancelOrderEdit = ({ preview }) => {
9981
+ const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9982
+ const onCancel = React.useCallback(async () => {
9983
+ if (!preview) {
9984
+ return true;
9985
+ }
9986
+ let res = false;
9987
+ await cancelOrderEdit(void 0, {
9988
+ onError: (e) => {
9989
+ ui.toast.error(e.message);
9990
+ },
9991
+ onSuccess: () => {
9992
+ res = true;
9993
+ }
9994
+ });
9995
+ return res;
9996
+ }, [preview, cancelOrderEdit]);
9997
+ return { onCancel };
9998
+ };
9999
+ let IS_REQUEST_RUNNING = false;
10000
+ const useInitiateOrderEdit = ({
10001
+ preview
10002
+ }) => {
10003
+ const navigate = reactRouterDom.useNavigate();
10004
+ const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10005
+ React.useEffect(() => {
10006
+ async function run() {
10007
+ if (IS_REQUEST_RUNNING || !preview) {
10008
+ return;
10009
+ }
10010
+ if (preview.order_change) {
10011
+ return;
10012
+ }
10013
+ IS_REQUEST_RUNNING = true;
10014
+ await mutateAsync(void 0, {
10015
+ onError: (e) => {
10016
+ ui.toast.error(e.message);
10017
+ navigate(`/draft-orders/${preview.id}`, { replace: true });
10018
+ return;
10019
+ }
10020
+ });
10021
+ IS_REQUEST_RUNNING = false;
10022
+ }
10023
+ run();
10024
+ }, [preview, navigate, mutateAsync]);
10025
+ };
10026
+ function convertNumber(value) {
10027
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10028
+ }
10029
+ const STACKED_MODAL_ID = "items_stacked_modal";
10030
+ const Items = () => {
9900
10031
  const { id } = reactRouterDom.useParams();
9901
- const { order, isPending, isError, error } = useOrder(id, {
9902
- fields: "metadata"
10032
+ const {
10033
+ order: preview,
10034
+ isPending: isPreviewPending,
10035
+ isError: isPreviewError,
10036
+ error: previewError
10037
+ } = useOrderPreview(id, void 0, {
10038
+ placeholderData: reactQuery.keepPreviousData
9903
10039
  });
10040
+ useInitiateOrderEdit({ preview });
10041
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10042
+ id,
10043
+ {
10044
+ fields: "currency_code"
10045
+ },
10046
+ {
10047
+ enabled: !!id
10048
+ }
10049
+ );
10050
+ const { onCancel } = useCancelOrderEdit({ preview });
9904
10051
  if (isError) {
9905
10052
  throw error;
9906
10053
  }
9907
- const isReady = !isPending && !!order;
9908
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9909
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9910
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
9911
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9912
- ] }),
9913
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9914
- ] });
10054
+ if (isPreviewError) {
10055
+ throw previewError;
10056
+ }
10057
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10058
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10059
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10060
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10061
+ ] }) });
9915
10062
  };
9916
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9917
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9918
- const MetadataForm = ({ orderId, metadata }) => {
10063
+ const ItemsForm = ({ preview, currencyCode }) => {
10064
+ var _a;
10065
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
10066
+ const [modalContent, setModalContent] = React.useState(
10067
+ null
10068
+ );
9919
10069
  const { handleSuccess } = useRouteModal();
9920
- const hasUneditableRows = getHasUneditableRows(metadata);
9921
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9922
- const form = reactHookForm.useForm({
9923
- defaultValues: {
9924
- metadata: getDefaultValues(metadata)
9925
- },
9926
- resolver: zod.zodResolver(MetadataSchema)
9927
- });
9928
- const handleSubmit = form.handleSubmit(async (data) => {
9929
- const parsedData = parseValues(data);
9930
- await mutateAsync(
9931
- {
9932
- metadata: parsedData
9933
- },
9934
- {
9935
- onSuccess: () => {
9936
- ui.toast.success("Metadata updated");
9937
- handleSuccess();
9938
- },
9939
- onError: (error) => {
9940
- ui.toast.error(error.message);
9941
- }
10070
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10071
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10072
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10073
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10074
+ const matches = React.useMemo(() => {
10075
+ return matchSorter.matchSorter(preview.items, query2, {
10076
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
10077
+ });
10078
+ }, [preview.items, query2]);
10079
+ const onSubmit = async () => {
10080
+ setIsSubmitting(true);
10081
+ let requestSucceeded = false;
10082
+ await requestOrderEdit(void 0, {
10083
+ onError: (e) => {
10084
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
10085
+ },
10086
+ onSuccess: () => {
10087
+ requestSucceeded = true;
9942
10088
  }
9943
- );
9944
- });
9945
- const { fields, insert, remove } = reactHookForm.useFieldArray({
9946
- control: form.control,
9947
- name: "metadata"
9948
- });
9949
- function deleteRow(index) {
9950
- remove(index);
9951
- if (fields.length === 1) {
9952
- insert(0, {
9953
- key: "",
9954
- value: "",
9955
- disabled: false
9956
- });
10089
+ });
10090
+ if (!requestSucceeded) {
10091
+ setIsSubmitting(false);
10092
+ return;
9957
10093
  }
9958
- }
9959
- function insertRow(index, position) {
9960
- insert(index + (position === "above" ? 0 : 1), {
9961
- key: "",
9962
- value: "",
9963
- disabled: false
10094
+ await confirmOrderEdit(void 0, {
10095
+ onError: (e) => {
10096
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10097
+ },
10098
+ onSuccess: () => {
10099
+ handleSuccess();
10100
+ },
10101
+ onSettled: () => {
10102
+ setIsSubmitting(false);
10103
+ }
9964
10104
  });
9965
- }
9966
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9967
- KeyboundForm,
9968
- {
9969
- onSubmit: handleSubmit,
9970
- className: "flex flex-1 flex-col overflow-hidden",
9971
- children: [
9972
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9973
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9974
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9975
- /* @__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" }) }),
9976
- /* @__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" }) })
10105
+ };
10106
+ const onKeyDown = React.useCallback(
10107
+ (e) => {
10108
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10109
+ if (modalContent || isSubmitting) {
10110
+ return;
10111
+ }
10112
+ onSubmit();
10113
+ }
10114
+ },
10115
+ [modalContent, isSubmitting, onSubmit]
10116
+ );
10117
+ React.useEffect(() => {
10118
+ document.addEventListener("keydown", onKeyDown);
10119
+ return () => {
10120
+ document.removeEventListener("keydown", onKeyDown);
10121
+ };
10122
+ }, [onKeyDown]);
10123
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10124
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10125
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10126
+ StackedFocusModal,
10127
+ {
10128
+ id: STACKED_MODAL_ID,
10129
+ onOpenChangeCallback: (open) => {
10130
+ if (!open) {
10131
+ setModalContent(null);
10132
+ }
10133
+ },
10134
+ children: [
10135
+ /* @__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: [
10136
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10137
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10138
+ /* @__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" }) })
9977
10139
  ] }),
9978
- fields.map((field, index) => {
9979
- const isDisabled = field.disabled || false;
9980
- let placeholder = "-";
9981
- if (typeof field.value === "object") {
9982
- placeholder = "{ ... }";
9983
- }
9984
- if (Array.isArray(field.value)) {
9985
- placeholder = "[ ... ]";
9986
- }
9987
- return /* @__PURE__ */ jsxRuntime.jsx(
9988
- ConditionalTooltip,
9989
- {
9990
- showTooltip: isDisabled,
9991
- content: "This row is disabled because it contains non-primitive data.",
9992
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
9993
- /* @__PURE__ */ jsxRuntime.jsxs(
9994
- "div",
9995
- {
9996
- className: ui.clx("grid grid-cols-2 divide-x", {
9997
- "overflow-hidden rounded-b-lg": index === fields.length - 1
9998
- }),
9999
- children: [
10000
- /* @__PURE__ */ jsxRuntime.jsx(
10001
- Form$2.Field,
10002
- {
10003
- control: form.control,
10004
- name: `metadata.${index}.key`,
10005
- render: ({ field: field2 }) => {
10006
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10007
- GridInput,
10008
- {
10009
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10010
- ...field2,
10011
- disabled: isDisabled,
10012
- placeholder: "Key"
10013
- }
10014
- ) }) });
10015
- }
10016
- }
10017
- ),
10018
- /* @__PURE__ */ jsxRuntime.jsx(
10019
- Form$2.Field,
10020
- {
10021
- control: form.control,
10022
- name: `metadata.${index}.value`,
10023
- render: ({ field: { value, ...field2 } }) => {
10024
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10025
- GridInput,
10026
- {
10027
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
10028
- ...field2,
10029
- value: isDisabled ? placeholder : value,
10030
- disabled: isDisabled,
10031
- placeholder: "Value"
10032
- }
10033
- ) }) });
10034
- }
10035
- }
10036
- )
10037
- ]
10038
- }
10039
- ),
10040
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10140
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10141
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10142
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10143
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10144
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10145
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10146
+ ] }),
10147
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10148
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10149
+ ui.Input,
10150
+ {
10151
+ type: "search",
10152
+ placeholder: "Search items",
10153
+ value: searchValue,
10154
+ onChange: (e) => onSearchValueChange(e.target.value)
10155
+ }
10156
+ ) }),
10157
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10158
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10159
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10041
10160
  /* @__PURE__ */ jsxRuntime.jsx(
10042
- ui.DropdownMenu.Trigger,
10161
+ StackedModalTrigger$1,
10043
10162
  {
10044
- className: ui.clx(
10045
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
10046
- {
10047
- hidden: isDisabled
10048
- }
10049
- ),
10050
- disabled: isDisabled,
10051
- asChild: true,
10052
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
10163
+ type: "add-items",
10164
+ setModalContent
10053
10165
  }
10054
10166
  ),
10055
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10056
- /* @__PURE__ */ jsxRuntime.jsxs(
10057
- ui.DropdownMenu.Item,
10058
- {
10059
- className: "gap-x-2",
10060
- onClick: () => insertRow(index, "above"),
10061
- children: [
10062
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
10063
- "Insert row above"
10064
- ]
10065
- }
10066
- ),
10067
- /* @__PURE__ */ jsxRuntime.jsxs(
10068
- ui.DropdownMenu.Item,
10069
- {
10070
- className: "gap-x-2",
10071
- onClick: () => insertRow(index, "below"),
10072
- children: [
10073
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
10074
- "Insert row below"
10075
- ]
10076
- }
10077
- ),
10078
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
10079
- /* @__PURE__ */ jsxRuntime.jsxs(
10080
- ui.DropdownMenu.Item,
10081
- {
10082
- className: "gap-x-2",
10083
- onClick: () => deleteRow(index),
10084
- children: [
10085
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
10086
- "Delete row"
10087
- ]
10088
- }
10089
- )
10090
- ] })
10167
+ /* @__PURE__ */ jsxRuntime.jsx(
10168
+ StackedModalTrigger$1,
10169
+ {
10170
+ type: "add-custom-item",
10171
+ setModalContent
10172
+ }
10173
+ )
10091
10174
  ] })
10092
10175
  ] })
10093
- },
10094
- field.id
10095
- );
10096
- })
10097
- ] }),
10098
- 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." })
10099
- ] }),
10100
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10101
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10102
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10103
- ] }) })
10104
- ]
10105
- }
10106
- ) });
10107
- };
10108
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
10109
- return /* @__PURE__ */ jsxRuntime.jsx(
10110
- "input",
10111
- {
10112
- ref,
10113
- ...props,
10114
- autoComplete: "off",
10115
- className: ui.clx(
10116
- "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",
10117
- className
10176
+ ] })
10177
+ ] }),
10178
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10179
+ /* @__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: [
10180
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10181
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10182
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10183
+ /* @__PURE__ */ jsxRuntime.jsx("div", {})
10184
+ ] }) }),
10185
+ /* @__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: [
10186
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10187
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10188
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10189
+ Item,
10190
+ {
10191
+ item,
10192
+ preview,
10193
+ currencyCode
10194
+ },
10195
+ item.id
10196
+ )) : /* @__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: [
10197
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10198
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10199
+ 'No items found for "',
10200
+ query2,
10201
+ '".'
10202
+ ] })
10203
+ ] }) })
10204
+ ] })
10205
+ ] }),
10206
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10207
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10208
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10209
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10210
+ ui.Text,
10211
+ {
10212
+ size: "small",
10213
+ leading: "compact",
10214
+ className: "text-ui-fg-subtle",
10215
+ children: [
10216
+ itemCount,
10217
+ " ",
10218
+ itemCount === 1 ? "item" : "items"
10219
+ ]
10220
+ }
10221
+ ) }),
10222
+ /* @__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) }) })
10223
+ ] })
10224
+ ] }) }),
10225
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10226
+ CustomItemForm,
10227
+ {
10228
+ orderId: preview.id,
10229
+ currencyCode
10230
+ }
10231
+ ) : null)
10232
+ ]
10233
+ }
10234
+ ) }),
10235
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10236
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10237
+ /* @__PURE__ */ jsxRuntime.jsx(
10238
+ ui.Button,
10239
+ {
10240
+ size: "small",
10241
+ type: "button",
10242
+ onClick: onSubmit,
10243
+ isLoading: isSubmitting,
10244
+ children: "Save"
10245
+ }
10118
10246
  )
10119
- }
10120
- );
10121
- });
10122
- GridInput.displayName = "MetadataForm.GridInput";
10123
- const PlaceholderInner = () => {
10124
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10125
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10126
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10127
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
10128
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
10129
10247
  ] }) })
10130
10248
  ] });
10131
10249
  };
10132
- const EDITABLE_TYPES = ["string", "number", "boolean"];
10133
- function getDefaultValues(metadata) {
10134
- if (!metadata || !Object.keys(metadata).length) {
10135
- return [
10136
- {
10137
- key: "",
10138
- value: "",
10139
- disabled: false
10140
- }
10141
- ];
10250
+ const Item = ({ item, preview, currencyCode }) => {
10251
+ if (item.variant_id) {
10252
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10142
10253
  }
10143
- return Object.entries(metadata).map(([key, value]) => {
10144
- if (!EDITABLE_TYPES.includes(typeof value)) {
10145
- return {
10146
- key,
10147
- value,
10148
- disabled: true
10149
- };
10150
- }
10151
- let stringValue = value;
10152
- if (typeof value !== "string") {
10153
- stringValue = JSON.stringify(value);
10154
- }
10155
- return {
10156
- key,
10157
- value: stringValue,
10158
- original_key: key
10159
- };
10254
+ return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10255
+ };
10256
+ const VariantItem = ({ item, preview, currencyCode }) => {
10257
+ const [editing, setEditing] = React.useState(false);
10258
+ const form = reactHookForm.useForm({
10259
+ defaultValues: {
10260
+ quantity: item.quantity,
10261
+ unit_price: item.unit_price
10262
+ },
10263
+ resolver: zod.zodResolver(variantItemSchema)
10160
10264
  });
10161
- }
10162
- function parseValues(values) {
10163
- const metadata = values.metadata;
10164
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10165
- if (isEmpty) {
10166
- return null;
10167
- }
10168
- const update = {};
10169
- metadata.forEach((field) => {
10170
- let key = field.key;
10171
- let value = field.value;
10172
- const disabled = field.disabled;
10173
- if (!key || !value) {
10265
+ const actionId = React.useMemo(() => {
10266
+ var _a, _b;
10267
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10268
+ }, [item]);
10269
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10270
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10271
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10272
+ const onSubmit = form.handleSubmit(async (data) => {
10273
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10274
+ setEditing(false);
10174
10275
  return;
10175
10276
  }
10176
- if (disabled) {
10177
- update[key] = value;
10277
+ if (!actionId) {
10278
+ await updateOriginalItem(
10279
+ {
10280
+ item_id: item.id,
10281
+ quantity: data.quantity,
10282
+ unit_price: convertNumber(data.unit_price)
10283
+ },
10284
+ {
10285
+ onSuccess: () => {
10286
+ setEditing(false);
10287
+ },
10288
+ onError: (e) => {
10289
+ ui.toast.error(e.message);
10290
+ }
10291
+ }
10292
+ );
10178
10293
  return;
10179
10294
  }
10180
- key = key.trim();
10181
- value = value.trim();
10182
- if (value === "true") {
10183
- update[key] = true;
10184
- } else if (value === "false") {
10185
- update[key] = false;
10186
- } else {
10187
- const parsedNumber = parseFloat(value);
10188
- if (!isNaN(parsedNumber)) {
10189
- update[key] = parsedNumber;
10190
- } else {
10191
- update[key] = value;
10295
+ await updateActionItem(
10296
+ {
10297
+ action_id: actionId,
10298
+ quantity: data.quantity,
10299
+ unit_price: convertNumber(data.unit_price)
10300
+ },
10301
+ {
10302
+ onSuccess: () => {
10303
+ setEditing(false);
10304
+ },
10305
+ onError: (e) => {
10306
+ ui.toast.error(e.message);
10307
+ }
10192
10308
  }
10193
- }
10309
+ );
10194
10310
  });
10195
- return update;
10196
- }
10197
- function getHasUneditableRows(metadata) {
10198
- if (!metadata) {
10199
- return false;
10200
- }
10201
- return Object.values(metadata).some(
10202
- (value) => !EDITABLE_TYPES.includes(typeof value)
10203
- );
10204
- }
10205
- const NumberInput = React.forwardRef(
10206
- ({
10207
- value,
10208
- onChange,
10209
- size = "base",
10210
- min = 0,
10211
- max = 100,
10212
- step = 1,
10213
- className,
10214
- disabled,
10215
- ...props
10216
- }, ref) => {
10217
- const handleChange = (event) => {
10218
- const newValue = event.target.value === "" ? min : Number(event.target.value);
10219
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
10220
- onChange(newValue);
10221
- }
10222
- };
10223
- const handleIncrement = () => {
10224
- const newValue = value + step;
10225
- if (max === void 0 || newValue <= max) {
10226
- onChange(newValue);
10227
- }
10228
- };
10229
- const handleDecrement = () => {
10230
- const newValue = value - step;
10231
- if (min === void 0 || newValue >= min) {
10232
- onChange(newValue);
10233
- }
10234
- };
10235
- return /* @__PURE__ */ jsxRuntime.jsxs(
10236
- "div",
10237
- {
10238
- className: ui.clx(
10239
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
10240
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
10241
- {
10242
- "h-7": size === "small",
10243
- "h-8": size === "base"
10244
- },
10245
- className
10246
- ),
10247
- children: [
10248
- /* @__PURE__ */ jsxRuntime.jsx(
10249
- "input",
10250
- {
10251
- ref,
10252
- type: "number",
10253
- value,
10254
- onChange: handleChange,
10255
- min,
10256
- max,
10257
- step,
10258
- className: ui.clx(
10259
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
10260
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
10261
- "placeholder:text-ui-fg-muted"
10262
- ),
10263
- ...props
10264
- }
10265
- ),
10311
+ 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: [
10312
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10313
+ /* @__PURE__ */ jsxRuntime.jsx(
10314
+ Thumbnail,
10315
+ {
10316
+ thumbnail: item.thumbnail,
10317
+ alt: item.product_title ?? void 0
10318
+ }
10319
+ ),
10320
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10321
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10322
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10266
10323
  /* @__PURE__ */ jsxRuntime.jsxs(
10267
- "button",
10324
+ ui.Text,
10268
10325
  {
10269
- className: ui.clx(
10270
- "flex items-center justify-center outline-none transition-fg",
10271
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10272
- "focus:bg-ui-bg-field-component-hover",
10273
- "hover:bg-ui-bg-field-component-hover",
10274
- {
10275
- "size-7": size === "small",
10276
- "size-8": size === "base"
10277
- }
10278
- ),
10279
- type: "button",
10280
- onClick: handleDecrement,
10281
- disabled: min !== void 0 && value <= min || disabled,
10326
+ size: "small",
10327
+ leading: "compact",
10328
+ className: "text-ui-fg-subtle",
10282
10329
  children: [
10283
- /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
10284
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
10330
+ "(",
10331
+ item.variant_title,
10332
+ ")"
10285
10333
  ]
10286
10334
  }
10287
- ),
10288
- /* @__PURE__ */ jsxRuntime.jsxs(
10289
- "button",
10335
+ )
10336
+ ] }),
10337
+ /* @__PURE__ */ jsxRuntime.jsx(
10338
+ ui.Text,
10339
+ {
10340
+ size: "small",
10341
+ leading: "compact",
10342
+ className: "text-ui-fg-subtle",
10343
+ children: item.variant_sku
10344
+ }
10345
+ )
10346
+ ] })
10347
+ ] }),
10348
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10349
+ Form$2.Field,
10350
+ {
10351
+ control: form.control,
10352
+ name: "quantity",
10353
+ render: ({ field }) => {
10354
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10355
+ }
10356
+ }
10357
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10358
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10359
+ Form$2.Field,
10360
+ {
10361
+ control: form.control,
10362
+ name: "unit_price",
10363
+ render: ({ field: { onChange, ...field } }) => {
10364
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10365
+ ui.CurrencyInput,
10290
10366
  {
10291
- className: ui.clx(
10292
- "flex items-center justify-center outline-none transition-fg",
10293
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10294
- "focus:bg-ui-bg-field-hover",
10295
- "hover:bg-ui-bg-field-hover",
10296
- {
10297
- "size-7": size === "small",
10298
- "size-8": size === "base"
10299
- }
10300
- ),
10301
- type: "button",
10302
- onClick: handleIncrement,
10303
- disabled: max !== void 0 && value >= max || disabled,
10304
- children: [
10305
- /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
10306
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
10307
- ]
10367
+ ...field,
10368
+ symbol: getNativeSymbol(currencyCode),
10369
+ code: currencyCode,
10370
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10308
10371
  }
10309
- )
10310
- ]
10372
+ ) }) });
10373
+ }
10311
10374
  }
10312
- );
10313
- }
10314
- );
10315
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
10316
- const productVariantsQueryKeys = {
10317
- list: (query2) => [
10318
- PRODUCT_VARIANTS_QUERY_KEY,
10319
- query2 ? query2 : void 0
10320
- ]
10321
- };
10322
- const useProductVariants = (query2, options) => {
10323
- const { data, ...rest } = reactQuery.useQuery({
10324
- queryKey: productVariantsQueryKeys.list(query2),
10325
- queryFn: async () => await sdk.admin.productVariant.list(query2),
10326
- ...options
10327
- });
10328
- return { ...data, ...rest };
10329
- };
10330
- const useCancelOrderEdit = ({ preview }) => {
10331
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10332
- const onCancel = React.useCallback(async () => {
10333
- if (!preview) {
10334
- return true;
10335
- }
10336
- let res = false;
10337
- await cancelOrderEdit(void 0, {
10338
- onError: (e) => {
10339
- ui.toast.error(e.message);
10340
- },
10341
- onSuccess: () => {
10342
- res = true;
10375
+ ) }) : /* @__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) }) }),
10376
+ /* @__PURE__ */ jsxRuntime.jsx(
10377
+ ui.IconButton,
10378
+ {
10379
+ type: "button",
10380
+ size: "small",
10381
+ onClick: editing ? onSubmit : () => {
10382
+ setEditing(true);
10383
+ },
10384
+ disabled: isPending,
10385
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10343
10386
  }
10344
- });
10345
- return res;
10346
- }, [preview, cancelOrderEdit]);
10347
- return { onCancel };
10387
+ )
10388
+ ] }) }) });
10348
10389
  };
10349
- let IS_REQUEST_RUNNING = false;
10350
- const useInitiateOrderEdit = ({
10351
- preview
10352
- }) => {
10353
- const navigate = reactRouterDom.useNavigate();
10354
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10390
+ const variantItemSchema = objectType({
10391
+ quantity: numberType(),
10392
+ unit_price: unionType([numberType(), stringType()])
10393
+ });
10394
+ const CustomItem = ({ item, preview, currencyCode }) => {
10395
+ const [editing, setEditing] = React.useState(false);
10396
+ const { quantity, unit_price, title } = item;
10397
+ const form = reactHookForm.useForm({
10398
+ defaultValues: {
10399
+ title,
10400
+ quantity,
10401
+ unit_price
10402
+ },
10403
+ resolver: zod.zodResolver(customItemSchema)
10404
+ });
10355
10405
  React.useEffect(() => {
10356
- async function run() {
10357
- if (IS_REQUEST_RUNNING || !preview) {
10358
- return;
10359
- }
10360
- if (preview.order_change) {
10361
- return;
10362
- }
10363
- IS_REQUEST_RUNNING = true;
10364
- await mutateAsync(void 0, {
10406
+ form.reset({
10407
+ title,
10408
+ quantity,
10409
+ unit_price
10410
+ });
10411
+ }, [form, title, quantity, unit_price]);
10412
+ const actionId = React.useMemo(() => {
10413
+ var _a, _b;
10414
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10415
+ }, [item]);
10416
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10417
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10418
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10419
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10420
+ const onSubmit = form.handleSubmit(async (data) => {
10421
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10422
+ setEditing(false);
10423
+ return;
10424
+ }
10425
+ if (!actionId) {
10426
+ await updateOriginalItem(
10427
+ {
10428
+ item_id: item.id,
10429
+ quantity: data.quantity,
10430
+ unit_price: convertNumber(data.unit_price)
10431
+ },
10432
+ {
10433
+ onSuccess: () => {
10434
+ setEditing(false);
10435
+ },
10436
+ onError: (e) => {
10437
+ ui.toast.error(e.message);
10438
+ }
10439
+ }
10440
+ );
10441
+ return;
10442
+ }
10443
+ if (data.quantity === 0) {
10444
+ await removeActionItem(actionId, {
10445
+ onSuccess: () => {
10446
+ setEditing(false);
10447
+ },
10365
10448
  onError: (e) => {
10366
10449
  ui.toast.error(e.message);
10367
- navigate(`/draft-orders/${preview.id}`, { replace: true });
10368
- return;
10369
10450
  }
10370
10451
  });
10371
- IS_REQUEST_RUNNING = false;
10372
- }
10373
- run();
10374
- }, [preview, navigate, mutateAsync]);
10375
- };
10376
- function convertNumber(value) {
10377
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10378
- }
10379
- const STACKED_MODAL_ID = "items_stacked_modal";
10380
- const Items = () => {
10381
- const { id } = reactRouterDom.useParams();
10382
- const {
10383
- order: preview,
10384
- isPending: isPreviewPending,
10385
- isError: isPreviewError,
10386
- error: previewError
10387
- } = useOrderPreview(id, void 0, {
10388
- placeholderData: reactQuery.keepPreviousData
10389
- });
10390
- useInitiateOrderEdit({ preview });
10391
- const { draft_order, isPending, isError, error } = useDraftOrder(
10392
- id,
10393
- {
10394
- fields: "currency_code"
10395
- },
10396
- {
10397
- enabled: !!id
10398
- }
10399
- );
10400
- const { onCancel } = useCancelOrderEdit({ preview });
10401
- if (isError) {
10402
- throw error;
10403
- }
10404
- if (isPreviewError) {
10405
- throw previewError;
10406
- }
10407
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10408
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10409
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10410
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10411
- ] }) });
10412
- };
10413
- const ItemsForm = ({ preview, currencyCode }) => {
10414
- var _a;
10415
- const [isSubmitting, setIsSubmitting] = React.useState(false);
10416
- const [modalContent, setModalContent] = React.useState(
10417
- null
10418
- );
10419
- const { handleSuccess } = useRouteModal();
10420
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10421
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10422
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10423
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10424
- const matches = React.useMemo(() => {
10425
- return matchSorter.matchSorter(preview.items, query2, {
10426
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10427
- });
10428
- }, [preview.items, query2]);
10429
- const onSubmit = async () => {
10430
- setIsSubmitting(true);
10431
- let requestSucceeded = false;
10432
- await requestOrderEdit(void 0, {
10433
- onError: (e) => {
10434
- ui.toast.error(`Failed to request order edit: ${e.message}`);
10435
- },
10436
- onSuccess: () => {
10437
- requestSucceeded = true;
10438
- }
10439
- });
10440
- if (!requestSucceeded) {
10441
- setIsSubmitting(false);
10442
- return;
10443
- }
10444
- await confirmOrderEdit(void 0, {
10445
- onError: (e) => {
10446
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10447
- },
10448
- onSuccess: () => {
10449
- handleSuccess();
10450
- },
10451
- onSettled: () => {
10452
- setIsSubmitting(false);
10453
- }
10454
- });
10455
- };
10456
- const onKeyDown = React.useCallback(
10457
- (e) => {
10458
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10459
- if (modalContent || isSubmitting) {
10460
- return;
10461
- }
10462
- onSubmit();
10463
- }
10464
- },
10465
- [modalContent, isSubmitting, onSubmit]
10466
- );
10467
- React.useEffect(() => {
10468
- document.addEventListener("keydown", onKeyDown);
10469
- return () => {
10470
- document.removeEventListener("keydown", onKeyDown);
10471
- };
10472
- }, [onKeyDown]);
10473
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10474
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10475
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
10476
- StackedFocusModal,
10477
- {
10478
- id: STACKED_MODAL_ID,
10479
- onOpenChangeCallback: (open) => {
10480
- if (!open) {
10481
- setModalContent(null);
10482
- }
10483
- },
10484
- children: [
10485
- /* @__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: [
10486
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10487
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
10488
- /* @__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" }) })
10489
- ] }),
10490
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10491
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
10492
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
10493
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10494
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
10495
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
10496
- ] }),
10497
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
10498
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10499
- ui.Input,
10500
- {
10501
- type: "search",
10502
- placeholder: "Search items",
10503
- value: searchValue,
10504
- onChange: (e) => onSearchValueChange(e.target.value)
10505
- }
10506
- ) }),
10507
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10508
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
10509
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10510
- /* @__PURE__ */ jsxRuntime.jsx(
10511
- StackedModalTrigger$1,
10512
- {
10513
- type: "add-items",
10514
- setModalContent
10515
- }
10516
- ),
10517
- /* @__PURE__ */ jsxRuntime.jsx(
10518
- StackedModalTrigger$1,
10519
- {
10520
- type: "add-custom-item",
10521
- setModalContent
10522
- }
10523
- )
10524
- ] })
10525
- ] })
10526
- ] })
10527
- ] }),
10528
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
10529
- /* @__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: [
10530
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
10531
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
10532
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
10533
- /* @__PURE__ */ jsxRuntime.jsx("div", {})
10534
- ] }) }),
10535
- /* @__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: [
10536
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
10537
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
10538
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
10539
- Item,
10540
- {
10541
- item,
10542
- preview,
10543
- currencyCode
10544
- },
10545
- item.id
10546
- )) : /* @__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: [
10547
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
10548
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
10549
- 'No items found for "',
10550
- query2,
10551
- '".'
10552
- ] })
10553
- ] }) })
10554
- ] })
10555
- ] }),
10556
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10557
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
10558
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
10559
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
10560
- ui.Text,
10561
- {
10562
- size: "small",
10563
- leading: "compact",
10564
- className: "text-ui-fg-subtle",
10565
- children: [
10566
- itemCount,
10567
- " ",
10568
- itemCount === 1 ? "item" : "items"
10569
- ]
10570
- }
10571
- ) }),
10572
- /* @__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) }) })
10573
- ] })
10574
- ] }) }),
10575
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10576
- CustomItemForm,
10577
- {
10578
- orderId: preview.id,
10579
- currencyCode
10580
- }
10581
- ) : null)
10582
- ]
10583
- }
10584
- ) }),
10585
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10586
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10587
- /* @__PURE__ */ jsxRuntime.jsx(
10588
- ui.Button,
10589
- {
10590
- size: "small",
10591
- type: "button",
10592
- onClick: onSubmit,
10593
- isLoading: isSubmitting,
10594
- children: "Save"
10595
- }
10596
- )
10597
- ] }) })
10598
- ] });
10599
- };
10600
- const Item = ({ item, preview, currencyCode }) => {
10601
- if (item.variant_id) {
10602
- return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10603
- }
10604
- return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10605
- };
10606
- const VariantItem = ({ item, preview, currencyCode }) => {
10607
- const [editing, setEditing] = React.useState(false);
10608
- const form = reactHookForm.useForm({
10609
- defaultValues: {
10610
- quantity: item.quantity,
10611
- unit_price: item.unit_price
10612
- },
10613
- resolver: zod.zodResolver(variantItemSchema)
10614
- });
10615
- const actionId = React.useMemo(() => {
10616
- var _a, _b;
10617
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10618
- }, [item]);
10619
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10620
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10621
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10622
- const onSubmit = form.handleSubmit(async (data) => {
10623
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10624
- setEditing(false);
10625
- return;
10626
- }
10627
- if (!actionId) {
10628
- await updateOriginalItem(
10629
- {
10630
- item_id: item.id,
10631
- quantity: data.quantity,
10632
- unit_price: convertNumber(data.unit_price)
10633
- },
10634
- {
10635
- onSuccess: () => {
10636
- setEditing(false);
10637
- },
10638
- onError: (e) => {
10639
- ui.toast.error(e.message);
10640
- }
10641
- }
10642
- );
10643
- return;
10452
+ return;
10644
10453
  }
10645
10454
  await updateActionItem(
10646
10455
  {
@@ -10659,171 +10468,12 @@ const VariantItem = ({ item, preview, currencyCode }) => {
10659
10468
  );
10660
10469
  });
10661
10470
  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: [
10662
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10471
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10663
10472
  /* @__PURE__ */ jsxRuntime.jsx(
10664
10473
  Thumbnail,
10665
10474
  {
10666
10475
  thumbnail: item.thumbnail,
10667
- alt: item.product_title ?? void 0
10668
- }
10669
- ),
10670
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10671
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10672
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10673
- /* @__PURE__ */ jsxRuntime.jsxs(
10674
- ui.Text,
10675
- {
10676
- size: "small",
10677
- leading: "compact",
10678
- className: "text-ui-fg-subtle",
10679
- children: [
10680
- "(",
10681
- item.variant_title,
10682
- ")"
10683
- ]
10684
- }
10685
- )
10686
- ] }),
10687
- /* @__PURE__ */ jsxRuntime.jsx(
10688
- ui.Text,
10689
- {
10690
- size: "small",
10691
- leading: "compact",
10692
- className: "text-ui-fg-subtle",
10693
- children: item.variant_sku
10694
- }
10695
- )
10696
- ] })
10697
- ] }),
10698
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10699
- Form$2.Field,
10700
- {
10701
- control: form.control,
10702
- name: "quantity",
10703
- render: ({ field }) => {
10704
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10705
- }
10706
- }
10707
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10708
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10709
- Form$2.Field,
10710
- {
10711
- control: form.control,
10712
- name: "unit_price",
10713
- render: ({ field: { onChange, ...field } }) => {
10714
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10715
- ui.CurrencyInput,
10716
- {
10717
- ...field,
10718
- symbol: getNativeSymbol(currencyCode),
10719
- code: currencyCode,
10720
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10721
- }
10722
- ) }) });
10723
- }
10724
- }
10725
- ) }) : /* @__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) }) }),
10726
- /* @__PURE__ */ jsxRuntime.jsx(
10727
- ui.IconButton,
10728
- {
10729
- type: "button",
10730
- size: "small",
10731
- onClick: editing ? onSubmit : () => {
10732
- setEditing(true);
10733
- },
10734
- disabled: isPending,
10735
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10736
- }
10737
- )
10738
- ] }) }) });
10739
- };
10740
- const variantItemSchema = objectType({
10741
- quantity: numberType(),
10742
- unit_price: unionType([numberType(), stringType()])
10743
- });
10744
- const CustomItem = ({ item, preview, currencyCode }) => {
10745
- const [editing, setEditing] = React.useState(false);
10746
- const { quantity, unit_price, title } = item;
10747
- const form = reactHookForm.useForm({
10748
- defaultValues: {
10749
- title,
10750
- quantity,
10751
- unit_price
10752
- },
10753
- resolver: zod.zodResolver(customItemSchema)
10754
- });
10755
- React.useEffect(() => {
10756
- form.reset({
10757
- title,
10758
- quantity,
10759
- unit_price
10760
- });
10761
- }, [form, title, quantity, unit_price]);
10762
- const actionId = React.useMemo(() => {
10763
- var _a, _b;
10764
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10765
- }, [item]);
10766
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10767
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10768
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10769
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10770
- const onSubmit = form.handleSubmit(async (data) => {
10771
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10772
- setEditing(false);
10773
- return;
10774
- }
10775
- if (!actionId) {
10776
- await updateOriginalItem(
10777
- {
10778
- item_id: item.id,
10779
- quantity: data.quantity,
10780
- unit_price: convertNumber(data.unit_price)
10781
- },
10782
- {
10783
- onSuccess: () => {
10784
- setEditing(false);
10785
- },
10786
- onError: (e) => {
10787
- ui.toast.error(e.message);
10788
- }
10789
- }
10790
- );
10791
- return;
10792
- }
10793
- if (data.quantity === 0) {
10794
- await removeActionItem(actionId, {
10795
- onSuccess: () => {
10796
- setEditing(false);
10797
- },
10798
- onError: (e) => {
10799
- ui.toast.error(e.message);
10800
- }
10801
- });
10802
- return;
10803
- }
10804
- await updateActionItem(
10805
- {
10806
- action_id: actionId,
10807
- quantity: data.quantity,
10808
- unit_price: convertNumber(data.unit_price)
10809
- },
10810
- {
10811
- onSuccess: () => {
10812
- setEditing(false);
10813
- },
10814
- onError: (e) => {
10815
- ui.toast.error(e.message);
10816
- }
10817
- }
10818
- );
10819
- });
10820
- 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: [
10821
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10822
- /* @__PURE__ */ jsxRuntime.jsx(
10823
- Thumbnail,
10824
- {
10825
- thumbnail: item.thumbnail,
10826
- alt: item.title ?? void 0
10476
+ alt: item.title ?? void 0
10827
10477
  }
10828
10478
  ),
10829
10479
  editing ? /* @__PURE__ */ jsxRuntime.jsx(
@@ -11176,6 +10826,356 @@ const customItemSchema = objectType({
11176
10826
  quantity: numberType(),
11177
10827
  unit_price: unionType([numberType(), stringType()])
11178
10828
  });
10829
+ const InlineTip = React.forwardRef(
10830
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
10831
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10832
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10833
+ "div",
10834
+ {
10835
+ ref,
10836
+ className: ui.clx(
10837
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10838
+ className
10839
+ ),
10840
+ ...props,
10841
+ children: [
10842
+ /* @__PURE__ */ jsxRuntime.jsx(
10843
+ "div",
10844
+ {
10845
+ role: "presentation",
10846
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10847
+ "bg-ui-tag-orange-icon": variant === "warning"
10848
+ })
10849
+ }
10850
+ ),
10851
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10852
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10853
+ labelValue,
10854
+ ":"
10855
+ ] }),
10856
+ " ",
10857
+ children
10858
+ ] })
10859
+ ]
10860
+ }
10861
+ );
10862
+ }
10863
+ );
10864
+ InlineTip.displayName = "InlineTip";
10865
+ const MetadataFieldSchema = objectType({
10866
+ key: stringType(),
10867
+ disabled: booleanType().optional(),
10868
+ value: anyType()
10869
+ });
10870
+ const MetadataSchema = objectType({
10871
+ metadata: arrayType(MetadataFieldSchema)
10872
+ });
10873
+ const Metadata = () => {
10874
+ const { id } = reactRouterDom.useParams();
10875
+ const { order, isPending, isError, error } = useOrder(id, {
10876
+ fields: "metadata"
10877
+ });
10878
+ if (isError) {
10879
+ throw error;
10880
+ }
10881
+ const isReady = !isPending && !!order;
10882
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10883
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10884
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10885
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10886
+ ] }),
10887
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10888
+ ] });
10889
+ };
10890
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10891
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10892
+ const MetadataForm = ({ orderId, metadata }) => {
10893
+ const { handleSuccess } = useRouteModal();
10894
+ const hasUneditableRows = getHasUneditableRows(metadata);
10895
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10896
+ const form = reactHookForm.useForm({
10897
+ defaultValues: {
10898
+ metadata: getDefaultValues(metadata)
10899
+ },
10900
+ resolver: zod.zodResolver(MetadataSchema)
10901
+ });
10902
+ const handleSubmit = form.handleSubmit(async (data) => {
10903
+ const parsedData = parseValues(data);
10904
+ await mutateAsync(
10905
+ {
10906
+ metadata: parsedData
10907
+ },
10908
+ {
10909
+ onSuccess: () => {
10910
+ ui.toast.success("Metadata updated");
10911
+ handleSuccess();
10912
+ },
10913
+ onError: (error) => {
10914
+ ui.toast.error(error.message);
10915
+ }
10916
+ }
10917
+ );
10918
+ });
10919
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
10920
+ control: form.control,
10921
+ name: "metadata"
10922
+ });
10923
+ function deleteRow(index) {
10924
+ remove(index);
10925
+ if (fields.length === 1) {
10926
+ insert(0, {
10927
+ key: "",
10928
+ value: "",
10929
+ disabled: false
10930
+ });
10931
+ }
10932
+ }
10933
+ function insertRow(index, position) {
10934
+ insert(index + (position === "above" ? 0 : 1), {
10935
+ key: "",
10936
+ value: "",
10937
+ disabled: false
10938
+ });
10939
+ }
10940
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10941
+ KeyboundForm,
10942
+ {
10943
+ onSubmit: handleSubmit,
10944
+ className: "flex flex-1 flex-col overflow-hidden",
10945
+ children: [
10946
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10947
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10948
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10949
+ /* @__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" }) }),
10950
+ /* @__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" }) })
10951
+ ] }),
10952
+ fields.map((field, index) => {
10953
+ const isDisabled = field.disabled || false;
10954
+ let placeholder = "-";
10955
+ if (typeof field.value === "object") {
10956
+ placeholder = "{ ... }";
10957
+ }
10958
+ if (Array.isArray(field.value)) {
10959
+ placeholder = "[ ... ]";
10960
+ }
10961
+ return /* @__PURE__ */ jsxRuntime.jsx(
10962
+ ConditionalTooltip,
10963
+ {
10964
+ showTooltip: isDisabled,
10965
+ content: "This row is disabled because it contains non-primitive data.",
10966
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
10967
+ /* @__PURE__ */ jsxRuntime.jsxs(
10968
+ "div",
10969
+ {
10970
+ className: ui.clx("grid grid-cols-2 divide-x", {
10971
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
10972
+ }),
10973
+ children: [
10974
+ /* @__PURE__ */ jsxRuntime.jsx(
10975
+ Form$2.Field,
10976
+ {
10977
+ control: form.control,
10978
+ name: `metadata.${index}.key`,
10979
+ render: ({ field: field2 }) => {
10980
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10981
+ GridInput,
10982
+ {
10983
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
10984
+ ...field2,
10985
+ disabled: isDisabled,
10986
+ placeholder: "Key"
10987
+ }
10988
+ ) }) });
10989
+ }
10990
+ }
10991
+ ),
10992
+ /* @__PURE__ */ jsxRuntime.jsx(
10993
+ Form$2.Field,
10994
+ {
10995
+ control: form.control,
10996
+ name: `metadata.${index}.value`,
10997
+ render: ({ field: { value, ...field2 } }) => {
10998
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10999
+ GridInput,
11000
+ {
11001
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
11002
+ ...field2,
11003
+ value: isDisabled ? placeholder : value,
11004
+ disabled: isDisabled,
11005
+ placeholder: "Value"
11006
+ }
11007
+ ) }) });
11008
+ }
11009
+ }
11010
+ )
11011
+ ]
11012
+ }
11013
+ ),
11014
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
11015
+ /* @__PURE__ */ jsxRuntime.jsx(
11016
+ ui.DropdownMenu.Trigger,
11017
+ {
11018
+ className: ui.clx(
11019
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11020
+ {
11021
+ hidden: isDisabled
11022
+ }
11023
+ ),
11024
+ disabled: isDisabled,
11025
+ asChild: true,
11026
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
11027
+ }
11028
+ ),
11029
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
11030
+ /* @__PURE__ */ jsxRuntime.jsxs(
11031
+ ui.DropdownMenu.Item,
11032
+ {
11033
+ className: "gap-x-2",
11034
+ onClick: () => insertRow(index, "above"),
11035
+ children: [
11036
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
11037
+ "Insert row above"
11038
+ ]
11039
+ }
11040
+ ),
11041
+ /* @__PURE__ */ jsxRuntime.jsxs(
11042
+ ui.DropdownMenu.Item,
11043
+ {
11044
+ className: "gap-x-2",
11045
+ onClick: () => insertRow(index, "below"),
11046
+ children: [
11047
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
11048
+ "Insert row below"
11049
+ ]
11050
+ }
11051
+ ),
11052
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
11053
+ /* @__PURE__ */ jsxRuntime.jsxs(
11054
+ ui.DropdownMenu.Item,
11055
+ {
11056
+ className: "gap-x-2",
11057
+ onClick: () => deleteRow(index),
11058
+ children: [
11059
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
11060
+ "Delete row"
11061
+ ]
11062
+ }
11063
+ )
11064
+ ] })
11065
+ ] })
11066
+ ] })
11067
+ },
11068
+ field.id
11069
+ );
11070
+ })
11071
+ ] }),
11072
+ 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." })
11073
+ ] }),
11074
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11075
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11076
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11077
+ ] }) })
11078
+ ]
11079
+ }
11080
+ ) });
11081
+ };
11082
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
11083
+ return /* @__PURE__ */ jsxRuntime.jsx(
11084
+ "input",
11085
+ {
11086
+ ref,
11087
+ ...props,
11088
+ autoComplete: "off",
11089
+ className: ui.clx(
11090
+ "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",
11091
+ className
11092
+ )
11093
+ }
11094
+ );
11095
+ });
11096
+ GridInput.displayName = "MetadataForm.GridInput";
11097
+ const PlaceholderInner = () => {
11098
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11099
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11100
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11101
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
11102
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
11103
+ ] }) })
11104
+ ] });
11105
+ };
11106
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
11107
+ function getDefaultValues(metadata) {
11108
+ if (!metadata || !Object.keys(metadata).length) {
11109
+ return [
11110
+ {
11111
+ key: "",
11112
+ value: "",
11113
+ disabled: false
11114
+ }
11115
+ ];
11116
+ }
11117
+ return Object.entries(metadata).map(([key, value]) => {
11118
+ if (!EDITABLE_TYPES.includes(typeof value)) {
11119
+ return {
11120
+ key,
11121
+ value,
11122
+ disabled: true
11123
+ };
11124
+ }
11125
+ let stringValue = value;
11126
+ if (typeof value !== "string") {
11127
+ stringValue = JSON.stringify(value);
11128
+ }
11129
+ return {
11130
+ key,
11131
+ value: stringValue,
11132
+ original_key: key
11133
+ };
11134
+ });
11135
+ }
11136
+ function parseValues(values) {
11137
+ const metadata = values.metadata;
11138
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11139
+ if (isEmpty) {
11140
+ return null;
11141
+ }
11142
+ const update = {};
11143
+ metadata.forEach((field) => {
11144
+ let key = field.key;
11145
+ let value = field.value;
11146
+ const disabled = field.disabled;
11147
+ if (!key || !value) {
11148
+ return;
11149
+ }
11150
+ if (disabled) {
11151
+ update[key] = value;
11152
+ return;
11153
+ }
11154
+ key = key.trim();
11155
+ value = value.trim();
11156
+ if (value === "true") {
11157
+ update[key] = true;
11158
+ } else if (value === "false") {
11159
+ update[key] = false;
11160
+ } else {
11161
+ const parsedNumber = parseFloat(value);
11162
+ if (!isNaN(parsedNumber)) {
11163
+ update[key] = parsedNumber;
11164
+ } else {
11165
+ update[key] = value;
11166
+ }
11167
+ }
11168
+ });
11169
+ return update;
11170
+ }
11171
+ function getHasUneditableRows(metadata) {
11172
+ if (!metadata) {
11173
+ return false;
11174
+ }
11175
+ return Object.values(metadata).some(
11176
+ (value) => !EDITABLE_TYPES.includes(typeof value)
11177
+ );
11178
+ }
11179
11179
  const PROMOTION_QUERY_KEY = "promotions";
11180
11180
  const promotionsQueryKeys = {
11181
11181
  list: (query2) => [
@@ -11453,112 +11453,6 @@ function getPromotionIds(items, shippingMethods) {
11453
11453
  }
11454
11454
  return Array.from(promotionIds);
11455
11455
  }
11456
- const SalesChannel = () => {
11457
- const { id } = reactRouterDom.useParams();
11458
- const { draft_order, isPending, isError, error } = useDraftOrder(
11459
- id,
11460
- {
11461
- fields: "+sales_channel_id"
11462
- },
11463
- {
11464
- enabled: !!id
11465
- }
11466
- );
11467
- if (isError) {
11468
- throw error;
11469
- }
11470
- const ISrEADY = !!draft_order && !isPending;
11471
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11472
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11473
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11474
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11475
- ] }),
11476
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11477
- ] });
11478
- };
11479
- const SalesChannelForm = ({ order }) => {
11480
- const form = reactHookForm.useForm({
11481
- defaultValues: {
11482
- sales_channel_id: order.sales_channel_id || ""
11483
- },
11484
- resolver: zod.zodResolver(schema$2)
11485
- });
11486
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11487
- const { handleSuccess } = useRouteModal();
11488
- const onSubmit = form.handleSubmit(async (data) => {
11489
- await mutateAsync(
11490
- {
11491
- sales_channel_id: data.sales_channel_id
11492
- },
11493
- {
11494
- onSuccess: () => {
11495
- ui.toast.success("Sales channel updated");
11496
- handleSuccess();
11497
- },
11498
- onError: (error) => {
11499
- ui.toast.error(error.message);
11500
- }
11501
- }
11502
- );
11503
- });
11504
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11505
- KeyboundForm,
11506
- {
11507
- className: "flex flex-1 flex-col overflow-hidden",
11508
- onSubmit,
11509
- children: [
11510
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11511
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11512
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11513
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11514
- ] }) })
11515
- ]
11516
- }
11517
- ) });
11518
- };
11519
- const SalesChannelField = ({ control, order }) => {
11520
- const salesChannels = useComboboxData({
11521
- queryFn: async (params) => {
11522
- return await sdk.admin.salesChannel.list(params);
11523
- },
11524
- queryKey: ["sales-channels"],
11525
- getOptions: (data) => {
11526
- return data.sales_channels.map((salesChannel) => ({
11527
- label: salesChannel.name,
11528
- value: salesChannel.id
11529
- }));
11530
- },
11531
- defaultValue: order.sales_channel_id || void 0
11532
- });
11533
- return /* @__PURE__ */ jsxRuntime.jsx(
11534
- Form$2.Field,
11535
- {
11536
- control,
11537
- name: "sales_channel_id",
11538
- render: ({ field }) => {
11539
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11540
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11541
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11542
- Combobox,
11543
- {
11544
- options: salesChannels.options,
11545
- fetchNextPage: salesChannels.fetchNextPage,
11546
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11547
- searchValue: salesChannels.searchValue,
11548
- onSearchValueChange: salesChannels.onSearchValueChange,
11549
- placeholder: "Select sales channel",
11550
- ...field
11551
- }
11552
- ) }),
11553
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11554
- ] });
11555
- }
11556
- }
11557
- );
11558
- };
11559
- const schema$2 = objectType({
11560
- sales_channel_id: stringType().min(1)
11561
- });
11562
11456
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
11563
11457
  const Shipping = () => {
11564
11458
  var _a;
@@ -12398,7 +12292,7 @@ const ShippingAddressForm = ({ order }) => {
12398
12292
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12399
12293
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12400
12294
  },
12401
- resolver: zod.zodResolver(schema$1)
12295
+ resolver: zod.zodResolver(schema$2)
12402
12296
  });
12403
12297
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12404
12298
  const { handleSuccess } = useRouteModal();
@@ -12568,7 +12462,7 @@ const ShippingAddressForm = ({ order }) => {
12568
12462
  }
12569
12463
  ) });
12570
12464
  };
12571
- const schema$1 = addressSchema;
12465
+ const schema$2 = addressSchema;
12572
12466
  const TransferOwnership = () => {
12573
12467
  const { id } = reactRouterDom.useParams();
12574
12468
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12592,7 +12486,7 @@ const TransferOwnershipForm = ({ order }) => {
12592
12486
  defaultValues: {
12593
12487
  customer_id: order.customer_id || ""
12594
12488
  },
12595
- resolver: zod.zodResolver(schema)
12489
+ resolver: zod.zodResolver(schema$1)
12596
12490
  });
12597
12491
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12598
12492
  const { handleSuccess } = useRouteModal();
@@ -13042,9 +12936,115 @@ const Illustration = () => {
13042
12936
  }
13043
12937
  );
13044
12938
  };
13045
- const schema = objectType({
12939
+ const schema$1 = objectType({
13046
12940
  customer_id: stringType().min(1)
13047
12941
  });
12942
+ const SalesChannel = () => {
12943
+ const { id } = reactRouterDom.useParams();
12944
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12945
+ id,
12946
+ {
12947
+ fields: "+sales_channel_id"
12948
+ },
12949
+ {
12950
+ enabled: !!id
12951
+ }
12952
+ );
12953
+ if (isError) {
12954
+ throw error;
12955
+ }
12956
+ const ISrEADY = !!draft_order && !isPending;
12957
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12958
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12959
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
12960
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12961
+ ] }),
12962
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
12963
+ ] });
12964
+ };
12965
+ const SalesChannelForm = ({ order }) => {
12966
+ const form = reactHookForm.useForm({
12967
+ defaultValues: {
12968
+ sales_channel_id: order.sales_channel_id || ""
12969
+ },
12970
+ resolver: zod.zodResolver(schema)
12971
+ });
12972
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12973
+ const { handleSuccess } = useRouteModal();
12974
+ const onSubmit = form.handleSubmit(async (data) => {
12975
+ await mutateAsync(
12976
+ {
12977
+ sales_channel_id: data.sales_channel_id
12978
+ },
12979
+ {
12980
+ onSuccess: () => {
12981
+ ui.toast.success("Sales channel updated");
12982
+ handleSuccess();
12983
+ },
12984
+ onError: (error) => {
12985
+ ui.toast.error(error.message);
12986
+ }
12987
+ }
12988
+ );
12989
+ });
12990
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12991
+ KeyboundForm,
12992
+ {
12993
+ className: "flex flex-1 flex-col overflow-hidden",
12994
+ onSubmit,
12995
+ children: [
12996
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
12997
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12998
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12999
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13000
+ ] }) })
13001
+ ]
13002
+ }
13003
+ ) });
13004
+ };
13005
+ const SalesChannelField = ({ control, order }) => {
13006
+ const salesChannels = useComboboxData({
13007
+ queryFn: async (params) => {
13008
+ return await sdk.admin.salesChannel.list(params);
13009
+ },
13010
+ queryKey: ["sales-channels"],
13011
+ getOptions: (data) => {
13012
+ return data.sales_channels.map((salesChannel) => ({
13013
+ label: salesChannel.name,
13014
+ value: salesChannel.id
13015
+ }));
13016
+ },
13017
+ defaultValue: order.sales_channel_id || void 0
13018
+ });
13019
+ return /* @__PURE__ */ jsxRuntime.jsx(
13020
+ Form$2.Field,
13021
+ {
13022
+ control,
13023
+ name: "sales_channel_id",
13024
+ render: ({ field }) => {
13025
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13026
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
13027
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
13028
+ Combobox,
13029
+ {
13030
+ options: salesChannels.options,
13031
+ fetchNextPage: salesChannels.fetchNextPage,
13032
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
13033
+ searchValue: salesChannels.searchValue,
13034
+ onSearchValueChange: salesChannels.onSearchValueChange,
13035
+ placeholder: "Select sales channel",
13036
+ ...field
13037
+ }
13038
+ ) }),
13039
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13040
+ ] });
13041
+ }
13042
+ }
13043
+ );
13044
+ };
13045
+ const schema = objectType({
13046
+ sales_channel_id: stringType().min(1)
13047
+ });
13048
13048
  const widgetModule = { widgets: [] };
13049
13049
  const routeModule = {
13050
13050
  routes: [
@@ -13077,21 +13077,17 @@ const routeModule = {
13077
13077
  Component: Email,
13078
13078
  path: "/draft-orders/:id/email"
13079
13079
  },
13080
- {
13081
- Component: Metadata,
13082
- path: "/draft-orders/:id/metadata"
13083
- },
13084
13080
  {
13085
13081
  Component: Items,
13086
13082
  path: "/draft-orders/:id/items"
13087
13083
  },
13088
13084
  {
13089
- Component: Promotions,
13090
- path: "/draft-orders/:id/promotions"
13085
+ Component: Metadata,
13086
+ path: "/draft-orders/:id/metadata"
13091
13087
  },
13092
13088
  {
13093
- Component: SalesChannel,
13094
- path: "/draft-orders/:id/sales-channel"
13089
+ Component: Promotions,
13090
+ path: "/draft-orders/:id/promotions"
13095
13091
  },
13096
13092
  {
13097
13093
  Component: Shipping,
@@ -13104,6 +13100,10 @@ const routeModule = {
13104
13100
  {
13105
13101
  Component: TransferOwnership,
13106
13102
  path: "/draft-orders/:id/transfer-ownership"
13103
+ },
13104
+ {
13105
+ Component: SalesChannel,
13106
+ path: "/draft-orders/:id/sales-channel"
13107
13107
  }
13108
13108
  ]
13109
13109
  }