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

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