@medusajs/draft-order 2.10.4-preview-20250922210211 → 2.10.4-preview-20250923031201

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