@medusajs/draft-order 2.10.3-preview-20250914031450 → 2.10.3-preview-20250914090153

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -4,7 +4,7 @@ import { Tooltip, DropdownMenu, clx, IconButton, useDataTable, DataTable as Data
4
4
  import { useQuery, useQueryClient, useMutation, keepPreviousData, useInfiniteQuery } from "@tanstack/react-query";
5
5
  import React, { useState, useCallback, useMemo, Fragment, createContext, forwardRef, useId, useContext, useTransition, useRef, useImperativeHandle, useDeferredValue, useEffect, Suspense } from "react";
6
6
  import { useSearchParams, Link, useNavigate, Outlet, useBlocker, useLocation, useParams } from "react-router-dom";
7
- import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, Minus, PencilSquare, EllipsisVertical, ArrowUpMini, ArrowDownMini } from "@medusajs/icons";
7
+ import { EllipsisHorizontal, XMark, InformationCircleSolid, XMarkMini, TrianglesMini, CheckMini, EllipseMiniSolid, PlusMini, ExclamationCircleSolid, ArrowPath, FlyingBox, CurrencyDollar, Envelope, Channels, Trash, ArrowUpRightOnBox, TriangleDownMini, Check, SquareTwoStack, Photo, TriangleRightMini, Shopping, Buildings, TruckFast, Plus, ReceiptPercent, EllipsisVertical, ArrowUpMini, ArrowDownMini, Minus, PencilSquare } from "@medusajs/icons";
8
8
  import Medusa from "@medusajs/js-sdk";
9
9
  import { format, formatDistance, sub, subDays, subMonths } from "date-fns";
10
10
  import { enUS } from "date-fns/locale";
@@ -9567,217 +9567,6 @@ const ID = () => {
9567
9567
  /* @__PURE__ */ jsx(Outlet, {})
9568
9568
  ] });
9569
9569
  };
9570
- const BillingAddress = () => {
9571
- const { id } = useParams();
9572
- const { order, isPending, isError, error } = useOrder(id, {
9573
- fields: "+billing_address"
9574
- });
9575
- if (isError) {
9576
- throw error;
9577
- }
9578
- const isReady = !isPending && !!order;
9579
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9580
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9581
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Billing Address" }) }),
9582
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
9583
- ] }),
9584
- isReady && /* @__PURE__ */ jsx(BillingAddressForm, { order })
9585
- ] });
9586
- };
9587
- const BillingAddressForm = ({ order }) => {
9588
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
9589
- const form = useForm({
9590
- defaultValues: {
9591
- first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
9592
- last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
9593
- company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
9594
- address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
9595
- address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
9596
- city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
9597
- province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
9598
- country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
9599
- postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9600
- phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9601
- },
9602
- resolver: zodResolver(schema$5)
9603
- });
9604
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9605
- const { handleSuccess } = useRouteModal();
9606
- const onSubmit = form.handleSubmit(async (data) => {
9607
- await mutateAsync(
9608
- { billing_address: data },
9609
- {
9610
- onSuccess: () => {
9611
- handleSuccess();
9612
- },
9613
- onError: (error) => {
9614
- toast.error(error.message);
9615
- }
9616
- }
9617
- );
9618
- });
9619
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9620
- KeyboundForm,
9621
- {
9622
- className: "flex flex-1 flex-col overflow-hidden",
9623
- onSubmit,
9624
- children: [
9625
- /* @__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: [
9626
- /* @__PURE__ */ jsx(
9627
- Form$2.Field,
9628
- {
9629
- control: form.control,
9630
- name: "country_code",
9631
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9632
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
9633
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
9634
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9635
- ] })
9636
- }
9637
- ),
9638
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9639
- /* @__PURE__ */ jsx(
9640
- Form$2.Field,
9641
- {
9642
- control: form.control,
9643
- name: "first_name",
9644
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9645
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
9646
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9647
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9648
- ] })
9649
- }
9650
- ),
9651
- /* @__PURE__ */ jsx(
9652
- Form$2.Field,
9653
- {
9654
- control: form.control,
9655
- name: "last_name",
9656
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9657
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
9658
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9659
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9660
- ] })
9661
- }
9662
- )
9663
- ] }),
9664
- /* @__PURE__ */ jsx(
9665
- Form$2.Field,
9666
- {
9667
- control: form.control,
9668
- name: "company",
9669
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9670
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
9671
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9672
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9673
- ] })
9674
- }
9675
- ),
9676
- /* @__PURE__ */ jsx(
9677
- Form$2.Field,
9678
- {
9679
- control: form.control,
9680
- name: "address_1",
9681
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9682
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
9683
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9684
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9685
- ] })
9686
- }
9687
- ),
9688
- /* @__PURE__ */ jsx(
9689
- Form$2.Field,
9690
- {
9691
- control: form.control,
9692
- name: "address_2",
9693
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9694
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
9695
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9696
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9697
- ] })
9698
- }
9699
- ),
9700
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9701
- /* @__PURE__ */ jsx(
9702
- Form$2.Field,
9703
- {
9704
- control: form.control,
9705
- name: "postal_code",
9706
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9707
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
9708
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9709
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9710
- ] })
9711
- }
9712
- ),
9713
- /* @__PURE__ */ jsx(
9714
- Form$2.Field,
9715
- {
9716
- control: form.control,
9717
- name: "city",
9718
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9719
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
9720
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9721
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9722
- ] })
9723
- }
9724
- )
9725
- ] }),
9726
- /* @__PURE__ */ jsx(
9727
- Form$2.Field,
9728
- {
9729
- control: form.control,
9730
- name: "province",
9731
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9732
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
9733
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9734
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9735
- ] })
9736
- }
9737
- ),
9738
- /* @__PURE__ */ jsx(
9739
- Form$2.Field,
9740
- {
9741
- control: form.control,
9742
- name: "phone",
9743
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9744
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
9745
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9746
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9747
- ] })
9748
- }
9749
- )
9750
- ] }) }),
9751
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9752
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9753
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9754
- ] }) })
9755
- ]
9756
- }
9757
- ) });
9758
- };
9759
- const schema$5 = addressSchema;
9760
- const CustomItems = () => {
9761
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9762
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
9763
- /* @__PURE__ */ jsx(CustomItemsForm, {})
9764
- ] });
9765
- };
9766
- const CustomItemsForm = () => {
9767
- const form = useForm({
9768
- resolver: zodResolver(schema$4)
9769
- });
9770
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9771
- /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
9772
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9773
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9774
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
9775
- ] }) })
9776
- ] }) });
9777
- };
9778
- const schema$4 = objectType({
9779
- email: stringType().email()
9780
- });
9781
9570
  const Email = () => {
9782
9571
  const { id } = useParams();
9783
9572
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9800,7 +9589,7 @@ const EmailForm = ({ order }) => {
9800
9589
  defaultValues: {
9801
9590
  email: order.email ?? ""
9802
9591
  },
9803
- resolver: zodResolver(schema$3)
9592
+ resolver: zodResolver(schema$5)
9804
9593
  });
9805
9594
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9806
9595
  const { handleSuccess } = useRouteModal();
@@ -9843,272 +9632,622 @@ const EmailForm = ({ order }) => {
9843
9632
  }
9844
9633
  ) });
9845
9634
  };
9846
- const schema$3 = objectType({
9635
+ const schema$5 = objectType({
9847
9636
  email: stringType().email()
9848
9637
  });
9849
- const NumberInput = forwardRef(
9850
- ({
9851
- value,
9852
- onChange,
9853
- size = "base",
9854
- min = 0,
9855
- max = 100,
9856
- step = 1,
9857
- className,
9858
- disabled,
9859
- ...props
9860
- }, ref) => {
9861
- const handleChange = (event) => {
9862
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9863
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9864
- onChange(newValue);
9865
- }
9866
- };
9867
- const handleIncrement = () => {
9868
- const newValue = value + step;
9869
- if (max === void 0 || newValue <= max) {
9870
- onChange(newValue);
9871
- }
9872
- };
9873
- const handleDecrement = () => {
9874
- const newValue = value - step;
9875
- if (min === void 0 || newValue >= min) {
9876
- onChange(newValue);
9877
- }
9878
- };
9638
+ const InlineTip = forwardRef(
9639
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
9640
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
9879
9641
  return /* @__PURE__ */ jsxs(
9880
9642
  "div",
9881
9643
  {
9644
+ ref,
9882
9645
  className: clx(
9883
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9884
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9885
- {
9886
- "h-7": size === "small",
9887
- "h-8": size === "base"
9888
- },
9646
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
9889
9647
  className
9890
9648
  ),
9649
+ ...props,
9891
9650
  children: [
9892
9651
  /* @__PURE__ */ jsx(
9893
- "input",
9894
- {
9895
- ref,
9896
- type: "number",
9897
- value,
9898
- onChange: handleChange,
9899
- min,
9900
- max,
9901
- step,
9902
- className: clx(
9903
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9904
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9905
- "placeholder:text-ui-fg-muted"
9906
- ),
9907
- ...props
9908
- }
9909
- ),
9910
- /* @__PURE__ */ jsxs(
9911
- "button",
9652
+ "div",
9912
9653
  {
9913
- className: clx(
9914
- "flex items-center justify-center outline-none transition-fg",
9915
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9916
- "focus:bg-ui-bg-field-component-hover",
9917
- "hover:bg-ui-bg-field-component-hover",
9918
- {
9919
- "size-7": size === "small",
9920
- "size-8": size === "base"
9921
- }
9922
- ),
9923
- type: "button",
9924
- onClick: handleDecrement,
9925
- disabled: min !== void 0 && value <= min || disabled,
9926
- children: [
9927
- /* @__PURE__ */ jsx(Minus, {}),
9928
- /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9929
- ]
9654
+ role: "presentation",
9655
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
9656
+ "bg-ui-tag-orange-icon": variant === "warning"
9657
+ })
9930
9658
  }
9931
9659
  ),
9932
- /* @__PURE__ */ jsxs(
9933
- "button",
9934
- {
9935
- className: clx(
9936
- "flex items-center justify-center outline-none transition-fg",
9937
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9938
- "focus:bg-ui-bg-field-hover",
9939
- "hover:bg-ui-bg-field-hover",
9940
- {
9941
- "size-7": size === "small",
9942
- "size-8": size === "base"
9943
- }
9944
- ),
9945
- type: "button",
9946
- onClick: handleIncrement,
9947
- disabled: max !== void 0 && value >= max || disabled,
9948
- children: [
9949
- /* @__PURE__ */ jsx(Plus, {}),
9950
- /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9951
- ]
9952
- }
9953
- )
9660
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
9661
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
9662
+ labelValue,
9663
+ ":"
9664
+ ] }),
9665
+ " ",
9666
+ children
9667
+ ] })
9954
9668
  ]
9955
9669
  }
9956
9670
  );
9957
9671
  }
9958
9672
  );
9959
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9960
- const productVariantsQueryKeys = {
9961
- list: (query2) => [
9962
- PRODUCT_VARIANTS_QUERY_KEY,
9963
- query2 ? query2 : void 0
9964
- ]
9965
- };
9966
- const useProductVariants = (query2, options) => {
9967
- const { data, ...rest } = useQuery({
9968
- queryKey: productVariantsQueryKeys.list(query2),
9969
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9970
- ...options
9971
- });
9972
- return { ...data, ...rest };
9973
- };
9974
- const useCancelOrderEdit = ({ preview }) => {
9975
- const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
9976
- const onCancel = useCallback(async () => {
9977
- if (!preview) {
9978
- return true;
9979
- }
9980
- let res = false;
9981
- await cancelOrderEdit(void 0, {
9982
- onError: (e) => {
9983
- toast.error(e.message);
9984
- },
9985
- onSuccess: () => {
9986
- res = true;
9987
- }
9988
- });
9989
- return res;
9990
- }, [preview, cancelOrderEdit]);
9991
- return { onCancel };
9992
- };
9993
- let IS_REQUEST_RUNNING = false;
9994
- const useInitiateOrderEdit = ({
9995
- preview
9996
- }) => {
9997
- const navigate = useNavigate();
9998
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
9999
- useEffect(() => {
10000
- async function run() {
10001
- if (IS_REQUEST_RUNNING || !preview) {
10002
- return;
10003
- }
10004
- if (preview.order_change) {
10005
- return;
10006
- }
10007
- IS_REQUEST_RUNNING = true;
10008
- await mutateAsync(void 0, {
10009
- onError: (e) => {
10010
- toast.error(e.message);
10011
- navigate(`/draft-orders/${preview.id}`, { replace: true });
10012
- return;
10013
- }
10014
- });
10015
- IS_REQUEST_RUNNING = false;
10016
- }
10017
- run();
10018
- }, [preview, navigate, mutateAsync]);
10019
- };
10020
- function convertNumber(value) {
10021
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10022
- }
10023
- const STACKED_MODAL_ID = "items_stacked_modal";
10024
- const Items = () => {
9673
+ InlineTip.displayName = "InlineTip";
9674
+ const MetadataFieldSchema = objectType({
9675
+ key: stringType(),
9676
+ disabled: booleanType().optional(),
9677
+ value: anyType()
9678
+ });
9679
+ const MetadataSchema = objectType({
9680
+ metadata: arrayType(MetadataFieldSchema)
9681
+ });
9682
+ const Metadata = () => {
10025
9683
  const { id } = useParams();
10026
- const {
10027
- order: preview,
10028
- isPending: isPreviewPending,
10029
- isError: isPreviewError,
10030
- error: previewError
10031
- } = useOrderPreview(id, void 0, {
10032
- placeholderData: keepPreviousData
9684
+ const { order, isPending, isError, error } = useOrder(id, {
9685
+ fields: "metadata"
10033
9686
  });
10034
- useInitiateOrderEdit({ preview });
10035
- const { draft_order, isPending, isError, error } = useDraftOrder(
10036
- id,
10037
- {
10038
- fields: "currency_code"
10039
- },
10040
- {
10041
- enabled: !!id
10042
- }
10043
- );
10044
- const { onCancel } = useCancelOrderEdit({ preview });
10045
9687
  if (isError) {
10046
9688
  throw error;
10047
9689
  }
10048
- if (isPreviewError) {
10049
- throw previewError;
10050
- }
10051
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10052
- return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
10053
- /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10054
- /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10055
- ] }) });
9690
+ const isReady = !isPending && !!order;
9691
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9692
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9693
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
9694
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
9695
+ ] }),
9696
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
9697
+ ] });
10056
9698
  };
10057
- const ItemsForm = ({ preview, currencyCode }) => {
10058
- var _a;
10059
- const [isSubmitting, setIsSubmitting] = useState(false);
10060
- const [modalContent, setModalContent] = useState(
10061
- null
10062
- );
9699
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
9700
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
9701
+ const MetadataForm = ({ orderId, metadata }) => {
10063
9702
  const { handleSuccess } = useRouteModal();
10064
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10065
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10066
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10067
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10068
- const matches = useMemo(() => {
10069
- return matchSorter(preview.items, query2, {
10070
- keys: ["product_title", "variant_title", "variant_sku", "title"]
10071
- });
10072
- }, [preview.items, query2]);
10073
- const onSubmit = async () => {
10074
- setIsSubmitting(true);
10075
- let requestSucceeded = false;
10076
- await requestOrderEdit(void 0, {
10077
- onError: (e) => {
10078
- toast.error(`Failed to request order edit: ${e.message}`);
10079
- },
10080
- onSuccess: () => {
10081
- requestSucceeded = true;
10082
- }
10083
- });
10084
- if (!requestSucceeded) {
10085
- setIsSubmitting(false);
10086
- return;
10087
- }
10088
- await confirmOrderEdit(void 0, {
10089
- onError: (e) => {
10090
- toast.error(`Failed to confirm order edit: ${e.message}`);
10091
- },
10092
- onSuccess: () => {
10093
- handleSuccess();
9703
+ const hasUneditableRows = getHasUneditableRows(metadata);
9704
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
9705
+ const form = useForm({
9706
+ defaultValues: {
9707
+ metadata: getDefaultValues(metadata)
9708
+ },
9709
+ resolver: zodResolver(MetadataSchema)
9710
+ });
9711
+ const handleSubmit = form.handleSubmit(async (data) => {
9712
+ const parsedData = parseValues(data);
9713
+ await mutateAsync(
9714
+ {
9715
+ metadata: parsedData
10094
9716
  },
10095
- onSettled: () => {
10096
- setIsSubmitting(false);
10097
- }
10098
- });
10099
- };
10100
- const onKeyDown = useCallback(
10101
- (e) => {
10102
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10103
- if (modalContent || isSubmitting) {
10104
- return;
9717
+ {
9718
+ onSuccess: () => {
9719
+ toast.success("Metadata updated");
9720
+ handleSuccess();
9721
+ },
9722
+ onError: (error) => {
9723
+ toast.error(error.message);
10105
9724
  }
10106
- onSubmit();
10107
9725
  }
10108
- },
10109
- [modalContent, isSubmitting, onSubmit]
10110
- );
10111
- useEffect(() => {
9726
+ );
9727
+ });
9728
+ const { fields, insert, remove } = useFieldArray({
9729
+ control: form.control,
9730
+ name: "metadata"
9731
+ });
9732
+ function deleteRow(index) {
9733
+ remove(index);
9734
+ if (fields.length === 1) {
9735
+ insert(0, {
9736
+ key: "",
9737
+ value: "",
9738
+ disabled: false
9739
+ });
9740
+ }
9741
+ }
9742
+ function insertRow(index, position) {
9743
+ insert(index + (position === "above" ? 0 : 1), {
9744
+ key: "",
9745
+ value: "",
9746
+ disabled: false
9747
+ });
9748
+ }
9749
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9750
+ KeyboundForm,
9751
+ {
9752
+ onSubmit: handleSubmit,
9753
+ className: "flex flex-1 flex-col overflow-hidden",
9754
+ children: [
9755
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
9756
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
9757
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
9758
+ /* @__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" }) }),
9759
+ /* @__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" }) })
9760
+ ] }),
9761
+ fields.map((field, index) => {
9762
+ const isDisabled = field.disabled || false;
9763
+ let placeholder = "-";
9764
+ if (typeof field.value === "object") {
9765
+ placeholder = "{ ... }";
9766
+ }
9767
+ if (Array.isArray(field.value)) {
9768
+ placeholder = "[ ... ]";
9769
+ }
9770
+ return /* @__PURE__ */ jsx(
9771
+ ConditionalTooltip,
9772
+ {
9773
+ showTooltip: isDisabled,
9774
+ content: "This row is disabled because it contains non-primitive data.",
9775
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
9776
+ /* @__PURE__ */ jsxs(
9777
+ "div",
9778
+ {
9779
+ className: clx("grid grid-cols-2 divide-x", {
9780
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
9781
+ }),
9782
+ children: [
9783
+ /* @__PURE__ */ jsx(
9784
+ Form$2.Field,
9785
+ {
9786
+ control: form.control,
9787
+ name: `metadata.${index}.key`,
9788
+ render: ({ field: field2 }) => {
9789
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9790
+ GridInput,
9791
+ {
9792
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
9793
+ ...field2,
9794
+ disabled: isDisabled,
9795
+ placeholder: "Key"
9796
+ }
9797
+ ) }) });
9798
+ }
9799
+ }
9800
+ ),
9801
+ /* @__PURE__ */ jsx(
9802
+ Form$2.Field,
9803
+ {
9804
+ control: form.control,
9805
+ name: `metadata.${index}.value`,
9806
+ render: ({ field: { value, ...field2 } }) => {
9807
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
9808
+ GridInput,
9809
+ {
9810
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
9811
+ ...field2,
9812
+ value: isDisabled ? placeholder : value,
9813
+ disabled: isDisabled,
9814
+ placeholder: "Value"
9815
+ }
9816
+ ) }) });
9817
+ }
9818
+ }
9819
+ )
9820
+ ]
9821
+ }
9822
+ ),
9823
+ /* @__PURE__ */ jsxs(DropdownMenu, { children: [
9824
+ /* @__PURE__ */ jsx(
9825
+ DropdownMenu.Trigger,
9826
+ {
9827
+ className: clx(
9828
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
9829
+ {
9830
+ hidden: isDisabled
9831
+ }
9832
+ ),
9833
+ disabled: isDisabled,
9834
+ asChild: true,
9835
+ children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
9836
+ }
9837
+ ),
9838
+ /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
9839
+ /* @__PURE__ */ jsxs(
9840
+ DropdownMenu.Item,
9841
+ {
9842
+ className: "gap-x-2",
9843
+ onClick: () => insertRow(index, "above"),
9844
+ children: [
9845
+ /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
9846
+ "Insert row above"
9847
+ ]
9848
+ }
9849
+ ),
9850
+ /* @__PURE__ */ jsxs(
9851
+ DropdownMenu.Item,
9852
+ {
9853
+ className: "gap-x-2",
9854
+ onClick: () => insertRow(index, "below"),
9855
+ children: [
9856
+ /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
9857
+ "Insert row below"
9858
+ ]
9859
+ }
9860
+ ),
9861
+ /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
9862
+ /* @__PURE__ */ jsxs(
9863
+ DropdownMenu.Item,
9864
+ {
9865
+ className: "gap-x-2",
9866
+ onClick: () => deleteRow(index),
9867
+ children: [
9868
+ /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
9869
+ "Delete row"
9870
+ ]
9871
+ }
9872
+ )
9873
+ ] })
9874
+ ] })
9875
+ ] })
9876
+ },
9877
+ field.id
9878
+ );
9879
+ })
9880
+ ] }),
9881
+ 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." })
9882
+ ] }),
9883
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9884
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
9885
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9886
+ ] }) })
9887
+ ]
9888
+ }
9889
+ ) });
9890
+ };
9891
+ const GridInput = forwardRef(({ className, ...props }, ref) => {
9892
+ return /* @__PURE__ */ jsx(
9893
+ "input",
9894
+ {
9895
+ ref,
9896
+ ...props,
9897
+ autoComplete: "off",
9898
+ className: clx(
9899
+ "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",
9900
+ className
9901
+ )
9902
+ }
9903
+ );
9904
+ });
9905
+ GridInput.displayName = "MetadataForm.GridInput";
9906
+ const PlaceholderInner = () => {
9907
+ return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
9908
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
9909
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
9910
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
9911
+ /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
9912
+ ] }) })
9913
+ ] });
9914
+ };
9915
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
9916
+ function getDefaultValues(metadata) {
9917
+ if (!metadata || !Object.keys(metadata).length) {
9918
+ return [
9919
+ {
9920
+ key: "",
9921
+ value: "",
9922
+ disabled: false
9923
+ }
9924
+ ];
9925
+ }
9926
+ return Object.entries(metadata).map(([key, value]) => {
9927
+ if (!EDITABLE_TYPES.includes(typeof value)) {
9928
+ return {
9929
+ key,
9930
+ value,
9931
+ disabled: true
9932
+ };
9933
+ }
9934
+ let stringValue = value;
9935
+ if (typeof value !== "string") {
9936
+ stringValue = JSON.stringify(value);
9937
+ }
9938
+ return {
9939
+ key,
9940
+ value: stringValue,
9941
+ original_key: key
9942
+ };
9943
+ });
9944
+ }
9945
+ function parseValues(values) {
9946
+ const metadata = values.metadata;
9947
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
9948
+ if (isEmpty) {
9949
+ return null;
9950
+ }
9951
+ const update = {};
9952
+ metadata.forEach((field) => {
9953
+ let key = field.key;
9954
+ let value = field.value;
9955
+ const disabled = field.disabled;
9956
+ if (!key || !value) {
9957
+ return;
9958
+ }
9959
+ if (disabled) {
9960
+ update[key] = value;
9961
+ return;
9962
+ }
9963
+ key = key.trim();
9964
+ value = value.trim();
9965
+ if (value === "true") {
9966
+ update[key] = true;
9967
+ } else if (value === "false") {
9968
+ update[key] = false;
9969
+ } else {
9970
+ const parsedNumber = parseFloat(value);
9971
+ if (!isNaN(parsedNumber)) {
9972
+ update[key] = parsedNumber;
9973
+ } else {
9974
+ update[key] = value;
9975
+ }
9976
+ }
9977
+ });
9978
+ return update;
9979
+ }
9980
+ function getHasUneditableRows(metadata) {
9981
+ if (!metadata) {
9982
+ return false;
9983
+ }
9984
+ return Object.values(metadata).some(
9985
+ (value) => !EDITABLE_TYPES.includes(typeof value)
9986
+ );
9987
+ }
9988
+ const NumberInput = forwardRef(
9989
+ ({
9990
+ value,
9991
+ onChange,
9992
+ size = "base",
9993
+ min = 0,
9994
+ max = 100,
9995
+ step = 1,
9996
+ className,
9997
+ disabled,
9998
+ ...props
9999
+ }, ref) => {
10000
+ const handleChange = (event) => {
10001
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
10002
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
10003
+ onChange(newValue);
10004
+ }
10005
+ };
10006
+ const handleIncrement = () => {
10007
+ const newValue = value + step;
10008
+ if (max === void 0 || newValue <= max) {
10009
+ onChange(newValue);
10010
+ }
10011
+ };
10012
+ const handleDecrement = () => {
10013
+ const newValue = value - step;
10014
+ if (min === void 0 || newValue >= min) {
10015
+ onChange(newValue);
10016
+ }
10017
+ };
10018
+ return /* @__PURE__ */ jsxs(
10019
+ "div",
10020
+ {
10021
+ className: clx(
10022
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
10023
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
10024
+ {
10025
+ "h-7": size === "small",
10026
+ "h-8": size === "base"
10027
+ },
10028
+ className
10029
+ ),
10030
+ children: [
10031
+ /* @__PURE__ */ jsx(
10032
+ "input",
10033
+ {
10034
+ ref,
10035
+ type: "number",
10036
+ value,
10037
+ onChange: handleChange,
10038
+ min,
10039
+ max,
10040
+ step,
10041
+ className: clx(
10042
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
10043
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
10044
+ "placeholder:text-ui-fg-muted"
10045
+ ),
10046
+ ...props
10047
+ }
10048
+ ),
10049
+ /* @__PURE__ */ jsxs(
10050
+ "button",
10051
+ {
10052
+ className: clx(
10053
+ "flex items-center justify-center outline-none transition-fg",
10054
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10055
+ "focus:bg-ui-bg-field-component-hover",
10056
+ "hover:bg-ui-bg-field-component-hover",
10057
+ {
10058
+ "size-7": size === "small",
10059
+ "size-8": size === "base"
10060
+ }
10061
+ ),
10062
+ type: "button",
10063
+ onClick: handleDecrement,
10064
+ disabled: min !== void 0 && value <= min || disabled,
10065
+ children: [
10066
+ /* @__PURE__ */ jsx(Minus, {}),
10067
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
10068
+ ]
10069
+ }
10070
+ ),
10071
+ /* @__PURE__ */ jsxs(
10072
+ "button",
10073
+ {
10074
+ className: clx(
10075
+ "flex items-center justify-center outline-none transition-fg",
10076
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
10077
+ "focus:bg-ui-bg-field-hover",
10078
+ "hover:bg-ui-bg-field-hover",
10079
+ {
10080
+ "size-7": size === "small",
10081
+ "size-8": size === "base"
10082
+ }
10083
+ ),
10084
+ type: "button",
10085
+ onClick: handleIncrement,
10086
+ disabled: max !== void 0 && value >= max || disabled,
10087
+ children: [
10088
+ /* @__PURE__ */ jsx(Plus, {}),
10089
+ /* @__PURE__ */ jsx("span", { className: "sr-only", children: `Increase by ${step}` })
10090
+ ]
10091
+ }
10092
+ )
10093
+ ]
10094
+ }
10095
+ );
10096
+ }
10097
+ );
10098
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
10099
+ const productVariantsQueryKeys = {
10100
+ list: (query2) => [
10101
+ PRODUCT_VARIANTS_QUERY_KEY,
10102
+ query2 ? query2 : void 0
10103
+ ]
10104
+ };
10105
+ const useProductVariants = (query2, options) => {
10106
+ const { data, ...rest } = useQuery({
10107
+ queryKey: productVariantsQueryKeys.list(query2),
10108
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
10109
+ ...options
10110
+ });
10111
+ return { ...data, ...rest };
10112
+ };
10113
+ const useCancelOrderEdit = ({ preview }) => {
10114
+ const { mutateAsync: cancelOrderEdit } = useDraftOrderCancelEdit(preview == null ? void 0 : preview.id);
10115
+ const onCancel = useCallback(async () => {
10116
+ if (!preview) {
10117
+ return true;
10118
+ }
10119
+ let res = false;
10120
+ await cancelOrderEdit(void 0, {
10121
+ onError: (e) => {
10122
+ toast.error(e.message);
10123
+ },
10124
+ onSuccess: () => {
10125
+ res = true;
10126
+ }
10127
+ });
10128
+ return res;
10129
+ }, [preview, cancelOrderEdit]);
10130
+ return { onCancel };
10131
+ };
10132
+ let IS_REQUEST_RUNNING = false;
10133
+ const useInitiateOrderEdit = ({
10134
+ preview
10135
+ }) => {
10136
+ const navigate = useNavigate();
10137
+ const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10138
+ useEffect(() => {
10139
+ async function run() {
10140
+ if (IS_REQUEST_RUNNING || !preview) {
10141
+ return;
10142
+ }
10143
+ if (preview.order_change) {
10144
+ return;
10145
+ }
10146
+ IS_REQUEST_RUNNING = true;
10147
+ await mutateAsync(void 0, {
10148
+ onError: (e) => {
10149
+ toast.error(e.message);
10150
+ navigate(`/draft-orders/${preview.id}`, { replace: true });
10151
+ return;
10152
+ }
10153
+ });
10154
+ IS_REQUEST_RUNNING = false;
10155
+ }
10156
+ run();
10157
+ }, [preview, navigate, mutateAsync]);
10158
+ };
10159
+ function convertNumber(value) {
10160
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10161
+ }
10162
+ const STACKED_MODAL_ID = "items_stacked_modal";
10163
+ const Items = () => {
10164
+ const { id } = useParams();
10165
+ const {
10166
+ order: preview,
10167
+ isPending: isPreviewPending,
10168
+ isError: isPreviewError,
10169
+ error: previewError
10170
+ } = useOrderPreview(id, void 0, {
10171
+ placeholderData: keepPreviousData
10172
+ });
10173
+ useInitiateOrderEdit({ preview });
10174
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10175
+ id,
10176
+ {
10177
+ fields: "currency_code"
10178
+ },
10179
+ {
10180
+ enabled: !!id
10181
+ }
10182
+ );
10183
+ const { onCancel } = useCancelOrderEdit({ preview });
10184
+ if (isError) {
10185
+ throw error;
10186
+ }
10187
+ if (isPreviewError) {
10188
+ throw previewError;
10189
+ }
10190
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
10191
+ return /* @__PURE__ */ jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxs("div", { children: [
10192
+ /* @__PURE__ */ jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit Items" }) }),
10193
+ /* @__PURE__ */ jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10194
+ ] }) });
10195
+ };
10196
+ const ItemsForm = ({ preview, currencyCode }) => {
10197
+ var _a;
10198
+ const [isSubmitting, setIsSubmitting] = useState(false);
10199
+ const [modalContent, setModalContent] = useState(
10200
+ null
10201
+ );
10202
+ const { handleSuccess } = useRouteModal();
10203
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
10204
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10205
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10206
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
10207
+ const matches = useMemo(() => {
10208
+ return matchSorter(preview.items, query2, {
10209
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
10210
+ });
10211
+ }, [preview.items, query2]);
10212
+ const onSubmit = async () => {
10213
+ setIsSubmitting(true);
10214
+ let requestSucceeded = false;
10215
+ await requestOrderEdit(void 0, {
10216
+ onError: (e) => {
10217
+ toast.error(`Failed to request order edit: ${e.message}`);
10218
+ },
10219
+ onSuccess: () => {
10220
+ requestSucceeded = true;
10221
+ }
10222
+ });
10223
+ if (!requestSucceeded) {
10224
+ setIsSubmitting(false);
10225
+ return;
10226
+ }
10227
+ await confirmOrderEdit(void 0, {
10228
+ onError: (e) => {
10229
+ toast.error(`Failed to confirm order edit: ${e.message}`);
10230
+ },
10231
+ onSuccess: () => {
10232
+ handleSuccess();
10233
+ },
10234
+ onSettled: () => {
10235
+ setIsSubmitting(false);
10236
+ }
10237
+ });
10238
+ };
10239
+ const onKeyDown = useCallback(
10240
+ (e) => {
10241
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10242
+ if (modalContent || isSubmitting) {
10243
+ return;
10244
+ }
10245
+ onSubmit();
10246
+ }
10247
+ },
10248
+ [modalContent, isSubmitting, onSubmit]
10249
+ );
10250
+ useEffect(() => {
10112
10251
  document.addEventListener("keydown", onKeyDown);
10113
10252
  return () => {
10114
10253
  document.removeEventListener("keydown", onKeyDown);
@@ -10250,169 +10389,21 @@ const Item = ({ item, preview, currencyCode }) => {
10250
10389
  const VariantItem = ({ item, preview, currencyCode }) => {
10251
10390
  const [editing, setEditing] = useState(false);
10252
10391
  const form = useForm({
10253
- defaultValues: {
10254
- quantity: item.quantity,
10255
- unit_price: item.unit_price
10256
- },
10257
- resolver: zodResolver(variantItemSchema)
10258
- });
10259
- const actionId = useMemo(() => {
10260
- var _a, _b;
10261
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10262
- }, [item]);
10263
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10264
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10265
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10266
- const onSubmit = form.handleSubmit(async (data) => {
10267
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10268
- setEditing(false);
10269
- return;
10270
- }
10271
- if (!actionId) {
10272
- await updateOriginalItem(
10273
- {
10274
- item_id: item.id,
10275
- quantity: data.quantity,
10276
- unit_price: convertNumber(data.unit_price)
10277
- },
10278
- {
10279
- onSuccess: () => {
10280
- setEditing(false);
10281
- },
10282
- onError: (e) => {
10283
- toast.error(e.message);
10284
- }
10285
- }
10286
- );
10287
- return;
10288
- }
10289
- await updateActionItem(
10290
- {
10291
- action_id: actionId,
10292
- quantity: data.quantity,
10293
- unit_price: convertNumber(data.unit_price)
10294
- },
10295
- {
10296
- onSuccess: () => {
10297
- setEditing(false);
10298
- },
10299
- onError: (e) => {
10300
- toast.error(e.message);
10301
- }
10302
- }
10303
- );
10304
- });
10305
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10306
- /* @__PURE__ */ jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10307
- /* @__PURE__ */ jsx(
10308
- Thumbnail,
10309
- {
10310
- thumbnail: item.thumbnail,
10311
- alt: item.product_title ?? void 0
10312
- }
10313
- ),
10314
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10315
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
10316
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10317
- /* @__PURE__ */ jsxs(
10318
- Text,
10319
- {
10320
- size: "small",
10321
- leading: "compact",
10322
- className: "text-ui-fg-subtle",
10323
- children: [
10324
- "(",
10325
- item.variant_title,
10326
- ")"
10327
- ]
10328
- }
10329
- )
10330
- ] }),
10331
- /* @__PURE__ */ jsx(
10332
- Text,
10333
- {
10334
- size: "small",
10335
- leading: "compact",
10336
- className: "text-ui-fg-subtle",
10337
- children: item.variant_sku
10338
- }
10339
- )
10340
- ] })
10341
- ] }),
10342
- editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10343
- Form$2.Field,
10344
- {
10345
- control: form.control,
10346
- name: "quantity",
10347
- render: ({ field }) => {
10348
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10349
- }
10350
- }
10351
- ) }) : /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
10352
- editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10353
- Form$2.Field,
10354
- {
10355
- control: form.control,
10356
- name: "unit_price",
10357
- render: ({ field: { onChange, ...field } }) => {
10358
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10359
- CurrencyInput,
10360
- {
10361
- ...field,
10362
- symbol: getNativeSymbol(currencyCode),
10363
- code: currencyCode,
10364
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10365
- }
10366
- ) }) });
10367
- }
10368
- }
10369
- ) }) : /* @__PURE__ */ jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10370
- /* @__PURE__ */ jsx(
10371
- IconButton,
10372
- {
10373
- type: "button",
10374
- size: "small",
10375
- onClick: editing ? onSubmit : () => {
10376
- setEditing(true);
10377
- },
10378
- disabled: isPending,
10379
- children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10380
- }
10381
- )
10382
- ] }) }) });
10383
- };
10384
- const variantItemSchema = objectType({
10385
- quantity: numberType(),
10386
- unit_price: unionType([numberType(), stringType()])
10387
- });
10388
- const CustomItem = ({ item, preview, currencyCode }) => {
10389
- const [editing, setEditing] = useState(false);
10390
- const { quantity, unit_price, title } = item;
10391
- const form = useForm({
10392
- defaultValues: {
10393
- title,
10394
- quantity,
10395
- unit_price
10396
- },
10397
- resolver: zodResolver(customItemSchema)
10398
- });
10399
- useEffect(() => {
10400
- form.reset({
10401
- title,
10402
- quantity,
10403
- unit_price
10404
- });
10405
- }, [form, title, quantity, unit_price]);
10392
+ defaultValues: {
10393
+ quantity: item.quantity,
10394
+ unit_price: item.unit_price
10395
+ },
10396
+ resolver: zodResolver(variantItemSchema)
10397
+ });
10406
10398
  const actionId = useMemo(() => {
10407
10399
  var _a, _b;
10408
10400
  return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10409
10401
  }, [item]);
10410
10402
  const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10411
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10412
10403
  const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10413
10404
  const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10414
10405
  const onSubmit = form.handleSubmit(async (data) => {
10415
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10406
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10416
10407
  setEditing(false);
10417
10408
  return;
10418
10409
  }
@@ -10434,17 +10425,6 @@ const CustomItem = ({ item, preview, currencyCode }) => {
10434
10425
  );
10435
10426
  return;
10436
10427
  }
10437
- if (data.quantity === 0) {
10438
- await removeActionItem(actionId, {
10439
- onSuccess: () => {
10440
- setEditing(false);
10441
- },
10442
- onError: (e) => {
10443
- toast.error(e.message);
10444
- }
10445
- });
10446
- return;
10447
- }
10448
10428
  await updateActionItem(
10449
10429
  {
10450
10430
  action_id: actionId,
@@ -10462,26 +10442,43 @@ const CustomItem = ({ item, preview, currencyCode }) => {
10462
10442
  );
10463
10443
  });
10464
10444
  return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10465
- /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10445
+ /* @__PURE__ */ jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10466
10446
  /* @__PURE__ */ jsx(
10467
10447
  Thumbnail,
10468
10448
  {
10469
10449
  thumbnail: item.thumbnail,
10470
- alt: item.title ?? void 0
10450
+ alt: item.product_title ?? void 0
10471
10451
  }
10472
10452
  ),
10473
- editing ? /* @__PURE__ */ jsx(
10474
- Form$2.Field,
10475
- {
10476
- control: form.control,
10477
- name: "title",
10478
- render: ({ field }) => {
10479
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
10453
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10454
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-1", children: [
10455
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10456
+ /* @__PURE__ */ jsxs(
10457
+ Text,
10458
+ {
10459
+ size: "small",
10460
+ leading: "compact",
10461
+ className: "text-ui-fg-subtle",
10462
+ children: [
10463
+ "(",
10464
+ item.variant_title,
10465
+ ")"
10466
+ ]
10467
+ }
10468
+ )
10469
+ ] }),
10470
+ /* @__PURE__ */ jsx(
10471
+ Text,
10472
+ {
10473
+ size: "small",
10474
+ leading: "compact",
10475
+ className: "text-ui-fg-subtle",
10476
+ children: item.variant_sku
10480
10477
  }
10481
- }
10482
- ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
10478
+ )
10479
+ ] })
10483
10480
  ] }),
10484
- editing ? /* @__PURE__ */ jsx(
10481
+ editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10485
10482
  Form$2.Field,
10486
10483
  {
10487
10484
  control: form.control,
@@ -10490,8 +10487,8 @@ const CustomItem = ({ item, preview, currencyCode }) => {
10490
10487
  return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10491
10488
  }
10492
10489
  }
10493
- ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
10494
- editing ? /* @__PURE__ */ jsx(
10490
+ ) }) : /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }) }),
10491
+ editing ? /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(
10495
10492
  Form$2.Field,
10496
10493
  {
10497
10494
  control: form.control,
@@ -10508,230 +10505,94 @@ const CustomItem = ({ item, preview, currencyCode }) => {
10508
10505
  ) }) });
10509
10506
  }
10510
10507
  }
10511
- ) : /* @__PURE__ */ jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10512
- /* @__PURE__ */ jsx(
10513
- IconButton,
10514
- {
10515
- type: "button",
10516
- size: "small",
10517
- onClick: editing ? onSubmit : () => {
10518
- setEditing(true);
10519
- },
10520
- disabled: isPending,
10521
- children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10522
- }
10523
- )
10524
- ] }) }) });
10525
- };
10526
- const StackedModalTrigger$1 = ({
10527
- type,
10528
- setModalContent
10529
- }) => {
10530
- const { setIsOpen } = useStackedModal();
10531
- const onClick = useCallback(() => {
10532
- setModalContent(type);
10533
- setIsOpen(STACKED_MODAL_ID, true);
10534
- }, [setModalContent, setIsOpen, type]);
10535
- return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10536
- };
10537
- const VARIANT_PREFIX = "items";
10538
- const LIMIT = 50;
10539
- const ExistingItemsForm = ({ orderId, items }) => {
10540
- const { setIsOpen } = useStackedModal();
10541
- const [rowSelection, setRowSelection] = useState(
10542
- items.reduce((acc, item) => {
10543
- acc[item.variant_id] = true;
10544
- return acc;
10545
- }, {})
10546
- );
10547
- useEffect(() => {
10548
- setRowSelection(
10549
- items.reduce((acc, item) => {
10550
- if (item.variant_id) {
10551
- acc[item.variant_id] = true;
10552
- }
10553
- return acc;
10554
- }, {})
10555
- );
10556
- }, [items]);
10557
- const { q, order, offset } = useQueryParams(
10558
- ["q", "order", "offset"],
10559
- VARIANT_PREFIX
10560
- );
10561
- const { variants, count, isPending, isError, error } = useProductVariants(
10562
- {
10563
- q,
10564
- order,
10565
- offset: offset ? parseInt(offset) : void 0,
10566
- limit: LIMIT
10567
- },
10568
- {
10569
- placeholderData: keepPreviousData
10570
- }
10571
- );
10572
- const columns = useColumns();
10573
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10574
- const onSubmit = async () => {
10575
- const ids = Object.keys(rowSelection).filter(
10576
- (id) => !items.find((i) => i.variant_id === id)
10577
- );
10578
- await mutateAsync(
10579
- {
10580
- items: ids.map((id) => ({
10581
- variant_id: id,
10582
- quantity: 1
10583
- }))
10584
- },
10585
- {
10586
- onSuccess: () => {
10587
- setRowSelection({});
10588
- setIsOpen(STACKED_MODAL_ID, false);
10589
- },
10590
- onError: (e) => {
10591
- toast.error(e.message);
10592
- }
10593
- }
10594
- );
10595
- };
10596
- if (isError) {
10597
- throw error;
10598
- }
10599
- return /* @__PURE__ */ jsxs(
10600
- StackedFocusModal.Content,
10601
- {
10602
- onOpenAutoFocus: (e) => {
10603
- e.preventDefault();
10604
- const searchInput = document.querySelector(
10605
- "[data-modal-id='modal-search-input']"
10606
- );
10607
- if (searchInput) {
10608
- searchInput.focus();
10609
- }
10610
- },
10611
- children: [
10612
- /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
10613
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10614
- /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10615
- ] }),
10616
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
10617
- DataTable,
10618
- {
10619
- data: variants,
10620
- columns,
10621
- isLoading: isPending,
10622
- getRowId: (row) => row.id,
10623
- rowCount: count,
10624
- prefix: VARIANT_PREFIX,
10625
- layout: "fill",
10626
- rowSelection: {
10627
- state: rowSelection,
10628
- onRowSelectionChange: setRowSelection,
10629
- enableRowSelection: (row) => {
10630
- return !items.find((i) => i.variant_id === row.original.id);
10631
- }
10632
- },
10633
- autoFocusSearch: true
10634
- }
10635
- ) }),
10636
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10637
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10638
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10639
- ] }) })
10640
- ]
10641
- }
10642
- );
10643
- };
10644
- const columnHelper = createDataTableColumnHelper();
10645
- const useColumns = () => {
10646
- return useMemo(() => {
10647
- return [
10648
- columnHelper.select(),
10649
- columnHelper.accessor("product.title", {
10650
- header: "Product",
10651
- cell: ({ row }) => {
10652
- var _a, _b, _c;
10653
- return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
10654
- /* @__PURE__ */ jsx(
10655
- Thumbnail,
10656
- {
10657
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10658
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10659
- }
10660
- ),
10661
- /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10662
- ] });
10663
- },
10664
- enableSorting: true
10665
- }),
10666
- columnHelper.accessor("title", {
10667
- header: "Variant",
10668
- enableSorting: true
10669
- }),
10670
- columnHelper.accessor("sku", {
10671
- header: "SKU",
10672
- cell: ({ getValue }) => {
10673
- return getValue() ?? "-";
10674
- },
10675
- enableSorting: true
10676
- }),
10677
- columnHelper.accessor("updated_at", {
10678
- header: "Updated",
10679
- cell: ({ getValue }) => {
10680
- return /* @__PURE__ */ jsx(
10681
- Tooltip,
10682
- {
10683
- content: getFullDate({ date: getValue(), includeTime: true }),
10684
- children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10685
- }
10686
- );
10687
- },
10688
- enableSorting: true,
10689
- sortAscLabel: "Oldest first",
10690
- sortDescLabel: "Newest first"
10691
- }),
10692
- columnHelper.accessor("created_at", {
10693
- header: "Created",
10694
- cell: ({ getValue }) => {
10695
- return /* @__PURE__ */ jsx(
10696
- Tooltip,
10697
- {
10698
- content: getFullDate({ date: getValue(), includeTime: true }),
10699
- children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10700
- }
10701
- );
10508
+ ) }) : /* @__PURE__ */ jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10509
+ /* @__PURE__ */ jsx(
10510
+ IconButton,
10511
+ {
10512
+ type: "button",
10513
+ size: "small",
10514
+ onClick: editing ? onSubmit : () => {
10515
+ setEditing(true);
10702
10516
  },
10703
- enableSorting: true,
10704
- sortAscLabel: "Oldest first",
10705
- sortDescLabel: "Newest first"
10706
- })
10707
- ];
10708
- }, []);
10517
+ disabled: isPending,
10518
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10519
+ }
10520
+ )
10521
+ ] }) }) });
10709
10522
  };
10710
- const CustomItemForm = ({ orderId, currencyCode }) => {
10711
- const { setIsOpen } = useStackedModal();
10712
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10523
+ const variantItemSchema = objectType({
10524
+ quantity: numberType(),
10525
+ unit_price: unionType([numberType(), stringType()])
10526
+ });
10527
+ const CustomItem = ({ item, preview, currencyCode }) => {
10528
+ const [editing, setEditing] = useState(false);
10529
+ const { quantity, unit_price, title } = item;
10713
10530
  const form = useForm({
10714
10531
  defaultValues: {
10715
- title: "",
10716
- quantity: 1,
10717
- unit_price: ""
10532
+ title,
10533
+ quantity,
10534
+ unit_price
10718
10535
  },
10719
10536
  resolver: zodResolver(customItemSchema)
10720
10537
  });
10538
+ useEffect(() => {
10539
+ form.reset({
10540
+ title,
10541
+ quantity,
10542
+ unit_price
10543
+ });
10544
+ }, [form, title, quantity, unit_price]);
10545
+ const actionId = useMemo(() => {
10546
+ var _a, _b;
10547
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10548
+ }, [item]);
10549
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10550
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10551
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10552
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10721
10553
  const onSubmit = form.handleSubmit(async (data) => {
10722
- await addItems(
10723
- {
10724
- items: [
10725
- {
10726
- title: data.title,
10727
- quantity: data.quantity,
10728
- unit_price: convertNumber(data.unit_price)
10554
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10555
+ setEditing(false);
10556
+ return;
10557
+ }
10558
+ if (!actionId) {
10559
+ await updateOriginalItem(
10560
+ {
10561
+ item_id: item.id,
10562
+ quantity: data.quantity,
10563
+ unit_price: convertNumber(data.unit_price)
10564
+ },
10565
+ {
10566
+ onSuccess: () => {
10567
+ setEditing(false);
10568
+ },
10569
+ onError: (e) => {
10570
+ toast.error(e.message);
10729
10571
  }
10730
- ]
10572
+ }
10573
+ );
10574
+ return;
10575
+ }
10576
+ if (data.quantity === 0) {
10577
+ await removeActionItem(actionId, {
10578
+ onSuccess: () => {
10579
+ setEditing(false);
10580
+ },
10581
+ onError: (e) => {
10582
+ toast.error(e.message);
10583
+ }
10584
+ });
10585
+ return;
10586
+ }
10587
+ await updateActionItem(
10588
+ {
10589
+ action_id: actionId,
10590
+ quantity: data.quantity,
10591
+ unit_price: convertNumber(data.unit_price)
10731
10592
  },
10732
10593
  {
10733
10594
  onSuccess: () => {
10734
- setIsOpen(STACKED_MODAL_ID, false);
10595
+ setEditing(false);
10735
10596
  },
10736
10597
  onError: (e) => {
10737
10598
  toast.error(e.message);
@@ -10739,714 +10600,663 @@ const CustomItemForm = ({ orderId, currencyCode }) => {
10739
10600
  }
10740
10601
  );
10741
10602
  });
10742
- return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
10743
- /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
10744
- /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
10745
- /* @__PURE__ */ jsxs("div", { children: [
10746
- /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
10747
- /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx(Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
10748
- ] }),
10749
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10603
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx("form", { onSubmit, children: /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10604
+ /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-3", children: [
10750
10605
  /* @__PURE__ */ jsx(
10751
- Form$2.Field,
10606
+ Thumbnail,
10752
10607
  {
10753
- control: form.control,
10754
- name: "title",
10755
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10756
- /* @__PURE__ */ jsxs("div", { children: [
10757
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
10758
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
10759
- ] }),
10760
- /* @__PURE__ */ jsxs("div", { children: [
10761
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10762
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10763
- ] })
10764
- ] }) })
10608
+ thumbnail: item.thumbnail,
10609
+ alt: item.title ?? void 0
10765
10610
  }
10766
10611
  ),
10767
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10768
- /* @__PURE__ */ jsx(
10612
+ editing ? /* @__PURE__ */ jsx(
10769
10613
  Form$2.Field,
10770
10614
  {
10771
10615
  control: form.control,
10772
- name: "unit_price",
10773
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10774
- /* @__PURE__ */ jsxs("div", { children: [
10775
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
10776
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10777
- ] }),
10778
- /* @__PURE__ */ jsxs("div", { children: [
10779
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10780
- CurrencyInput,
10781
- {
10782
- symbol: getNativeSymbol(currencyCode),
10783
- code: currencyCode,
10784
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10785
- ...field
10786
- }
10787
- ) }),
10788
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10789
- ] })
10790
- ] }) })
10616
+ name: "title",
10617
+ render: ({ field }) => {
10618
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }) });
10619
+ }
10791
10620
  }
10792
- ),
10793
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10794
- /* @__PURE__ */ jsx(
10795
- Form$2.Field,
10796
- {
10797
- control: form.control,
10798
- name: "quantity",
10799
- render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10800
- /* @__PURE__ */ jsxs("div", { children: [
10801
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
10802
- /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10803
- ] }),
10804
- /* @__PURE__ */ jsxs("div", { className: "w-full flex-1", children: [
10805
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10806
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10807
- ] })
10808
- ] }) })
10621
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.title })
10622
+ ] }),
10623
+ editing ? /* @__PURE__ */ jsx(
10624
+ Form$2.Field,
10625
+ {
10626
+ control: form.control,
10627
+ name: "quantity",
10628
+ render: ({ field }) => {
10629
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(NumberInput, { ...field }) }) });
10809
10630
  }
10810
- )
10811
- ] }) }) }),
10812
- /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10813
- /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10814
- /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10815
- ] }) })
10816
- ] }) }) });
10817
- };
10818
- const customItemSchema = objectType({
10819
- title: stringType().min(1),
10820
- quantity: numberType(),
10821
- unit_price: unionType([numberType(), stringType()])
10822
- });
10823
- const PROMOTION_QUERY_KEY = "promotions";
10824
- const promotionsQueryKeys = {
10825
- list: (query2) => [
10826
- PROMOTION_QUERY_KEY,
10827
- query2 ? query2 : void 0
10828
- ],
10829
- detail: (id, query2) => [
10830
- PROMOTION_QUERY_KEY,
10831
- id,
10832
- query2 ? query2 : void 0
10833
- ]
10834
- };
10835
- const usePromotions = (query2, options) => {
10836
- const { data, ...rest } = useQuery({
10837
- queryKey: promotionsQueryKeys.list(query2),
10838
- queryFn: async () => sdk.admin.promotion.list(query2),
10839
- ...options
10840
- });
10841
- return { ...data, ...rest };
10842
- };
10843
- const Promotions = () => {
10844
- const { id } = useParams();
10845
- const {
10846
- order: preview,
10847
- isError: isPreviewError,
10848
- error: previewError
10849
- } = useOrderPreview(id, void 0);
10850
- useInitiateOrderEdit({ preview });
10851
- const { onCancel } = useCancelOrderEdit({ preview });
10852
- if (isPreviewError) {
10853
- throw previewError;
10854
- }
10855
- const isReady = !!preview;
10856
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10857
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10858
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10859
- ] });
10631
+ }
10632
+ ) : /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: item.quantity }),
10633
+ editing ? /* @__PURE__ */ jsx(
10634
+ Form$2.Field,
10635
+ {
10636
+ control: form.control,
10637
+ name: "unit_price",
10638
+ render: ({ field: { onChange, ...field } }) => {
10639
+ return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10640
+ CurrencyInput,
10641
+ {
10642
+ ...field,
10643
+ symbol: getNativeSymbol(currencyCode),
10644
+ code: currencyCode,
10645
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10646
+ }
10647
+ ) }) });
10648
+ }
10649
+ }
10650
+ ) : /* @__PURE__ */ jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10651
+ /* @__PURE__ */ jsx(
10652
+ IconButton,
10653
+ {
10654
+ type: "button",
10655
+ size: "small",
10656
+ onClick: editing ? onSubmit : () => {
10657
+ setEditing(true);
10658
+ },
10659
+ disabled: isPending,
10660
+ children: editing ? /* @__PURE__ */ jsx(Check, {}) : /* @__PURE__ */ jsx(PencilSquare, {})
10661
+ }
10662
+ )
10663
+ ] }) }) });
10860
10664
  };
10861
- const PromotionForm = ({ preview }) => {
10862
- const { items, shipping_methods } = preview;
10863
- const [isSubmitting, setIsSubmitting] = useState(false);
10864
- const [comboboxValue, setComboboxValue] = useState("");
10865
- const { handleSuccess } = useRouteModal();
10866
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10867
- const promoIds = getPromotionIds(items, shipping_methods);
10868
- const { promotions, isPending, isError, error } = usePromotions(
10665
+ const StackedModalTrigger$1 = ({
10666
+ type,
10667
+ setModalContent
10668
+ }) => {
10669
+ const { setIsOpen } = useStackedModal();
10670
+ const onClick = useCallback(() => {
10671
+ setModalContent(type);
10672
+ setIsOpen(STACKED_MODAL_ID, true);
10673
+ }, [setModalContent, setIsOpen, type]);
10674
+ return /* @__PURE__ */ jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsx(DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10675
+ };
10676
+ const VARIANT_PREFIX = "items";
10677
+ const LIMIT = 50;
10678
+ const ExistingItemsForm = ({ orderId, items }) => {
10679
+ const { setIsOpen } = useStackedModal();
10680
+ const [rowSelection, setRowSelection] = useState(
10681
+ items.reduce((acc, item) => {
10682
+ acc[item.variant_id] = true;
10683
+ return acc;
10684
+ }, {})
10685
+ );
10686
+ useEffect(() => {
10687
+ setRowSelection(
10688
+ items.reduce((acc, item) => {
10689
+ if (item.variant_id) {
10690
+ acc[item.variant_id] = true;
10691
+ }
10692
+ return acc;
10693
+ }, {})
10694
+ );
10695
+ }, [items]);
10696
+ const { q, order, offset } = useQueryParams(
10697
+ ["q", "order", "offset"],
10698
+ VARIANT_PREFIX
10699
+ );
10700
+ const { variants, count, isPending, isError, error } = useProductVariants(
10869
10701
  {
10870
- id: promoIds
10702
+ q,
10703
+ order,
10704
+ offset: offset ? parseInt(offset) : void 0,
10705
+ limit: LIMIT
10871
10706
  },
10872
10707
  {
10873
- enabled: !!promoIds.length
10708
+ placeholderData: keepPreviousData
10874
10709
  }
10875
10710
  );
10876
- const comboboxData = useComboboxData({
10877
- queryKey: ["promotions", "combobox", promoIds],
10878
- queryFn: async (params) => {
10879
- return await sdk.admin.promotion.list({
10880
- ...params,
10881
- id: {
10882
- $nin: promoIds
10883
- }
10884
- });
10885
- },
10886
- getOptions: (data) => {
10887
- return data.promotions.map((promotion) => ({
10888
- label: promotion.code,
10889
- value: promotion.code
10890
- }));
10891
- }
10892
- });
10893
- const add = async (value) => {
10894
- if (!value) {
10895
- return;
10896
- }
10897
- addPromotions(
10711
+ const columns = useColumns();
10712
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
10713
+ const onSubmit = async () => {
10714
+ const ids = Object.keys(rowSelection).filter(
10715
+ (id) => !items.find((i) => i.variant_id === id)
10716
+ );
10717
+ await mutateAsync(
10898
10718
  {
10899
- promo_codes: [value]
10719
+ items: ids.map((id) => ({
10720
+ variant_id: id,
10721
+ quantity: 1
10722
+ }))
10900
10723
  },
10901
10724
  {
10725
+ onSuccess: () => {
10726
+ setRowSelection({});
10727
+ setIsOpen(STACKED_MODAL_ID, false);
10728
+ },
10902
10729
  onError: (e) => {
10903
10730
  toast.error(e.message);
10904
- comboboxData.onSearchValueChange("");
10905
- setComboboxValue("");
10906
- },
10907
- onSuccess: () => {
10908
- comboboxData.onSearchValueChange("");
10909
- setComboboxValue("");
10910
10731
  }
10911
10732
  }
10912
10733
  );
10913
10734
  };
10914
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10915
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10916
- const onSubmit = async () => {
10917
- setIsSubmitting(true);
10918
- let requestSucceeded = false;
10919
- await requestOrderEdit(void 0, {
10920
- onError: (e) => {
10921
- toast.error(e.message);
10922
- },
10923
- onSuccess: () => {
10924
- requestSucceeded = true;
10925
- }
10926
- });
10927
- if (!requestSucceeded) {
10928
- setIsSubmitting(false);
10929
- return;
10930
- }
10931
- await confirmOrderEdit(void 0, {
10932
- onError: (e) => {
10933
- toast.error(e.message);
10934
- },
10935
- onSuccess: () => {
10936
- handleSuccess();
10937
- },
10938
- onSettled: () => {
10939
- setIsSubmitting(false);
10940
- }
10941
- });
10942
- };
10943
10735
  if (isError) {
10944
10736
  throw error;
10945
10737
  }
10946
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10947
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
10948
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
10949
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10950
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10951
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10738
+ return /* @__PURE__ */ jsxs(
10739
+ StackedFocusModal.Content,
10740
+ {
10741
+ onOpenAutoFocus: (e) => {
10742
+ e.preventDefault();
10743
+ const searchInput = document.querySelector(
10744
+ "[data-modal-id='modal-search-input']"
10745
+ );
10746
+ if (searchInput) {
10747
+ searchInput.focus();
10748
+ }
10749
+ },
10750
+ children: [
10751
+ /* @__PURE__ */ jsxs(StackedFocusModal.Header, { children: [
10752
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10753
+ /* @__PURE__ */ jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10952
10754
  ] }),
10953
- /* @__PURE__ */ jsx(
10954
- Combobox,
10755
+ /* @__PURE__ */ jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsx(
10756
+ DataTable,
10955
10757
  {
10956
- id: "promotion-combobox",
10957
- "aria-describedby": "promotion-combobox-hint",
10958
- isFetchingNextPage: comboboxData.isFetchingNextPage,
10959
- fetchNextPage: comboboxData.fetchNextPage,
10960
- options: comboboxData.options,
10961
- onSearchValueChange: comboboxData.onSearchValueChange,
10962
- searchValue: comboboxData.searchValue,
10963
- disabled: comboboxData.disabled || isAddingPromotions,
10964
- onChange: add,
10965
- value: comboboxValue
10758
+ data: variants,
10759
+ columns,
10760
+ isLoading: isPending,
10761
+ getRowId: (row) => row.id,
10762
+ rowCount: count,
10763
+ prefix: VARIANT_PREFIX,
10764
+ layout: "fill",
10765
+ rowSelection: {
10766
+ state: rowSelection,
10767
+ onRowSelectionChange: setRowSelection,
10768
+ enableRowSelection: (row) => {
10769
+ return !items.find((i) => i.variant_id === row.original.id);
10770
+ }
10771
+ },
10772
+ autoFocusSearch: true
10966
10773
  }
10967
- )
10968
- ] }),
10969
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10970
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
10971
- PromotionItem,
10972
- {
10973
- promotion,
10974
- orderId: preview.id,
10975
- isLoading: isPending
10774
+ ) }),
10775
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10776
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10777
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10778
+ ] }) })
10779
+ ]
10780
+ }
10781
+ );
10782
+ };
10783
+ const columnHelper = createDataTableColumnHelper();
10784
+ const useColumns = () => {
10785
+ return useMemo(() => {
10786
+ return [
10787
+ columnHelper.select(),
10788
+ columnHelper.accessor("product.title", {
10789
+ header: "Product",
10790
+ cell: ({ row }) => {
10791
+ var _a, _b, _c;
10792
+ return /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-x-2", children: [
10793
+ /* @__PURE__ */ jsx(
10794
+ Thumbnail,
10795
+ {
10796
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10797
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
10798
+ }
10799
+ ),
10800
+ /* @__PURE__ */ jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10801
+ ] });
10976
10802
  },
10977
- promotion.id
10978
- )) })
10979
- ] }) }),
10980
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10981
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10982
- /* @__PURE__ */ jsx(
10983
- Button,
10984
- {
10985
- size: "small",
10986
- type: "submit",
10987
- isLoading: isSubmitting || isAddingPromotions,
10988
- children: "Save"
10989
- }
10990
- )
10991
- ] }) })
10992
- ] });
10803
+ enableSorting: true
10804
+ }),
10805
+ columnHelper.accessor("title", {
10806
+ header: "Variant",
10807
+ enableSorting: true
10808
+ }),
10809
+ columnHelper.accessor("sku", {
10810
+ header: "SKU",
10811
+ cell: ({ getValue }) => {
10812
+ return getValue() ?? "-";
10813
+ },
10814
+ enableSorting: true
10815
+ }),
10816
+ columnHelper.accessor("updated_at", {
10817
+ header: "Updated",
10818
+ cell: ({ getValue }) => {
10819
+ return /* @__PURE__ */ jsx(
10820
+ Tooltip,
10821
+ {
10822
+ content: getFullDate({ date: getValue(), includeTime: true }),
10823
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10824
+ }
10825
+ );
10826
+ },
10827
+ enableSorting: true,
10828
+ sortAscLabel: "Oldest first",
10829
+ sortDescLabel: "Newest first"
10830
+ }),
10831
+ columnHelper.accessor("created_at", {
10832
+ header: "Created",
10833
+ cell: ({ getValue }) => {
10834
+ return /* @__PURE__ */ jsx(
10835
+ Tooltip,
10836
+ {
10837
+ content: getFullDate({ date: getValue(), includeTime: true }),
10838
+ children: /* @__PURE__ */ jsx("span", { children: getFullDate({ date: getValue() }) })
10839
+ }
10840
+ );
10841
+ },
10842
+ enableSorting: true,
10843
+ sortAscLabel: "Oldest first",
10844
+ sortDescLabel: "Newest first"
10845
+ })
10846
+ ];
10847
+ }, []);
10993
10848
  };
10994
- const PromotionItem = ({
10995
- promotion,
10996
- orderId,
10997
- isLoading
10998
- }) => {
10999
- var _a;
11000
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11001
- const onRemove = async () => {
11002
- removePromotions(
10849
+ const CustomItemForm = ({ orderId, currencyCode }) => {
10850
+ const { setIsOpen } = useStackedModal();
10851
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10852
+ const form = useForm({
10853
+ defaultValues: {
10854
+ title: "",
10855
+ quantity: 1,
10856
+ unit_price: ""
10857
+ },
10858
+ resolver: zodResolver(customItemSchema)
10859
+ });
10860
+ const onSubmit = form.handleSubmit(async (data) => {
10861
+ await addItems(
11003
10862
  {
11004
- promo_codes: [promotion.code]
10863
+ items: [
10864
+ {
10865
+ title: data.title,
10866
+ quantity: data.quantity,
10867
+ unit_price: convertNumber(data.unit_price)
10868
+ }
10869
+ ]
11005
10870
  },
11006
10871
  {
10872
+ onSuccess: () => {
10873
+ setIsOpen(STACKED_MODAL_ID, false);
10874
+ },
11007
10875
  onError: (e) => {
11008
10876
  toast.error(e.message);
11009
10877
  }
11010
10878
  }
11011
10879
  );
11012
- };
11013
- const displayValue = getDisplayValue(promotion);
11014
- return /* @__PURE__ */ jsxs(
11015
- "div",
11016
- {
11017
- className: clx(
11018
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
10880
+ });
10881
+ return /* @__PURE__ */ jsx(Form$2, { ...form, children: /* @__PURE__ */ jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxs(StackedFocusModal.Content, { children: [
10882
+ /* @__PURE__ */ jsx(StackedFocusModal.Header, {}),
10883
+ /* @__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: [
10884
+ /* @__PURE__ */ jsxs("div", { children: [
10885
+ /* @__PURE__ */ jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Add custom item" }) }),
10886
+ /* @__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." }) })
10887
+ ] }),
10888
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10889
+ /* @__PURE__ */ jsx(
10890
+ Form$2.Field,
11019
10891
  {
11020
- "animate-pulse": isLoading
10892
+ control: form.control,
10893
+ name: "title",
10894
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10895
+ /* @__PURE__ */ jsxs("div", { children: [
10896
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Title" }),
10897
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the title of the item" })
10898
+ ] }),
10899
+ /* @__PURE__ */ jsxs("div", { children: [
10900
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
10901
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10902
+ ] })
10903
+ ] }) })
11021
10904
  }
11022
10905
  ),
11023
- children: [
11024
- /* @__PURE__ */ jsxs("div", { children: [
11025
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11026
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11027
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11028
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11029
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
10906
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10907
+ /* @__PURE__ */ jsx(
10908
+ Form$2.Field,
10909
+ {
10910
+ control: form.control,
10911
+ name: "unit_price",
10912
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10913
+ /* @__PURE__ */ jsxs("div", { children: [
10914
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Unit price" }),
10915
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
11030
10916
  ] }),
11031
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11032
- ] })
11033
- ] }),
11034
- /* @__PURE__ */ jsx(
11035
- IconButton,
11036
- {
11037
- size: "small",
11038
- type: "button",
11039
- variant: "transparent",
11040
- onClick: onRemove,
11041
- isLoading: isPending || isLoading,
11042
- children: /* @__PURE__ */ jsx(XMark, {})
11043
- }
11044
- )
11045
- ]
11046
- },
11047
- promotion.id
11048
- );
11049
- };
11050
- function getDisplayValue(promotion) {
11051
- var _a, _b, _c, _d;
11052
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11053
- if (!value) {
11054
- return null;
11055
- }
11056
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11057
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11058
- if (!currency) {
11059
- return null;
11060
- }
11061
- return getLocaleAmount(value, currency);
11062
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11063
- return formatPercentage(value);
11064
- }
11065
- return null;
11066
- }
11067
- const formatter = new Intl.NumberFormat([], {
11068
- style: "percent",
11069
- minimumFractionDigits: 2
11070
- });
11071
- const formatPercentage = (value, isPercentageValue = false) => {
11072
- let val = value || 0;
11073
- if (!isPercentageValue) {
11074
- val = val / 100;
11075
- }
11076
- return formatter.format(val);
11077
- };
11078
- function getPromotionIds(items, shippingMethods) {
11079
- const promotionIds = /* @__PURE__ */ new Set();
11080
- for (const item of items) {
11081
- if (item.adjustments) {
11082
- for (const adjustment of item.adjustments) {
11083
- if (adjustment.promotion_id) {
11084
- promotionIds.add(adjustment.promotion_id);
11085
- }
11086
- }
11087
- }
11088
- }
11089
- for (const shippingMethod of shippingMethods) {
11090
- if (shippingMethod.adjustments) {
11091
- for (const adjustment of shippingMethod.adjustments) {
11092
- if (adjustment.promotion_id) {
11093
- promotionIds.add(adjustment.promotion_id);
10917
+ /* @__PURE__ */ jsxs("div", { children: [
10918
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
10919
+ CurrencyInput,
10920
+ {
10921
+ symbol: getNativeSymbol(currencyCode),
10922
+ code: currencyCode,
10923
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10924
+ ...field
10925
+ }
10926
+ ) }),
10927
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10928
+ ] })
10929
+ ] }) })
11094
10930
  }
11095
- }
11096
- }
11097
- }
11098
- return Array.from(promotionIds);
11099
- }
11100
- const InlineTip = forwardRef(
11101
- ({ variant = "tip", label, className, children, ...props }, ref) => {
11102
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
11103
- return /* @__PURE__ */ jsxs(
11104
- "div",
11105
- {
11106
- ref,
11107
- className: clx(
11108
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
11109
- className
11110
- ),
11111
- ...props,
11112
- children: [
11113
- /* @__PURE__ */ jsx(
11114
- "div",
11115
- {
11116
- role: "presentation",
11117
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
11118
- "bg-ui-tag-orange-icon": variant === "warning"
11119
- })
11120
- }
11121
- ),
11122
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
11123
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
11124
- labelValue,
11125
- ":"
10931
+ ),
10932
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10933
+ /* @__PURE__ */ jsx(
10934
+ Form$2.Field,
10935
+ {
10936
+ control: form.control,
10937
+ name: "quantity",
10938
+ render: ({ field }) => /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10939
+ /* @__PURE__ */ jsxs("div", { children: [
10940
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Quantity" }),
10941
+ /* @__PURE__ */ jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
11126
10942
  ] }),
11127
- " ",
11128
- children
11129
- ] })
11130
- ]
11131
- }
11132
- );
11133
- }
11134
- );
11135
- InlineTip.displayName = "InlineTip";
11136
- const MetadataFieldSchema = objectType({
11137
- key: stringType(),
11138
- disabled: booleanType().optional(),
11139
- value: anyType()
11140
- });
11141
- const MetadataSchema = objectType({
11142
- metadata: arrayType(MetadataFieldSchema)
10943
+ /* @__PURE__ */ jsxs("div", { className: "w-full flex-1", children: [
10944
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10945
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
10946
+ ] })
10947
+ ] }) })
10948
+ }
10949
+ )
10950
+ ] }) }) }),
10951
+ /* @__PURE__ */ jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10952
+ /* @__PURE__ */ jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10953
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10954
+ ] }) })
10955
+ ] }) }) });
10956
+ };
10957
+ const customItemSchema = objectType({
10958
+ title: stringType().min(1),
10959
+ quantity: numberType(),
10960
+ unit_price: unionType([numberType(), stringType()])
11143
10961
  });
11144
- const Metadata = () => {
11145
- const { id } = useParams();
11146
- const { order, isPending, isError, error } = useOrder(id, {
11147
- fields: "metadata"
10962
+ const PROMOTION_QUERY_KEY = "promotions";
10963
+ const promotionsQueryKeys = {
10964
+ list: (query2) => [
10965
+ PROMOTION_QUERY_KEY,
10966
+ query2 ? query2 : void 0
10967
+ ],
10968
+ detail: (id, query2) => [
10969
+ PROMOTION_QUERY_KEY,
10970
+ id,
10971
+ query2 ? query2 : void 0
10972
+ ]
10973
+ };
10974
+ const usePromotions = (query2, options) => {
10975
+ const { data, ...rest } = useQuery({
10976
+ queryKey: promotionsQueryKeys.list(query2),
10977
+ queryFn: async () => sdk.admin.promotion.list(query2),
10978
+ ...options
11148
10979
  });
11149
- if (isError) {
11150
- throw error;
10980
+ return { ...data, ...rest };
10981
+ };
10982
+ const Promotions = () => {
10983
+ const { id } = useParams();
10984
+ const {
10985
+ order: preview,
10986
+ isError: isPreviewError,
10987
+ error: previewError
10988
+ } = useOrderPreview(id, void 0);
10989
+ useInitiateOrderEdit({ preview });
10990
+ const { onCancel } = useCancelOrderEdit({ preview });
10991
+ if (isPreviewError) {
10992
+ throw previewError;
11151
10993
  }
11152
- const isReady = !isPending && !!order;
11153
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11154
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11155
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
11156
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
11157
- ] }),
11158
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10994
+ const isReady = !!preview;
10995
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10996
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10997
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11159
10998
  ] });
11160
10999
  };
11161
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
11162
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
11163
- const MetadataForm = ({ orderId, metadata }) => {
11000
+ const PromotionForm = ({ preview }) => {
11001
+ const { items, shipping_methods } = preview;
11002
+ const [isSubmitting, setIsSubmitting] = useState(false);
11003
+ const [comboboxValue, setComboboxValue] = useState("");
11164
11004
  const { handleSuccess } = useRouteModal();
11165
- const hasUneditableRows = getHasUneditableRows(metadata);
11166
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
11167
- const form = useForm({
11168
- defaultValues: {
11169
- metadata: getDefaultValues(metadata)
11005
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11006
+ const promoIds = getPromotionIds(items, shipping_methods);
11007
+ const { promotions, isPending, isError, error } = usePromotions(
11008
+ {
11009
+ id: promoIds
11170
11010
  },
11171
- resolver: zodResolver(MetadataSchema)
11172
- });
11173
- const handleSubmit = form.handleSubmit(async (data) => {
11174
- const parsedData = parseValues(data);
11175
- await mutateAsync(
11176
- {
11177
- metadata: parsedData
11178
- },
11179
- {
11180
- onSuccess: () => {
11181
- toast.success("Metadata updated");
11182
- handleSuccess();
11183
- },
11184
- onError: (error) => {
11185
- toast.error(error.message);
11011
+ {
11012
+ enabled: !!promoIds.length
11013
+ }
11014
+ );
11015
+ const comboboxData = useComboboxData({
11016
+ queryKey: ["promotions", "combobox", promoIds],
11017
+ queryFn: async (params) => {
11018
+ return await sdk.admin.promotion.list({
11019
+ ...params,
11020
+ id: {
11021
+ $nin: promoIds
11186
11022
  }
11187
- }
11188
- );
11189
- });
11190
- const { fields, insert, remove } = useFieldArray({
11191
- control: form.control,
11192
- name: "metadata"
11193
- });
11194
- function deleteRow(index) {
11195
- remove(index);
11196
- if (fields.length === 1) {
11197
- insert(0, {
11198
- key: "",
11199
- value: "",
11200
- disabled: false
11201
11023
  });
11024
+ },
11025
+ getOptions: (data) => {
11026
+ return data.promotions.map((promotion) => ({
11027
+ label: promotion.code,
11028
+ value: promotion.code
11029
+ }));
11202
11030
  }
11203
- }
11204
- function insertRow(index, position) {
11205
- insert(index + (position === "above" ? 0 : 1), {
11206
- key: "",
11207
- value: "",
11208
- disabled: false
11209
- });
11210
- }
11211
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11212
- KeyboundForm,
11213
- {
11214
- onSubmit: handleSubmit,
11215
- className: "flex flex-1 flex-col overflow-hidden",
11216
- children: [
11217
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
11218
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
11219
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
11220
- /* @__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" }) }),
11221
- /* @__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" }) })
11222
- ] }),
11223
- fields.map((field, index) => {
11224
- const isDisabled = field.disabled || false;
11225
- let placeholder = "-";
11226
- if (typeof field.value === "object") {
11227
- placeholder = "{ ... }";
11228
- }
11229
- if (Array.isArray(field.value)) {
11230
- placeholder = "[ ... ]";
11231
- }
11232
- return /* @__PURE__ */ jsx(
11233
- ConditionalTooltip,
11234
- {
11235
- showTooltip: isDisabled,
11236
- content: "This row is disabled because it contains non-primitive data.",
11237
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
11238
- /* @__PURE__ */ jsxs(
11239
- "div",
11240
- {
11241
- className: clx("grid grid-cols-2 divide-x", {
11242
- "overflow-hidden rounded-b-lg": index === fields.length - 1
11243
- }),
11244
- children: [
11245
- /* @__PURE__ */ jsx(
11246
- Form$2.Field,
11247
- {
11248
- control: form.control,
11249
- name: `metadata.${index}.key`,
11250
- render: ({ field: field2 }) => {
11251
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11252
- GridInput,
11253
- {
11254
- "aria-labelledby": METADATA_KEY_LABEL_ID,
11255
- ...field2,
11256
- disabled: isDisabled,
11257
- placeholder: "Key"
11258
- }
11259
- ) }) });
11260
- }
11261
- }
11262
- ),
11263
- /* @__PURE__ */ jsx(
11264
- Form$2.Field,
11265
- {
11266
- control: form.control,
11267
- name: `metadata.${index}.value`,
11268
- render: ({ field: { value, ...field2 } }) => {
11269
- return /* @__PURE__ */ jsx(Form$2.Item, { children: /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(
11270
- GridInput,
11271
- {
11272
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
11273
- ...field2,
11274
- value: isDisabled ? placeholder : value,
11275
- disabled: isDisabled,
11276
- placeholder: "Value"
11277
- }
11278
- ) }) });
11279
- }
11280
- }
11281
- )
11282
- ]
11283
- }
11284
- ),
11285
- /* @__PURE__ */ jsxs(DropdownMenu, { children: [
11286
- /* @__PURE__ */ jsx(
11287
- DropdownMenu.Trigger,
11288
- {
11289
- className: clx(
11290
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
11291
- {
11292
- hidden: isDisabled
11293
- }
11294
- ),
11295
- disabled: isDisabled,
11296
- asChild: true,
11297
- children: /* @__PURE__ */ jsx(IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsx(EllipsisVertical, {}) })
11298
- }
11299
- ),
11300
- /* @__PURE__ */ jsxs(DropdownMenu.Content, { children: [
11301
- /* @__PURE__ */ jsxs(
11302
- DropdownMenu.Item,
11303
- {
11304
- className: "gap-x-2",
11305
- onClick: () => insertRow(index, "above"),
11306
- children: [
11307
- /* @__PURE__ */ jsx(ArrowUpMini, { className: "text-ui-fg-subtle" }),
11308
- "Insert row above"
11309
- ]
11310
- }
11311
- ),
11312
- /* @__PURE__ */ jsxs(
11313
- DropdownMenu.Item,
11314
- {
11315
- className: "gap-x-2",
11316
- onClick: () => insertRow(index, "below"),
11317
- children: [
11318
- /* @__PURE__ */ jsx(ArrowDownMini, { className: "text-ui-fg-subtle" }),
11319
- "Insert row below"
11320
- ]
11321
- }
11322
- ),
11323
- /* @__PURE__ */ jsx(DropdownMenu.Separator, {}),
11324
- /* @__PURE__ */ jsxs(
11325
- DropdownMenu.Item,
11326
- {
11327
- className: "gap-x-2",
11328
- onClick: () => deleteRow(index),
11329
- children: [
11330
- /* @__PURE__ */ jsx(Trash, { className: "text-ui-fg-subtle" }),
11331
- "Delete row"
11332
- ]
11333
- }
11334
- )
11335
- ] })
11336
- ] })
11337
- ] })
11338
- },
11339
- field.id
11340
- );
11341
- })
11342
- ] }),
11343
- 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." })
11344
- ] }),
11345
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11346
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
11347
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11348
- ] }) })
11349
- ]
11031
+ });
11032
+ const add = async (value) => {
11033
+ if (!value) {
11034
+ return;
11350
11035
  }
11351
- ) });
11352
- };
11353
- const GridInput = forwardRef(({ className, ...props }, ref) => {
11354
- return /* @__PURE__ */ jsx(
11355
- "input",
11356
- {
11357
- ref,
11358
- ...props,
11359
- autoComplete: "off",
11360
- className: clx(
11361
- "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",
11362
- className
11363
- )
11036
+ addPromotions(
11037
+ {
11038
+ promo_codes: [value]
11039
+ },
11040
+ {
11041
+ onError: (e) => {
11042
+ toast.error(e.message);
11043
+ comboboxData.onSearchValueChange("");
11044
+ setComboboxValue("");
11045
+ },
11046
+ onSuccess: () => {
11047
+ comboboxData.onSearchValueChange("");
11048
+ setComboboxValue("");
11049
+ }
11050
+ }
11051
+ );
11052
+ };
11053
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11054
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11055
+ const onSubmit = async () => {
11056
+ setIsSubmitting(true);
11057
+ let requestSucceeded = false;
11058
+ await requestOrderEdit(void 0, {
11059
+ onError: (e) => {
11060
+ toast.error(e.message);
11061
+ },
11062
+ onSuccess: () => {
11063
+ requestSucceeded = true;
11064
+ }
11065
+ });
11066
+ if (!requestSucceeded) {
11067
+ setIsSubmitting(false);
11068
+ return;
11364
11069
  }
11365
- );
11366
- });
11367
- GridInput.displayName = "MetadataForm.GridInput";
11368
- const PlaceholderInner = () => {
11369
- return /* @__PURE__ */ jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
11370
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsx(Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
11371
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
11372
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" }),
11373
- /* @__PURE__ */ jsx(Skeleton, { className: "h-7 w-12 rounded-md" })
11070
+ await confirmOrderEdit(void 0, {
11071
+ onError: (e) => {
11072
+ toast.error(e.message);
11073
+ },
11074
+ onSuccess: () => {
11075
+ handleSuccess();
11076
+ },
11077
+ onSettled: () => {
11078
+ setIsSubmitting(false);
11079
+ }
11080
+ });
11081
+ };
11082
+ if (isError) {
11083
+ throw error;
11084
+ }
11085
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11086
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11087
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11088
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11089
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11090
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11091
+ ] }),
11092
+ /* @__PURE__ */ jsx(
11093
+ Combobox,
11094
+ {
11095
+ id: "promotion-combobox",
11096
+ "aria-describedby": "promotion-combobox-hint",
11097
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
11098
+ fetchNextPage: comboboxData.fetchNextPage,
11099
+ options: comboboxData.options,
11100
+ onSearchValueChange: comboboxData.onSearchValueChange,
11101
+ searchValue: comboboxData.searchValue,
11102
+ disabled: comboboxData.disabled || isAddingPromotions,
11103
+ onChange: add,
11104
+ value: comboboxValue
11105
+ }
11106
+ )
11107
+ ] }),
11108
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11109
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11110
+ PromotionItem,
11111
+ {
11112
+ promotion,
11113
+ orderId: preview.id,
11114
+ isLoading: isPending
11115
+ },
11116
+ promotion.id
11117
+ )) })
11118
+ ] }) }),
11119
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11120
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11121
+ /* @__PURE__ */ jsx(
11122
+ Button,
11123
+ {
11124
+ size: "small",
11125
+ type: "submit",
11126
+ isLoading: isSubmitting || isAddingPromotions,
11127
+ children: "Save"
11128
+ }
11129
+ )
11374
11130
  ] }) })
11375
11131
  ] });
11376
11132
  };
11377
- const EDITABLE_TYPES = ["string", "number", "boolean"];
11378
- function getDefaultValues(metadata) {
11379
- if (!metadata || !Object.keys(metadata).length) {
11380
- return [
11133
+ const PromotionItem = ({
11134
+ promotion,
11135
+ orderId,
11136
+ isLoading
11137
+ }) => {
11138
+ var _a;
11139
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11140
+ const onRemove = async () => {
11141
+ removePromotions(
11381
11142
  {
11382
- key: "",
11383
- value: "",
11384
- disabled: false
11143
+ promo_codes: [promotion.code]
11144
+ },
11145
+ {
11146
+ onError: (e) => {
11147
+ toast.error(e.message);
11148
+ }
11385
11149
  }
11386
- ];
11150
+ );
11151
+ };
11152
+ const displayValue = getDisplayValue(promotion);
11153
+ return /* @__PURE__ */ jsxs(
11154
+ "div",
11155
+ {
11156
+ className: clx(
11157
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11158
+ {
11159
+ "animate-pulse": isLoading
11160
+ }
11161
+ ),
11162
+ children: [
11163
+ /* @__PURE__ */ jsxs("div", { children: [
11164
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11165
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11166
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11167
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11168
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11169
+ ] }),
11170
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11171
+ ] })
11172
+ ] }),
11173
+ /* @__PURE__ */ jsx(
11174
+ IconButton,
11175
+ {
11176
+ size: "small",
11177
+ type: "button",
11178
+ variant: "transparent",
11179
+ onClick: onRemove,
11180
+ isLoading: isPending || isLoading,
11181
+ children: /* @__PURE__ */ jsx(XMark, {})
11182
+ }
11183
+ )
11184
+ ]
11185
+ },
11186
+ promotion.id
11187
+ );
11188
+ };
11189
+ function getDisplayValue(promotion) {
11190
+ var _a, _b, _c, _d;
11191
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11192
+ if (!value) {
11193
+ return null;
11387
11194
  }
11388
- return Object.entries(metadata).map(([key, value]) => {
11389
- if (!EDITABLE_TYPES.includes(typeof value)) {
11390
- return {
11391
- key,
11392
- value,
11393
- disabled: true
11394
- };
11395
- }
11396
- let stringValue = value;
11397
- if (typeof value !== "string") {
11398
- stringValue = JSON.stringify(value);
11195
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11196
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11197
+ if (!currency) {
11198
+ return null;
11399
11199
  }
11400
- return {
11401
- key,
11402
- value: stringValue,
11403
- original_key: key
11404
- };
11405
- });
11200
+ return getLocaleAmount(value, currency);
11201
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11202
+ return formatPercentage(value);
11203
+ }
11204
+ return null;
11406
11205
  }
11407
- function parseValues(values) {
11408
- const metadata = values.metadata;
11409
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11410
- if (isEmpty) {
11411
- return null;
11206
+ const formatter = new Intl.NumberFormat([], {
11207
+ style: "percent",
11208
+ minimumFractionDigits: 2
11209
+ });
11210
+ const formatPercentage = (value, isPercentageValue = false) => {
11211
+ let val = value || 0;
11212
+ if (!isPercentageValue) {
11213
+ val = val / 100;
11412
11214
  }
11413
- const update = {};
11414
- metadata.forEach((field) => {
11415
- let key = field.key;
11416
- let value = field.value;
11417
- const disabled = field.disabled;
11418
- if (!key || !value) {
11419
- return;
11420
- }
11421
- if (disabled) {
11422
- update[key] = value;
11423
- return;
11424
- }
11425
- key = key.trim();
11426
- value = value.trim();
11427
- if (value === "true") {
11428
- update[key] = true;
11429
- } else if (value === "false") {
11430
- update[key] = false;
11431
- } else {
11432
- const parsedNumber = parseFloat(value);
11433
- if (!isNaN(parsedNumber)) {
11434
- update[key] = parsedNumber;
11435
- } else {
11436
- update[key] = value;
11215
+ return formatter.format(val);
11216
+ };
11217
+ function getPromotionIds(items, shippingMethods) {
11218
+ const promotionIds = /* @__PURE__ */ new Set();
11219
+ for (const item of items) {
11220
+ if (item.adjustments) {
11221
+ for (const adjustment of item.adjustments) {
11222
+ if (adjustment.promotion_id) {
11223
+ promotionIds.add(adjustment.promotion_id);
11224
+ }
11437
11225
  }
11438
11226
  }
11439
- });
11440
- return update;
11441
- }
11442
- function getHasUneditableRows(metadata) {
11443
- if (!metadata) {
11444
- return false;
11445
11227
  }
11446
- return Object.values(metadata).some(
11447
- (value) => !EDITABLE_TYPES.includes(typeof value)
11448
- );
11228
+ for (const shippingMethod of shippingMethods) {
11229
+ if (shippingMethod.adjustments) {
11230
+ for (const adjustment of shippingMethod.adjustments) {
11231
+ if (adjustment.promotion_id) {
11232
+ promotionIds.add(adjustment.promotion_id);
11233
+ }
11234
+ }
11235
+ }
11236
+ }
11237
+ return Array.from(promotionIds);
11449
11238
  }
11239
+ const CustomItems = () => {
11240
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11241
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
11242
+ /* @__PURE__ */ jsx(CustomItemsForm, {})
11243
+ ] });
11244
+ };
11245
+ const CustomItemsForm = () => {
11246
+ const form = useForm({
11247
+ resolver: zodResolver(schema$4)
11248
+ });
11249
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
11250
+ /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
11251
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11252
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11253
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", children: "Save" })
11254
+ ] }) })
11255
+ ] }) });
11256
+ };
11257
+ const schema$4 = objectType({
11258
+ email: stringType().email()
11259
+ });
11450
11260
  const SalesChannel = () => {
11451
11261
  const { id } = useParams();
11452
11262
  const { draft_order, isPending, isError, error } = useDraftOrder(
@@ -11475,7 +11285,7 @@ const SalesChannelForm = ({ order }) => {
11475
11285
  defaultValues: {
11476
11286
  sales_channel_id: order.sales_channel_id || ""
11477
11287
  },
11478
- resolver: zodResolver(schema$2)
11288
+ resolver: zodResolver(schema$3)
11479
11289
  });
11480
11290
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11481
11291
  const { handleSuccess } = useRouteModal();
@@ -11550,7 +11360,7 @@ const SalesChannelField = ({ control, order }) => {
11550
11360
  }
11551
11361
  );
11552
11362
  };
11553
- const schema$2 = objectType({
11363
+ const schema$3 = objectType({
11554
11364
  sales_channel_id: stringType().min(1)
11555
11365
  });
11556
11366
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
@@ -12392,7 +12202,7 @@ const ShippingAddressForm = ({ order }) => {
12392
12202
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12393
12203
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12394
12204
  },
12395
- resolver: zodResolver(schema$1)
12205
+ resolver: zodResolver(schema$2)
12396
12206
  });
12397
12207
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12398
12208
  const { handleSuccess } = useRouteModal();
@@ -12562,7 +12372,7 @@ const ShippingAddressForm = ({ order }) => {
12562
12372
  }
12563
12373
  ) });
12564
12374
  };
12565
- const schema$1 = addressSchema;
12375
+ const schema$2 = addressSchema;
12566
12376
  const TransferOwnership = () => {
12567
12377
  const { id } = useParams();
12568
12378
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12586,7 +12396,7 @@ const TransferOwnershipForm = ({ order }) => {
12586
12396
  defaultValues: {
12587
12397
  customer_id: order.customer_id || ""
12588
12398
  },
12589
- resolver: zodResolver(schema)
12399
+ resolver: zodResolver(schema$1)
12590
12400
  });
12591
12401
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12592
12402
  const { handleSuccess } = useRouteModal();
@@ -13036,9 +12846,199 @@ const Illustration = () => {
13036
12846
  }
13037
12847
  );
13038
12848
  };
13039
- const schema = objectType({
12849
+ const schema$1 = objectType({
13040
12850
  customer_id: stringType().min(1)
13041
12851
  });
12852
+ const BillingAddress = () => {
12853
+ const { id } = useParams();
12854
+ const { order, isPending, isError, error } = useOrder(id, {
12855
+ fields: "+billing_address"
12856
+ });
12857
+ if (isError) {
12858
+ throw error;
12859
+ }
12860
+ const isReady = !isPending && !!order;
12861
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12862
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12863
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Billing Address" }) }),
12864
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
12865
+ ] }),
12866
+ isReady && /* @__PURE__ */ jsx(BillingAddressForm, { order })
12867
+ ] });
12868
+ };
12869
+ const BillingAddressForm = ({ order }) => {
12870
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12871
+ const form = useForm({
12872
+ defaultValues: {
12873
+ first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
12874
+ last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
12875
+ company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
12876
+ address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
12877
+ address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
12878
+ city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
12879
+ province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
12880
+ country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
12881
+ postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
12882
+ phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
12883
+ },
12884
+ resolver: zodResolver(schema)
12885
+ });
12886
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12887
+ const { handleSuccess } = useRouteModal();
12888
+ const onSubmit = form.handleSubmit(async (data) => {
12889
+ await mutateAsync(
12890
+ { billing_address: data },
12891
+ {
12892
+ onSuccess: () => {
12893
+ handleSuccess();
12894
+ },
12895
+ onError: (error) => {
12896
+ toast.error(error.message);
12897
+ }
12898
+ }
12899
+ );
12900
+ });
12901
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12902
+ KeyboundForm,
12903
+ {
12904
+ className: "flex flex-1 flex-col overflow-hidden",
12905
+ onSubmit,
12906
+ children: [
12907
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
12908
+ /* @__PURE__ */ jsx(
12909
+ Form$2.Field,
12910
+ {
12911
+ control: form.control,
12912
+ name: "country_code",
12913
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12914
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12915
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12916
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12917
+ ] })
12918
+ }
12919
+ ),
12920
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12921
+ /* @__PURE__ */ jsx(
12922
+ Form$2.Field,
12923
+ {
12924
+ control: form.control,
12925
+ name: "first_name",
12926
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12927
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12928
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12929
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12930
+ ] })
12931
+ }
12932
+ ),
12933
+ /* @__PURE__ */ jsx(
12934
+ Form$2.Field,
12935
+ {
12936
+ control: form.control,
12937
+ name: "last_name",
12938
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12939
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12940
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12941
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12942
+ ] })
12943
+ }
12944
+ )
12945
+ ] }),
12946
+ /* @__PURE__ */ jsx(
12947
+ Form$2.Field,
12948
+ {
12949
+ control: form.control,
12950
+ name: "company",
12951
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12952
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12953
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12954
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12955
+ ] })
12956
+ }
12957
+ ),
12958
+ /* @__PURE__ */ jsx(
12959
+ Form$2.Field,
12960
+ {
12961
+ control: form.control,
12962
+ name: "address_1",
12963
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12964
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12965
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12966
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12967
+ ] })
12968
+ }
12969
+ ),
12970
+ /* @__PURE__ */ jsx(
12971
+ Form$2.Field,
12972
+ {
12973
+ control: form.control,
12974
+ name: "address_2",
12975
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12976
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12977
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12978
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12979
+ ] })
12980
+ }
12981
+ ),
12982
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12983
+ /* @__PURE__ */ jsx(
12984
+ Form$2.Field,
12985
+ {
12986
+ control: form.control,
12987
+ name: "postal_code",
12988
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12989
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12990
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12991
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12992
+ ] })
12993
+ }
12994
+ ),
12995
+ /* @__PURE__ */ jsx(
12996
+ Form$2.Field,
12997
+ {
12998
+ control: form.control,
12999
+ name: "city",
13000
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13001
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
13002
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13003
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13004
+ ] })
13005
+ }
13006
+ )
13007
+ ] }),
13008
+ /* @__PURE__ */ jsx(
13009
+ Form$2.Field,
13010
+ {
13011
+ control: form.control,
13012
+ name: "province",
13013
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13014
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
13015
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13016
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13017
+ ] })
13018
+ }
13019
+ ),
13020
+ /* @__PURE__ */ jsx(
13021
+ Form$2.Field,
13022
+ {
13023
+ control: form.control,
13024
+ name: "phone",
13025
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13026
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
13027
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13028
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13029
+ ] })
13030
+ }
13031
+ )
13032
+ ] }) }),
13033
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13034
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13035
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13036
+ ] }) })
13037
+ ]
13038
+ }
13039
+ ) });
13040
+ };
13041
+ const schema = addressSchema;
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
13044
13044
  routes: [
@@ -13059,18 +13059,14 @@ const routeModule = {
13059
13059
  handle,
13060
13060
  loader,
13061
13061
  children: [
13062
- {
13063
- Component: BillingAddress,
13064
- path: "/draft-orders/:id/billing-address"
13065
- },
13066
- {
13067
- Component: CustomItems,
13068
- path: "/draft-orders/:id/custom-items"
13069
- },
13070
13062
  {
13071
13063
  Component: Email,
13072
13064
  path: "/draft-orders/:id/email"
13073
13065
  },
13066
+ {
13067
+ Component: Metadata,
13068
+ path: "/draft-orders/:id/metadata"
13069
+ },
13074
13070
  {
13075
13071
  Component: Items,
13076
13072
  path: "/draft-orders/:id/items"
@@ -13080,8 +13076,8 @@ const routeModule = {
13080
13076
  path: "/draft-orders/:id/promotions"
13081
13077
  },
13082
13078
  {
13083
- Component: Metadata,
13084
- path: "/draft-orders/:id/metadata"
13079
+ Component: CustomItems,
13080
+ path: "/draft-orders/:id/custom-items"
13085
13081
  },
13086
13082
  {
13087
13083
  Component: SalesChannel,
@@ -13098,6 +13094,10 @@ const routeModule = {
13098
13094
  {
13099
13095
  Component: TransferOwnership,
13100
13096
  path: "/draft-orders/:id/transfer-ownership"
13097
+ },
13098
+ {
13099
+ Component: BillingAddress,
13100
+ path: "/draft-orders/:id/billing-address"
13101
13101
  }
13102
13102
  ]
13103
13103
  }