@medusajs/draft-order 2.10.4-preview-20251010070816 → 2.10.4-preview-20251010120204

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,6 +9567,95 @@ 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
+ const Email = () => {
9592
+ const { id } = useParams();
9593
+ const { order, isPending, isError, error } = useOrder(id, {
9594
+ fields: "+email"
9595
+ });
9596
+ if (isError) {
9597
+ throw error;
9598
+ }
9599
+ const isReady = !isPending && !!order;
9600
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9601
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9602
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Email" }) }),
9603
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the email for the draft order" }) })
9604
+ ] }),
9605
+ isReady && /* @__PURE__ */ jsx(EmailForm, { order })
9606
+ ] });
9607
+ };
9608
+ const EmailForm = ({ order }) => {
9609
+ const form = useForm({
9610
+ defaultValues: {
9611
+ email: order.email ?? ""
9612
+ },
9613
+ resolver: zodResolver(schema$4)
9614
+ });
9615
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9616
+ const { handleSuccess } = useRouteModal();
9617
+ const onSubmit = form.handleSubmit(async (data) => {
9618
+ await mutateAsync(
9619
+ { email: data.email },
9620
+ {
9621
+ onSuccess: () => {
9622
+ handleSuccess();
9623
+ },
9624
+ onError: (error) => {
9625
+ toast.error(error.message);
9626
+ }
9627
+ }
9628
+ );
9629
+ });
9630
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9631
+ KeyboundForm,
9632
+ {
9633
+ className: "flex flex-1 flex-col overflow-hidden",
9634
+ onSubmit,
9635
+ children: [
9636
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(
9637
+ Form$2.Field,
9638
+ {
9639
+ control: form.control,
9640
+ name: "email",
9641
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9642
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Email" }),
9643
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9644
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9645
+ ] })
9646
+ }
9647
+ ) }),
9648
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9649
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9650
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9651
+ ] }) })
9652
+ ]
9653
+ }
9654
+ ) });
9655
+ };
9656
+ const schema$4 = objectType({
9657
+ email: stringType().email()
9658
+ });
9570
9659
  const BillingAddress = () => {
9571
9660
  const { id } = useParams();
9572
9661
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9599,7 +9688,7 @@ const BillingAddressForm = ({ order }) => {
9599
9688
  postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9600
9689
  phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9601
9690
  },
9602
- resolver: zodResolver(schema$5)
9691
+ resolver: zodResolver(schema$3)
9603
9692
  });
9604
9693
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9605
9694
  const { handleSuccess } = useRouteModal();
@@ -9756,32 +9845,55 @@ const BillingAddressForm = ({ order }) => {
9756
9845
  }
9757
9846
  ) });
9758
9847
  };
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()
9848
+ const schema$3 = addressSchema;
9849
+ const InlineTip = forwardRef(
9850
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9851
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9852
+ return /* @__PURE__ */ jsxs(
9853
+ "div",
9854
+ {
9855
+ ref,
9856
+ className: clx(
9857
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9858
+ className
9859
+ ),
9860
+ ...props,
9861
+ children: [
9862
+ /* @__PURE__ */ jsx(
9863
+ "div",
9864
+ {
9865
+ role: "presentation",
9866
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9867
+ "bg-ui-tag-orange-icon": variant === "warning"
9868
+ })
9869
+ }
9870
+ ),
9871
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9872
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9873
+ labelValue,
9874
+ ":"
9875
+ ] }),
9876
+ " ",
9877
+ children
9878
+ ] })
9879
+ ]
9880
+ }
9881
+ );
9882
+ }
9883
+ );
9884
+ InlineTip.displayName = "InlineTip";
9885
+ const MetadataFieldSchema = objectType({
9886
+ key: stringType(),
9887
+ disabled: booleanType().optional(),
9888
+ value: anyType()
9780
9889
  });
9781
- const Email = () => {
9890
+ const MetadataSchema = objectType({
9891
+ metadata: arrayType(MetadataFieldSchema)
9892
+ });
9893
+ const Metadata = () => {
9782
9894
  const { id } = useParams();
9783
9895
  const { order, isPending, isError, error } = useOrder(id, {
9784
- fields: "+email"
9896
+ fields: "metadata"
9785
9897
  });
9786
9898
  if (isError) {
9787
9899
  throw error;
@@ -9789,26 +9901,33 @@ const Email = () => {
9789
9901
  const isReady = !isPending && !!order;
9790
9902
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9791
9903
  /* @__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" }) })
9904
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9905
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9794
9906
  ] }),
9795
- isReady && /* @__PURE__ */ jsx(EmailForm, { order })
9907
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9796
9908
  ] });
9797
9909
  };
9798
- const EmailForm = ({ order }) => {
9910
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9911
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9912
+ const MetadataForm = ({ orderId, metadata }) => {
9913
+ const { handleSuccess } = useRouteModal();
9914
+ const hasUneditableRows = getHasUneditableRows(metadata);
9915
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9799
9916
  const form = useForm({
9800
9917
  defaultValues: {
9801
- email: order.email ?? ""
9918
+ metadata: getDefaultValues(metadata)
9802
9919
  },
9803
- resolver: zodResolver(schema$3)
9920
+ resolver: zodResolver(MetadataSchema)
9804
9921
  });
9805
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9806
- const { handleSuccess } = useRouteModal();
9807
- const onSubmit = form.handleSubmit(async (data) => {
9922
+ const handleSubmit = form.handleSubmit(async (data) => {
9923
+ const parsedData = parseValues(data);
9808
9924
  await mutateAsync(
9809
- { email: data.email },
9925
+ {
9926
+ metadata: parsedData
9927
+ },
9810
9928
  {
9811
9929
  onSuccess: () => {
9930
+ toast.success("Metadata updated");
9812
9931
  handleSuccess();
9813
9932
  },
9814
9933
  onError: (error) => {
@@ -9817,188 +9936,314 @@ const EmailForm = ({ order }) => {
9817
9936
  }
9818
9937
  );
9819
9938
  });
9939
+ const { fields, insert, remove } = useFieldArray({
9940
+ control: form.control,
9941
+ name: "metadata"
9942
+ });
9943
+ function deleteRow(index) {
9944
+ remove(index);
9945
+ if (fields.length === 1) {
9946
+ insert(0, {
9947
+ key: "",
9948
+ value: "",
9949
+ disabled: false
9950
+ });
9951
+ }
9952
+ }
9953
+ function insertRow(index, position) {
9954
+ insert(index + (position === "above" ? 0 : 1), {
9955
+ key: "",
9956
+ value: "",
9957
+ disabled: false
9958
+ });
9959
+ }
9820
9960
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9821
9961
  KeyboundForm,
9822
9962
  {
9963
+ onSubmit: handleSubmit,
9823
9964
  className: "flex flex-1 flex-col overflow-hidden",
9824
- onSubmit,
9825
9965
  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",
9918
- {
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",
9966
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9967
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9968
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9969
+ /* @__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" }) }),
9970
+ /* @__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" }) })
9971
+ ] }),
9972
+ fields.map((field, index) => {
9973
+ const isDisabled = field.disabled || false;
9974
+ let placeholder = "-";
9975
+ if (typeof field.value === "object") {
9976
+ placeholder = "{ ... }";
9977
+ }
9978
+ if (Array.isArray(field.value)) {
9979
+ placeholder = "[ ... ]";
9980
+ }
9981
+ return /* @__PURE__ */ jsx(
9982
+ ConditionalTooltip,
9940
9983
  {
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
- ]
9955
- }
9956
- );
9957
- }
9958
- );
9959
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9960
- const productVariantsQueryKeys = {
9961
- list: (query2) => [
9962
- PRODUCT_VARIANTS_QUERY_KEY,
9963
- query2 ? query2 : void 0
9964
- ]
9965
- };
9966
- const useProductVariants = (query2, options) => {
9967
- const { data, ...rest } = useQuery({
9968
- queryKey: productVariantsQueryKeys.list(query2),
9969
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9970
- ...options
9971
- });
9972
- return { ...data, ...rest };
9973
- };
9974
- const useCancelOrderEdit = ({ preview }) => {
9975
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9976
- const onCancel = useCallback(async () => {
9977
- if (!preview) {
9978
- return true;
9984
+ showTooltip: isDisabled,
9985
+ content: "This row is disabled because it contains non-primitive data.",
9986
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9987
+ /* @__PURE__ */ jsxs(
9988
+ "div",
9989
+ {
9990
+ className: clx("grid grid-cols-2 divide-x", {
9991
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9992
+ }),
9993
+ children: [
9994
+ /* @__PURE__ */ jsx(
9995
+ Form$2.Field,
9996
+ {
9997
+ control: form.control,
9998
+ name: `metadata.${index}.key`,
9999
+ render: ({ field: field2 }) => {
10000
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10001
+ GridInput,
10002
+ {
10003
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
10004
+ ...field2,
10005
+ disabled: isDisabled,
10006
+ placeholder: "Key"
10007
+ }
10008
+ ) }) });
10009
+ }
10010
+ }
10011
+ ),
10012
+ /* @__PURE__ */ jsx(
10013
+ Form$2.Field,
10014
+ {
10015
+ control: form.control,
10016
+ name: `metadata.${index}.value`,
10017
+ render: ({ field: { value, ...field2 } }) => {
10018
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10019
+ GridInput,
10020
+ {
10021
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
10022
+ ...field2,
10023
+ value: isDisabled ? placeholder : value,
10024
+ disabled: isDisabled,
10025
+ placeholder: "Value"
10026
+ }
10027
+ ) }) });
10028
+ }
10029
+ }
10030
+ )
10031
+ ]
10032
+ }
10033
+ ),
10034
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
10035
+ /* @__PURE__ */ jsx(
10036
+ DropdownMenu.Trigger,
10037
+ {
10038
+ className: clx(
10039
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
10040
+ {
10041
+ hidden: isDisabled
10042
+ }
10043
+ ),
10044
+ disabled: isDisabled,
10045
+ asChild: true,
10046
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
10047
+ }
10048
+ ),
10049
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
10050
+ /* @__PURE__ */ jsxs(
10051
+ DropdownMenu.Item,
10052
+ {
10053
+ className: "gap-x-2",
10054
+ onClick: () => insertRow(index, "above"),
10055
+ children: [
10056
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
10057
+ "Insert row above"
10058
+ ]
10059
+ }
10060
+ ),
10061
+ /* @__PURE__ */ jsxs(
10062
+ DropdownMenu.Item,
10063
+ {
10064
+ className: "gap-x-2",
10065
+ onClick: () => insertRow(index, "below"),
10066
+ children: [
10067
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
10068
+ "Insert row below"
10069
+ ]
10070
+ }
10071
+ ),
10072
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
10073
+ /* @__PURE__ */ jsxs(
10074
+ DropdownMenu.Item,
10075
+ {
10076
+ className: "gap-x-2",
10077
+ onClick: () => deleteRow(index),
10078
+ children: [
10079
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
10080
+ "Delete row"
10081
+ ]
10082
+ }
10083
+ )
10084
+ ] })
10085
+ ] })
10086
+ ] })
10087
+ },
10088
+ field.id
10089
+ );
10090
+ })
10091
+ ] }),
10092
+ 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." })
10093
+ ] }),
10094
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10095
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10096
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10097
+ ] }) })
10098
+ ]
9979
10099
  }
9980
- let res = false;
9981
- await cancelOrderEdit(void 0, {
9982
- onError: (e) => {
9983
- toast.error(e.message);
9984
- },
9985
- onSuccess: () => {
9986
- res = true;
9987
- }
9988
- });
9989
- return res;
9990
- }, [preview, cancelOrderEdit]);
9991
- return { onCancel };
10100
+ ) });
9992
10101
  };
9993
- let IS_REQUEST_RUNNING = false;
9994
- const useInitiateOrderEdit = ({
9995
- preview
9996
- }) => {
9997
- const navigate = useNavigate();
9998
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
9999
- useEffect(() => {
10000
- async function run() {
10001
- if (IS_REQUEST_RUNNING || !preview) {
10102
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
10103
+ return /* @__PURE__ */ jsx(
10104
+ "input",
10105
+ {
10106
+ ref,
10107
+ ...props,
10108
+ autoComplete: "off",
10109
+ className: clx(
10110
+ "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",
10111
+ className
10112
+ )
10113
+ }
10114
+ );
10115
+ });
10116
+ GridInput.displayName = "MetadataForm.GridInput";
10117
+ const PlaceholderInner = () => {
10118
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10119
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10120
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10121
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
10122
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
10123
+ ] }) })
10124
+ ] });
10125
+ };
10126
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
10127
+ function getDefaultValues(metadata) {
10128
+ if (!metadata || !Object.keys(metadata).length) {
10129
+ return [
10130
+ {
10131
+ key: "",
10132
+ value: "",
10133
+ disabled: false
10134
+ }
10135
+ ];
10136
+ }
10137
+ return Object.entries(metadata).map(([key, value]) => {
10138
+ if (!EDITABLE_TYPES.includes(typeof value)) {
10139
+ return {
10140
+ key,
10141
+ value,
10142
+ disabled: true
10143
+ };
10144
+ }
10145
+ let stringValue = value;
10146
+ if (typeof value !== "string") {
10147
+ stringValue = JSON.stringify(value);
10148
+ }
10149
+ return {
10150
+ key,
10151
+ value: stringValue,
10152
+ original_key: key
10153
+ };
10154
+ });
10155
+ }
10156
+ function parseValues(values) {
10157
+ const metadata = values.metadata;
10158
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10159
+ if (isEmpty) {
10160
+ return null;
10161
+ }
10162
+ const update = {};
10163
+ metadata.forEach((field) => {
10164
+ let key = field.key;
10165
+ let value = field.value;
10166
+ const disabled = field.disabled;
10167
+ if (!key || !value) {
10168
+ return;
10169
+ }
10170
+ if (disabled) {
10171
+ update[key] = value;
10172
+ return;
10173
+ }
10174
+ key = key.trim();
10175
+ value = value.trim();
10176
+ if (value === "true") {
10177
+ update[key] = true;
10178
+ } else if (value === "false") {
10179
+ update[key] = false;
10180
+ } else {
10181
+ const parsedNumber = parseFloat(value);
10182
+ if (!isNaN(parsedNumber)) {
10183
+ update[key] = parsedNumber;
10184
+ } else {
10185
+ update[key] = value;
10186
+ }
10187
+ }
10188
+ });
10189
+ return update;
10190
+ }
10191
+ function getHasUneditableRows(metadata) {
10192
+ if (!metadata) {
10193
+ return false;
10194
+ }
10195
+ return Object.values(metadata).some(
10196
+ (value) => !EDITABLE_TYPES.includes(typeof value)
10197
+ );
10198
+ }
10199
+ const PROMOTION_QUERY_KEY = "promotions";
10200
+ const promotionsQueryKeys = {
10201
+ list: (query2) => [
10202
+ PROMOTION_QUERY_KEY,
10203
+ query2 ? query2 : void 0
10204
+ ],
10205
+ detail: (id, query2) => [
10206
+ PROMOTION_QUERY_KEY,
10207
+ id,
10208
+ query2 ? query2 : void 0
10209
+ ]
10210
+ };
10211
+ const usePromotions = (query2, options) => {
10212
+ const { data, ...rest } = useQuery({
10213
+ queryKey: promotionsQueryKeys.list(query2),
10214
+ queryFn: async () => sdk.admin.promotion.list(query2),
10215
+ ...options
10216
+ });
10217
+ return { ...data, ...rest };
10218
+ };
10219
+ const useCancelOrderEdit = ({ preview }) => {
10220
+ const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10221
+ const onCancel = useCallback(async () => {
10222
+ if (!preview) {
10223
+ return true;
10224
+ }
10225
+ let res = false;
10226
+ await cancelOrderEdit(void 0, {
10227
+ onError: (e) => {
10228
+ toast.error(e.message);
10229
+ },
10230
+ onSuccess: () => {
10231
+ res = true;
10232
+ }
10233
+ });
10234
+ return res;
10235
+ }, [preview, cancelOrderEdit]);
10236
+ return { onCancel };
10237
+ };
10238
+ let IS_REQUEST_RUNNING = false;
10239
+ const useInitiateOrderEdit = ({
10240
+ preview
10241
+ }) => {
10242
+ const navigate = useNavigate();
10243
+ const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10244
+ useEffect(() => {
10245
+ async function run() {
10246
+ if (IS_REQUEST_RUNNING || !preview) {
10002
10247
  return;
10003
10248
  }
10004
10249
  if (preview.order_change) {
@@ -10017,65 +10262,85 @@ const useInitiateOrderEdit = ({
10017
10262
  run();
10018
10263
  }, [preview, navigate, mutateAsync]);
10019
10264
  };
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 = () => {
10265
+ const Promotions = () => {
10025
10266
  const { id } = useParams();
10026
10267
  const {
10027
10268
  order: preview,
10028
- isPending: isPreviewPending,
10029
10269
  isError: isPreviewError,
10030
10270
  error: previewError
10031
- } = useOrderPreview(id, void 0, {
10032
- placeholderData: keepPreviousData
10033
- });
10271
+ } = useOrderPreview(id, void 0);
10034
10272
  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
10273
  const { onCancel } = useCancelOrderEdit({ preview });
10045
- if (isError) {
10046
- throw error;
10047
- }
10048
10274
  if (isPreviewError) {
10049
10275
  throw previewError;
10050
10276
  }
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
- ] }) });
10277
+ const isReady = !!preview;
10278
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10279
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10280
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10281
+ ] });
10056
10282
  };
10057
- const ItemsForm = ({ preview, currencyCode }) => {
10058
- var _a;
10283
+ const PromotionForm = ({ preview }) => {
10284
+ const { items, shipping_methods } = preview;
10059
10285
  const [isSubmitting, setIsSubmitting] = useState(false);
10060
- const [modalContent, setModalContent] = useState(
10061
- null
10062
- );
10286
+ const [comboboxValue, setComboboxValue] = useState("");
10063
10287
  const { handleSuccess } = useRouteModal();
10064
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10288
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10289
+ const promoIds = getPromotionIds(items, shipping_methods);
10290
+ const { promotions, isPending, isError, error } = usePromotions(
10291
+ {
10292
+ id: promoIds
10293
+ },
10294
+ {
10295
+ enabled: !!promoIds.length
10296
+ }
10297
+ );
10298
+ const comboboxData = useComboboxData({
10299
+ queryKey: ["promotions", "combobox", promoIds],
10300
+ queryFn: async (params) => {
10301
+ return await sdk.admin.promotion.list({
10302
+ ...params,
10303
+ id: {
10304
+ $nin: promoIds
10305
+ }
10306
+ });
10307
+ },
10308
+ getOptions: (data) => {
10309
+ return data.promotions.map((promotion) => ({
10310
+ label: promotion.code,
10311
+ value: promotion.code
10312
+ }));
10313
+ }
10314
+ });
10315
+ const add = async (value) => {
10316
+ if (!value) {
10317
+ return;
10318
+ }
10319
+ addPromotions(
10320
+ {
10321
+ promo_codes: [value]
10322
+ },
10323
+ {
10324
+ onError: (e) => {
10325
+ toast.error(e.message);
10326
+ comboboxData.onSearchValueChange("");
10327
+ setComboboxValue("");
10328
+ },
10329
+ onSuccess: () => {
10330
+ comboboxData.onSearchValueChange("");
10331
+ setComboboxValue("");
10332
+ }
10333
+ }
10334
+ );
10335
+ };
10065
10336
  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]);
10337
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10073
10338
  const onSubmit = async () => {
10074
10339
  setIsSubmitting(true);
10075
10340
  let requestSucceeded = false;
10076
10341
  await requestOrderEdit(void 0, {
10077
10342
  onError: (e) => {
10078
- toast.error(`Failed to request order edit: ${e.message}`);
10343
+ toast.error(e.message);
10079
10344
  },
10080
10345
  onSuccess: () => {
10081
10346
  requestSucceeded = true;
@@ -10087,7 +10352,7 @@ const ItemsForm = ({ preview, currencyCode }) => {
10087
10352
  }
10088
10353
  await confirmOrderEdit(void 0, {
10089
10354
  onError: (e) => {
10090
- toast.error(`Failed to confirm order edit: ${e.message}`);
10355
+ toast.error(e.message);
10091
10356
  },
10092
10357
  onSuccess: () => {
10093
10358
  handleSuccess();
@@ -10097,1483 +10362,293 @@ const ItemsForm = ({ preview, currencyCode }) => {
10097
10362
  }
10098
10363
  });
10099
10364
  };
10100
- const onKeyDown = useCallback(
10101
- (e) => {
10102
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10103
- if (modalContent || isSubmitting) {
10104
- return;
10105
- }
10106
- onSubmit();
10107
- }
10108
- },
10109
- [modalContent, isSubmitting, onSubmit]
10110
- );
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);
10365
+ if (isError) {
10366
+ throw error;
10367
+ }
10368
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10369
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
10370
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
10371
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10372
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10373
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10374
+ ] }),
10375
+ /* @__PURE__ */ jsx(
10376
+ Combobox,
10377
+ {
10378
+ id: "promotion-combobox",
10379
+ "aria-describedby": "promotion-combobox-hint",
10380
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10381
+ fetchNextPage: comboboxData.fetchNextPage,
10382
+ options: comboboxData.options,
10383
+ onSearchValueChange: comboboxData.onSearchValueChange,
10384
+ searchValue: comboboxData.searchValue,
10385
+ disabled: comboboxData.disabled || isAddingPromotions,
10386
+ onChange: add,
10387
+ value: comboboxValue
10126
10388
  }
10389
+ )
10390
+ ] }),
10391
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10392
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
10393
+ PromotionItem,
10394
+ {
10395
+ promotion,
10396
+ orderId: preview.id,
10397
+ isLoading: isPending
10127
10398
  },
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
- ]
10227
- }
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" }) }),
10399
+ promotion.id
10400
+ )) })
10401
+ ] }) }),
10402
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10403
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10231
10404
  /* @__PURE__ */ jsx(
10232
10405
  Button,
10233
10406
  {
10234
10407
  size: "small",
10235
- type: "button",
10236
- onClick: onSubmit,
10237
- isLoading: isSubmitting,
10408
+ type: "submit",
10409
+ isLoading: isSubmitting || isAddingPromotions,
10238
10410
  children: "Save"
10239
10411
  }
10240
10412
  )
10241
10413
  ] }) })
10242
10414
  ] });
10243
10415
  };
10244
- const Item = ({ item, preview, currencyCode }) => {
10245
- if (item.variant_id) {
10246
- return /* @__PURE__ */ jsx(VariantItem, { item, preview, currencyCode });
10247
- }
10248
- return /* @__PURE__ */ jsx(CustomItem, { item, preview, currencyCode });
10249
- };
10250
- const VariantItem = ({ item, preview, currencyCode }) => {
10251
- const [editing, setEditing] = useState(false);
10252
- const form = useForm({
10253
- defaultValues: {
10254
- quantity: item.quantity,
10255
- unit_price: item.unit_price
10256
- },
10257
- resolver: zodResolver(variantItemSchema)
10258
- });
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;
10266
- 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(
10416
+ const PromotionItem = ({
10417
+ promotion,
10418
+ orderId,
10419
+ isLoading
10420
+ }) => {
10421
+ var _a;
10422
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10423
+ const onRemove = async () => {
10424
+ removePromotions(
10290
10425
  {
10291
- action_id: actionId,
10292
- quantity: data.quantity,
10293
- unit_price: convertNumber(data.unit_price)
10426
+ promo_codes: [promotion.code]
10294
10427
  },
10295
10428
  {
10296
- onSuccess: () => {
10297
- setEditing(false);
10298
- },
10299
10429
  onError: (e) => {
10300
10430
  toast.error(e.message);
10301
10431
  }
10302
10432
  }
10303
10433
  );
10304
- });
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,
10434
+ };
10435
+ const displayValue = getDisplayValue(promotion);
10436
+ return /* @__PURE__ */ jsxs(
10437
+ "div",
10438
+ {
10439
+ className: clx(
10440
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
10309
10441
  {
10310
- thumbnail: item.thumbnail,
10311
- alt: item.product_title ?? void 0
10442
+ "animate-pulse": isLoading
10312
10443
  }
10313
10444
  ),
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,
10445
+ children: [
10446
+ /* @__PURE__ */ jsxs("div", { children: [
10447
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
10448
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
10449
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
10450
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
10451
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
10452
+ ] }),
10453
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
10454
+ ] })
10455
+ ] }),
10456
+ /* @__PURE__ */ jsx(
10457
+ IconButton,
10333
10458
  {
10334
10459
  size: "small",
10335
- leading: "compact",
10336
- className: "text-ui-fg-subtle",
10337
- children: item.variant_sku
10460
+ type: "button",
10461
+ variant: "transparent",
10462
+ onClick: onRemove,
10463
+ isLoading: isPending || isLoading,
10464
+ children: /* @__PURE__ */ jsx(XMark, {})
10338
10465
  }
10339
10466
  )
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 }) }) });
10467
+ ]
10468
+ },
10469
+ promotion.id
10470
+ );
10471
+ };
10472
+ function getDisplayValue(promotion) {
10473
+ var _a, _b, _c, _d;
10474
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
10475
+ if (!value) {
10476
+ return null;
10477
+ }
10478
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
10479
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
10480
+ if (!currency) {
10481
+ return null;
10482
+ }
10483
+ return getLocaleAmount(value, currency);
10484
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
10485
+ return formatPercentage(value);
10486
+ }
10487
+ return null;
10488
+ }
10489
+ const formatter = new Intl.NumberFormat([], {
10490
+ style: "percent",
10491
+ minimumFractionDigits: 2
10492
+ });
10493
+ const formatPercentage = (value, isPercentageValue = false) => {
10494
+ let val = value || 0;
10495
+ if (!isPercentageValue) {
10496
+ val = val / 100;
10497
+ }
10498
+ return formatter.format(val);
10499
+ };
10500
+ function getPromotionIds(items, shippingMethods) {
10501
+ const promotionIds = /* @__PURE__ */ new Set();
10502
+ for (const item of items) {
10503
+ if (item.adjustments) {
10504
+ for (const adjustment of item.adjustments) {
10505
+ if (adjustment.promotion_id) {
10506
+ promotionIds.add(adjustment.promotion_id);
10349
10507
  }
10350
10508
  }
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,
10360
- {
10361
- ...field,
10362
- symbol: getNativeSymbol(currencyCode),
10363
- code: currencyCode,
10364
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10365
- }
10366
- ) }) });
10509
+ }
10510
+ }
10511
+ for (const shippingMethod of shippingMethods) {
10512
+ if (shippingMethod.adjustments) {
10513
+ for (const adjustment of shippingMethod.adjustments) {
10514
+ if (adjustment.promotion_id) {
10515
+ promotionIds.add(adjustment.promotion_id);
10367
10516
  }
10368
10517
  }
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, {})
10380
- }
10381
- )
10382
- ] }) }) });
10518
+ }
10519
+ }
10520
+ return Array.from(promotionIds);
10521
+ }
10522
+ function convertNumber(value) {
10523
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10524
+ }
10525
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
10526
+ const Shipping = () => {
10527
+ var _a;
10528
+ const { id } = useParams();
10529
+ const { order, isPending, isError, error } = useOrder(id, {
10530
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
10531
+ });
10532
+ const {
10533
+ order: preview,
10534
+ isPending: isPreviewPending,
10535
+ isError: isPreviewError,
10536
+ error: previewError
10537
+ } = useOrderPreview(id);
10538
+ useInitiateOrderEdit({ preview });
10539
+ const { onCancel } = useCancelOrderEdit({ preview });
10540
+ if (isError) {
10541
+ throw error;
10542
+ }
10543
+ if (isPreviewError) {
10544
+ throw previewError;
10545
+ }
10546
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
10547
+ const isReady = preview && !isPreviewPending && order && !isPending;
10548
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
10549
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10550
+ /* @__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: [
10551
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10552
+ /* @__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." }) })
10553
+ ] }) }) }),
10554
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
10555
+ ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
10556
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
10557
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10558
+ ] }) });
10383
10559
  };
10384
- const variantItemSchema = objectType({
10385
- quantity: numberType(),
10386
- unit_price: unionType([numberType(), stringType()])
10387
- });
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
10560
+ const ShippingForm = ({ preview, order }) => {
10561
+ var _a;
10562
+ const { setIsOpen } = useStackedModal();
10563
+ const [isSubmitting, setIsSubmitting] = useState(false);
10564
+ const [data, setData] = useState(null);
10565
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
10566
+ const { shipping_options } = useShippingOptions(
10567
+ {
10568
+ id: appliedShippingOptionIds,
10569
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
10396
10570
  },
10397
- resolver: zodResolver(customItemSchema)
10398
- });
10399
- useEffect(() => {
10400
- form.reset({
10401
- title,
10402
- quantity,
10403
- unit_price
10571
+ {
10572
+ enabled: appliedShippingOptionIds.length > 0
10573
+ }
10574
+ );
10575
+ const uniqueShippingProfiles = useMemo(() => {
10576
+ const profiles = /* @__PURE__ */ new Map();
10577
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
10578
+ profiles.set(profile.id, profile);
10404
10579
  });
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);
10580
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
10581
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
10582
+ });
10583
+ return Array.from(profiles.values());
10584
+ }, [order.items, shipping_options]);
10585
+ const { handleSuccess } = useRouteModal();
10586
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10587
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10588
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
10589
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
10590
+ const onSubmit = async () => {
10591
+ setIsSubmitting(true);
10592
+ let requestSucceeded = false;
10593
+ await requestOrderEdit(void 0, {
10594
+ onError: (e) => {
10595
+ toast.error(`Failed to request order edit: ${e.message}`);
10596
+ },
10597
+ onSuccess: () => {
10598
+ requestSucceeded = true;
10599
+ }
10600
+ });
10601
+ if (!requestSucceeded) {
10602
+ setIsSubmitting(false);
10417
10603
  return;
10418
10604
  }
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
- }
10605
+ await confirmOrderEdit(void 0, {
10606
+ onError: (e) => {
10607
+ toast.error(`Failed to confirm order edit: ${e.message}`);
10608
+ },
10609
+ onSuccess: () => {
10610
+ handleSuccess();
10611
+ },
10612
+ onSettled: () => {
10613
+ setIsSubmitting(false);
10614
+ }
10615
+ });
10616
+ };
10617
+ const onKeydown = useCallback(
10618
+ (e) => {
10619
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10620
+ if (data || isSubmitting) {
10621
+ return;
10433
10622
  }
10434
- );
10435
- return;
10436
- }
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
- });
10446
- return;
10447
- }
10448
- await updateActionItem(
10449
- {
10450
- action_id: actionId,
10451
- quantity: data.quantity,
10452
- unit_price: convertNumber(data.unit_price)
10453
- },
10454
- {
10455
- onSuccess: () => {
10456
- setEditing(false);
10457
- },
10458
- onError: (e) => {
10459
- toast.error(e.message);
10460
- }
10461
- }
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, {})
10623
+ onSubmit();
10522
10624
  }
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
10625
  },
10568
- {
10569
- placeholderData: keepPreviousData
10570
- }
10626
+ [data, isSubmitting, onSubmit]
10571
10627
  );
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." }) })
10628
+ useEffect(() => {
10629
+ document.addEventListener("keydown", onKeydown);
10630
+ return () => {
10631
+ document.removeEventListener("keydown", onKeydown);
10632
+ };
10633
+ }, [onKeydown]);
10634
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10635
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
10636
+ /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
10637
+ /* @__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: [
10638
+ /* @__PURE__ */ jsxs("div", { children: [
10639
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
10640
+ /* @__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
10641
  ] }),
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: [
10642
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10643
+ /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
10644
+ /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
10654
10645
  /* @__PURE__ */ jsx(
10655
- Thumbnail,
10646
+ Text,
10656
10647
  {
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 STACKED_FOCUS_MODAL_ID = "shipping-form";
11451
- const Shipping = () => {
11452
- var _a;
11453
- const { id } = useParams();
11454
- const { order, isPending, isError, error } = useOrder(id, {
11455
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11456
- });
11457
- const {
11458
- order: preview,
11459
- isPending: isPreviewPending,
11460
- isError: isPreviewError,
11461
- error: previewError
11462
- } = useOrderPreview(id);
11463
- useInitiateOrderEdit({ preview });
11464
- const { onCancel } = useCancelOrderEdit({ preview });
11465
- if (isError) {
11466
- throw error;
11467
- }
11468
- if (isPreviewError) {
11469
- throw previewError;
11470
- }
11471
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11472
- const isReady = preview && !isPreviewPending && order && !isPending;
11473
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11474
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11475
- /* @__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: [
11476
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11477
- /* @__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." }) })
11478
- ] }) }) }),
11479
- /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11480
- ] }) : isReady ? /* @__PURE__ */ jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxs("div", { children: [
11481
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11482
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11483
- ] }) });
11484
- };
11485
- const ShippingForm = ({ preview, order }) => {
11486
- var _a;
11487
- const { setIsOpen } = useStackedModal();
11488
- const [isSubmitting, setIsSubmitting] = useState(false);
11489
- const [data, setData] = useState(null);
11490
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11491
- const { shipping_options } = useShippingOptions(
11492
- {
11493
- id: appliedShippingOptionIds,
11494
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11495
- },
11496
- {
11497
- enabled: appliedShippingOptionIds.length > 0
11498
- }
11499
- );
11500
- const uniqueShippingProfiles = useMemo(() => {
11501
- const profiles = /* @__PURE__ */ new Map();
11502
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11503
- profiles.set(profile.id, profile);
11504
- });
11505
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11506
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11507
- });
11508
- return Array.from(profiles.values());
11509
- }, [order.items, shipping_options]);
11510
- const { handleSuccess } = useRouteModal();
11511
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11512
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11513
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11514
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11515
- const onSubmit = async () => {
11516
- setIsSubmitting(true);
11517
- let requestSucceeded = false;
11518
- await requestOrderEdit(void 0, {
11519
- onError: (e) => {
11520
- toast.error(`Failed to request order edit: ${e.message}`);
11521
- },
11522
- onSuccess: () => {
11523
- requestSucceeded = true;
11524
- }
11525
- });
11526
- if (!requestSucceeded) {
11527
- setIsSubmitting(false);
11528
- return;
11529
- }
11530
- await confirmOrderEdit(void 0, {
11531
- onError: (e) => {
11532
- toast.error(`Failed to confirm order edit: ${e.message}`);
11533
- },
11534
- onSuccess: () => {
11535
- handleSuccess();
11536
- },
11537
- onSettled: () => {
11538
- setIsSubmitting(false);
11539
- }
11540
- });
11541
- };
11542
- const onKeydown = useCallback(
11543
- (e) => {
11544
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11545
- if (data || isSubmitting) {
11546
- return;
11547
- }
11548
- onSubmit();
11549
- }
11550
- },
11551
- [data, isSubmitting, onSubmit]
11552
- );
11553
- useEffect(() => {
11554
- document.addEventListener("keydown", onKeydown);
11555
- return () => {
11556
- document.removeEventListener("keydown", onKeydown);
11557
- };
11558
- }, [onKeydown]);
11559
- return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11560
- /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
11561
- /* @__PURE__ */ jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11562
- /* @__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: [
11563
- /* @__PURE__ */ jsxs("div", { children: [
11564
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Shipping" }) }),
11565
- /* @__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." }) })
11566
- ] }),
11567
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11568
- /* @__PURE__ */ jsx(Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11569
- /* @__PURE__ */ jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11570
- /* @__PURE__ */ jsx(
11571
- Text,
11572
- {
11573
- size: "xsmall",
11574
- weight: "plus",
11575
- className: "text-ui-fg-muted",
11576
- children: "Shipping profile"
10648
+ size: "xsmall",
10649
+ weight: "plus",
10650
+ className: "text-ui-fg-muted",
10651
+ children: "Shipping profile"
11577
10652
  }
11578
10653
  ),
11579
10654
  /* @__PURE__ */ jsx(
@@ -11754,7 +10829,7 @@ const ShippingForm = ({ preview, order }) => {
11754
10829
  ]
11755
10830
  }
11756
10831
  ) : /* @__PURE__ */ jsx(
11757
- StackedModalTrigger,
10832
+ StackedModalTrigger$1,
11758
10833
  {
11759
10834
  shippingProfileId: profile.id,
11760
10835
  shippingOption,
@@ -11865,7 +10940,7 @@ const ShippingForm = ({ preview, order }) => {
11865
10940
  ] }) })
11866
10941
  ] });
11867
10942
  };
11868
- const StackedModalTrigger = ({
10943
+ const StackedModalTrigger$1 = ({
11869
10944
  shippingProfileId,
11870
10945
  shippingOption,
11871
10946
  shippingMethod,
@@ -12233,230 +11308,27 @@ const CustomAmountField = ({
12233
11308
  Form$2.Field,
12234
11309
  {
12235
11310
  control,
12236
- name: "custom_amount",
12237
- render: ({ field: { onChange, ...field } }) => {
12238
- return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12239
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12240
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
12241
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12242
- ] }),
12243
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12244
- CurrencyInput,
12245
- {
12246
- ...field,
12247
- onValueChange: (value) => onChange(value),
12248
- symbol: getNativeSymbol(currencyCode),
12249
- code: currencyCode
12250
- }
12251
- ) })
12252
- ] });
12253
- }
12254
- }
12255
- );
12256
- };
12257
- const ShippingAddress = () => {
12258
- const { id } = useParams();
12259
- const { order, isPending, isError, error } = useOrder(id, {
12260
- fields: "+shipping_address"
12261
- });
12262
- if (isError) {
12263
- throw error;
12264
- }
12265
- const isReady = !isPending && !!order;
12266
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12267
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12268
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12269
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12270
- ] }),
12271
- isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12272
- ] });
12273
- };
12274
- const ShippingAddressForm = ({ order }) => {
12275
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12276
- const form = useForm({
12277
- defaultValues: {
12278
- first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12279
- last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12280
- company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12281
- address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12282
- address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12283
- city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12284
- province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12285
- country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12286
- postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12287
- phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12288
- },
12289
- resolver: zodResolver(schema$2)
12290
- });
12291
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12292
- const { handleSuccess } = useRouteModal();
12293
- const onSubmit = form.handleSubmit(async (data) => {
12294
- await mutateAsync(
12295
- {
12296
- shipping_address: {
12297
- first_name: data.first_name,
12298
- last_name: data.last_name,
12299
- company: data.company,
12300
- address_1: data.address_1,
12301
- address_2: data.address_2,
12302
- city: data.city,
12303
- province: data.province,
12304
- country_code: data.country_code,
12305
- postal_code: data.postal_code,
12306
- phone: data.phone
12307
- }
12308
- },
12309
- {
12310
- onSuccess: () => {
12311
- handleSuccess();
12312
- },
12313
- onError: (error) => {
12314
- toast.error(error.message);
12315
- }
12316
- }
12317
- );
12318
- });
12319
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12320
- KeyboundForm,
12321
- {
12322
- className: "flex flex-1 flex-col overflow-hidden",
12323
- onSubmit,
12324
- children: [
12325
- /* @__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: [
12326
- /* @__PURE__ */ jsx(
12327
- Form$2.Field,
12328
- {
12329
- control: form.control,
12330
- name: "country_code",
12331
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12332
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12333
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12334
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12335
- ] })
12336
- }
12337
- ),
12338
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12339
- /* @__PURE__ */ jsx(
12340
- Form$2.Field,
12341
- {
12342
- control: form.control,
12343
- name: "first_name",
12344
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12345
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12346
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12347
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12348
- ] })
12349
- }
12350
- ),
12351
- /* @__PURE__ */ jsx(
12352
- Form$2.Field,
12353
- {
12354
- control: form.control,
12355
- name: "last_name",
12356
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12357
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12358
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12359
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12360
- ] })
12361
- }
12362
- )
12363
- ] }),
12364
- /* @__PURE__ */ jsx(
12365
- Form$2.Field,
12366
- {
12367
- control: form.control,
12368
- name: "company",
12369
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12370
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12371
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12372
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12373
- ] })
12374
- }
12375
- ),
12376
- /* @__PURE__ */ jsx(
12377
- Form$2.Field,
12378
- {
12379
- control: form.control,
12380
- name: "address_1",
12381
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12382
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12383
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12384
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12385
- ] })
12386
- }
12387
- ),
12388
- /* @__PURE__ */ jsx(
12389
- Form$2.Field,
12390
- {
12391
- control: form.control,
12392
- name: "address_2",
12393
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12394
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12395
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12396
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12397
- ] })
12398
- }
12399
- ),
12400
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12401
- /* @__PURE__ */ jsx(
12402
- Form$2.Field,
12403
- {
12404
- control: form.control,
12405
- name: "postal_code",
12406
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12407
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12408
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12409
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12410
- ] })
12411
- }
12412
- ),
12413
- /* @__PURE__ */ jsx(
12414
- Form$2.Field,
12415
- {
12416
- control: form.control,
12417
- name: "city",
12418
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12419
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
12420
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12421
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12422
- ] })
12423
- }
12424
- )
11311
+ name: "custom_amount",
11312
+ render: ({ field: { onChange, ...field } }) => {
11313
+ return /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
11314
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11315
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Custom amount" }),
11316
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Set a custom amount for the shipping option." })
12425
11317
  ] }),
12426
- /* @__PURE__ */ jsx(
12427
- Form$2.Field,
12428
- {
12429
- control: form.control,
12430
- name: "province",
12431
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12432
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12433
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12434
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12435
- ] })
12436
- }
12437
- ),
12438
- /* @__PURE__ */ jsx(
12439
- Form$2.Field,
11318
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11319
+ CurrencyInput,
12440
11320
  {
12441
- control: form.control,
12442
- name: "phone",
12443
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12444
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
12445
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12446
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12447
- ] })
11321
+ ...field,
11322
+ onValueChange: (value) => onChange(value),
11323
+ symbol: getNativeSymbol(currencyCode),
11324
+ code: currencyCode
12448
11325
  }
12449
- )
12450
- ] }) }),
12451
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12452
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12453
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12454
- ] }) })
12455
- ]
11326
+ ) })
11327
+ ] });
11328
+ }
12456
11329
  }
12457
- ) });
11330
+ );
12458
11331
  };
12459
- const schema$2 = addressSchema;
12460
11332
  const TransferOwnership = () => {
12461
11333
  const { id } = useParams();
12462
11334
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12480,7 +11352,7 @@ const TransferOwnershipForm = ({ order }) => {
12480
11352
  defaultValues: {
12481
11353
  customer_id: order.customer_id || ""
12482
11354
  },
12483
- resolver: zodResolver(schema$1)
11355
+ resolver: zodResolver(schema$2)
12484
11356
  });
12485
11357
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12486
11358
  const { handleSuccess } = useRouteModal();
@@ -12806,160 +11678,1195 @@ const Illustration = () => {
12806
11678
  /* @__PURE__ */ jsx(
12807
11679
  "path",
12808
11680
  {
12809
- 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",
12810
- fill: "#52525B"
12811
- }
11681
+ 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",
11682
+ fill: "#52525B"
11683
+ }
11684
+ ),
11685
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsx(
11686
+ "path",
11687
+ {
11688
+ d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
11689
+ stroke: "#A1A1AA",
11690
+ strokeWidth: "1.5",
11691
+ strokeLinecap: "round",
11692
+ strokeLinejoin: "round"
11693
+ }
11694
+ ) }),
11695
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsx(
11696
+ "path",
11697
+ {
11698
+ d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
11699
+ stroke: "#A1A1AA",
11700
+ strokeWidth: "1.5",
11701
+ strokeLinecap: "round",
11702
+ strokeLinejoin: "round"
11703
+ }
11704
+ ) }),
11705
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsx(
11706
+ "path",
11707
+ {
11708
+ d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
11709
+ stroke: "#A1A1AA",
11710
+ strokeWidth: "1.5",
11711
+ strokeLinecap: "round",
11712
+ strokeLinejoin: "round"
11713
+ }
11714
+ ) }),
11715
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsx(
11716
+ "path",
11717
+ {
11718
+ d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
11719
+ stroke: "#A1A1AA",
11720
+ strokeWidth: "1.5",
11721
+ strokeLinecap: "round",
11722
+ strokeLinejoin: "round"
11723
+ }
11724
+ ) }),
11725
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsx(
11726
+ "path",
11727
+ {
11728
+ d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
11729
+ stroke: "#A1A1AA",
11730
+ strokeWidth: "1.5",
11731
+ strokeLinecap: "round",
11732
+ strokeLinejoin: "round"
11733
+ }
11734
+ ) }),
11735
+ /* @__PURE__ */ jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsx(
11736
+ "path",
11737
+ {
11738
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
11739
+ stroke: "#A1A1AA",
11740
+ strokeWidth: "1.5",
11741
+ strokeLinecap: "round",
11742
+ strokeLinejoin: "round"
11743
+ }
11744
+ ) }),
11745
+ /* @__PURE__ */ jsxs("defs", { children: [
11746
+ /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
11747
+ "rect",
11748
+ {
11749
+ width: "12",
11750
+ height: "12",
11751
+ fill: "white",
11752
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
11753
+ }
11754
+ ) }),
11755
+ /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
11756
+ "rect",
11757
+ {
11758
+ width: "12",
11759
+ height: "12",
11760
+ fill: "white",
11761
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
11762
+ }
11763
+ ) }),
11764
+ /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
11765
+ "rect",
11766
+ {
11767
+ width: "12",
11768
+ height: "12",
11769
+ fill: "white",
11770
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
11771
+ }
11772
+ ) }),
11773
+ /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
11774
+ "rect",
11775
+ {
11776
+ width: "12",
11777
+ height: "12",
11778
+ fill: "white",
11779
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
11780
+ }
11781
+ ) }),
11782
+ /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
11783
+ "rect",
11784
+ {
11785
+ width: "12",
11786
+ height: "12",
11787
+ fill: "white",
11788
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
11789
+ }
11790
+ ) }),
11791
+ /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
11792
+ "rect",
11793
+ {
11794
+ width: "12",
11795
+ height: "12",
11796
+ fill: "white",
11797
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
11798
+ }
11799
+ ) })
11800
+ ] })
11801
+ ]
11802
+ }
11803
+ );
11804
+ };
11805
+ const schema$2 = objectType({
11806
+ customer_id: stringType().min(1)
11807
+ });
11808
+ const SalesChannel = () => {
11809
+ const { id } = useParams();
11810
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11811
+ id,
11812
+ {
11813
+ fields: "+sales_channel_id"
11814
+ },
11815
+ {
11816
+ enabled: !!id
11817
+ }
11818
+ );
11819
+ if (isError) {
11820
+ throw error;
11821
+ }
11822
+ const ISrEADY = !!draft_order && !isPending;
11823
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11824
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11825
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
11826
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
11827
+ ] }),
11828
+ ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
11829
+ ] });
11830
+ };
11831
+ const SalesChannelForm = ({ order }) => {
11832
+ const form = useForm({
11833
+ defaultValues: {
11834
+ sales_channel_id: order.sales_channel_id || ""
11835
+ },
11836
+ resolver: zodResolver(schema$1)
11837
+ });
11838
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11839
+ const { handleSuccess } = useRouteModal();
11840
+ const onSubmit = form.handleSubmit(async (data) => {
11841
+ await mutateAsync(
11842
+ {
11843
+ sales_channel_id: data.sales_channel_id
11844
+ },
11845
+ {
11846
+ onSuccess: () => {
11847
+ toast.success("Sales channel updated");
11848
+ handleSuccess();
11849
+ },
11850
+ onError: (error) => {
11851
+ toast.error(error.message);
11852
+ }
11853
+ }
11854
+ );
11855
+ });
11856
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11857
+ KeyboundForm,
11858
+ {
11859
+ className: "flex flex-1 flex-col overflow-hidden",
11860
+ onSubmit,
11861
+ children: [
11862
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
11863
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11864
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11865
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11866
+ ] }) })
11867
+ ]
11868
+ }
11869
+ ) });
11870
+ };
11871
+ const SalesChannelField = ({ control, order }) => {
11872
+ const salesChannels = useComboboxData({
11873
+ queryFn: async (params) => {
11874
+ return await sdk.admin.salesChannel.list(params);
11875
+ },
11876
+ queryKey: ["sales-channels"],
11877
+ getOptions: (data) => {
11878
+ return data.sales_channels.map((salesChannel) => ({
11879
+ label: salesChannel.name,
11880
+ value: salesChannel.id
11881
+ }));
11882
+ },
11883
+ defaultValue: order.sales_channel_id || void 0
11884
+ });
11885
+ return /* @__PURE__ */ jsx(
11886
+ Form$2.Field,
11887
+ {
11888
+ control,
11889
+ name: "sales_channel_id",
11890
+ render: ({ field }) => {
11891
+ return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
11892
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
11893
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11894
+ Combobox,
11895
+ {
11896
+ options: salesChannels.options,
11897
+ fetchNextPage: salesChannels.fetchNextPage,
11898
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
11899
+ searchValue: salesChannels.searchValue,
11900
+ onSearchValueChange: salesChannels.onSearchValueChange,
11901
+ placeholder: "Select sales channel",
11902
+ ...field
11903
+ }
11904
+ ) }),
11905
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
11906
+ ] });
11907
+ }
11908
+ }
11909
+ );
11910
+ };
11911
+ const schema$1 = objectType({
11912
+ sales_channel_id: stringType().min(1)
11913
+ });
11914
+ const NumberInput = forwardRef(
11915
+ ({
11916
+ value,
11917
+ onChange,
11918
+ size = "base",
11919
+ min = 0,
11920
+ max = 100,
11921
+ step = 1,
11922
+ className,
11923
+ disabled,
11924
+ ...props
11925
+ }, ref) => {
11926
+ const handleChange = (event) => {
11927
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
11928
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
11929
+ onChange(newValue);
11930
+ }
11931
+ };
11932
+ const handleIncrement = () => {
11933
+ const newValue = value + step;
11934
+ if (max === void 0 || newValue <= max) {
11935
+ onChange(newValue);
11936
+ }
11937
+ };
11938
+ const handleDecrement = () => {
11939
+ const newValue = value - step;
11940
+ if (min === void 0 || newValue >= min) {
11941
+ onChange(newValue);
11942
+ }
11943
+ };
11944
+ return /* @__PURE__ */ jsxs(
11945
+ "div",
11946
+ {
11947
+ className: clx(
11948
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
11949
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
11950
+ {
11951
+ "h-7": size === "small",
11952
+ "h-8": size === "base"
11953
+ },
11954
+ className
12812
11955
  ),
12813
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsx(
12814
- "path",
12815
- {
12816
- d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
12817
- stroke: "#A1A1AA",
12818
- strokeWidth: "1.5",
12819
- strokeLinecap: "round",
12820
- strokeLinejoin: "round"
11956
+ children: [
11957
+ /* @__PURE__ */ jsx(
11958
+ "input",
11959
+ {
11960
+ ref,
11961
+ type: "number",
11962
+ value,
11963
+ onChange: handleChange,
11964
+ min,
11965
+ max,
11966
+ step,
11967
+ className: clx(
11968
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
11969
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
11970
+ "placeholder:text-ui-fg-muted"
11971
+ ),
11972
+ ...props
11973
+ }
11974
+ ),
11975
+ /* @__PURE__ */ jsxs(
11976
+ "button",
11977
+ {
11978
+ className: clx(
11979
+ "flex items-center justify-center outline-none transition-fg",
11980
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
11981
+ "focus:bg-ui-bg-field-component-hover",
11982
+ "hover:bg-ui-bg-field-component-hover",
11983
+ {
11984
+ "size-7": size === "small",
11985
+ "size-8": size === "base"
11986
+ }
11987
+ ),
11988
+ type: "button",
11989
+ onClick: handleDecrement,
11990
+ disabled: min !== void 0 && value <= min || disabled,
11991
+ children: [
11992
+ /* @__PURE__ */ jsx(Minus, {}),
11993
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
11994
+ ]
11995
+ }
11996
+ ),
11997
+ /* @__PURE__ */ jsxs(
11998
+ "button",
11999
+ {
12000
+ className: clx(
12001
+ "flex items-center justify-center outline-none transition-fg",
12002
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
12003
+ "focus:bg-ui-bg-field-hover",
12004
+ "hover:bg-ui-bg-field-hover",
12005
+ {
12006
+ "size-7": size === "small",
12007
+ "size-8": size === "base"
12008
+ }
12009
+ ),
12010
+ type: "button",
12011
+ onClick: handleIncrement,
12012
+ disabled: max !== void 0 && value >= max || disabled,
12013
+ children: [
12014
+ /* @__PURE__ */ jsx(Plus, {}),
12015
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
12016
+ ]
12017
+ }
12018
+ )
12019
+ ]
12020
+ }
12021
+ );
12022
+ }
12023
+ );
12024
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
12025
+ const productVariantsQueryKeys = {
12026
+ list: (query2) => [
12027
+ PRODUCT_VARIANTS_QUERY_KEY,
12028
+ query2 ? query2 : void 0
12029
+ ]
12030
+ };
12031
+ const useProductVariants = (query2, options) => {
12032
+ const { data, ...rest } = useQuery({
12033
+ queryKey: productVariantsQueryKeys.list(query2),
12034
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
12035
+ ...options
12036
+ });
12037
+ return { ...data, ...rest };
12038
+ };
12039
+ const STACKED_MODAL_ID = "items_stacked_modal";
12040
+ const Items = () => {
12041
+ const { id } = useParams();
12042
+ const {
12043
+ order: preview,
12044
+ isPending: isPreviewPending,
12045
+ isError: isPreviewError,
12046
+ error: previewError
12047
+ } = useOrderPreview(id, void 0, {
12048
+ placeholderData: keepPreviousData
12049
+ });
12050
+ useInitiateOrderEdit({ preview });
12051
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12052
+ id,
12053
+ {
12054
+ fields: "currency_code"
12055
+ },
12056
+ {
12057
+ enabled: !!id
12058
+ }
12059
+ );
12060
+ const { onCancel } = useCancelOrderEdit({ preview });
12061
+ if (isError) {
12062
+ throw error;
12063
+ }
12064
+ if (isPreviewError) {
12065
+ throw previewError;
12066
+ }
12067
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
12068
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
12069
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
12070
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
12071
+ ] }) });
12072
+ };
12073
+ const ItemsForm = ({ preview, currencyCode }) => {
12074
+ var _a;
12075
+ const [isSubmitting, setIsSubmitting] = useState(false);
12076
+ const [modalContent, setModalContent] = useState(
12077
+ null
12078
+ );
12079
+ const { handleSuccess } = useRouteModal();
12080
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
12081
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
12082
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
12083
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
12084
+ const matches = useMemo(() => {
12085
+ return matchSorter(preview.items, query2, {
12086
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
12087
+ });
12088
+ }, [preview.items, query2]);
12089
+ const onSubmit = async () => {
12090
+ setIsSubmitting(true);
12091
+ let requestSucceeded = false;
12092
+ await requestOrderEdit(void 0, {
12093
+ onError: (e) => {
12094
+ toast.error(`Failed to request order edit: ${e.message}`);
12095
+ },
12096
+ onSuccess: () => {
12097
+ requestSucceeded = true;
12098
+ }
12099
+ });
12100
+ if (!requestSucceeded) {
12101
+ setIsSubmitting(false);
12102
+ return;
12103
+ }
12104
+ await confirmOrderEdit(void 0, {
12105
+ onError: (e) => {
12106
+ toast.error(`Failed to confirm order edit: ${e.message}`);
12107
+ },
12108
+ onSuccess: () => {
12109
+ handleSuccess();
12110
+ },
12111
+ onSettled: () => {
12112
+ setIsSubmitting(false);
12113
+ }
12114
+ });
12115
+ };
12116
+ const onKeyDown = useCallback(
12117
+ (e) => {
12118
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
12119
+ if (modalContent || isSubmitting) {
12120
+ return;
12121
+ }
12122
+ onSubmit();
12123
+ }
12124
+ },
12125
+ [modalContent, isSubmitting, onSubmit]
12126
+ );
12127
+ useEffect(() => {
12128
+ document.addEventListener("keydown", onKeyDown);
12129
+ return () => {
12130
+ document.removeEventListener("keydown", onKeyDown);
12131
+ };
12132
+ }, [onKeyDown]);
12133
+ return /* @__PURE__ */ jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
12134
+ /* @__PURE__ */ jsx(RouteFocusModal.Header, {}),
12135
+ /* @__PURE__ */ jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxs(
12136
+ StackedFocusModal,
12137
+ {
12138
+ id: STACKED_MODAL_ID,
12139
+ onOpenChangeCallback: (open) => {
12140
+ if (!open) {
12141
+ setModalContent(null);
12821
12142
  }
12822
- ) }),
12823
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsx(
12824
- "path",
12825
- {
12826
- d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
12827
- stroke: "#A1A1AA",
12828
- strokeWidth: "1.5",
12829
- strokeLinecap: "round",
12830
- strokeLinejoin: "round"
12143
+ },
12144
+ children: [
12145
+ /* @__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: [
12146
+ /* @__PURE__ */ jsxs("div", { children: [
12147
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Items" }) }),
12148
+ /* @__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" }) })
12149
+ ] }),
12150
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12151
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-6", children: [
12152
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
12153
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12154
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
12155
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
12156
+ ] }),
12157
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-2", children: [
12158
+ /* @__PURE__ */ jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsx(
12159
+ Input,
12160
+ {
12161
+ type: "search",
12162
+ placeholder: "Search items",
12163
+ value: searchValue,
12164
+ onChange: (e) => onSearchValueChange(e.target.value)
12165
+ }
12166
+ ) }),
12167
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
12168
+ /* @__PURE__ */ jsx(DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(IconButton, { type: "button", children: /* @__PURE__ */ jsx(Plus, {}) }) }),
12169
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
12170
+ /* @__PURE__ */ jsx(
12171
+ StackedModalTrigger,
12172
+ {
12173
+ type: "add-items",
12174
+ setModalContent
12175
+ }
12176
+ ),
12177
+ /* @__PURE__ */ jsx(
12178
+ StackedModalTrigger,
12179
+ {
12180
+ type: "add-custom-item",
12181
+ setModalContent
12182
+ }
12183
+ )
12184
+ ] })
12185
+ ] })
12186
+ ] })
12187
+ ] }),
12188
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12189
+ /* @__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: [
12190
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Item" }) }),
12191
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Quantity" }) }),
12192
+ /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: "Price" }) }),
12193
+ /* @__PURE__ */ jsx("div", {})
12194
+ ] }) }),
12195
+ /* @__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: [
12196
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
12197
+ /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
12198
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsx(
12199
+ Item,
12200
+ {
12201
+ item,
12202
+ preview,
12203
+ currencyCode
12204
+ },
12205
+ item.id
12206
+ )) : /* @__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: [
12207
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12208
+ /* @__PURE__ */ jsxs(Text, { size: "small", className: "text-ui-fg-subtle", children: [
12209
+ 'No items found for "',
12210
+ query2,
12211
+ '".'
12212
+ ] })
12213
+ ] }) })
12214
+ ] })
12215
+ ] }),
12216
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12217
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
12218
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
12219
+ /* @__PURE__ */ jsx("div", { children: /* @__PURE__ */ jsxs(
12220
+ Text,
12221
+ {
12222
+ size: "small",
12223
+ leading: "compact",
12224
+ className: "text-ui-fg-subtle",
12225
+ children: [
12226
+ itemCount,
12227
+ " ",
12228
+ itemCount === 1 ? "item" : "items"
12229
+ ]
12230
+ }
12231
+ ) }),
12232
+ /* @__PURE__ */ jsx("div", { className: "text-right", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: getStylizedAmount(preview.item_subtotal, currencyCode) }) })
12233
+ ] })
12234
+ ] }) }),
12235
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsx(
12236
+ CustomItemForm,
12237
+ {
12238
+ orderId: preview.id,
12239
+ currencyCode
12240
+ }
12241
+ ) : null)
12242
+ ]
12243
+ }
12244
+ ) }),
12245
+ /* @__PURE__ */ jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12246
+ /* @__PURE__ */ jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12247
+ /* @__PURE__ */ jsx(
12248
+ Button,
12249
+ {
12250
+ size: "small",
12251
+ type: "button",
12252
+ onClick: onSubmit,
12253
+ isLoading: isSubmitting,
12254
+ children: "Save"
12255
+ }
12256
+ )
12257
+ ] }) })
12258
+ ] });
12259
+ };
12260
+ const Item = ({ item, preview, currencyCode }) => {
12261
+ if (item.variant_id) {
12262
+ return /* @__PURE__ */ jsx(VariantItem, { item, preview, currencyCode });
12263
+ }
12264
+ return /* @__PURE__ */ jsx(CustomItem, { item, preview, currencyCode });
12265
+ };
12266
+ const VariantItem = ({ item, preview, currencyCode }) => {
12267
+ const [editing, setEditing] = useState(false);
12268
+ const form = useForm({
12269
+ defaultValues: {
12270
+ quantity: item.quantity,
12271
+ unit_price: item.unit_price
12272
+ },
12273
+ resolver: zodResolver(variantItemSchema)
12274
+ });
12275
+ const actionId = useMemo(() => {
12276
+ var _a, _b;
12277
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12278
+ }, [item]);
12279
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12280
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12281
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12282
+ const onSubmit = form.handleSubmit(async (data) => {
12283
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
12284
+ setEditing(false);
12285
+ return;
12286
+ }
12287
+ if (!actionId) {
12288
+ await updateOriginalItem(
12289
+ {
12290
+ item_id: item.id,
12291
+ quantity: data.quantity,
12292
+ unit_price: convertNumber(data.unit_price)
12293
+ },
12294
+ {
12295
+ onSuccess: () => {
12296
+ setEditing(false);
12297
+ },
12298
+ onError: (e) => {
12299
+ toast.error(e.message);
12831
12300
  }
12832
- ) }),
12833
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsx(
12834
- "path",
12301
+ }
12302
+ );
12303
+ return;
12304
+ }
12305
+ await updateActionItem(
12306
+ {
12307
+ action_id: actionId,
12308
+ quantity: data.quantity,
12309
+ unit_price: convertNumber(data.unit_price)
12310
+ },
12311
+ {
12312
+ onSuccess: () => {
12313
+ setEditing(false);
12314
+ },
12315
+ onError: (e) => {
12316
+ toast.error(e.message);
12317
+ }
12318
+ }
12319
+ );
12320
+ });
12321
+ 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: [
12322
+ /* @__PURE__ */ jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
12323
+ /* @__PURE__ */ jsx(
12324
+ Thumbnail,
12325
+ {
12326
+ thumbnail: item.thumbnail,
12327
+ alt: item.product_title ?? void 0
12328
+ }
12329
+ ),
12330
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
12331
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
12332
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12333
+ /* @__PURE__ */ jsxs(
12334
+ Text,
12335
+ {
12336
+ size: "small",
12337
+ leading: "compact",
12338
+ className: "text-ui-fg-subtle",
12339
+ children: [
12340
+ "(",
12341
+ item.variant_title,
12342
+ ")"
12343
+ ]
12344
+ }
12345
+ )
12346
+ ] }),
12347
+ /* @__PURE__ */ jsx(
12348
+ Text,
12835
12349
  {
12836
- d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12837
- stroke: "#A1A1AA",
12838
- strokeWidth: "1.5",
12839
- strokeLinecap: "round",
12840
- strokeLinejoin: "round"
12350
+ size: "small",
12351
+ leading: "compact",
12352
+ className: "text-ui-fg-subtle",
12353
+ children: item.variant_sku
12841
12354
  }
12842
- ) }),
12843
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsx(
12844
- "path",
12845
- {
12846
- d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12847
- stroke: "#A1A1AA",
12848
- strokeWidth: "1.5",
12849
- strokeLinecap: "round",
12850
- strokeLinejoin: "round"
12355
+ )
12356
+ ] })
12357
+ ] }),
12358
+ editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
12359
+ Form$2.Field,
12360
+ {
12361
+ control: form.control,
12362
+ name: "quantity",
12363
+ render: ({ field }) => {
12364
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
12365
+ }
12366
+ }
12367
+ ) }) : /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
12368
+ editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
12369
+ Form$2.Field,
12370
+ {
12371
+ control: form.control,
12372
+ name: "unit_price",
12373
+ render: ({ field: { onChange, ...field } }) => {
12374
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12375
+ CurrencyInput,
12376
+ {
12377
+ ...field,
12378
+ symbol: getNativeSymbol(currencyCode),
12379
+ code: currencyCode,
12380
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12381
+ }
12382
+ ) }) });
12383
+ }
12384
+ }
12385
+ ) }) : /* @__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) }) }),
12386
+ /* @__PURE__ */ jsx(
12387
+ IconButton,
12388
+ {
12389
+ type: "button",
12390
+ size: "small",
12391
+ onClick: editing ? onSubmit : () => {
12392
+ setEditing(true);
12393
+ },
12394
+ disabled: isPending,
12395
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
12396
+ }
12397
+ )
12398
+ ] }) }) });
12399
+ };
12400
+ const variantItemSchema = objectType({
12401
+ quantity: numberType(),
12402
+ unit_price: unionType([numberType(), stringType()])
12403
+ });
12404
+ const CustomItem = ({ item, preview, currencyCode }) => {
12405
+ const [editing, setEditing] = useState(false);
12406
+ const { quantity, unit_price, title } = item;
12407
+ const form = useForm({
12408
+ defaultValues: {
12409
+ title,
12410
+ quantity,
12411
+ unit_price
12412
+ },
12413
+ resolver: zodResolver(customItemSchema)
12414
+ });
12415
+ useEffect(() => {
12416
+ form.reset({
12417
+ title,
12418
+ quantity,
12419
+ unit_price
12420
+ });
12421
+ }, [form, title, quantity, unit_price]);
12422
+ const actionId = useMemo(() => {
12423
+ var _a, _b;
12424
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12425
+ }, [item]);
12426
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12427
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
12428
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12429
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12430
+ const onSubmit = form.handleSubmit(async (data) => {
12431
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
12432
+ setEditing(false);
12433
+ return;
12434
+ }
12435
+ if (!actionId) {
12436
+ await updateOriginalItem(
12437
+ {
12438
+ item_id: item.id,
12439
+ quantity: data.quantity,
12440
+ unit_price: convertNumber(data.unit_price)
12441
+ },
12442
+ {
12443
+ onSuccess: () => {
12444
+ setEditing(false);
12445
+ },
12446
+ onError: (e) => {
12447
+ toast.error(e.message);
12851
12448
  }
12852
- ) }),
12853
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsx(
12854
- "path",
12855
- {
12856
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12857
- stroke: "#A1A1AA",
12858
- strokeWidth: "1.5",
12859
- strokeLinecap: "round",
12860
- strokeLinejoin: "round"
12449
+ }
12450
+ );
12451
+ return;
12452
+ }
12453
+ if (data.quantity === 0) {
12454
+ await removeActionItem(actionId, {
12455
+ onSuccess: () => {
12456
+ setEditing(false);
12457
+ },
12458
+ onError: (e) => {
12459
+ toast.error(e.message);
12460
+ }
12461
+ });
12462
+ return;
12463
+ }
12464
+ await updateActionItem(
12465
+ {
12466
+ action_id: actionId,
12467
+ quantity: data.quantity,
12468
+ unit_price: convertNumber(data.unit_price)
12469
+ },
12470
+ {
12471
+ onSuccess: () => {
12472
+ setEditing(false);
12473
+ },
12474
+ onError: (e) => {
12475
+ toast.error(e.message);
12476
+ }
12477
+ }
12478
+ );
12479
+ });
12480
+ 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: [
12481
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
12482
+ /* @__PURE__ */ jsx(
12483
+ Thumbnail,
12484
+ {
12485
+ thumbnail: item.thumbnail,
12486
+ alt: item.title ?? void 0
12487
+ }
12488
+ ),
12489
+ editing ? /* @__PURE__ */ jsx(
12490
+ Form$2.Field,
12491
+ {
12492
+ control: form.control,
12493
+ name: "title",
12494
+ render: ({ field }) => {
12495
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
12861
12496
  }
12862
- ) }),
12863
- /* @__PURE__ */ jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsx(
12864
- "path",
12497
+ }
12498
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
12499
+ ] }),
12500
+ editing ? /* @__PURE__ */ jsx(
12501
+ Form$2.Field,
12502
+ {
12503
+ control: form.control,
12504
+ name: "quantity",
12505
+ render: ({ field }) => {
12506
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
12507
+ }
12508
+ }
12509
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
12510
+ editing ? /* @__PURE__ */ jsx(
12511
+ Form$2.Field,
12512
+ {
12513
+ control: form.control,
12514
+ name: "unit_price",
12515
+ render: ({ field: { onChange, ...field } }) => {
12516
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12517
+ CurrencyInput,
12518
+ {
12519
+ ...field,
12520
+ symbol: getNativeSymbol(currencyCode),
12521
+ code: currencyCode,
12522
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12523
+ }
12524
+ ) }) });
12525
+ }
12526
+ }
12527
+ ) : /* @__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) }) }),
12528
+ /* @__PURE__ */ jsx(
12529
+ IconButton,
12530
+ {
12531
+ type: "button",
12532
+ size: "small",
12533
+ onClick: editing ? onSubmit : () => {
12534
+ setEditing(true);
12535
+ },
12536
+ disabled: isPending,
12537
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
12538
+ }
12539
+ )
12540
+ ] }) }) });
12541
+ };
12542
+ const StackedModalTrigger = ({
12543
+ type,
12544
+ setModalContent
12545
+ }) => {
12546
+ const { setIsOpen } = useStackedModal();
12547
+ const onClick = useCallback(() => {
12548
+ setModalContent(type);
12549
+ setIsOpen(STACKED_MODAL_ID, true);
12550
+ }, [setModalContent, setIsOpen, type]);
12551
+ return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
12552
+ };
12553
+ const VARIANT_PREFIX = "items";
12554
+ const LIMIT = 50;
12555
+ const ExistingItemsForm = ({ orderId, items }) => {
12556
+ const { setIsOpen } = useStackedModal();
12557
+ const [rowSelection, setRowSelection] = useState(
12558
+ items.reduce((acc, item) => {
12559
+ acc[item.variant_id] = true;
12560
+ return acc;
12561
+ }, {})
12562
+ );
12563
+ useEffect(() => {
12564
+ setRowSelection(
12565
+ items.reduce((acc, item) => {
12566
+ if (item.variant_id) {
12567
+ acc[item.variant_id] = true;
12568
+ }
12569
+ return acc;
12570
+ }, {})
12571
+ );
12572
+ }, [items]);
12573
+ const { q, order, offset } = useQueryParams(
12574
+ ["q", "order", "offset"],
12575
+ VARIANT_PREFIX
12576
+ );
12577
+ const { variants, count, isPending, isError, error } = useProductVariants(
12578
+ {
12579
+ q,
12580
+ order,
12581
+ offset: offset ? parseInt(offset) : void 0,
12582
+ limit: LIMIT
12583
+ },
12584
+ {
12585
+ placeholderData: keepPreviousData
12586
+ }
12587
+ );
12588
+ const columns = useColumns();
12589
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
12590
+ const onSubmit = async () => {
12591
+ const ids = Object.keys(rowSelection).filter(
12592
+ (id) => !items.find((i) => i.variant_id === id)
12593
+ );
12594
+ await mutateAsync(
12595
+ {
12596
+ items: ids.map((id) => ({
12597
+ variant_id: id,
12598
+ quantity: 1
12599
+ }))
12600
+ },
12601
+ {
12602
+ onSuccess: () => {
12603
+ setRowSelection({});
12604
+ setIsOpen(STACKED_MODAL_ID, false);
12605
+ },
12606
+ onError: (e) => {
12607
+ toast.error(e.message);
12608
+ }
12609
+ }
12610
+ );
12611
+ };
12612
+ if (isError) {
12613
+ throw error;
12614
+ }
12615
+ return /* @__PURE__ */ jsxs(
12616
+ StackedFocusModal.Content,
12617
+ {
12618
+ onOpenAutoFocus: (e) => {
12619
+ e.preventDefault();
12620
+ const searchInput = document.querySelector(
12621
+ "[data-modal-id='modal-search-input']"
12622
+ );
12623
+ if (searchInput) {
12624
+ searchInput.focus();
12625
+ }
12626
+ },
12627
+ children: [
12628
+ /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
12629
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
12630
+ /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
12631
+ ] }),
12632
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
12633
+ DataTable,
12865
12634
  {
12866
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12867
- stroke: "#A1A1AA",
12868
- strokeWidth: "1.5",
12869
- strokeLinecap: "round",
12870
- strokeLinejoin: "round"
12635
+ data: variants,
12636
+ columns,
12637
+ isLoading: isPending,
12638
+ getRowId: (row) => row.id,
12639
+ rowCount: count,
12640
+ prefix: VARIANT_PREFIX,
12641
+ layout: "fill",
12642
+ rowSelection: {
12643
+ state: rowSelection,
12644
+ onRowSelectionChange: setRowSelection,
12645
+ enableRowSelection: (row) => {
12646
+ return !items.find((i) => i.variant_id === row.original.id);
12647
+ }
12648
+ },
12649
+ autoFocusSearch: true
12871
12650
  }
12872
12651
  ) }),
12873
- /* @__PURE__ */ jsxs("defs", { children: [
12874
- /* @__PURE__ */ jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsx(
12875
- "rect",
12876
- {
12877
- width: "12",
12878
- height: "12",
12879
- fill: "white",
12880
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12881
- }
12882
- ) }),
12883
- /* @__PURE__ */ jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsx(
12884
- "rect",
12885
- {
12886
- width: "12",
12887
- height: "12",
12888
- fill: "white",
12889
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12890
- }
12891
- ) }),
12892
- /* @__PURE__ */ jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsx(
12893
- "rect",
12894
- {
12895
- width: "12",
12896
- height: "12",
12897
- fill: "white",
12898
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12899
- }
12900
- ) }),
12901
- /* @__PURE__ */ jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsx(
12902
- "rect",
12903
- {
12904
- width: "12",
12905
- height: "12",
12906
- fill: "white",
12907
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12908
- }
12909
- ) }),
12910
- /* @__PURE__ */ jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsx(
12911
- "rect",
12652
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12653
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12654
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
12655
+ ] }) })
12656
+ ]
12657
+ }
12658
+ );
12659
+ };
12660
+ const columnHelper = createDataTableColumnHelper();
12661
+ const useColumns = () => {
12662
+ return useMemo(() => {
12663
+ return [
12664
+ columnHelper.select(),
12665
+ columnHelper.accessor("product.title", {
12666
+ header: "Product",
12667
+ cell: ({ row }) => {
12668
+ var _a, _b, _c;
12669
+ return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
12670
+ /* @__PURE__ */ jsx(
12671
+ Thumbnail,
12672
+ {
12673
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
12674
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
12675
+ }
12676
+ ),
12677
+ /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
12678
+ ] });
12679
+ },
12680
+ enableSorting: true
12681
+ }),
12682
+ columnHelper.accessor("title", {
12683
+ header: "Variant",
12684
+ enableSorting: true
12685
+ }),
12686
+ columnHelper.accessor("sku", {
12687
+ header: "SKU",
12688
+ cell: ({ getValue }) => {
12689
+ return getValue() ?? "-";
12690
+ },
12691
+ enableSorting: true
12692
+ }),
12693
+ columnHelper.accessor("updated_at", {
12694
+ header: "Updated",
12695
+ cell: ({ getValue }) => {
12696
+ return /* @__PURE__ */ jsx(
12697
+ Tooltip,
12912
12698
  {
12913
- width: "12",
12914
- height: "12",
12915
- fill: "white",
12916
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12699
+ content: getFullDate({ date: getValue(), includeTime: true }),
12700
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
12917
12701
  }
12918
- ) }),
12919
- /* @__PURE__ */ jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsx(
12920
- "rect",
12702
+ );
12703
+ },
12704
+ enableSorting: true,
12705
+ sortAscLabel: "Oldest first",
12706
+ sortDescLabel: "Newest first"
12707
+ }),
12708
+ columnHelper.accessor("created_at", {
12709
+ header: "Created",
12710
+ cell: ({ getValue }) => {
12711
+ return /* @__PURE__ */ jsx(
12712
+ Tooltip,
12921
12713
  {
12922
- width: "12",
12923
- height: "12",
12924
- fill: "white",
12925
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12714
+ content: getFullDate({ date: getValue(), includeTime: true }),
12715
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
12926
12716
  }
12927
- ) })
12928
- ] })
12929
- ]
12930
- }
12931
- );
12717
+ );
12718
+ },
12719
+ enableSorting: true,
12720
+ sortAscLabel: "Oldest first",
12721
+ sortDescLabel: "Newest first"
12722
+ })
12723
+ ];
12724
+ }, []);
12725
+ };
12726
+ const CustomItemForm = ({ orderId, currencyCode }) => {
12727
+ const { setIsOpen } = useStackedModal();
12728
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
12729
+ const form = useForm({
12730
+ defaultValues: {
12731
+ title: "",
12732
+ quantity: 1,
12733
+ unit_price: ""
12734
+ },
12735
+ resolver: zodResolver(customItemSchema)
12736
+ });
12737
+ const onSubmit = form.handleSubmit(async (data) => {
12738
+ await addItems(
12739
+ {
12740
+ items: [
12741
+ {
12742
+ title: data.title,
12743
+ quantity: data.quantity,
12744
+ unit_price: convertNumber(data.unit_price)
12745
+ }
12746
+ ]
12747
+ },
12748
+ {
12749
+ onSuccess: () => {
12750
+ setIsOpen(STACKED_MODAL_ID, false);
12751
+ },
12752
+ onError: (e) => {
12753
+ toast.error(e.message);
12754
+ }
12755
+ }
12756
+ );
12757
+ });
12758
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
12759
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
12760
+ /* @__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: [
12761
+ /* @__PURE__ */ jsxs("div", { children: [
12762
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
12763
+ /* @__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." }) })
12764
+ ] }),
12765
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12766
+ /* @__PURE__ */ jsx(
12767
+ Form$2.Field,
12768
+ {
12769
+ control: form.control,
12770
+ name: "title",
12771
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12772
+ /* @__PURE__ */ jsxs("div", { children: [
12773
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
12774
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
12775
+ ] }),
12776
+ /* @__PURE__ */ jsxs("div", { children: [
12777
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12778
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12779
+ ] })
12780
+ ] }) })
12781
+ }
12782
+ ),
12783
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12784
+ /* @__PURE__ */ jsx(
12785
+ Form$2.Field,
12786
+ {
12787
+ control: form.control,
12788
+ name: "unit_price",
12789
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12790
+ /* @__PURE__ */ jsxs("div", { children: [
12791
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
12792
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
12793
+ ] }),
12794
+ /* @__PURE__ */ jsxs("div", { children: [
12795
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
12796
+ CurrencyInput,
12797
+ {
12798
+ symbol: getNativeSymbol(currencyCode),
12799
+ code: currencyCode,
12800
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
12801
+ ...field
12802
+ }
12803
+ ) }),
12804
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12805
+ ] })
12806
+ ] }) })
12807
+ }
12808
+ ),
12809
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
12810
+ /* @__PURE__ */ jsx(
12811
+ Form$2.Field,
12812
+ {
12813
+ control: form.control,
12814
+ name: "quantity",
12815
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12816
+ /* @__PURE__ */ jsxs("div", { children: [
12817
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
12818
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
12819
+ ] }),
12820
+ /* @__PURE__ */ jsxs("div", { className: "w-full flex-1", children: [
12821
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
12822
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12823
+ ] })
12824
+ ] }) })
12825
+ }
12826
+ )
12827
+ ] }) }) }),
12828
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12829
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12830
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
12831
+ ] }) })
12832
+ ] }) }) });
12932
12833
  };
12933
- const schema$1 = objectType({
12934
- customer_id: stringType().min(1)
12834
+ const customItemSchema = objectType({
12835
+ title: stringType().min(1),
12836
+ quantity: numberType(),
12837
+ unit_price: unionType([numberType(), stringType()])
12935
12838
  });
12936
- const SalesChannel = () => {
12839
+ const ShippingAddress = () => {
12937
12840
  const { id } = useParams();
12938
- const { draft_order, isPending, isError, error } = useDraftOrder(
12939
- id,
12940
- {
12941
- fields: "+sales_channel_id"
12942
- },
12943
- {
12944
- enabled: !!id
12945
- }
12946
- );
12841
+ const { order, isPending, isError, error } = useOrder(id, {
12842
+ fields: "+shipping_address"
12843
+ });
12947
12844
  if (isError) {
12948
12845
  throw error;
12949
12846
  }
12950
- const ISrEADY = !!draft_order && !isPending;
12847
+ const isReady = !isPending && !!order;
12951
12848
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12952
12849
  /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12953
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Sales Channel" }) }),
12954
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Update which sales channel the draft order is associated with" }) })
12850
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Shipping Address" }) }),
12851
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the shipping address for the draft order" }) })
12955
12852
  ] }),
12956
- ISrEADY && /* @__PURE__ */ jsx(SalesChannelForm, { order: draft_order })
12853
+ isReady && /* @__PURE__ */ jsx(ShippingAddressForm, { order })
12957
12854
  ] });
12958
12855
  };
12959
- const SalesChannelForm = ({ order }) => {
12856
+ const ShippingAddressForm = ({ order }) => {
12857
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12960
12858
  const form = useForm({
12961
12859
  defaultValues: {
12962
- sales_channel_id: order.sales_channel_id || ""
12860
+ first_name: ((_a = order.shipping_address) == null ? void 0 : _a.first_name) ?? "",
12861
+ last_name: ((_b = order.shipping_address) == null ? void 0 : _b.last_name) ?? "",
12862
+ company: ((_c = order.shipping_address) == null ? void 0 : _c.company) ?? "",
12863
+ address_1: ((_d = order.shipping_address) == null ? void 0 : _d.address_1) ?? "",
12864
+ address_2: ((_e = order.shipping_address) == null ? void 0 : _e.address_2) ?? "",
12865
+ city: ((_f = order.shipping_address) == null ? void 0 : _f.city) ?? "",
12866
+ province: ((_g = order.shipping_address) == null ? void 0 : _g.province) ?? "",
12867
+ country_code: ((_h = order.shipping_address) == null ? void 0 : _h.country_code) ?? "",
12868
+ postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12869
+ phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12963
12870
  },
12964
12871
  resolver: zodResolver(schema)
12965
12872
  });
@@ -12968,11 +12875,21 @@ const SalesChannelForm = ({ order }) => {
12968
12875
  const onSubmit = form.handleSubmit(async (data) => {
12969
12876
  await mutateAsync(
12970
12877
  {
12971
- sales_channel_id: data.sales_channel_id
12878
+ shipping_address: {
12879
+ first_name: data.first_name,
12880
+ last_name: data.last_name,
12881
+ company: data.company,
12882
+ address_1: data.address_1,
12883
+ address_2: data.address_2,
12884
+ city: data.city,
12885
+ province: data.province,
12886
+ country_code: data.country_code,
12887
+ postal_code: data.postal_code,
12888
+ phone: data.phone
12889
+ }
12972
12890
  },
12973
12891
  {
12974
12892
  onSuccess: () => {
12975
- toast.success("Sales channel updated");
12976
12893
  handleSuccess();
12977
12894
  },
12978
12895
  onError: (error) => {
@@ -12987,7 +12904,132 @@ const SalesChannelForm = ({ order }) => {
12987
12904
  className: "flex flex-1 flex-col overflow-hidden",
12988
12905
  onSubmit,
12989
12906
  children: [
12990
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsx(SalesChannelField, { control: form.control, order }) }),
12907
+ /* @__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: [
12908
+ /* @__PURE__ */ jsx(
12909
+ Form$2.Field,
12910
+ {
12911
+ control: form.control,
12912
+ name: "country_code",
12913
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12914
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12915
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12916
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12917
+ ] })
12918
+ }
12919
+ ),
12920
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12921
+ /* @__PURE__ */ jsx(
12922
+ Form$2.Field,
12923
+ {
12924
+ control: form.control,
12925
+ name: "first_name",
12926
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12927
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12928
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12929
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12930
+ ] })
12931
+ }
12932
+ ),
12933
+ /* @__PURE__ */ jsx(
12934
+ Form$2.Field,
12935
+ {
12936
+ control: form.control,
12937
+ name: "last_name",
12938
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12939
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12940
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12941
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12942
+ ] })
12943
+ }
12944
+ )
12945
+ ] }),
12946
+ /* @__PURE__ */ jsx(
12947
+ Form$2.Field,
12948
+ {
12949
+ control: form.control,
12950
+ name: "company",
12951
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12952
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12953
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12954
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12955
+ ] })
12956
+ }
12957
+ ),
12958
+ /* @__PURE__ */ jsx(
12959
+ Form$2.Field,
12960
+ {
12961
+ control: form.control,
12962
+ name: "address_1",
12963
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12964
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12965
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12966
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12967
+ ] })
12968
+ }
12969
+ ),
12970
+ /* @__PURE__ */ jsx(
12971
+ Form$2.Field,
12972
+ {
12973
+ control: form.control,
12974
+ name: "address_2",
12975
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12976
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12977
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12978
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12979
+ ] })
12980
+ }
12981
+ ),
12982
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12983
+ /* @__PURE__ */ jsx(
12984
+ Form$2.Field,
12985
+ {
12986
+ control: form.control,
12987
+ name: "postal_code",
12988
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12989
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12990
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12991
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12992
+ ] })
12993
+ }
12994
+ ),
12995
+ /* @__PURE__ */ jsx(
12996
+ Form$2.Field,
12997
+ {
12998
+ control: form.control,
12999
+ name: "city",
13000
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13001
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
13002
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13003
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13004
+ ] })
13005
+ }
13006
+ )
13007
+ ] }),
13008
+ /* @__PURE__ */ jsx(
13009
+ Form$2.Field,
13010
+ {
13011
+ control: form.control,
13012
+ name: "province",
13013
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13014
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
13015
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13016
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13017
+ ] })
13018
+ }
13019
+ ),
13020
+ /* @__PURE__ */ jsx(
13021
+ Form$2.Field,
13022
+ {
13023
+ control: form.control,
13024
+ name: "phone",
13025
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13026
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
13027
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13028
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13029
+ ] })
13030
+ }
13031
+ )
13032
+ ] }) }),
12991
13033
  /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
12992
13034
  /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12993
13035
  /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
@@ -12996,49 +13038,7 @@ const SalesChannelForm = ({ order }) => {
12996
13038
  }
12997
13039
  ) });
12998
13040
  };
12999
- const SalesChannelField = ({ control, order }) => {
13000
- const salesChannels = useComboboxData({
13001
- queryFn: async (params) => {
13002
- return await sdk.admin.salesChannel.list(params);
13003
- },
13004
- queryKey: ["sales-channels"],
13005
- getOptions: (data) => {
13006
- return data.sales_channels.map((salesChannel) => ({
13007
- label: salesChannel.name,
13008
- value: salesChannel.id
13009
- }));
13010
- },
13011
- defaultValue: order.sales_channel_id || void 0
13012
- });
13013
- return /* @__PURE__ */ jsx(
13014
- Form$2.Field,
13015
- {
13016
- control,
13017
- name: "sales_channel_id",
13018
- render: ({ field }) => {
13019
- return /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13020
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Sales Channel" }),
13021
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
13022
- Combobox,
13023
- {
13024
- options: salesChannels.options,
13025
- fetchNextPage: salesChannels.fetchNextPage,
13026
- isFetchingNextPage: salesChannels.isFetchingNextPage,
13027
- searchValue: salesChannels.searchValue,
13028
- onSearchValueChange: salesChannels.onSearchValueChange,
13029
- placeholder: "Select sales channel",
13030
- ...field
13031
- }
13032
- ) }),
13033
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13034
- ] });
13035
- }
13036
- }
13037
- );
13038
- };
13039
- const schema = objectType({
13040
- sales_channel_id: stringType().min(1)
13041
- });
13041
+ const schema = addressSchema;
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
13044
13044
  routes: [
@@ -13059,10 +13059,6 @@ const routeModule = {
13059
13059
  handle,
13060
13060
  loader,
13061
13061
  children: [
13062
- {
13063
- Component: BillingAddress,
13064
- path: "/draft-orders/:id/billing-address"
13065
- },
13066
13062
  {
13067
13063
  Component: CustomItems,
13068
13064
  path: "/draft-orders/:id/custom-items"
@@ -13072,8 +13068,8 @@ const routeModule = {
13072
13068
  path: "/draft-orders/:id/email"
13073
13069
  },
13074
13070
  {
13075
- Component: Items,
13076
- path: "/draft-orders/:id/items"
13071
+ Component: BillingAddress,
13072
+ path: "/draft-orders/:id/billing-address"
13077
13073
  },
13078
13074
  {
13079
13075
  Component: Metadata,
@@ -13087,10 +13083,6 @@ const routeModule = {
13087
13083
  Component: Shipping,
13088
13084
  path: "/draft-orders/:id/shipping"
13089
13085
  },
13090
- {
13091
- Component: ShippingAddress,
13092
- path: "/draft-orders/:id/shipping-address"
13093
- },
13094
13086
  {
13095
13087
  Component: TransferOwnership,
13096
13088
  path: "/draft-orders/:id/transfer-ownership"
@@ -13098,6 +13090,14 @@ const routeModule = {
13098
13090
  {
13099
13091
  Component: SalesChannel,
13100
13092
  path: "/draft-orders/:id/sales-channel"
13093
+ },
13094
+ {
13095
+ Component: Items,
13096
+ path: "/draft-orders/:id/items"
13097
+ },
13098
+ {
13099
+ Component: ShippingAddress,
13100
+ path: "/draft-orders/:id/shipping-address"
13101
13101
  }
13102
13102
  ]
13103
13103
  }