@medusajs/draft-order 2.10.4-snapshot-20250918173105 → 2.10.4-snapshot-20250922090257

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