@medusajs/draft-order 2.10.1 → 2.10.2-preview-20250829180153

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,196 +9560,6 @@ 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;
9753
9563
  const CustomItems = () => {
9754
9564
  return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9755
9565
  /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
@@ -9758,7 +9568,7 @@ const CustomItems = () => {
9758
9568
  };
9759
9569
  const CustomItemsForm = () => {
9760
9570
  const form = reactHookForm.useForm({
9761
- resolver: zod.zodResolver(schema$4)
9571
+ resolver: zod.zodResolver(schema$5)
9762
9572
  });
9763
9573
  return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9764
9574
  /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
@@ -9768,7 +9578,7 @@ const CustomItemsForm = () => {
9768
9578
  ] }) })
9769
9579
  ] }) });
9770
9580
  };
9771
- const schema$4 = objectType({
9581
+ const schema$5 = objectType({
9772
9582
  email: stringType().email()
9773
9583
  });
9774
9584
  const Email = () => {
@@ -9793,7 +9603,7 @@ const EmailForm = ({ order }) => {
9793
9603
  defaultValues: {
9794
9604
  email: order.email ?? ""
9795
9605
  },
9796
- resolver: zod.zodResolver(schema$3)
9606
+ resolver: zod.zodResolver(schema$4)
9797
9607
  });
9798
9608
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9799
9609
  const { handleSuccess } = useRouteModal();
@@ -9836,1716 +9646,1610 @@ const EmailForm = ({ order }) => {
9836
9646
  }
9837
9647
  ) });
9838
9648
  };
9839
- const schema$3 = objectType({
9649
+ const schema$4 = objectType({
9840
9650
  email: stringType().email()
9841
9651
  });
9842
- const InlineTip = React.forwardRef(
9843
- ({ variant = "tip", label, className, children, ...props }, ref) => {
9844
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
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
+ };
9845
9682
  return /* @__PURE__ */ jsxRuntime.jsxs(
9846
9683
  "div",
9847
9684
  {
9848
- ref,
9849
9685
  className: ui.clx(
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",
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
+ },
9851
9692
  className
9852
9693
  ),
9853
- ...props,
9854
9694
  children: [
9855
9695
  /* @__PURE__ */ jsxRuntime.jsx(
9856
- "div",
9696
+ "input",
9857
9697
  {
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
- })
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
9862
9711
  }
9863
9712
  ),
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
- ] })
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
+ ]
9733
+ }
9734
+ ),
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
+ )
9872
9757
  ]
9873
9758
  }
9874
9759
  );
9875
9760
  }
9876
9761
  );
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 = () => {
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 = () => {
9887
9828
  const { id } = reactRouterDom.useParams();
9888
- const { order, isPending, isError, error } = useOrder(id, {
9889
- fields: "metadata"
9829
+ const {
9830
+ order: preview,
9831
+ isPending: isPreviewPending,
9832
+ isError: isPreviewError,
9833
+ error: previewError
9834
+ } = useOrderPreview(id, void 0, {
9835
+ placeholderData: reactQuery.keepPreviousData
9890
9836
  });
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 });
9891
9848
  if (isError) {
9892
9849
  throw error;
9893
9850
  }
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
- ] });
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
+ ] }) });
9902
9859
  };
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 }) => {
9860
+ const ItemsForm = ({ preview, currencyCode }) => {
9861
+ var _a;
9862
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
9863
+ const [modalContent, setModalContent] = React.useState(
9864
+ null
9865
+ );
9906
9866
  const { handleSuccess } = useRouteModal();
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
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}`);
9920
9882
  },
9921
- {
9922
- onSuccess: () => {
9923
- ui.toast.success("Metadata updated");
9924
- handleSuccess();
9925
- },
9926
- onError: (error) => {
9927
- ui.toast.error(error.message);
9928
- }
9883
+ onSuccess: () => {
9884
+ requestSucceeded = true;
9929
9885
  }
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
- });
9886
+ });
9887
+ if (!requestSucceeded) {
9888
+ setIsSubmitting(false);
9889
+ return;
9944
9890
  }
9945
- }
9946
- function insertRow(index, position) {
9947
- insert(index + (position === "above" ? 0 : 1), {
9948
- key: "",
9949
- value: "",
9950
- disabled: false
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
+ }
9951
9901
  });
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" }) })
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." }) })
9964
9936
  ] }),
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: [
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: [
10028
9957
  /* @__PURE__ */ jsxRuntime.jsx(
10029
- ui.DropdownMenu.Trigger,
9958
+ StackedModalTrigger$1,
10030
9959
  {
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, {}) })
9960
+ type: "add-items",
9961
+ setModalContent
10040
9962
  }
10041
9963
  ),
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
- ] })
9964
+ /* @__PURE__ */ jsxRuntime.jsx(
9965
+ StackedModalTrigger$1,
9966
+ {
9967
+ type: "add-custom-item",
9968
+ setModalContent
9969
+ }
9970
+ )
10078
9971
  ] })
10079
9972
  ] })
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
- ) });
10094
- };
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
- );
10108
- });
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
- };
10137
- }
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) {
10161
- return;
10162
- }
10163
- if (disabled) {
10164
- update[key] = value;
10165
- return;
10166
- }
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;
10179
- }
10180
- }
10181
- });
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;
10225
- }
10226
- });
10227
- return res;
10228
- }, [preview, cancelOrderEdit]);
10229
- return { onCancel };
10230
- };
10231
- let IS_REQUEST_RUNNING = false;
10232
- const useInitiateOrderEdit = ({
10233
- preview
10234
- }) => {
10235
- const navigate = reactRouterDom.useNavigate();
10236
- const { mutateAsync } = useDraftOrderBeginEdit(preview == null ? void 0 : preview.id);
10237
- React.useEffect(() => {
10238
- async function run() {
10239
- if (IS_REQUEST_RUNNING || !preview) {
10240
- return;
10241
- }
10242
- if (preview.order_change) {
10243
- return;
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
+ ]
10244
10030
  }
10245
- IS_REQUEST_RUNNING = true;
10246
- await mutateAsync(void 0, {
10247
- onError: (e) => {
10248
- ui.toast.error(e.message);
10249
- navigate(`/draft-orders/${preview.id}`, { replace: true });
10250
- return;
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"
10251
10042
  }
10252
- });
10253
- IS_REQUEST_RUNNING = false;
10254
- }
10255
- run();
10256
- }, [preview, navigate, mutateAsync]);
10043
+ )
10044
+ ] }) })
10045
+ ] });
10257
10046
  };
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;
10047
+ const Item = ({ item, preview, currencyCode }) => {
10048
+ if (item.variant_id) {
10049
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10269
10050
  }
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
- ] });
10051
+ return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10275
10052
  };
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(
10284
- {
10285
- code: promoCodes
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
10286
10059
  },
10287
- {
10288
- enabled: !!promoCodes.length
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;
10289
10073
  }
10290
- );
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
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
+ }
10298
10088
  }
10299
- });
10300
- },
10301
- getOptions: (data) => {
10302
- return data.promotions.map((promotion) => ({
10303
- label: promotion.code,
10304
- value: promotion.code
10305
- }));
10306
- }
10307
- });
10308
- const add = async (value) => {
10309
- if (!value) {
10089
+ );
10310
10090
  return;
10311
10091
  }
10312
- addPromotions(
10092
+ await updateActionItem(
10313
10093
  {
10314
- promo_codes: [value]
10094
+ action_id: actionId,
10095
+ quantity: data.quantity,
10096
+ unit_price: convertNumber(data.unit_price)
10315
10097
  },
10316
10098
  {
10317
- onError: (e) => {
10318
- ui.toast.error(e.message);
10319
- comboboxData.onSearchValueChange("");
10320
- setComboboxValue("");
10321
- },
10322
10099
  onSuccess: () => {
10323
- comboboxData.onSearchValueChange("");
10324
- setComboboxValue("");
10100
+ setEditing(false);
10101
+ },
10102
+ onError: (e) => {
10103
+ ui.toast.error(e.message);
10325
10104
  }
10326
10105
  }
10327
10106
  );
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." })
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
+ )
10367
10133
  ] }),
10368
10134
  /* @__PURE__ */ jsxRuntime.jsx(
10369
- Combobox,
10135
+ ui.Text,
10370
10136
  {
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
10137
+ size: "small",
10138
+ leading: "compact",
10139
+ className: "text-ui-fg-subtle",
10140
+ children: item.variant_sku
10381
10141
  }
10382
10142
  )
10383
- ] }),
10384
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10385
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
10386
- PromotionItem,
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
+ ] }) }) });
10186
+ };
10187
+ const variantItemSchema = objectType({
10188
+ quantity: numberType(),
10189
+ unit_price: unionType([numberType(), stringType()])
10190
+ });
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;
10221
+ }
10222
+ if (!actionId) {
10223
+ await updateOriginalItem(
10387
10224
  {
10388
- promotion,
10389
- orderId: preview.id,
10390
- isLoading: isPending
10225
+ item_id: item.id,
10226
+ quantity: data.quantity,
10227
+ unit_price: convertNumber(data.unit_price)
10391
10228
  },
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" }) }),
10397
- /* @__PURE__ */ jsxRuntime.jsx(
10398
- ui.Button,
10399
10229
  {
10400
- size: "small",
10401
- type: "submit",
10402
- isLoading: isSubmitting || isAddingPromotions,
10403
- children: "Save"
10230
+ onSuccess: () => {
10231
+ setEditing(false);
10232
+ },
10233
+ onError: (e) => {
10234
+ ui.toast.error(e.message);
10235
+ }
10404
10236
  }
10405
- )
10406
- ] }) })
10407
- ] });
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(
10237
+ );
10238
+ return;
10239
+ }
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
+ });
10249
+ return;
10250
+ }
10251
+ await updateActionItem(
10418
10252
  {
10419
- promo_codes: [promotion.code]
10253
+ action_id: actionId,
10254
+ quantity: data.quantity,
10255
+ unit_price: convertNumber(data.unit_price)
10420
10256
  },
10421
10257
  {
10258
+ onSuccess: () => {
10259
+ setEditing(false);
10260
+ },
10422
10261
  onError: (e) => {
10423
10262
  ui.toast.error(e.message);
10424
10263
  }
10425
10264
  }
10426
10265
  );
10427
- };
10428
- const displayValue = getDisplayValue(promotion);
10429
- return /* @__PURE__ */ jsxRuntime.jsxs(
10430
- "div",
10431
- {
10432
- className: ui.clx(
10433
- "px-3 py-2 rounded-lg bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between",
10266
+ });
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,
10434
10271
  {
10435
- "animate-pulse": isLoading
10272
+ thumbnail: item.thumbnail,
10273
+ alt: item.title ?? void 0
10436
10274
  }
10437
10275
  ),
10438
- children: [
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: "·" })
10445
- ] }),
10446
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
10447
- ] })
10448
- ] }),
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, {})
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 }) }) });
10458
10283
  }
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);
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 }) }) });
10500
10294
  }
10501
10295
  }
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);
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
+ ) }) });
10509
10312
  }
10510
10313
  }
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,
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, {})
10325
+ }
10326
+ )
10327
+ ] }) }) });
10328
+ };
10329
+ const StackedModalTrigger$1 = ({
10330
+ type,
10331
+ setModalContent
10332
+ }) => {
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
+ );
10350
+ 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(
10519
10365
  {
10520
- fields: "+sales_channel_id"
10366
+ q,
10367
+ order,
10368
+ offset: offset ? parseInt(offset) : void 0,
10369
+ limit: LIMIT
10521
10370
  },
10522
10371
  {
10523
- enabled: !!id
10372
+ placeholderData: reactQuery.keepPreviousData
10524
10373
  }
10525
10374
  );
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) => {
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
+ );
10548
10381
  await mutateAsync(
10549
10382
  {
10550
- sales_channel_id: data.sales_channel_id
10383
+ items: ids.map((id) => ({
10384
+ variant_id: id,
10385
+ quantity: 1
10386
+ }))
10551
10387
  },
10552
10388
  {
10553
10389
  onSuccess: () => {
10554
- ui.toast.success("Sales channel updated");
10555
- handleSuccess();
10390
+ setRowSelection({});
10391
+ setIsOpen(STACKED_MODAL_ID, false);
10556
10392
  },
10557
- onError: (error) => {
10558
- ui.toast.error(error.message);
10393
+ onError: (e) => {
10394
+ ui.toast.error(e.message);
10559
10395
  }
10560
10396
  }
10561
10397
  );
10562
- });
10563
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10564
- KeyboundForm,
10398
+ };
10399
+ if (isError) {
10400
+ throw error;
10401
+ }
10402
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10403
+ StackedFocusModal.Content,
10565
10404
  {
10566
- className: "flex flex-1 flex-col overflow-hidden",
10567
- onSubmit,
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
+ },
10568
10414
  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" }) }),
10572
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
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" })
10573
10442
  ] }) })
10574
10443
  ]
10575
10444
  }
10576
- ) });
10445
+ );
10577
10446
  };
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
- });
10592
- return /* @__PURE__ */ jsxRuntime.jsx(
10593
- Form$2.Field,
10594
- {
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,
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,
10602
10485
  {
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
10486
+ content: getFullDate({ date: getValue(), includeTime: true }),
10487
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10610
10488
  }
10611
- ) }),
10612
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10613
- ] });
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: ""
10521
+ },
10522
+ resolver: zod.zodResolver(customItemSchema)
10523
+ });
10524
+ const onSubmit = form.handleSubmit(async (data) => {
10525
+ await addItems(
10526
+ {
10527
+ items: [
10528
+ {
10529
+ title: data.title,
10530
+ quantity: data.quantity,
10531
+ unit_price: convertNumber(data.unit_price)
10532
+ }
10533
+ ]
10534
+ },
10535
+ {
10536
+ onSuccess: () => {
10537
+ setIsOpen(STACKED_MODAL_ID, false);
10538
+ },
10539
+ onError: (e) => {
10540
+ ui.toast.error(e.message);
10541
+ }
10614
10542
  }
10615
- }
10616
- );
10543
+ );
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." }) })
10551
+ ] }),
10552
+ /* @__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,
10573
+ {
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" }),
10597
+ /* @__PURE__ */ jsxRuntime.jsx(
10598
+ Form$2.Field,
10599
+ {
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
+ ] }) })
10612
+ }
10613
+ )
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
+ ] }) })
10619
+ ] }) }) });
10617
10620
  };
10618
- const schema$2 = objectType({
10619
- sales_channel_id: stringType().min(1)
10621
+ const customItemSchema = objectType({
10622
+ title: stringType().min(1),
10623
+ quantity: numberType(),
10624
+ unit_price: unionType([numberType(), stringType()])
10620
10625
  });
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);
10637
- }
10638
- };
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
- };
10626
+ const InlineTip = React.forwardRef(
10627
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
10628
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10651
10629
  return /* @__PURE__ */ jsxRuntime.jsxs(
10652
10630
  "div",
10653
10631
  {
10632
+ ref,
10654
10633
  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
- },
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",
10661
10635
  className
10662
10636
  ),
10637
+ ...props,
10663
10638
  children: [
10664
10639
  /* @__PURE__ */ jsxRuntime.jsx(
10665
- "input",
10640
+ "div",
10666
10641
  {
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
- ]
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
+ })
10702
10646
  }
10703
10647
  ),
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
- )
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
+ ] })
10726
10656
  ]
10727
10657
  }
10728
10658
  );
10729
10659
  }
10730
10660
  );
10731
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
10732
- const productVariantsQueryKeys = {
10733
- list: (query2) => [
10734
- PRODUCT_VARIANTS_QUERY_KEY,
10735
- query2 ? query2 : void 0
10736
- ]
10737
- };
10738
- const useProductVariants = (query2, options) => {
10739
- const { data, ...rest } = reactQuery.useQuery({
10740
- queryKey: productVariantsQueryKeys.list(query2),
10741
- queryFn: async () => await sdk.admin.productVariant.list(query2),
10742
- ...options
10743
- });
10744
- return { ...data, ...rest };
10745
- };
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 = () => {
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 = () => {
10751
10671
  const { id } = reactRouterDom.useParams();
10752
- const {
10753
- order: preview,
10754
- isPending: isPreviewPending,
10755
- isError: isPreviewError,
10756
- error: previewError
10757
- } = useOrderPreview(id, void 0, {
10758
- placeholderData: reactQuery.keepPreviousData
10672
+ const { order, isPending, isError, error } = useOrder(id, {
10673
+ fields: "metadata"
10759
10674
  });
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
- );
10770
- const { onCancel } = useCancelOrderEdit({ preview });
10771
10675
  if (isError) {
10772
10676
  throw error;
10773
10677
  }
10774
- if (isPreviewError) {
10775
- throw previewError;
10776
- }
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
- ] }) });
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
+ ] });
10782
10686
  };
10783
- const ItemsForm = ({ preview, currencyCode }) => {
10784
- var _a;
10785
- const [isSubmitting, setIsSubmitting] = React.useState(false);
10786
- const [modalContent, setModalContent] = React.useState(
10787
- null
10788
- );
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 }) => {
10789
10690
  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}`);
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(
10702
+ {
10703
+ metadata: parsedData
10805
10704
  },
10806
- onSuccess: () => {
10807
- requestSucceeded = true;
10705
+ {
10706
+ onSuccess: () => {
10707
+ ui.toast.success("Metadata updated");
10708
+ handleSuccess();
10709
+ },
10710
+ onError: (error) => {
10711
+ ui.toast.error(error.message);
10712
+ }
10808
10713
  }
10809
- });
10810
- if (!requestSucceeded) {
10811
- setIsSubmitting(false);
10812
- return;
10714
+ );
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
+ });
10813
10728
  }
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
- }
10729
+ }
10730
+ function insertRow(index, position) {
10731
+ insert(index + (position === "above" ? 0 : 1), {
10732
+ key: "",
10733
+ value: "",
10734
+ disabled: false
10824
10735
  });
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." }) })
10736
+ }
10737
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10738
+ KeyboundForm,
10739
+ {
10740
+ onSubmit: handleSubmit,
10741
+ className: "flex flex-1 flex-col overflow-hidden",
10742
+ 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" }) })
10859
10748
  ] }),
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: [
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: [
10880
10812
  /* @__PURE__ */ jsxRuntime.jsx(
10881
- StackedModalTrigger$1,
10813
+ ui.DropdownMenu.Trigger,
10882
10814
  {
10883
- type: "add-items",
10884
- setModalContent
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, {}) })
10885
10824
  }
10886
10825
  ),
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
- }
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." })
10870
+ ] }),
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" }) }),
10873
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10874
+ ] }) })
10875
+ ]
10876
+ }
10877
+ ) });
10878
+ };
10879
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
10880
+ return /* @__PURE__ */ jsxRuntime.jsx(
10881
+ "input",
10882
+ {
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
10966
10889
  )
10890
+ }
10891
+ );
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" })
10967
10900
  ] }) })
10968
10901
  ] });
10969
10902
  };
10970
- const Item = ({ item, preview, currencyCode }) => {
10971
- if (item.variant_id) {
10972
- return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
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
10911
+ }
10912
+ ];
10973
10913
  }
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
10982
- },
10983
- resolver: zod.zodResolver(variantItemSchema)
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
+ };
10984
10931
  });
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);
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) {
10995
10945
  return;
10996
10946
  }
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
- }
11011
- }
11012
- );
10947
+ if (disabled) {
10948
+ update[key] = value;
11013
10949
  return;
11014
10950
  }
11015
- await updateActionItem(
11016
- {
11017
- action_id: actionId,
11018
- quantity: data.quantity,
11019
- unit_price: convertNumber(data.unit_price)
11020
- },
11021
- {
11022
- onSuccess: () => {
11023
- setEditing(false);
11024
- },
11025
- onError: (e) => {
11026
- ui.toast.error(e.message);
11027
- }
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;
11028
10963
  }
11029
- );
10964
+ }
11030
10965
  });
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
- )
11056
- ] }),
11057
- /* @__PURE__ */ jsxRuntime.jsx(
11058
- ui.Text,
11059
- {
11060
- size: "small",
11061
- leading: "compact",
11062
- className: "text-ui-fg-subtle",
11063
- children: item.variant_sku
11064
- }
11065
- )
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
- ] }) }) });
10966
+ return update;
10967
+ }
10968
+ function getHasUneditableRows(metadata) {
10969
+ if (!metadata) {
10970
+ return false;
10971
+ }
10972
+ return Object.values(metadata).some(
10973
+ (value) => !EDITABLE_TYPES.includes(typeof value)
10974
+ );
10975
+ }
10976
+ const PROMOTION_QUERY_KEY = "promotions";
10977
+ const promotionsQueryKeys = {
10978
+ list: (query2) => [
10979
+ PROMOTION_QUERY_KEY,
10980
+ query2 ? query2 : void 0
10981
+ ],
10982
+ detail: (id, query2) => [
10983
+ PROMOTION_QUERY_KEY,
10984
+ id,
10985
+ query2 ? query2 : void 0
10986
+ ]
10987
+ };
10988
+ const usePromotions = (query2, options) => {
10989
+ const { data, ...rest } = reactQuery.useQuery({
10990
+ queryKey: promotionsQueryKeys.list(query2),
10991
+ queryFn: async () => sdk.admin.promotion.list(query2),
10992
+ ...options
10993
+ });
10994
+ return { ...data, ...rest };
10995
+ };
10996
+ const Promotions = () => {
10997
+ const { id } = reactRouterDom.useParams();
10998
+ const {
10999
+ order: preview,
11000
+ isError: isPreviewError,
11001
+ error: previewError
11002
+ } = useOrderPreview(id, void 0);
11003
+ useInitiateOrderEdit({ preview });
11004
+ const { onCancel } = useCancelOrderEdit({ preview });
11005
+ if (isPreviewError) {
11006
+ throw previewError;
11007
+ }
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 })
11012
+ ] });
11109
11013
  };
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
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
11122
11024
  },
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(
11147
- {
11148
- item_id: item.id,
11149
- quantity: data.quantity,
11150
- unit_price: convertNumber(data.unit_price)
11151
- },
11152
- {
11153
- onSuccess: () => {
11154
- setEditing(false);
11155
- },
11156
- onError: (e) => {
11157
- ui.toast.error(e.message);
11158
- }
11159
- }
11160
- );
11161
- return;
11025
+ {
11026
+ enabled: !!promoCodes.length
11162
11027
  }
11163
- if (data.quantity === 0) {
11164
- await removeActionItem(actionId, {
11165
- onSuccess: () => {
11166
- setEditing(false);
11167
- },
11168
- onError: (e) => {
11169
- ui.toast.error(e.message);
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
11170
11036
  }
11171
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) {
11172
11048
  return;
11173
11049
  }
11174
- await updateActionItem(
11050
+ addPromotions(
11175
11051
  {
11176
- action_id: actionId,
11177
- quantity: data.quantity,
11178
- unit_price: convertNumber(data.unit_price)
11052
+ promo_codes: [value]
11179
11053
  },
11180
11054
  {
11181
- onSuccess: () => {
11182
- setEditing(false);
11183
- },
11184
11055
  onError: (e) => {
11185
11056
  ui.toast.error(e.message);
11057
+ comboboxData.onSearchValueChange("");
11058
+ setComboboxValue("");
11059
+ },
11060
+ onSuccess: () => {
11061
+ comboboxData.onSearchValueChange("");
11062
+ setComboboxValue("");
11186
11063
  }
11187
11064
  }
11188
11065
  );
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,
11194
- {
11195
- thumbnail: item.thumbnail,
11196
- alt: item.title ?? void 0
11197
- }
11198
- ),
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 }) }) });
11206
- }
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, {})
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;
11248
11078
  }
11249
- )
11250
- ] }) }) });
11251
- };
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" }) });
11262
- };
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;
11278
- }
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
11079
+ });
11080
+ if (!requestSucceeded) {
11081
+ setIsSubmitting(false);
11082
+ return;
11296
11083
  }
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
- }))
11084
+ await confirmOrderEdit(void 0, {
11085
+ onError: (e) => {
11086
+ ui.toast.error(e.message);
11310
11087
  },
11311
- {
11312
- onSuccess: () => {
11313
- setRowSelection({});
11314
- setIsOpen(STACKED_MODAL_ID, false);
11315
- },
11316
- onError: (e) => {
11317
- ui.toast.error(e.message);
11318
- }
11088
+ onSuccess: () => {
11089
+ handleSuccess();
11090
+ },
11091
+ onSettled: () => {
11092
+ setIsSubmitting(false);
11319
11093
  }
11320
- );
11094
+ });
11321
11095
  };
11322
11096
  if (isError) {
11323
11097
  throw error;
11324
11098
  }
11325
- return /* @__PURE__ */ jsxRuntime.jsxs(
11326
- StackedFocusModal.Content,
11327
- {
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." }) })
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." })
11341
11105
  ] }),
11342
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
11343
- DataTable,
11106
+ /* @__PURE__ */ jsxRuntime.jsx(
11107
+ Combobox,
11344
11108
  {
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
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
11360
11119
  }
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
- ]
11367
- }
11368
- );
11369
- };
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
- );
11120
+ )
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,
11125
+ {
11126
+ promotion,
11127
+ orderId: preview.id,
11128
+ isLoading: isPending
11428
11129
  },
11429
- enableSorting: true,
11430
- sortAscLabel: "Oldest first",
11431
- sortDescLabel: "Newest first"
11432
- })
11433
- ];
11434
- }, []);
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
+ {
11138
+ size: "small",
11139
+ type: "submit",
11140
+ isLoading: isSubmitting || isAddingPromotions,
11141
+ children: "Save"
11142
+ }
11143
+ )
11144
+ ] }) })
11145
+ ] });
11435
11146
  };
11436
- const CustomItemForm = ({ orderId, currencyCode }) => {
11437
- const { setIsOpen } = useStackedModal();
11438
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
11439
- const form = reactHookForm.useForm({
11440
- defaultValues: {
11441
- title: "",
11442
- quantity: 1,
11443
- unit_price: ""
11444
- },
11445
- resolver: zod.zodResolver(customItemSchema)
11446
- });
11447
- const onSubmit = form.handleSubmit(async (data) => {
11448
- await addItems(
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(
11449
11156
  {
11450
- items: [
11451
- {
11452
- title: data.title,
11453
- quantity: data.quantity,
11454
- unit_price: convertNumber(data.unit_price)
11455
- }
11456
- ]
11157
+ promo_codes: [promotion.code]
11457
11158
  },
11458
11159
  {
11459
- onSuccess: () => {
11460
- setIsOpen(STACKED_MODAL_ID, false);
11461
- },
11462
11160
  onError: (e) => {
11463
11161
  ui.toast.error(e.message);
11464
11162
  }
11465
11163
  }
11466
11164
  );
11467
- });
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,
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",
11496
11172
  {
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
- ] }) })
11173
+ "animate-pulse": isLoading
11517
11174
  }
11518
11175
  ),
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" })
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: "·" })
11529
11183
  ] }),
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
- ] }) }) });
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, {})
11196
+ }
11197
+ )
11198
+ ]
11199
+ },
11200
+ promotion.id
11201
+ );
11543
11202
  };
11544
- const customItemSchema = objectType({
11545
- title: stringType().min(1),
11546
- quantity: numberType(),
11547
- unit_price: unionType([numberType(), stringType()])
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
11548
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);
11230
+ };
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);
11238
+ }
11239
+ }
11240
+ }
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);
11247
+ }
11248
+ }
11249
+ }
11250
+ }
11251
+ return Array.from(codes);
11252
+ }
11549
11253
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
11550
11254
  const Shipping = () => {
11551
11255
  var _a;
@@ -12385,7 +12089,7 @@ const ShippingAddressForm = ({ order }) => {
12385
12089
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12386
12090
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12387
12091
  },
12388
- resolver: zod.zodResolver(schema$1)
12092
+ resolver: zod.zodResolver(schema$3)
12389
12093
  });
12390
12094
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12391
12095
  const { handleSuccess } = useRouteModal();
@@ -12555,7 +12259,7 @@ const ShippingAddressForm = ({ order }) => {
12555
12259
  }
12556
12260
  ) });
12557
12261
  };
12558
- const schema$1 = addressSchema;
12262
+ const schema$3 = addressSchema;
12559
12263
  const TransferOwnership = () => {
12560
12264
  const { id } = reactRouterDom.useParams();
12561
12265
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12579,7 +12283,7 @@ const TransferOwnershipForm = ({ order }) => {
12579
12283
  defaultValues: {
12580
12284
  customer_id: order.customer_id || ""
12581
12285
  },
12582
- resolver: zod.zodResolver(schema)
12286
+ resolver: zod.zodResolver(schema$2)
12583
12287
  });
12584
12288
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12585
12289
  const { handleSuccess } = useRouteModal();
@@ -12973,64 +12677,360 @@ const Illustration = () => {
12973
12677
  /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12974
12678
  "rect",
12975
12679
  {
12976
- width: "12",
12977
- height: "12",
12978
- fill: "white",
12979
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12680
+ width: "12",
12681
+ height: "12",
12682
+ fill: "white",
12683
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12684
+ }
12685
+ ) }),
12686
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12687
+ "rect",
12688
+ {
12689
+ width: "12",
12690
+ height: "12",
12691
+ fill: "white",
12692
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12693
+ }
12694
+ ) }),
12695
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12696
+ "rect",
12697
+ {
12698
+ width: "12",
12699
+ height: "12",
12700
+ fill: "white",
12701
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12702
+ }
12703
+ ) }),
12704
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12705
+ "rect",
12706
+ {
12707
+ width: "12",
12708
+ height: "12",
12709
+ fill: "white",
12710
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12711
+ }
12712
+ ) }),
12713
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12714
+ "rect",
12715
+ {
12716
+ width: "12",
12717
+ height: "12",
12718
+ fill: "white",
12719
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12720
+ }
12721
+ ) }),
12722
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12723
+ "rect",
12724
+ {
12725
+ width: "12",
12726
+ height: "12",
12727
+ fill: "white",
12728
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12729
+ }
12730
+ ) })
12731
+ ] })
12732
+ ]
12733
+ }
12734
+ );
12735
+ };
12736
+ const schema$2 = objectType({
12737
+ customer_id: stringType().min(1)
12738
+ });
12739
+ const BillingAddress = () => {
12740
+ const { id } = reactRouterDom.useParams();
12741
+ const { order, isPending, isError, error } = useOrder(id, {
12742
+ fields: "+billing_address"
12743
+ });
12744
+ if (isError) {
12745
+ throw error;
12746
+ }
12747
+ const isReady = !isPending && !!order;
12748
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12749
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12750
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Billing Address" }) }),
12751
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
12752
+ ] }),
12753
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(BillingAddressForm, { order })
12754
+ ] });
12755
+ };
12756
+ const BillingAddressForm = ({ order }) => {
12757
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12758
+ const form = reactHookForm.useForm({
12759
+ defaultValues: {
12760
+ first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
12761
+ last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
12762
+ company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
12763
+ address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
12764
+ address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
12765
+ city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
12766
+ province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
12767
+ country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
12768
+ postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
12769
+ phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
12770
+ },
12771
+ resolver: zod.zodResolver(schema$1)
12772
+ });
12773
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12774
+ const { handleSuccess } = useRouteModal();
12775
+ const onSubmit = form.handleSubmit(async (data) => {
12776
+ await mutateAsync(
12777
+ { billing_address: data },
12778
+ {
12779
+ onSuccess: () => {
12780
+ handleSuccess();
12781
+ },
12782
+ onError: (error) => {
12783
+ ui.toast.error(error.message);
12784
+ }
12785
+ }
12786
+ );
12787
+ });
12788
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12789
+ KeyboundForm,
12790
+ {
12791
+ className: "flex flex-1 flex-col overflow-hidden",
12792
+ onSubmit,
12793
+ children: [
12794
+ /* @__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: [
12795
+ /* @__PURE__ */ jsxRuntime.jsx(
12796
+ Form$2.Field,
12797
+ {
12798
+ control: form.control,
12799
+ name: "country_code",
12800
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12801
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
12802
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
12803
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12804
+ ] })
12805
+ }
12806
+ ),
12807
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12808
+ /* @__PURE__ */ jsxRuntime.jsx(
12809
+ Form$2.Field,
12810
+ {
12811
+ control: form.control,
12812
+ name: "first_name",
12813
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12814
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
12815
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12816
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12817
+ ] })
12818
+ }
12819
+ ),
12820
+ /* @__PURE__ */ jsxRuntime.jsx(
12821
+ Form$2.Field,
12822
+ {
12823
+ control: form.control,
12824
+ name: "last_name",
12825
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12826
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
12827
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12828
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12829
+ ] })
12830
+ }
12831
+ )
12832
+ ] }),
12833
+ /* @__PURE__ */ jsxRuntime.jsx(
12834
+ Form$2.Field,
12835
+ {
12836
+ control: form.control,
12837
+ name: "company",
12838
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12839
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
12840
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12841
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12842
+ ] })
12980
12843
  }
12981
- ) }),
12982
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12983
- "rect",
12844
+ ),
12845
+ /* @__PURE__ */ jsxRuntime.jsx(
12846
+ Form$2.Field,
12984
12847
  {
12985
- width: "12",
12986
- height: "12",
12987
- fill: "white",
12988
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12848
+ control: form.control,
12849
+ name: "address_1",
12850
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12851
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
12852
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12853
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12854
+ ] })
12989
12855
  }
12990
- ) }),
12991
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12992
- "rect",
12856
+ ),
12857
+ /* @__PURE__ */ jsxRuntime.jsx(
12858
+ Form$2.Field,
12993
12859
  {
12994
- width: "12",
12995
- height: "12",
12996
- fill: "white",
12997
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12860
+ control: form.control,
12861
+ name: "address_2",
12862
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12863
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12864
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12865
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12866
+ ] })
12998
12867
  }
12999
- ) }),
13000
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13001
- "rect",
12868
+ ),
12869
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12870
+ /* @__PURE__ */ jsxRuntime.jsx(
12871
+ Form$2.Field,
12872
+ {
12873
+ control: form.control,
12874
+ name: "postal_code",
12875
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12876
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
12877
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12878
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12879
+ ] })
12880
+ }
12881
+ ),
12882
+ /* @__PURE__ */ jsxRuntime.jsx(
12883
+ Form$2.Field,
12884
+ {
12885
+ control: form.control,
12886
+ name: "city",
12887
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12888
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
12889
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12890
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12891
+ ] })
12892
+ }
12893
+ )
12894
+ ] }),
12895
+ /* @__PURE__ */ jsxRuntime.jsx(
12896
+ Form$2.Field,
13002
12897
  {
13003
- width: "12",
13004
- height: "12",
13005
- fill: "white",
13006
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12898
+ control: form.control,
12899
+ name: "province",
12900
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12901
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
12902
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12903
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12904
+ ] })
13007
12905
  }
13008
- ) }),
13009
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13010
- "rect",
12906
+ ),
12907
+ /* @__PURE__ */ jsxRuntime.jsx(
12908
+ Form$2.Field,
13011
12909
  {
13012
- width: "12",
13013
- height: "12",
13014
- fill: "white",
13015
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12910
+ control: form.control,
12911
+ name: "phone",
12912
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12913
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
12914
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12915
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12916
+ ] })
13016
12917
  }
13017
- ) }),
13018
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
13019
- "rect",
12918
+ )
12919
+ ] }) }),
12920
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12921
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12922
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12923
+ ] }) })
12924
+ ]
12925
+ }
12926
+ ) });
12927
+ };
12928
+ const schema$1 = addressSchema;
12929
+ const SalesChannel = () => {
12930
+ const { id } = reactRouterDom.useParams();
12931
+ const { draft_order, isPending, isError, error } = useDraftOrder(
12932
+ id,
12933
+ {
12934
+ fields: "+sales_channel_id"
12935
+ },
12936
+ {
12937
+ enabled: !!id
12938
+ }
12939
+ );
12940
+ if (isError) {
12941
+ throw error;
12942
+ }
12943
+ const ISrEADY = !!draft_order && !isPending;
12944
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12945
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12946
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
12947
+ /* @__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" }) })
12948
+ ] }),
12949
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
12950
+ ] });
12951
+ };
12952
+ const SalesChannelForm = ({ order }) => {
12953
+ const form = reactHookForm.useForm({
12954
+ defaultValues: {
12955
+ sales_channel_id: order.sales_channel_id || ""
12956
+ },
12957
+ resolver: zod.zodResolver(schema)
12958
+ });
12959
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12960
+ const { handleSuccess } = useRouteModal();
12961
+ const onSubmit = form.handleSubmit(async (data) => {
12962
+ await mutateAsync(
12963
+ {
12964
+ sales_channel_id: data.sales_channel_id
12965
+ },
12966
+ {
12967
+ onSuccess: () => {
12968
+ ui.toast.success("Sales channel updated");
12969
+ handleSuccess();
12970
+ },
12971
+ onError: (error) => {
12972
+ ui.toast.error(error.message);
12973
+ }
12974
+ }
12975
+ );
12976
+ });
12977
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12978
+ KeyboundForm,
12979
+ {
12980
+ className: "flex flex-1 flex-col overflow-hidden",
12981
+ onSubmit,
12982
+ children: [
12983
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
12984
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
12985
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12986
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
12987
+ ] }) })
12988
+ ]
12989
+ }
12990
+ ) });
12991
+ };
12992
+ const SalesChannelField = ({ control, order }) => {
12993
+ const salesChannels = useComboboxData({
12994
+ queryFn: async (params) => {
12995
+ return await sdk.admin.salesChannel.list(params);
12996
+ },
12997
+ queryKey: ["sales-channels"],
12998
+ getOptions: (data) => {
12999
+ return data.sales_channels.map((salesChannel) => ({
13000
+ label: salesChannel.name,
13001
+ value: salesChannel.id
13002
+ }));
13003
+ },
13004
+ defaultValue: order.sales_channel_id || void 0
13005
+ });
13006
+ return /* @__PURE__ */ jsxRuntime.jsx(
13007
+ Form$2.Field,
13008
+ {
13009
+ control,
13010
+ name: "sales_channel_id",
13011
+ render: ({ field }) => {
13012
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13013
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
13014
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
13015
+ Combobox,
13020
13016
  {
13021
- width: "12",
13022
- height: "12",
13023
- fill: "white",
13024
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
13017
+ options: salesChannels.options,
13018
+ fetchNextPage: salesChannels.fetchNextPage,
13019
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
13020
+ searchValue: salesChannels.searchValue,
13021
+ onSearchValueChange: salesChannels.onSearchValueChange,
13022
+ placeholder: "Select sales channel",
13023
+ ...field
13025
13024
  }
13026
- ) })
13027
- ] })
13028
- ]
13025
+ ) }),
13026
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13027
+ ] });
13028
+ }
13029
13029
  }
13030
13030
  );
13031
13031
  };
13032
13032
  const schema = objectType({
13033
- customer_id: stringType().min(1)
13033
+ sales_channel_id: stringType().min(1)
13034
13034
  });
13035
13035
  const widgetModule = { widgets: [] };
13036
13036
  const routeModule = {
@@ -13052,10 +13052,6 @@ const routeModule = {
13052
13052
  handle,
13053
13053
  loader,
13054
13054
  children: [
13055
- {
13056
- Component: BillingAddress,
13057
- path: "/draft-orders/:id/billing-address"
13058
- },
13059
13055
  {
13060
13056
  Component: CustomItems,
13061
13057
  path: "/draft-orders/:id/custom-items"
@@ -13064,6 +13060,10 @@ const routeModule = {
13064
13060
  Component: Email,
13065
13061
  path: "/draft-orders/:id/email"
13066
13062
  },
13063
+ {
13064
+ Component: Items,
13065
+ path: "/draft-orders/:id/items"
13066
+ },
13067
13067
  {
13068
13068
  Component: Metadata,
13069
13069
  path: "/draft-orders/:id/metadata"
@@ -13072,14 +13072,6 @@ const routeModule = {
13072
13072
  Component: Promotions,
13073
13073
  path: "/draft-orders/:id/promotions"
13074
13074
  },
13075
- {
13076
- Component: SalesChannel,
13077
- path: "/draft-orders/:id/sales-channel"
13078
- },
13079
- {
13080
- Component: Items,
13081
- path: "/draft-orders/:id/items"
13082
- },
13083
13075
  {
13084
13076
  Component: Shipping,
13085
13077
  path: "/draft-orders/:id/shipping"
@@ -13091,6 +13083,14 @@ const routeModule = {
13091
13083
  {
13092
13084
  Component: TransferOwnership,
13093
13085
  path: "/draft-orders/:id/transfer-ownership"
13086
+ },
13087
+ {
13088
+ Component: BillingAddress,
13089
+ path: "/draft-orders/:id/billing-address"
13090
+ },
13091
+ {
13092
+ Component: SalesChannel,
13093
+ path: "/draft-orders/:id/sales-channel"
13094
13094
  }
13095
13095
  ]
13096
13096
  }