@medusajs/draft-order 2.11.2-snapshot-20251026124930 → 2.11.2-snapshot-20251029122124

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