@medusajs/draft-order 2.10.4-preview-20250926120205 → 2.10.4-preview-20250926180153

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