@medusajs/draft-order 2.10.2-preview-20250903180151 → 2.10.2-preview-20250903210146

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