@medusajs/draft-order 2.10.4-snapshot-20251003141424 → 2.10.4-snapshot-20251003221002

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