@medusajs/draft-order 2.10.1-snapshot-20250828200637 → 2.10.1

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