@medusajs/draft-order 2.10.2-preview-20250902210158 → 2.10.2-preview-20250903030934

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