@medusajs/draft-order 2.10.2-preview-20250908180200 → 2.10.2-preview-20250909000311

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9573,6 +9573,217 @@ const ID = () => {
9573
9573
  /* @__PURE__ */ jsxRuntime.jsx(reactRouterDom.Outlet, {})
9574
9574
  ] });
9575
9575
  };
9576
+ const CustomItems = () => {
9577
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9578
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
9579
+ /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
9580
+ ] });
9581
+ };
9582
+ const CustomItemsForm = () => {
9583
+ const form = reactHookForm.useForm({
9584
+ resolver: zod.zodResolver(schema$5)
9585
+ });
9586
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9587
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
9588
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9589
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9590
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
9591
+ ] }) })
9592
+ ] }) });
9593
+ };
9594
+ const schema$5 = objectType({
9595
+ email: stringType().email()
9596
+ });
9597
+ const BillingAddress = () => {
9598
+ const { id } = reactRouterDom.useParams();
9599
+ const { order, isPending, isError, error } = useOrder(id, {
9600
+ fields: "+billing_address"
9601
+ });
9602
+ if (isError) {
9603
+ throw error;
9604
+ }
9605
+ const isReady = !isPending && !!order;
9606
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
9607
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
9608
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Billing Address" }) }),
9609
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
9610
+ ] }),
9611
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(BillingAddressForm, { order })
9612
+ ] });
9613
+ };
9614
+ const BillingAddressForm = ({ order }) => {
9615
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
9616
+ const form = reactHookForm.useForm({
9617
+ defaultValues: {
9618
+ first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
9619
+ last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
9620
+ company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
9621
+ address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
9622
+ address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
9623
+ city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
9624
+ province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
9625
+ country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
9626
+ postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9627
+ phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9628
+ },
9629
+ resolver: zod.zodResolver(schema$4)
9630
+ });
9631
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9632
+ const { handleSuccess } = useRouteModal();
9633
+ const onSubmit = form.handleSubmit(async (data) => {
9634
+ await mutateAsync(
9635
+ { billing_address: data },
9636
+ {
9637
+ onSuccess: () => {
9638
+ handleSuccess();
9639
+ },
9640
+ onError: (error) => {
9641
+ ui.toast.error(error.message);
9642
+ }
9643
+ }
9644
+ );
9645
+ });
9646
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
9647
+ KeyboundForm,
9648
+ {
9649
+ className: "flex flex-1 flex-col overflow-hidden",
9650
+ onSubmit,
9651
+ children: [
9652
+ /* @__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: [
9653
+ /* @__PURE__ */ jsxRuntime.jsx(
9654
+ Form$2.Field,
9655
+ {
9656
+ control: form.control,
9657
+ name: "country_code",
9658
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9659
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
9660
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
9661
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9662
+ ] })
9663
+ }
9664
+ ),
9665
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9666
+ /* @__PURE__ */ jsxRuntime.jsx(
9667
+ Form$2.Field,
9668
+ {
9669
+ control: form.control,
9670
+ name: "first_name",
9671
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9672
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
9673
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9674
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9675
+ ] })
9676
+ }
9677
+ ),
9678
+ /* @__PURE__ */ jsxRuntime.jsx(
9679
+ Form$2.Field,
9680
+ {
9681
+ control: form.control,
9682
+ name: "last_name",
9683
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9684
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
9685
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9686
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9687
+ ] })
9688
+ }
9689
+ )
9690
+ ] }),
9691
+ /* @__PURE__ */ jsxRuntime.jsx(
9692
+ Form$2.Field,
9693
+ {
9694
+ control: form.control,
9695
+ name: "company",
9696
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9697
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
9698
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9699
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9700
+ ] })
9701
+ }
9702
+ ),
9703
+ /* @__PURE__ */ jsxRuntime.jsx(
9704
+ Form$2.Field,
9705
+ {
9706
+ control: form.control,
9707
+ name: "address_1",
9708
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9709
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
9710
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9711
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9712
+ ] })
9713
+ }
9714
+ ),
9715
+ /* @__PURE__ */ jsxRuntime.jsx(
9716
+ Form$2.Field,
9717
+ {
9718
+ control: form.control,
9719
+ name: "address_2",
9720
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9721
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
9722
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9723
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9724
+ ] })
9725
+ }
9726
+ ),
9727
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9728
+ /* @__PURE__ */ jsxRuntime.jsx(
9729
+ Form$2.Field,
9730
+ {
9731
+ control: form.control,
9732
+ name: "postal_code",
9733
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9734
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
9735
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9736
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9737
+ ] })
9738
+ }
9739
+ ),
9740
+ /* @__PURE__ */ jsxRuntime.jsx(
9741
+ Form$2.Field,
9742
+ {
9743
+ control: form.control,
9744
+ name: "city",
9745
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9746
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
9747
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9748
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9749
+ ] })
9750
+ }
9751
+ )
9752
+ ] }),
9753
+ /* @__PURE__ */ jsxRuntime.jsx(
9754
+ Form$2.Field,
9755
+ {
9756
+ control: form.control,
9757
+ name: "province",
9758
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9759
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
9760
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9761
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9762
+ ] })
9763
+ }
9764
+ ),
9765
+ /* @__PURE__ */ jsxRuntime.jsx(
9766
+ Form$2.Field,
9767
+ {
9768
+ control: form.control,
9769
+ name: "phone",
9770
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
9771
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
9772
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
9773
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
9774
+ ] })
9775
+ }
9776
+ )
9777
+ ] }) }),
9778
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
9779
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9780
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9781
+ ] }) })
9782
+ ]
9783
+ }
9784
+ ) });
9785
+ };
9786
+ const schema$4 = addressSchema;
9576
9787
  const Email = () => {
9577
9788
  const { id } = reactRouterDom.useParams();
9578
9789
  const { order, isPending, isError, error } = useOrder(id, {
@@ -9595,7 +9806,7 @@ const EmailForm = ({ order }) => {
9595
9806
  defaultValues: {
9596
9807
  email: order.email ?? ""
9597
9808
  },
9598
- resolver: zod.zodResolver(schema$5)
9809
+ resolver: zod.zodResolver(schema$3)
9599
9810
  });
9600
9811
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9601
9812
  const { handleSuccess } = useRouteModal();
@@ -9638,130 +9849,25 @@ const EmailForm = ({ order }) => {
9638
9849
  }
9639
9850
  ) });
9640
9851
  };
9641
- const schema$5 = objectType({
9852
+ const schema$3 = objectType({
9642
9853
  email: stringType().email()
9643
9854
  });
9644
- const NumberInput = React.forwardRef(
9645
- ({
9646
- value,
9647
- onChange,
9648
- size = "base",
9649
- min = 0,
9650
- max = 100,
9651
- step = 1,
9652
- className,
9653
- disabled,
9654
- ...props
9655
- }, ref) => {
9656
- const handleChange = (event) => {
9657
- const newValue = event.target.value === "" ? min : Number(event.target.value);
9658
- if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
9659
- onChange(newValue);
9660
- }
9661
- };
9662
- const handleIncrement = () => {
9663
- const newValue = value + step;
9664
- if (max === void 0 || newValue <= max) {
9665
- onChange(newValue);
9666
- }
9667
- };
9668
- const handleDecrement = () => {
9669
- const newValue = value - step;
9670
- if (min === void 0 || newValue >= min) {
9671
- onChange(newValue);
9672
- }
9673
- };
9674
- return /* @__PURE__ */ jsxRuntime.jsxs(
9675
- "div",
9676
- {
9677
- className: ui.clx(
9678
- "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
9679
- "[&:has(input:focus)]:shadow-borders-interactive-with-active",
9680
- {
9681
- "h-7": size === "small",
9682
- "h-8": size === "base"
9683
- },
9684
- className
9685
- ),
9686
- children: [
9687
- /* @__PURE__ */ jsxRuntime.jsx(
9688
- "input",
9689
- {
9690
- ref,
9691
- type: "number",
9692
- value,
9693
- onChange: handleChange,
9694
- min,
9695
- max,
9696
- step,
9697
- className: ui.clx(
9698
- "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
9699
- "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
9700
- "placeholder:text-ui-fg-muted"
9701
- ),
9702
- ...props
9703
- }
9704
- ),
9705
- /* @__PURE__ */ jsxRuntime.jsxs(
9706
- "button",
9707
- {
9708
- className: ui.clx(
9709
- "flex items-center justify-center outline-none transition-fg",
9710
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9711
- "focus:bg-ui-bg-field-component-hover",
9712
- "hover:bg-ui-bg-field-component-hover",
9713
- {
9714
- "size-7": size === "small",
9715
- "size-8": size === "base"
9716
- }
9717
- ),
9718
- type: "button",
9719
- onClick: handleDecrement,
9720
- disabled: min !== void 0 && value <= min || disabled,
9721
- children: [
9722
- /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
9723
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
9724
- ]
9725
- }
9726
- ),
9727
- /* @__PURE__ */ jsxRuntime.jsxs(
9728
- "button",
9729
- {
9730
- className: ui.clx(
9731
- "flex items-center justify-center outline-none transition-fg",
9732
- "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
9733
- "focus:bg-ui-bg-field-hover",
9734
- "hover:bg-ui-bg-field-hover",
9735
- {
9736
- "size-7": size === "small",
9737
- "size-8": size === "base"
9738
- }
9739
- ),
9740
- type: "button",
9741
- onClick: handleIncrement,
9742
- disabled: max !== void 0 && value >= max || disabled,
9743
- children: [
9744
- /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
9745
- /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
9746
- ]
9747
- }
9748
- )
9749
- ]
9750
- }
9751
- );
9752
- }
9753
- );
9754
- const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
9755
- const productVariantsQueryKeys = {
9855
+ const PROMOTION_QUERY_KEY = "promotions";
9856
+ const promotionsQueryKeys = {
9756
9857
  list: (query2) => [
9757
- PRODUCT_VARIANTS_QUERY_KEY,
9858
+ PROMOTION_QUERY_KEY,
9859
+ query2 ? query2 : void 0
9860
+ ],
9861
+ detail: (id, query2) => [
9862
+ PROMOTION_QUERY_KEY,
9863
+ id,
9758
9864
  query2 ? query2 : void 0
9759
9865
  ]
9760
9866
  };
9761
- const useProductVariants = (query2, options) => {
9867
+ const usePromotions = (query2, options) => {
9762
9868
  const { data, ...rest } = reactQuery.useQuery({
9763
- queryKey: productVariantsQueryKeys.list(query2),
9764
- queryFn: async () => await sdk.admin.productVariant.list(query2),
9869
+ queryKey: promotionsQueryKeys.list(query2),
9870
+ queryFn: async () => sdk.admin.promotion.list(query2),
9765
9871
  ...options
9766
9872
  });
9767
9873
  return { ...data, ...rest };
@@ -9812,65 +9918,85 @@ const useInitiateOrderEdit = ({
9812
9918
  run();
9813
9919
  }, [preview, navigate, mutateAsync]);
9814
9920
  };
9815
- function convertNumber(value) {
9816
- return typeof value === "string" ? Number(value.replace(",", ".")) : value;
9817
- }
9818
- const STACKED_MODAL_ID = "items_stacked_modal";
9819
- const Items = () => {
9921
+ const Promotions = () => {
9820
9922
  const { id } = reactRouterDom.useParams();
9821
9923
  const {
9822
9924
  order: preview,
9823
- isPending: isPreviewPending,
9824
9925
  isError: isPreviewError,
9825
9926
  error: previewError
9826
- } = useOrderPreview(id, void 0, {
9827
- placeholderData: reactQuery.keepPreviousData
9828
- });
9927
+ } = useOrderPreview(id, void 0);
9829
9928
  useInitiateOrderEdit({ preview });
9830
- const { draft_order, isPending, isError, error } = useDraftOrder(
9831
- id,
9832
- {
9833
- fields: "currency_code"
9834
- },
9835
- {
9836
- enabled: !!id
9837
- }
9838
- );
9839
9929
  const { onCancel } = useCancelOrderEdit({ preview });
9840
- if (isError) {
9841
- throw error;
9842
- }
9843
9930
  if (isPreviewError) {
9844
9931
  throw previewError;
9845
9932
  }
9846
- const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
9847
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
9848
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
9849
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
9850
- ] }) });
9933
+ const isReady = !!preview;
9934
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
9935
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
9936
+ isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
9937
+ ] });
9851
9938
  };
9852
- const ItemsForm = ({ preview, currencyCode }) => {
9853
- var _a;
9939
+ const PromotionForm = ({ preview }) => {
9940
+ const { items, shipping_methods } = preview;
9854
9941
  const [isSubmitting, setIsSubmitting] = React.useState(false);
9855
- const [modalContent, setModalContent] = React.useState(
9856
- null
9857
- );
9942
+ const [comboboxValue, setComboboxValue] = React.useState("");
9858
9943
  const { handleSuccess } = useRouteModal();
9859
- const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
9944
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
9945
+ const promoIds = getPromotionIds(items, shipping_methods);
9946
+ const { promotions, isPending, isError, error } = usePromotions(
9947
+ {
9948
+ id: promoIds
9949
+ },
9950
+ {
9951
+ enabled: !!promoIds.length
9952
+ }
9953
+ );
9954
+ const comboboxData = useComboboxData({
9955
+ queryKey: ["promotions", "combobox", promoIds],
9956
+ queryFn: async (params) => {
9957
+ return await sdk.admin.promotion.list({
9958
+ ...params,
9959
+ id: {
9960
+ $nin: promoIds
9961
+ }
9962
+ });
9963
+ },
9964
+ getOptions: (data) => {
9965
+ return data.promotions.map((promotion) => ({
9966
+ label: promotion.code,
9967
+ value: promotion.code
9968
+ }));
9969
+ }
9970
+ });
9971
+ const add = async (value) => {
9972
+ if (!value) {
9973
+ return;
9974
+ }
9975
+ addPromotions(
9976
+ {
9977
+ promo_codes: [value]
9978
+ },
9979
+ {
9980
+ onError: (e) => {
9981
+ ui.toast.error(e.message);
9982
+ comboboxData.onSearchValueChange("");
9983
+ setComboboxValue("");
9984
+ },
9985
+ onSuccess: () => {
9986
+ comboboxData.onSearchValueChange("");
9987
+ setComboboxValue("");
9988
+ }
9989
+ }
9990
+ );
9991
+ };
9860
9992
  const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
9861
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
9862
- const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
9863
- const matches = React.useMemo(() => {
9864
- return matchSorter.matchSorter(preview.items, query2, {
9865
- keys: ["product_title", "variant_title", "variant_sku", "title"]
9866
- });
9867
- }, [preview.items, query2]);
9993
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
9868
9994
  const onSubmit = async () => {
9869
9995
  setIsSubmitting(true);
9870
9996
  let requestSucceeded = false;
9871
9997
  await requestOrderEdit(void 0, {
9872
9998
  onError: (e) => {
9873
- ui.toast.error(`Failed to request order edit: ${e.message}`);
9999
+ ui.toast.error(e.message);
9874
10000
  },
9875
10001
  onSuccess: () => {
9876
10002
  requestSucceeded = true;
@@ -9882,7 +10008,7 @@ const ItemsForm = ({ preview, currencyCode }) => {
9882
10008
  }
9883
10009
  await confirmOrderEdit(void 0, {
9884
10010
  onError: (e) => {
9885
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10011
+ ui.toast.error(e.message);
9886
10012
  },
9887
10013
  onSuccess: () => {
9888
10014
  handleSuccess();
@@ -9892,1589 +10018,399 @@ const ItemsForm = ({ preview, currencyCode }) => {
9892
10018
  }
9893
10019
  });
9894
10020
  };
9895
- const onKeyDown = React.useCallback(
9896
- (e) => {
9897
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
9898
- if (modalContent || isSubmitting) {
9899
- return;
9900
- }
9901
- onSubmit();
9902
- }
9903
- },
9904
- [modalContent, isSubmitting, onSubmit]
9905
- );
9906
- React.useEffect(() => {
9907
- document.addEventListener("keydown", onKeyDown);
9908
- return () => {
9909
- document.removeEventListener("keydown", onKeyDown);
9910
- };
9911
- }, [onKeyDown]);
9912
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
9913
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
9914
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
9915
- StackedFocusModal,
9916
- {
9917
- id: STACKED_MODAL_ID,
9918
- onOpenChangeCallback: (open) => {
9919
- if (!open) {
9920
- setModalContent(null);
10021
+ if (isError) {
10022
+ throw error;
10023
+ }
10024
+ return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10025
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
10026
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
10027
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10028
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10029
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10030
+ ] }),
10031
+ /* @__PURE__ */ jsxRuntime.jsx(
10032
+ Combobox,
10033
+ {
10034
+ id: "promotion-combobox",
10035
+ "aria-describedby": "promotion-combobox-hint",
10036
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10037
+ fetchNextPage: comboboxData.fetchNextPage,
10038
+ options: comboboxData.options,
10039
+ onSearchValueChange: comboboxData.onSearchValueChange,
10040
+ searchValue: comboboxData.searchValue,
10041
+ disabled: comboboxData.disabled || isAddingPromotions,
10042
+ onChange: add,
10043
+ value: comboboxValue
9921
10044
  }
10045
+ )
10046
+ ] }),
10047
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10048
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
10049
+ PromotionItem,
10050
+ {
10051
+ promotion,
10052
+ orderId: preview.id,
10053
+ isLoading: isPending
9922
10054
  },
9923
- children: [
9924
- /* @__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: [
9925
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
9926
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
9927
- /* @__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" }) })
9928
- ] }),
9929
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
9930
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
9931
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
9932
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
9933
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
9934
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
9935
- ] }),
9936
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
9937
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
9938
- ui.Input,
9939
- {
9940
- type: "search",
9941
- placeholder: "Search items",
9942
- value: searchValue,
9943
- onChange: (e) => onSearchValueChange(e.target.value)
9944
- }
9945
- ) }),
9946
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
9947
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
9948
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
9949
- /* @__PURE__ */ jsxRuntime.jsx(
9950
- StackedModalTrigger$1,
9951
- {
9952
- type: "add-items",
9953
- setModalContent
9954
- }
9955
- ),
9956
- /* @__PURE__ */ jsxRuntime.jsx(
9957
- StackedModalTrigger$1,
9958
- {
9959
- type: "add-custom-item",
9960
- setModalContent
9961
- }
9962
- )
9963
- ] })
9964
- ] })
9965
- ] })
9966
- ] }),
9967
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
9968
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-muted grid grid-cols-[2fr_1fr_2fr_28px] gap-3 px-4 py-2", children: [
9969
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
9970
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
9971
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
9972
- /* @__PURE__ */ jsxRuntime.jsx("div", {})
9973
- ] }) }),
9974
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
9975
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
9976
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
9977
- ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
9978
- Item,
9979
- {
9980
- item,
9981
- preview,
9982
- currencyCode
9983
- },
9984
- item.id
9985
- )) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
9986
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
9987
- /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
9988
- 'No items found for "',
9989
- query2,
9990
- '".'
9991
- ] })
9992
- ] }) })
9993
- ] })
9994
- ] }),
9995
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
9996
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
9997
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
9998
- /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
9999
- ui.Text,
10000
- {
10001
- size: "small",
10002
- leading: "compact",
10003
- className: "text-ui-fg-subtle",
10004
- children: [
10005
- itemCount,
10006
- " ",
10007
- itemCount === 1 ? "item" : "items"
10008
- ]
10009
- }
10010
- ) }),
10011
- /* @__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) }) })
10012
- ] })
10013
- ] }) }),
10014
- modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
10015
- CustomItemForm,
10016
- {
10017
- orderId: preview.id,
10018
- currencyCode
10019
- }
10020
- ) : null)
10021
- ]
10022
- }
10023
- ) }),
10024
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10025
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10055
+ promotion.id
10056
+ )) })
10057
+ ] }) }),
10058
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10059
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10026
10060
  /* @__PURE__ */ jsxRuntime.jsx(
10027
10061
  ui.Button,
10028
10062
  {
10029
10063
  size: "small",
10030
- type: "button",
10031
- onClick: onSubmit,
10032
- isLoading: isSubmitting,
10064
+ type: "submit",
10065
+ isLoading: isSubmitting || isAddingPromotions,
10033
10066
  children: "Save"
10034
10067
  }
10035
10068
  )
10036
10069
  ] }) })
10037
10070
  ] });
10038
10071
  };
10039
- const Item = ({ item, preview, currencyCode }) => {
10040
- if (item.variant_id) {
10041
- return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
10042
- }
10043
- return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
10044
- };
10045
- const VariantItem = ({ item, preview, currencyCode }) => {
10046
- const [editing, setEditing] = React.useState(false);
10047
- const form = reactHookForm.useForm({
10048
- defaultValues: {
10049
- quantity: item.quantity,
10050
- unit_price: item.unit_price
10051
- },
10052
- resolver: zod.zodResolver(variantItemSchema)
10053
- });
10054
- const actionId = React.useMemo(() => {
10055
- var _a, _b;
10056
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10057
- }, [item]);
10058
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10059
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10060
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10061
- const onSubmit = form.handleSubmit(async (data) => {
10062
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
10063
- setEditing(false);
10064
- return;
10065
- }
10066
- if (!actionId) {
10067
- await updateOriginalItem(
10068
- {
10069
- item_id: item.id,
10070
- quantity: data.quantity,
10071
- unit_price: convertNumber(data.unit_price)
10072
- },
10073
- {
10074
- onSuccess: () => {
10075
- setEditing(false);
10076
- },
10077
- onError: (e) => {
10078
- ui.toast.error(e.message);
10079
- }
10080
- }
10081
- );
10082
- return;
10083
- }
10084
- await updateActionItem(
10072
+ const PromotionItem = ({
10073
+ promotion,
10074
+ orderId,
10075
+ isLoading
10076
+ }) => {
10077
+ var _a;
10078
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
10079
+ const onRemove = async () => {
10080
+ removePromotions(
10085
10081
  {
10086
- action_id: actionId,
10087
- quantity: data.quantity,
10088
- unit_price: convertNumber(data.unit_price)
10082
+ promo_codes: [promotion.code]
10089
10083
  },
10090
10084
  {
10091
- onSuccess: () => {
10092
- setEditing(false);
10093
- },
10094
10085
  onError: (e) => {
10095
10086
  ui.toast.error(e.message);
10096
10087
  }
10097
10088
  }
10098
10089
  );
10099
- });
10100
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10101
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
10102
- /* @__PURE__ */ jsxRuntime.jsx(
10103
- Thumbnail,
10090
+ };
10091
+ const displayValue = getDisplayValue(promotion);
10092
+ return /* @__PURE__ */ jsxRuntime.jsxs(
10093
+ "div",
10094
+ {
10095
+ className: ui.clx(
10096
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
10104
10097
  {
10105
- thumbnail: item.thumbnail,
10106
- alt: item.product_title ?? void 0
10098
+ "animate-pulse": isLoading
10107
10099
  }
10108
10100
  ),
10109
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
10110
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
10111
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
10112
- /* @__PURE__ */ jsxRuntime.jsxs(
10113
- ui.Text,
10114
- {
10115
- size: "small",
10116
- leading: "compact",
10117
- className: "text-ui-fg-subtle",
10118
- children: [
10119
- "(",
10120
- item.variant_title,
10121
- ")"
10122
- ]
10123
- }
10124
- )
10101
+ children: [
10102
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10103
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
10104
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
10105
+ displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
10106
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
10107
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
10108
+ ] }),
10109
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
10110
+ ] })
10125
10111
  ] }),
10126
10112
  /* @__PURE__ */ jsxRuntime.jsx(
10127
- ui.Text,
10113
+ ui.IconButton,
10128
10114
  {
10129
10115
  size: "small",
10130
- leading: "compact",
10131
- className: "text-ui-fg-subtle",
10132
- children: item.variant_sku
10116
+ type: "button",
10117
+ variant: "transparent",
10118
+ onClick: onRemove,
10119
+ isLoading: isPending || isLoading,
10120
+ children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
10133
10121
  }
10134
10122
  )
10135
- ] })
10136
- ] }),
10137
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10138
- Form$2.Field,
10139
- {
10140
- control: form.control,
10141
- name: "quantity",
10142
- render: ({ field }) => {
10143
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10144
- }
10145
- }
10146
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
10147
- editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
10148
- Form$2.Field,
10149
- {
10150
- control: form.control,
10151
- name: "unit_price",
10152
- render: ({ field: { onChange, ...field } }) => {
10153
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10154
- ui.CurrencyInput,
10155
- {
10156
- ...field,
10157
- symbol: getNativeSymbol(currencyCode),
10158
- code: currencyCode,
10159
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10160
- }
10161
- ) }) });
10162
- }
10163
- }
10164
- ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10165
- /* @__PURE__ */ jsxRuntime.jsx(
10166
- ui.IconButton,
10167
- {
10168
- type: "button",
10169
- size: "small",
10170
- onClick: editing ? onSubmit : () => {
10171
- setEditing(true);
10172
- },
10173
- disabled: isPending,
10174
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10175
- }
10176
- )
10177
- ] }) }) });
10178
- };
10179
- const variantItemSchema = objectType({
10180
- quantity: numberType(),
10181
- unit_price: unionType([numberType(), stringType()])
10182
- });
10183
- const CustomItem = ({ item, preview, currencyCode }) => {
10184
- const [editing, setEditing] = React.useState(false);
10185
- const { quantity, unit_price, title } = item;
10186
- const form = reactHookForm.useForm({
10187
- defaultValues: {
10188
- title,
10189
- quantity,
10190
- unit_price
10123
+ ]
10191
10124
  },
10192
- resolver: zod.zodResolver(customItemSchema)
10193
- });
10194
- React.useEffect(() => {
10195
- form.reset({
10196
- title,
10197
- quantity,
10198
- unit_price
10199
- });
10200
- }, [form, title, quantity, unit_price]);
10201
- const actionId = React.useMemo(() => {
10202
- var _a, _b;
10203
- return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
10204
- }, [item]);
10205
- const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
10206
- const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
10207
- const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
10208
- const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
10209
- const onSubmit = form.handleSubmit(async (data) => {
10210
- if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
10211
- setEditing(false);
10212
- return;
10125
+ promotion.id
10126
+ );
10127
+ };
10128
+ function getDisplayValue(promotion) {
10129
+ var _a, _b, _c, _d;
10130
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
10131
+ if (!value) {
10132
+ return null;
10133
+ }
10134
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
10135
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
10136
+ if (!currency) {
10137
+ return null;
10213
10138
  }
10214
- if (!actionId) {
10215
- await updateOriginalItem(
10216
- {
10217
- item_id: item.id,
10218
- quantity: data.quantity,
10219
- unit_price: convertNumber(data.unit_price)
10220
- },
10221
- {
10222
- onSuccess: () => {
10223
- setEditing(false);
10224
- },
10225
- onError: (e) => {
10226
- ui.toast.error(e.message);
10227
- }
10139
+ return getLocaleAmount(value, currency);
10140
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
10141
+ return formatPercentage(value);
10142
+ }
10143
+ return null;
10144
+ }
10145
+ const formatter = new Intl.NumberFormat([], {
10146
+ style: "percent",
10147
+ minimumFractionDigits: 2
10148
+ });
10149
+ const formatPercentage = (value, isPercentageValue = false) => {
10150
+ let val = value || 0;
10151
+ if (!isPercentageValue) {
10152
+ val = val / 100;
10153
+ }
10154
+ return formatter.format(val);
10155
+ };
10156
+ function getPromotionIds(items, shippingMethods) {
10157
+ const promotionIds = /* @__PURE__ */ new Set();
10158
+ for (const item of items) {
10159
+ if (item.adjustments) {
10160
+ for (const adjustment of item.adjustments) {
10161
+ if (adjustment.promotion_id) {
10162
+ promotionIds.add(adjustment.promotion_id);
10228
10163
  }
10229
- );
10230
- return;
10164
+ }
10231
10165
  }
10232
- if (data.quantity === 0) {
10233
- await removeActionItem(actionId, {
10234
- onSuccess: () => {
10235
- setEditing(false);
10236
- },
10237
- onError: (e) => {
10238
- ui.toast.error(e.message);
10166
+ }
10167
+ for (const shippingMethod of shippingMethods) {
10168
+ if (shippingMethod.adjustments) {
10169
+ for (const adjustment of shippingMethod.adjustments) {
10170
+ if (adjustment.promotion_id) {
10171
+ promotionIds.add(adjustment.promotion_id);
10239
10172
  }
10240
- });
10241
- return;
10173
+ }
10242
10174
  }
10243
- await updateActionItem(
10175
+ }
10176
+ return Array.from(promotionIds);
10177
+ }
10178
+ const SalesChannel = () => {
10179
+ const { id } = reactRouterDom.useParams();
10180
+ const { draft_order, isPending, isError, error } = useDraftOrder(
10181
+ id,
10182
+ {
10183
+ fields: "+sales_channel_id"
10184
+ },
10185
+ {
10186
+ enabled: !!id
10187
+ }
10188
+ );
10189
+ if (isError) {
10190
+ throw error;
10191
+ }
10192
+ const ISrEADY = !!draft_order && !isPending;
10193
+ return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10194
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10195
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
10196
+ /* @__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" }) })
10197
+ ] }),
10198
+ ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
10199
+ ] });
10200
+ };
10201
+ const SalesChannelForm = ({ order }) => {
10202
+ const form = reactHookForm.useForm({
10203
+ defaultValues: {
10204
+ sales_channel_id: order.sales_channel_id || ""
10205
+ },
10206
+ resolver: zod.zodResolver(schema$2)
10207
+ });
10208
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
10209
+ const { handleSuccess } = useRouteModal();
10210
+ const onSubmit = form.handleSubmit(async (data) => {
10211
+ await mutateAsync(
10244
10212
  {
10245
- action_id: actionId,
10246
- quantity: data.quantity,
10247
- unit_price: convertNumber(data.unit_price)
10213
+ sales_channel_id: data.sales_channel_id
10248
10214
  },
10249
10215
  {
10250
10216
  onSuccess: () => {
10251
- setEditing(false);
10217
+ ui.toast.success("Sales channel updated");
10218
+ handleSuccess();
10252
10219
  },
10253
- onError: (e) => {
10254
- ui.toast.error(e.message);
10220
+ onError: (error) => {
10221
+ ui.toast.error(error.message);
10255
10222
  }
10256
10223
  }
10257
10224
  );
10258
10225
  });
10259
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
10260
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
10261
- /* @__PURE__ */ jsxRuntime.jsx(
10262
- Thumbnail,
10263
- {
10264
- thumbnail: item.thumbnail,
10265
- alt: item.title ?? void 0
10266
- }
10267
- ),
10268
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10269
- Form$2.Field,
10270
- {
10271
- control: form.control,
10272
- name: "title",
10273
- render: ({ field }) => {
10274
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
10275
- }
10276
- }
10277
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
10278
- ] }),
10279
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10280
- Form$2.Field,
10281
- {
10282
- control: form.control,
10283
- name: "quantity",
10284
- render: ({ field }) => {
10285
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
10286
- }
10287
- }
10288
- ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
10289
- editing ? /* @__PURE__ */ jsxRuntime.jsx(
10290
- Form$2.Field,
10291
- {
10292
- control: form.control,
10293
- name: "unit_price",
10294
- render: ({ field: { onChange, ...field } }) => {
10295
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10296
- ui.CurrencyInput,
10226
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10227
+ KeyboundForm,
10228
+ {
10229
+ className: "flex flex-1 flex-col overflow-hidden",
10230
+ onSubmit,
10231
+ children: [
10232
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
10233
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
10234
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10235
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10236
+ ] }) })
10237
+ ]
10238
+ }
10239
+ ) });
10240
+ };
10241
+ const SalesChannelField = ({ control, order }) => {
10242
+ const salesChannels = useComboboxData({
10243
+ queryFn: async (params) => {
10244
+ return await sdk.admin.salesChannel.list(params);
10245
+ },
10246
+ queryKey: ["sales-channels"],
10247
+ getOptions: (data) => {
10248
+ return data.sales_channels.map((salesChannel) => ({
10249
+ label: salesChannel.name,
10250
+ value: salesChannel.id
10251
+ }));
10252
+ },
10253
+ defaultValue: order.sales_channel_id || void 0
10254
+ });
10255
+ return /* @__PURE__ */ jsxRuntime.jsx(
10256
+ Form$2.Field,
10257
+ {
10258
+ control,
10259
+ name: "sales_channel_id",
10260
+ render: ({ field }) => {
10261
+ return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
10262
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
10263
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10264
+ Combobox,
10297
10265
  {
10298
- ...field,
10299
- symbol: getNativeSymbol(currencyCode),
10300
- code: currencyCode,
10301
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
10266
+ options: salesChannels.options,
10267
+ fetchNextPage: salesChannels.fetchNextPage,
10268
+ isFetchingNextPage: salesChannels.isFetchingNextPage,
10269
+ searchValue: salesChannels.searchValue,
10270
+ onSearchValueChange: salesChannels.onSearchValueChange,
10271
+ placeholder: "Select sales channel",
10272
+ ...field
10302
10273
  }
10303
- ) }) });
10304
- }
10274
+ ) }),
10275
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10276
+ ] });
10305
10277
  }
10306
- ) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
10307
- /* @__PURE__ */ jsxRuntime.jsx(
10308
- ui.IconButton,
10309
- {
10310
- type: "button",
10311
- size: "small",
10312
- onClick: editing ? onSubmit : () => {
10313
- setEditing(true);
10314
- },
10315
- disabled: isPending,
10316
- children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
10317
- }
10318
- )
10319
- ] }) }) });
10278
+ }
10279
+ );
10320
10280
  };
10321
- const StackedModalTrigger$1 = ({
10322
- type,
10323
- setModalContent
10324
- }) => {
10325
- const { setIsOpen } = useStackedModal();
10326
- const onClick = React.useCallback(() => {
10327
- setModalContent(type);
10328
- setIsOpen(STACKED_MODAL_ID, true);
10329
- }, [setModalContent, setIsOpen, type]);
10330
- return /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Item, { onClick, children: type === "add-items" ? "Add items" : "Add custom item" }) });
10281
+ const schema$2 = objectType({
10282
+ sales_channel_id: stringType().min(1)
10283
+ });
10284
+ function convertNumber(value) {
10285
+ return typeof value === "string" ? Number(value.replace(",", ".")) : value;
10286
+ }
10287
+ const STACKED_FOCUS_MODAL_ID = "shipping-form";
10288
+ const Shipping = () => {
10289
+ var _a;
10290
+ const { id } = reactRouterDom.useParams();
10291
+ const { order, isPending, isError, error } = useOrder(id, {
10292
+ fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
10293
+ });
10294
+ const {
10295
+ order: preview,
10296
+ isPending: isPreviewPending,
10297
+ isError: isPreviewError,
10298
+ error: previewError
10299
+ } = useOrderPreview(id);
10300
+ useInitiateOrderEdit({ preview });
10301
+ const { onCancel } = useCancelOrderEdit({ preview });
10302
+ if (isError) {
10303
+ throw error;
10304
+ }
10305
+ if (isPreviewError) {
10306
+ throw previewError;
10307
+ }
10308
+ const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
10309
+ const isReady = preview && !isPreviewPending && order && !isPending;
10310
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
10311
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10312
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.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 py-16 px-6", children: [
10313
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
10314
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
10315
+ ] }) }) }),
10316
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
10317
+ ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10318
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
10319
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
10320
+ ] }) });
10331
10321
  };
10332
- const VARIANT_PREFIX = "items";
10333
- const LIMIT = 50;
10334
- const ExistingItemsForm = ({ orderId, items }) => {
10322
+ const ShippingForm = ({ preview, order }) => {
10323
+ var _a;
10335
10324
  const { setIsOpen } = useStackedModal();
10336
- const [rowSelection, setRowSelection] = React.useState(
10337
- items.reduce((acc, item) => {
10338
- acc[item.variant_id] = true;
10339
- return acc;
10340
- }, {})
10341
- );
10342
- React.useEffect(() => {
10343
- setRowSelection(
10344
- items.reduce((acc, item) => {
10345
- if (item.variant_id) {
10346
- acc[item.variant_id] = true;
10347
- }
10348
- return acc;
10349
- }, {})
10350
- );
10351
- }, [items]);
10352
- const { q, order, offset } = useQueryParams(
10353
- ["q", "order", "offset"],
10354
- VARIANT_PREFIX
10355
- );
10356
- const { variants, count, isPending, isError, error } = useProductVariants(
10325
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
10326
+ const [data, setData] = React.useState(null);
10327
+ const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
10328
+ const { shipping_options } = useShippingOptions(
10357
10329
  {
10358
- q,
10359
- order,
10360
- offset: offset ? parseInt(offset) : void 0,
10361
- limit: LIMIT
10330
+ id: appliedShippingOptionIds,
10331
+ fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
10362
10332
  },
10363
10333
  {
10364
- placeholderData: reactQuery.keepPreviousData
10334
+ enabled: appliedShippingOptionIds.length > 0
10365
10335
  }
10366
10336
  );
10367
- const columns = useColumns();
10368
- const { mutateAsync } = useDraftOrderAddItems(orderId);
10337
+ const uniqueShippingProfiles = React.useMemo(() => {
10338
+ const profiles = /* @__PURE__ */ new Map();
10339
+ getUniqueShippingProfiles(order.items).forEach((profile) => {
10340
+ profiles.set(profile.id, profile);
10341
+ });
10342
+ shipping_options == null ? void 0 : shipping_options.forEach((option) => {
10343
+ profiles.set(option.shipping_profile_id, option.shipping_profile);
10344
+ });
10345
+ return Array.from(profiles.values());
10346
+ }, [order.items, shipping_options]);
10347
+ const { handleSuccess } = useRouteModal();
10348
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10349
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
10350
+ const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
10351
+ const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
10369
10352
  const onSubmit = async () => {
10370
- const ids = Object.keys(rowSelection).filter(
10371
- (id) => !items.find((i) => i.variant_id === id)
10372
- );
10373
- await mutateAsync(
10374
- {
10375
- items: ids.map((id) => ({
10376
- variant_id: id,
10377
- quantity: 1
10378
- }))
10353
+ setIsSubmitting(true);
10354
+ let requestSucceeded = false;
10355
+ await requestOrderEdit(void 0, {
10356
+ onError: (e) => {
10357
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
10379
10358
  },
10380
- {
10381
- onSuccess: () => {
10382
- setRowSelection({});
10383
- setIsOpen(STACKED_MODAL_ID, false);
10384
- },
10385
- onError: (e) => {
10386
- ui.toast.error(e.message);
10387
- }
10359
+ onSuccess: () => {
10360
+ requestSucceeded = true;
10388
10361
  }
10389
- );
10362
+ });
10363
+ if (!requestSucceeded) {
10364
+ setIsSubmitting(false);
10365
+ return;
10366
+ }
10367
+ await confirmOrderEdit(void 0, {
10368
+ onError: (e) => {
10369
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
10370
+ },
10371
+ onSuccess: () => {
10372
+ handleSuccess();
10373
+ },
10374
+ onSettled: () => {
10375
+ setIsSubmitting(false);
10376
+ }
10377
+ });
10390
10378
  };
10391
- if (isError) {
10392
- throw error;
10393
- }
10394
- return /* @__PURE__ */ jsxRuntime.jsxs(
10395
- StackedFocusModal.Content,
10396
- {
10397
- onOpenAutoFocus: (e) => {
10398
- e.preventDefault();
10399
- const searchInput = document.querySelector(
10400
- "[data-modal-id='modal-search-input']"
10401
- );
10402
- if (searchInput) {
10403
- searchInput.focus();
10379
+ const onKeydown = React.useCallback(
10380
+ (e) => {
10381
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
10382
+ if (data || isSubmitting) {
10383
+ return;
10404
10384
  }
10405
- },
10406
- children: [
10407
- /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
10408
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
10409
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
10410
- ] }),
10411
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
10412
- DataTable,
10413
- {
10414
- data: variants,
10415
- columns,
10416
- isLoading: isPending,
10417
- getRowId: (row) => row.id,
10418
- rowCount: count,
10419
- prefix: VARIANT_PREFIX,
10420
- layout: "fill",
10421
- rowSelection: {
10422
- state: rowSelection,
10423
- onRowSelectionChange: setRowSelection,
10424
- enableRowSelection: (row) => {
10425
- return !items.find((i) => i.variant_id === row.original.id);
10426
- }
10427
- },
10428
- autoFocusSearch: true
10429
- }
10430
- ) }),
10431
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10432
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10433
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
10434
- ] }) })
10435
- ]
10436
- }
10385
+ onSubmit();
10386
+ }
10387
+ },
10388
+ [data, isSubmitting, onSubmit]
10437
10389
  );
10438
- };
10439
- const columnHelper = ui.createDataTableColumnHelper();
10440
- const useColumns = () => {
10441
- return React.useMemo(() => {
10442
- return [
10443
- columnHelper.select(),
10444
- columnHelper.accessor("product.title", {
10445
- header: "Product",
10446
- cell: ({ row }) => {
10447
- var _a, _b, _c;
10448
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
10390
+ React.useEffect(() => {
10391
+ document.addEventListener("keydown", onKeydown);
10392
+ return () => {
10393
+ document.removeEventListener("keydown", onKeydown);
10394
+ };
10395
+ }, [onKeydown]);
10396
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
10397
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
10398
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
10399
+ /* @__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 py-16 px-6", children: [
10400
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10401
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
10402
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
10403
+ ] }),
10404
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10405
+ /* @__PURE__ */ jsxRuntime.jsx(radixUi.Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
10406
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
10449
10407
  /* @__PURE__ */ jsxRuntime.jsx(
10450
- Thumbnail,
10408
+ ui.Text,
10451
10409
  {
10452
- thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
10453
- alt: (_b = row.original.product) == null ? void 0 : _b.title
10454
- }
10455
- ),
10456
- /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
10457
- ] });
10458
- },
10459
- enableSorting: true
10460
- }),
10461
- columnHelper.accessor("title", {
10462
- header: "Variant",
10463
- enableSorting: true
10464
- }),
10465
- columnHelper.accessor("sku", {
10466
- header: "SKU",
10467
- cell: ({ getValue }) => {
10468
- return getValue() ?? "-";
10469
- },
10470
- enableSorting: true
10471
- }),
10472
- columnHelper.accessor("updated_at", {
10473
- header: "Updated",
10474
- cell: ({ getValue }) => {
10475
- return /* @__PURE__ */ jsxRuntime.jsx(
10476
- ui.Tooltip,
10477
- {
10478
- content: getFullDate({ date: getValue(), includeTime: true }),
10479
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10480
- }
10481
- );
10482
- },
10483
- enableSorting: true,
10484
- sortAscLabel: "Oldest first",
10485
- sortDescLabel: "Newest first"
10486
- }),
10487
- columnHelper.accessor("created_at", {
10488
- header: "Created",
10489
- cell: ({ getValue }) => {
10490
- return /* @__PURE__ */ jsxRuntime.jsx(
10491
- ui.Tooltip,
10492
- {
10493
- content: getFullDate({ date: getValue(), includeTime: true }),
10494
- children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
10495
- }
10496
- );
10497
- },
10498
- enableSorting: true,
10499
- sortAscLabel: "Oldest first",
10500
- sortDescLabel: "Newest first"
10501
- })
10502
- ];
10503
- }, []);
10504
- };
10505
- const CustomItemForm = ({ orderId, currencyCode }) => {
10506
- const { setIsOpen } = useStackedModal();
10507
- const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
10508
- const form = reactHookForm.useForm({
10509
- defaultValues: {
10510
- title: "",
10511
- quantity: 1,
10512
- unit_price: ""
10513
- },
10514
- resolver: zod.zodResolver(customItemSchema)
10515
- });
10516
- const onSubmit = form.handleSubmit(async (data) => {
10517
- await addItems(
10518
- {
10519
- items: [
10520
- {
10521
- title: data.title,
10522
- quantity: data.quantity,
10523
- unit_price: convertNumber(data.unit_price)
10524
- }
10525
- ]
10526
- },
10527
- {
10528
- onSuccess: () => {
10529
- setIsOpen(STACKED_MODAL_ID, false);
10530
- },
10531
- onError: (e) => {
10532
- ui.toast.error(e.message);
10533
- }
10534
- }
10535
- );
10536
- });
10537
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
10538
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
10539
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 flex-col items-center overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full max-w-[720px] flex-col gap-y-6 px-2 py-16", children: [
10540
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10541
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
10542
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add a custom item to the order. This will add a new line item that is not associated with an existing product." }) })
10543
- ] }),
10544
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10545
- /* @__PURE__ */ jsxRuntime.jsx(
10546
- Form$2.Field,
10547
- {
10548
- control: form.control,
10549
- name: "title",
10550
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10551
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10552
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
10553
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
10554
- ] }),
10555
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10556
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
10557
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10558
- ] })
10559
- ] }) })
10560
- }
10561
- ),
10562
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10563
- /* @__PURE__ */ jsxRuntime.jsx(
10564
- Form$2.Field,
10565
- {
10566
- control: form.control,
10567
- name: "unit_price",
10568
- render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10569
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10570
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
10571
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
10572
- ] }),
10573
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10574
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10575
- ui.CurrencyInput,
10576
- {
10577
- symbol: getNativeSymbol(currencyCode),
10578
- code: currencyCode,
10579
- onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
10580
- ...field
10581
- }
10582
- ) }),
10583
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10584
- ] })
10585
- ] }) })
10586
- }
10587
- ),
10588
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
10589
- /* @__PURE__ */ jsxRuntime.jsx(
10590
- Form$2.Field,
10591
- {
10592
- control: form.control,
10593
- name: "quantity",
10594
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
10595
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
10596
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
10597
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
10598
- ] }),
10599
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
10600
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field, className: "w-full" }) }) }),
10601
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
10602
- ] })
10603
- ] }) })
10604
- }
10605
- )
10606
- ] }) }) }),
10607
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10608
- /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10609
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
10610
- ] }) })
10611
- ] }) }) });
10612
- };
10613
- const customItemSchema = objectType({
10614
- title: stringType().min(1),
10615
- quantity: numberType(),
10616
- unit_price: unionType([numberType(), stringType()])
10617
- });
10618
- const InlineTip = React.forwardRef(
10619
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10620
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10621
- return /* @__PURE__ */ jsxRuntime.jsxs(
10622
- "div",
10623
- {
10624
- ref,
10625
- className: ui.clx(
10626
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10627
- className
10628
- ),
10629
- ...props,
10630
- children: [
10631
- /* @__PURE__ */ jsxRuntime.jsx(
10632
- "div",
10633
- {
10634
- role: "presentation",
10635
- className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10636
- "bg-ui-tag-orange-icon": variant === "warning"
10637
- })
10638
- }
10639
- ),
10640
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
10641
- /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10642
- labelValue,
10643
- ":"
10644
- ] }),
10645
- " ",
10646
- children
10647
- ] })
10648
- ]
10649
- }
10650
- );
10651
- }
10652
- );
10653
- InlineTip.displayName = "InlineTip";
10654
- const MetadataFieldSchema = objectType({
10655
- key: stringType(),
10656
- disabled: booleanType().optional(),
10657
- value: anyType()
10658
- });
10659
- const MetadataSchema = objectType({
10660
- metadata: arrayType(MetadataFieldSchema)
10661
- });
10662
- const Metadata = () => {
10663
- const { id } = reactRouterDom.useParams();
10664
- const { order, isPending, isError, error } = useOrder(id, {
10665
- fields: "metadata"
10666
- });
10667
- if (isError) {
10668
- throw error;
10669
- }
10670
- const isReady = !isPending && !!order;
10671
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
10672
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
10673
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
10674
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10675
- ] }),
10676
- !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10677
- ] });
10678
- };
10679
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10680
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10681
- const MetadataForm = ({ orderId, metadata }) => {
10682
- const { handleSuccess } = useRouteModal();
10683
- const hasUneditableRows = getHasUneditableRows(metadata);
10684
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10685
- const form = reactHookForm.useForm({
10686
- defaultValues: {
10687
- metadata: getDefaultValues(metadata)
10688
- },
10689
- resolver: zod.zodResolver(MetadataSchema)
10690
- });
10691
- const handleSubmit = form.handleSubmit(async (data) => {
10692
- const parsedData = parseValues(data);
10693
- await mutateAsync(
10694
- {
10695
- metadata: parsedData
10696
- },
10697
- {
10698
- onSuccess: () => {
10699
- ui.toast.success("Metadata updated");
10700
- handleSuccess();
10701
- },
10702
- onError: (error) => {
10703
- ui.toast.error(error.message);
10704
- }
10705
- }
10706
- );
10707
- });
10708
- const { fields, insert, remove } = reactHookForm.useFieldArray({
10709
- control: form.control,
10710
- name: "metadata"
10711
- });
10712
- function deleteRow(index) {
10713
- remove(index);
10714
- if (fields.length === 1) {
10715
- insert(0, {
10716
- key: "",
10717
- value: "",
10718
- disabled: false
10719
- });
10720
- }
10721
- }
10722
- function insertRow(index, position) {
10723
- insert(index + (position === "above" ? 0 : 1), {
10724
- key: "",
10725
- value: "",
10726
- disabled: false
10727
- });
10728
- }
10729
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
10730
- KeyboundForm,
10731
- {
10732
- onSubmit: handleSubmit,
10733
- className: "flex flex-1 flex-col overflow-hidden",
10734
- children: [
10735
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10736
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10737
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10738
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
10739
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsxRuntime.jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
10740
- ] }),
10741
- fields.map((field, index) => {
10742
- const isDisabled = field.disabled || false;
10743
- let placeholder = "-";
10744
- if (typeof field.value === "object") {
10745
- placeholder = "{ ... }";
10746
- }
10747
- if (Array.isArray(field.value)) {
10748
- placeholder = "[ ... ]";
10749
- }
10750
- return /* @__PURE__ */ jsxRuntime.jsx(
10751
- ConditionalTooltip,
10752
- {
10753
- showTooltip: isDisabled,
10754
- content: "This row is disabled because it contains non-primitive data.",
10755
- children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
10756
- /* @__PURE__ */ jsxRuntime.jsxs(
10757
- "div",
10758
- {
10759
- className: ui.clx("grid grid-cols-2 divide-x", {
10760
- "overflow-hidden rounded-b-lg": index === fields.length - 1
10761
- }),
10762
- children: [
10763
- /* @__PURE__ */ jsxRuntime.jsx(
10764
- Form$2.Field,
10765
- {
10766
- control: form.control,
10767
- name: `metadata.${index}.key`,
10768
- render: ({ field: field2 }) => {
10769
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10770
- GridInput,
10771
- {
10772
- "aria-labelledby": METADATA_KEY_LABEL_ID,
10773
- ...field2,
10774
- disabled: isDisabled,
10775
- placeholder: "Key"
10776
- }
10777
- ) }) });
10778
- }
10779
- }
10780
- ),
10781
- /* @__PURE__ */ jsxRuntime.jsx(
10782
- Form$2.Field,
10783
- {
10784
- control: form.control,
10785
- name: `metadata.${index}.value`,
10786
- render: ({ field: { value, ...field2 } }) => {
10787
- return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
10788
- GridInput,
10789
- {
10790
- "aria-labelledby": METADATA_VALUE_LABEL_ID,
10791
- ...field2,
10792
- value: isDisabled ? placeholder : value,
10793
- disabled: isDisabled,
10794
- placeholder: "Value"
10795
- }
10796
- ) }) });
10797
- }
10798
- }
10799
- )
10800
- ]
10801
- }
10802
- ),
10803
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
10804
- /* @__PURE__ */ jsxRuntime.jsx(
10805
- ui.DropdownMenu.Trigger,
10806
- {
10807
- className: ui.clx(
10808
- "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
10809
- {
10810
- hidden: isDisabled
10811
- }
10812
- ),
10813
- disabled: isDisabled,
10814
- asChild: true,
10815
- children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
10816
- }
10817
- ),
10818
- /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
10819
- /* @__PURE__ */ jsxRuntime.jsxs(
10820
- ui.DropdownMenu.Item,
10821
- {
10822
- className: "gap-x-2",
10823
- onClick: () => insertRow(index, "above"),
10824
- children: [
10825
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
10826
- "Insert row above"
10827
- ]
10828
- }
10829
- ),
10830
- /* @__PURE__ */ jsxRuntime.jsxs(
10831
- ui.DropdownMenu.Item,
10832
- {
10833
- className: "gap-x-2",
10834
- onClick: () => insertRow(index, "below"),
10835
- children: [
10836
- /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
10837
- "Insert row below"
10838
- ]
10839
- }
10840
- ),
10841
- /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
10842
- /* @__PURE__ */ jsxRuntime.jsxs(
10843
- ui.DropdownMenu.Item,
10844
- {
10845
- className: "gap-x-2",
10846
- onClick: () => deleteRow(index),
10847
- children: [
10848
- /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
10849
- "Delete row"
10850
- ]
10851
- }
10852
- )
10853
- ] })
10854
- ] })
10855
- ] })
10856
- },
10857
- field.id
10858
- );
10859
- })
10860
- ] }),
10861
- hasUneditableRows && /* @__PURE__ */ jsxRuntime.jsx(InlineTip, { variant: "warning", label: "Some rows are disabled", children: "This object contains non-primitive metadata, such as arrays or objects, that can't be edited here. To edit the disabled rows, use the API directly." })
10862
- ] }),
10863
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10864
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
10865
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
10866
- ] }) })
10867
- ]
10868
- }
10869
- ) });
10870
- };
10871
- const GridInput = React.forwardRef(({ className, ...props }, ref) => {
10872
- return /* @__PURE__ */ jsxRuntime.jsx(
10873
- "input",
10874
- {
10875
- ref,
10876
- ...props,
10877
- autoComplete: "off",
10878
- className: ui.clx(
10879
- "txt-compact-small text-ui-fg-base placeholder:text-ui-fg-muted disabled:text-ui-fg-disabled disabled:bg-ui-bg-base bg-transparent px-2 py-1.5 outline-none",
10880
- className
10881
- )
10882
- }
10883
- );
10884
- });
10885
- GridInput.displayName = "MetadataForm.GridInput";
10886
- const PlaceholderInner = () => {
10887
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
10888
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
10889
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
10890
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
10891
- /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
10892
- ] }) })
10893
- ] });
10894
- };
10895
- const EDITABLE_TYPES = ["string", "number", "boolean"];
10896
- function getDefaultValues(metadata) {
10897
- if (!metadata || !Object.keys(metadata).length) {
10898
- return [
10899
- {
10900
- key: "",
10901
- value: "",
10902
- disabled: false
10903
- }
10904
- ];
10905
- }
10906
- return Object.entries(metadata).map(([key, value]) => {
10907
- if (!EDITABLE_TYPES.includes(typeof value)) {
10908
- return {
10909
- key,
10910
- value,
10911
- disabled: true
10912
- };
10913
- }
10914
- let stringValue = value;
10915
- if (typeof value !== "string") {
10916
- stringValue = JSON.stringify(value);
10917
- }
10918
- return {
10919
- key,
10920
- value: stringValue,
10921
- original_key: key
10922
- };
10923
- });
10924
- }
10925
- function parseValues(values) {
10926
- const metadata = values.metadata;
10927
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10928
- if (isEmpty) {
10929
- return null;
10930
- }
10931
- const update = {};
10932
- metadata.forEach((field) => {
10933
- let key = field.key;
10934
- let value = field.value;
10935
- const disabled = field.disabled;
10936
- if (!key || !value) {
10937
- return;
10938
- }
10939
- if (disabled) {
10940
- update[key] = value;
10941
- return;
10942
- }
10943
- key = key.trim();
10944
- value = value.trim();
10945
- if (value === "true") {
10946
- update[key] = true;
10947
- } else if (value === "false") {
10948
- update[key] = false;
10949
- } else {
10950
- const parsedNumber = parseFloat(value);
10951
- if (!isNaN(parsedNumber)) {
10952
- update[key] = parsedNumber;
10953
- } else {
10954
- update[key] = value;
10955
- }
10956
- }
10957
- });
10958
- return update;
10959
- }
10960
- function getHasUneditableRows(metadata) {
10961
- if (!metadata) {
10962
- return false;
10963
- }
10964
- return Object.values(metadata).some(
10965
- (value) => !EDITABLE_TYPES.includes(typeof value)
10966
- );
10967
- }
10968
- const PROMOTION_QUERY_KEY = "promotions";
10969
- const promotionsQueryKeys = {
10970
- list: (query2) => [
10971
- PROMOTION_QUERY_KEY,
10972
- query2 ? query2 : void 0
10973
- ],
10974
- detail: (id, query2) => [
10975
- PROMOTION_QUERY_KEY,
10976
- id,
10977
- query2 ? query2 : void 0
10978
- ]
10979
- };
10980
- const usePromotions = (query2, options) => {
10981
- const { data, ...rest } = reactQuery.useQuery({
10982
- queryKey: promotionsQueryKeys.list(query2),
10983
- queryFn: async () => sdk.admin.promotion.list(query2),
10984
- ...options
10985
- });
10986
- return { ...data, ...rest };
10987
- };
10988
- const Promotions = () => {
10989
- const { id } = reactRouterDom.useParams();
10990
- const {
10991
- order: preview,
10992
- isError: isPreviewError,
10993
- error: previewError
10994
- } = useOrderPreview(id, void 0);
10995
- useInitiateOrderEdit({ preview });
10996
- const { onCancel } = useCancelOrderEdit({ preview });
10997
- if (isPreviewError) {
10998
- throw previewError;
10999
- }
11000
- const isReady = !!preview;
11001
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { onClose: onCancel, children: [
11002
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Promotions" }) }) }),
11003
- isReady && /* @__PURE__ */ jsxRuntime.jsx(PromotionForm, { preview })
11004
- ] });
11005
- };
11006
- const PromotionForm = ({ preview }) => {
11007
- const { items, shipping_methods } = preview;
11008
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11009
- const [comboboxValue, setComboboxValue] = React.useState("");
11010
- const { handleSuccess } = useRouteModal();
11011
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11012
- const promoIds = getPromotionIds(items, shipping_methods);
11013
- const { promotions, isPending, isError, error } = usePromotions(
11014
- {
11015
- id: promoIds
11016
- },
11017
- {
11018
- enabled: !!promoIds.length
11019
- }
11020
- );
11021
- const comboboxData = useComboboxData({
11022
- queryKey: ["promotions", "combobox", promoIds],
11023
- queryFn: async (params) => {
11024
- return await sdk.admin.promotion.list({
11025
- ...params,
11026
- id: {
11027
- $nin: promoIds
11028
- }
11029
- });
11030
- },
11031
- getOptions: (data) => {
11032
- return data.promotions.map((promotion) => ({
11033
- label: promotion.code,
11034
- value: promotion.code
11035
- }));
11036
- }
11037
- });
11038
- const add = async (value) => {
11039
- if (!value) {
11040
- return;
11041
- }
11042
- addPromotions(
11043
- {
11044
- promo_codes: [value]
11045
- },
11046
- {
11047
- onError: (e) => {
11048
- ui.toast.error(e.message);
11049
- comboboxData.onSearchValueChange("");
11050
- setComboboxValue("");
11051
- },
11052
- onSuccess: () => {
11053
- comboboxData.onSearchValueChange("");
11054
- setComboboxValue("");
11055
- }
11056
- }
11057
- );
11058
- };
11059
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11060
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11061
- const onSubmit = async () => {
11062
- setIsSubmitting(true);
11063
- let requestSucceeded = false;
11064
- await requestOrderEdit(void 0, {
11065
- onError: (e) => {
11066
- ui.toast.error(e.message);
11067
- },
11068
- onSuccess: () => {
11069
- requestSucceeded = true;
11070
- }
11071
- });
11072
- if (!requestSucceeded) {
11073
- setIsSubmitting(false);
11074
- return;
11075
- }
11076
- await confirmOrderEdit(void 0, {
11077
- onError: (e) => {
11078
- ui.toast.error(e.message);
11079
- },
11080
- onSuccess: () => {
11081
- handleSuccess();
11082
- },
11083
- onSettled: () => {
11084
- setIsSubmitting(false);
11085
- }
11086
- });
11087
- };
11088
- if (isError) {
11089
- throw error;
11090
- }
11091
- return /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11092
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-4", children: [
11093
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-3", children: [
11094
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
11095
- /* @__PURE__ */ jsxRuntime.jsx(ui.Label, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11096
- /* @__PURE__ */ jsxRuntime.jsx(ui.Hint, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11097
- ] }),
11098
- /* @__PURE__ */ jsxRuntime.jsx(
11099
- Combobox,
11100
- {
11101
- id: "promotion-combobox",
11102
- "aria-describedby": "promotion-combobox-hint",
11103
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11104
- fetchNextPage: comboboxData.fetchNextPage,
11105
- options: comboboxData.options,
11106
- onSearchValueChange: comboboxData.onSearchValueChange,
11107
- searchValue: comboboxData.searchValue,
11108
- disabled: comboboxData.disabled || isAddingPromotions,
11109
- onChange: add,
11110
- value: comboboxValue
11111
- }
11112
- )
11113
- ] }),
11114
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11115
- /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsxRuntime.jsx(
11116
- PromotionItem,
11117
- {
11118
- promotion,
11119
- orderId: preview.id,
11120
- isLoading: isPending
11121
- },
11122
- promotion.id
11123
- )) })
11124
- ] }) }),
11125
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11126
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11127
- /* @__PURE__ */ jsxRuntime.jsx(
11128
- ui.Button,
11129
- {
11130
- size: "small",
11131
- type: "submit",
11132
- isLoading: isSubmitting || isAddingPromotions,
11133
- children: "Save"
11134
- }
11135
- )
11136
- ] }) })
11137
- ] });
11138
- };
11139
- const PromotionItem = ({
11140
- promotion,
11141
- orderId,
11142
- isLoading
11143
- }) => {
11144
- var _a;
11145
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11146
- const onRemove = async () => {
11147
- removePromotions(
11148
- {
11149
- promo_codes: [promotion.code]
11150
- },
11151
- {
11152
- onError: (e) => {
11153
- ui.toast.error(e.message);
11154
- }
11155
- }
11156
- );
11157
- };
11158
- const displayValue = getDisplayValue(promotion);
11159
- return /* @__PURE__ */ jsxRuntime.jsxs(
11160
- "div",
11161
- {
11162
- className: ui.clx(
11163
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11164
- {
11165
- "animate-pulse": isLoading
11166
- }
11167
- ),
11168
- children: [
11169
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11170
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11171
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11172
- displayValue && /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-1.5", children: [
11173
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: displayValue }),
11174
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", children: "·" })
11175
- ] }),
11176
- /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11177
- ] })
11178
- ] }),
11179
- /* @__PURE__ */ jsxRuntime.jsx(
11180
- ui.IconButton,
11181
- {
11182
- size: "small",
11183
- type: "button",
11184
- variant: "transparent",
11185
- onClick: onRemove,
11186
- isLoading: isPending || isLoading,
11187
- children: /* @__PURE__ */ jsxRuntime.jsx(icons.XMark, {})
11188
- }
11189
- )
11190
- ]
11191
- },
11192
- promotion.id
11193
- );
11194
- };
11195
- function getDisplayValue(promotion) {
11196
- var _a, _b, _c, _d;
11197
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11198
- if (!value) {
11199
- return null;
11200
- }
11201
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11202
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11203
- if (!currency) {
11204
- return null;
11205
- }
11206
- return getLocaleAmount(value, currency);
11207
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11208
- return formatPercentage(value);
11209
- }
11210
- return null;
11211
- }
11212
- const formatter = new Intl.NumberFormat([], {
11213
- style: "percent",
11214
- minimumFractionDigits: 2
11215
- });
11216
- const formatPercentage = (value, isPercentageValue = false) => {
11217
- let val = value || 0;
11218
- if (!isPercentageValue) {
11219
- val = val / 100;
11220
- }
11221
- return formatter.format(val);
11222
- };
11223
- function getPromotionIds(items, shippingMethods) {
11224
- const promotionIds = /* @__PURE__ */ new Set();
11225
- for (const item of items) {
11226
- if (item.adjustments) {
11227
- for (const adjustment of item.adjustments) {
11228
- if (adjustment.promotion_id) {
11229
- promotionIds.add(adjustment.promotion_id);
11230
- }
11231
- }
11232
- }
11233
- }
11234
- for (const shippingMethod of shippingMethods) {
11235
- if (shippingMethod.adjustments) {
11236
- for (const adjustment of shippingMethod.adjustments) {
11237
- if (adjustment.promotion_id) {
11238
- promotionIds.add(adjustment.promotion_id);
11239
- }
11240
- }
11241
- }
11242
- }
11243
- return Array.from(promotionIds);
11244
- }
11245
- const SalesChannel = () => {
11246
- const { id } = reactRouterDom.useParams();
11247
- const { draft_order, isPending, isError, error } = useDraftOrder(
11248
- id,
11249
- {
11250
- fields: "+sales_channel_id"
11251
- },
11252
- {
11253
- enabled: !!id
11254
- }
11255
- );
11256
- if (isError) {
11257
- throw error;
11258
- }
11259
- const ISrEADY = !!draft_order && !isPending;
11260
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
11261
- /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
11262
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Sales Channel" }) }),
11263
- /* @__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" }) })
11264
- ] }),
11265
- ISrEADY && /* @__PURE__ */ jsxRuntime.jsx(SalesChannelForm, { order: draft_order })
11266
- ] });
11267
- };
11268
- const SalesChannelForm = ({ order }) => {
11269
- const form = reactHookForm.useForm({
11270
- defaultValues: {
11271
- sales_channel_id: order.sales_channel_id || ""
11272
- },
11273
- resolver: zod.zodResolver(schema$4)
11274
- });
11275
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11276
- const { handleSuccess } = useRouteModal();
11277
- const onSubmit = form.handleSubmit(async (data) => {
11278
- await mutateAsync(
11279
- {
11280
- sales_channel_id: data.sales_channel_id
11281
- },
11282
- {
11283
- onSuccess: () => {
11284
- ui.toast.success("Sales channel updated");
11285
- handleSuccess();
11286
- },
11287
- onError: (error) => {
11288
- ui.toast.error(error.message);
11289
- }
11290
- }
11291
- );
11292
- });
11293
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
11294
- KeyboundForm,
11295
- {
11296
- className: "flex flex-1 flex-col overflow-hidden",
11297
- onSubmit,
11298
- children: [
11299
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsx(SalesChannelField, { control: form.control, order }) }),
11300
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
11301
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11302
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
11303
- ] }) })
11304
- ]
11305
- }
11306
- ) });
11307
- };
11308
- const SalesChannelField = ({ control, order }) => {
11309
- const salesChannels = useComboboxData({
11310
- queryFn: async (params) => {
11311
- return await sdk.admin.salesChannel.list(params);
11312
- },
11313
- queryKey: ["sales-channels"],
11314
- getOptions: (data) => {
11315
- return data.sales_channels.map((salesChannel) => ({
11316
- label: salesChannel.name,
11317
- value: salesChannel.id
11318
- }));
11319
- },
11320
- defaultValue: order.sales_channel_id || void 0
11321
- });
11322
- return /* @__PURE__ */ jsxRuntime.jsx(
11323
- Form$2.Field,
11324
- {
11325
- control,
11326
- name: "sales_channel_id",
11327
- render: ({ field }) => {
11328
- return /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
11329
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Sales Channel" }),
11330
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
11331
- Combobox,
11332
- {
11333
- options: salesChannels.options,
11334
- fetchNextPage: salesChannels.fetchNextPage,
11335
- isFetchingNextPage: salesChannels.isFetchingNextPage,
11336
- searchValue: salesChannels.searchValue,
11337
- onSearchValueChange: salesChannels.onSearchValueChange,
11338
- placeholder: "Select sales channel",
11339
- ...field
11340
- }
11341
- ) }),
11342
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
11343
- ] });
11344
- }
11345
- }
11346
- );
11347
- };
11348
- const schema$4 = objectType({
11349
- sales_channel_id: stringType().min(1)
11350
- });
11351
- const STACKED_FOCUS_MODAL_ID = "shipping-form";
11352
- const Shipping = () => {
11353
- var _a;
11354
- const { id } = reactRouterDom.useParams();
11355
- const { order, isPending, isError, error } = useOrder(id, {
11356
- fields: "+items.*,+items.variant.*,+items.variant.product.*,+items.variant.product.shipping_profile.*,+currency_code"
11357
- });
11358
- const {
11359
- order: preview,
11360
- isPending: isPreviewPending,
11361
- isError: isPreviewError,
11362
- error: previewError
11363
- } = useOrderPreview(id);
11364
- useInitiateOrderEdit({ preview });
11365
- const { onCancel } = useCancelOrderEdit({ preview });
11366
- if (isError) {
11367
- throw error;
11368
- }
11369
- if (isPreviewError) {
11370
- throw previewError;
11371
- }
11372
- const orderHasItems = (((_a = order == null ? void 0 : order.items) == null ? void 0 : _a.length) || 0) > 0;
11373
- const isReady = preview && !isPreviewPending && order && !isPending;
11374
- return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: !orderHasItems ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden ", children: [
11375
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11376
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.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 py-16 px-6", children: [
11377
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11378
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "This draft order currently has no items. Add items to the order before adding shipping." }) })
11379
- ] }) }) }),
11380
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }) })
11381
- ] }) : isReady ? /* @__PURE__ */ jsxRuntime.jsx(ShippingForm, { preview, order }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11382
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Shipping" }) }),
11383
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11384
- ] }) });
11385
- };
11386
- const ShippingForm = ({ preview, order }) => {
11387
- var _a;
11388
- const { setIsOpen } = useStackedModal();
11389
- const [isSubmitting, setIsSubmitting] = React.useState(false);
11390
- const [data, setData] = React.useState(null);
11391
- const appliedShippingOptionIds = (_a = preview.shipping_methods) == null ? void 0 : _a.map((method) => method.shipping_option_id).filter(Boolean);
11392
- const { shipping_options } = useShippingOptions(
11393
- {
11394
- id: appliedShippingOptionIds,
11395
- fields: "+service_zone.*,+service_zone.fulfillment_set.*,+service_zone.fulfillment_set.location.*"
11396
- },
11397
- {
11398
- enabled: appliedShippingOptionIds.length > 0
11399
- }
11400
- );
11401
- const uniqueShippingProfiles = React.useMemo(() => {
11402
- const profiles = /* @__PURE__ */ new Map();
11403
- getUniqueShippingProfiles(order.items).forEach((profile) => {
11404
- profiles.set(profile.id, profile);
11405
- });
11406
- shipping_options == null ? void 0 : shipping_options.forEach((option) => {
11407
- profiles.set(option.shipping_profile_id, option.shipping_profile);
11408
- });
11409
- return Array.from(profiles.values());
11410
- }, [order.items, shipping_options]);
11411
- const { handleSuccess } = useRouteModal();
11412
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11413
- const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11414
- const { mutateAsync: removeShippingMethod } = useDraftOrderRemoveShippingMethod(preview.id);
11415
- const { mutateAsync: removeActionShippingMethod } = useDraftOrderRemoveActionShippingMethod(preview.id);
11416
- const onSubmit = async () => {
11417
- setIsSubmitting(true);
11418
- let requestSucceeded = false;
11419
- await requestOrderEdit(void 0, {
11420
- onError: (e) => {
11421
- ui.toast.error(`Failed to request order edit: ${e.message}`);
11422
- },
11423
- onSuccess: () => {
11424
- requestSucceeded = true;
11425
- }
11426
- });
11427
- if (!requestSucceeded) {
11428
- setIsSubmitting(false);
11429
- return;
11430
- }
11431
- await confirmOrderEdit(void 0, {
11432
- onError: (e) => {
11433
- ui.toast.error(`Failed to confirm order edit: ${e.message}`);
11434
- },
11435
- onSuccess: () => {
11436
- handleSuccess();
11437
- },
11438
- onSettled: () => {
11439
- setIsSubmitting(false);
11440
- }
11441
- });
11442
- };
11443
- const onKeydown = React.useCallback(
11444
- (e) => {
11445
- if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11446
- if (data || isSubmitting) {
11447
- return;
11448
- }
11449
- onSubmit();
11450
- }
11451
- },
11452
- [data, isSubmitting, onSubmit]
11453
- );
11454
- React.useEffect(() => {
11455
- document.addEventListener("keydown", onKeydown);
11456
- return () => {
11457
- document.removeEventListener("keydown", onKeydown);
11458
- };
11459
- }, [onKeydown]);
11460
- return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11461
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11462
- /* @__PURE__ */ jsxRuntime.jsxs(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: [
11463
- /* @__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 py-16 px-6", children: [
11464
- /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11465
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Shipping" }) }),
11466
- /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose which shipping method(s) to use for the items in the order." }) })
11467
- ] }),
11468
- /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
11469
- /* @__PURE__ */ jsxRuntime.jsx(radixUi.Accordion.Root, { type: "multiple", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle rounded-xl shadow-elevation-card-rest", children: [
11470
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "px-4 py-2 flex items-center justify-between", children: [
11471
- /* @__PURE__ */ jsxRuntime.jsx(
11472
- ui.Text,
11473
- {
11474
- size: "xsmall",
11475
- weight: "plus",
11476
- className: "text-ui-fg-muted",
11477
- children: "Shipping profile"
10410
+ size: "xsmall",
10411
+ weight: "plus",
10412
+ className: "text-ui-fg-muted",
10413
+ children: "Shipping profile"
11478
10414
  }
11479
10415
  ),
11480
10416
  /* @__PURE__ */ jsxRuntime.jsx(
@@ -11655,7 +10591,7 @@ const ShippingForm = ({ preview, order }) => {
11655
10591
  ]
11656
10592
  }
11657
10593
  ) : /* @__PURE__ */ jsxRuntime.jsx(
11658
- StackedModalTrigger,
10594
+ StackedModalTrigger$1,
11659
10595
  {
11660
10596
  shippingProfileId: profile.id,
11661
10597
  shippingOption,
@@ -11766,7 +10702,7 @@ const ShippingForm = ({ preview, order }) => {
11766
10702
  ] }) })
11767
10703
  ] });
11768
10704
  };
11769
- const StackedModalTrigger = ({
10705
+ const StackedModalTrigger$1 = ({
11770
10706
  shippingProfileId,
11771
10707
  shippingOption,
11772
10708
  shippingMethod,
@@ -12187,7 +11123,7 @@ const ShippingAddressForm = ({ order }) => {
12187
11123
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12188
11124
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12189
11125
  },
12190
- resolver: zod.zodResolver(schema$3)
11126
+ resolver: zod.zodResolver(schema$1)
12191
11127
  });
12192
11128
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12193
11129
  const { handleSuccess } = useRouteModal();
@@ -12357,7 +11293,7 @@ const ShippingAddressForm = ({ order }) => {
12357
11293
  }
12358
11294
  ) });
12359
11295
  };
12360
- const schema$3 = addressSchema;
11296
+ const schema$1 = addressSchema;
12361
11297
  const TransferOwnership = () => {
12362
11298
  const { id } = reactRouterDom.useParams();
12363
11299
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12381,7 +11317,7 @@ const TransferOwnershipForm = ({ order }) => {
12381
11317
  defaultValues: {
12382
11318
  customer_id: order.customer_id || ""
12383
11319
  },
12384
- resolver: zod.zodResolver(schema$2)
11320
+ resolver: zod.zodResolver(schema)
12385
11321
  });
12386
11322
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12387
11323
  const { handleSuccess } = useRouteModal();
@@ -12707,137 +11643,1106 @@ const Illustration = () => {
12707
11643
  /* @__PURE__ */ jsxRuntime.jsx(
12708
11644
  "path",
12709
11645
  {
12710
- d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
12711
- fill: "#52525B"
12712
- }
11646
+ d: "M104.562 57.0927C103.13 56.265 100.792 56.2515 99.3501 57.0626C97.9081 57.8738 97.9004 59.2065 99.333 60.0343C100.766 60.862 103.103 60.8754 104.545 60.0643C105.987 59.2532 105.995 57.9204 104.562 57.0927ZM103.858 58.8972L100.815 59.1265C100.683 59.1367 100.55 59.1134 100.449 59.063C100.44 59.0585 100.432 59.0545 100.425 59.05C100.339 59.0005 100.29 58.9336 100.291 58.8637L100.294 58.1201C100.294 57.9752 100.501 57.8585 100.756 57.86C101.01 57.8615 101.217 57.98 101.216 58.1256L101.214 58.5669L103.732 58.3769C103.984 58.3578 104.217 58.4584 104.251 58.603C104.286 58.7468 104.11 58.8788 103.858 58.8977L103.858 58.8972Z",
11647
+ fill: "#52525B"
11648
+ }
11649
+ ),
11650
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11651
+ "path",
11652
+ {
11653
+ d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
11654
+ stroke: "#A1A1AA",
11655
+ strokeWidth: "1.5",
11656
+ strokeLinecap: "round",
11657
+ strokeLinejoin: "round"
11658
+ }
11659
+ ) }),
11660
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11661
+ "path",
11662
+ {
11663
+ d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
11664
+ stroke: "#A1A1AA",
11665
+ strokeWidth: "1.5",
11666
+ strokeLinecap: "round",
11667
+ strokeLinejoin: "round"
11668
+ }
11669
+ ) }),
11670
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11671
+ "path",
11672
+ {
11673
+ d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
11674
+ stroke: "#A1A1AA",
11675
+ strokeWidth: "1.5",
11676
+ strokeLinecap: "round",
11677
+ strokeLinejoin: "round"
11678
+ }
11679
+ ) }),
11680
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11681
+ "path",
11682
+ {
11683
+ d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
11684
+ stroke: "#A1A1AA",
11685
+ strokeWidth: "1.5",
11686
+ strokeLinecap: "round",
11687
+ strokeLinejoin: "round"
11688
+ }
11689
+ ) }),
11690
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11691
+ "path",
11692
+ {
11693
+ d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
11694
+ stroke: "#A1A1AA",
11695
+ strokeWidth: "1.5",
11696
+ strokeLinecap: "round",
11697
+ strokeLinejoin: "round"
11698
+ }
11699
+ ) }),
11700
+ /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
11701
+ "path",
11702
+ {
11703
+ d: "M146.894 101.198L139.51 101.155L139.486 105.365",
11704
+ stroke: "#A1A1AA",
11705
+ strokeWidth: "1.5",
11706
+ strokeLinecap: "round",
11707
+ strokeLinejoin: "round"
11708
+ }
11709
+ ) }),
11710
+ /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
11711
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11712
+ "rect",
11713
+ {
11714
+ width: "12",
11715
+ height: "12",
11716
+ fill: "white",
11717
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
11718
+ }
11719
+ ) }),
11720
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11721
+ "rect",
11722
+ {
11723
+ width: "12",
11724
+ height: "12",
11725
+ fill: "white",
11726
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
11727
+ }
11728
+ ) }),
11729
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11730
+ "rect",
11731
+ {
11732
+ width: "12",
11733
+ height: "12",
11734
+ fill: "white",
11735
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
11736
+ }
11737
+ ) }),
11738
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11739
+ "rect",
11740
+ {
11741
+ width: "12",
11742
+ height: "12",
11743
+ fill: "white",
11744
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
11745
+ }
11746
+ ) }),
11747
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11748
+ "rect",
11749
+ {
11750
+ width: "12",
11751
+ height: "12",
11752
+ fill: "white",
11753
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
11754
+ }
11755
+ ) }),
11756
+ /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
11757
+ "rect",
11758
+ {
11759
+ width: "12",
11760
+ height: "12",
11761
+ fill: "white",
11762
+ transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
11763
+ }
11764
+ ) })
11765
+ ] })
11766
+ ]
11767
+ }
11768
+ );
11769
+ };
11770
+ const schema = objectType({
11771
+ customer_id: stringType().min(1)
11772
+ });
11773
+ const NumberInput = React.forwardRef(
11774
+ ({
11775
+ value,
11776
+ onChange,
11777
+ size = "base",
11778
+ min = 0,
11779
+ max = 100,
11780
+ step = 1,
11781
+ className,
11782
+ disabled,
11783
+ ...props
11784
+ }, ref) => {
11785
+ const handleChange = (event) => {
11786
+ const newValue = event.target.value === "" ? min : Number(event.target.value);
11787
+ if (!isNaN(newValue) && (max === void 0 || newValue <= max) && (min === void 0 || newValue >= min)) {
11788
+ onChange(newValue);
11789
+ }
11790
+ };
11791
+ const handleIncrement = () => {
11792
+ const newValue = value + step;
11793
+ if (max === void 0 || newValue <= max) {
11794
+ onChange(newValue);
11795
+ }
11796
+ };
11797
+ const handleDecrement = () => {
11798
+ const newValue = value - step;
11799
+ if (min === void 0 || newValue >= min) {
11800
+ onChange(newValue);
11801
+ }
11802
+ };
11803
+ return /* @__PURE__ */ jsxRuntime.jsxs(
11804
+ "div",
11805
+ {
11806
+ className: ui.clx(
11807
+ "inline-flex rounded-md bg-ui-bg-field shadow-borders-base overflow-hidden divide-x transition-fg",
11808
+ "[&:has(input:focus)]:shadow-borders-interactive-with-active",
11809
+ {
11810
+ "h-7": size === "small",
11811
+ "h-8": size === "base"
11812
+ },
11813
+ className
12713
11814
  ),
12714
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip0_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12715
- "path",
12716
- {
12717
- d: "M133.106 81.8022L140.49 81.8447L140.515 77.6349",
12718
- stroke: "#A1A1AA",
12719
- strokeWidth: "1.5",
12720
- strokeLinecap: "round",
12721
- strokeLinejoin: "round"
11815
+ children: [
11816
+ /* @__PURE__ */ jsxRuntime.jsx(
11817
+ "input",
11818
+ {
11819
+ ref,
11820
+ type: "number",
11821
+ value,
11822
+ onChange: handleChange,
11823
+ min,
11824
+ max,
11825
+ step,
11826
+ className: ui.clx(
11827
+ "flex-1 px-2 py-1 bg-transparent txt-compact-small text-ui-fg-base outline-none [appearance:textfield]",
11828
+ "[&::-webkit-outer-spin-button]:appearance-none [&::-webkit-inner-spin-button]:appearance-none",
11829
+ "placeholder:text-ui-fg-muted"
11830
+ ),
11831
+ ...props
11832
+ }
11833
+ ),
11834
+ /* @__PURE__ */ jsxRuntime.jsxs(
11835
+ "button",
11836
+ {
11837
+ className: ui.clx(
11838
+ "flex items-center justify-center outline-none transition-fg",
11839
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
11840
+ "focus:bg-ui-bg-field-component-hover",
11841
+ "hover:bg-ui-bg-field-component-hover",
11842
+ {
11843
+ "size-7": size === "small",
11844
+ "size-8": size === "base"
11845
+ }
11846
+ ),
11847
+ type: "button",
11848
+ onClick: handleDecrement,
11849
+ disabled: min !== void 0 && value <= min || disabled,
11850
+ children: [
11851
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Minus, {}),
11852
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Decrease by ${step}` })
11853
+ ]
11854
+ }
11855
+ ),
11856
+ /* @__PURE__ */ jsxRuntime.jsxs(
11857
+ "button",
11858
+ {
11859
+ className: ui.clx(
11860
+ "flex items-center justify-center outline-none transition-fg",
11861
+ "disabled:cursor-not-allowed disabled:text-ui-fg-muted",
11862
+ "focus:bg-ui-bg-field-hover",
11863
+ "hover:bg-ui-bg-field-hover",
11864
+ {
11865
+ "size-7": size === "small",
11866
+ "size-8": size === "base"
11867
+ }
11868
+ ),
11869
+ type: "button",
11870
+ onClick: handleIncrement,
11871
+ disabled: max !== void 0 && value >= max || disabled,
11872
+ children: [
11873
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}),
11874
+ /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: `Increase by ${step}` })
11875
+ ]
11876
+ }
11877
+ )
11878
+ ]
11879
+ }
11880
+ );
11881
+ }
11882
+ );
11883
+ const PRODUCT_VARIANTS_QUERY_KEY = "product-variants";
11884
+ const productVariantsQueryKeys = {
11885
+ list: (query2) => [
11886
+ PRODUCT_VARIANTS_QUERY_KEY,
11887
+ query2 ? query2 : void 0
11888
+ ]
11889
+ };
11890
+ const useProductVariants = (query2, options) => {
11891
+ const { data, ...rest } = reactQuery.useQuery({
11892
+ queryKey: productVariantsQueryKeys.list(query2),
11893
+ queryFn: async () => await sdk.admin.productVariant.list(query2),
11894
+ ...options
11895
+ });
11896
+ return { ...data, ...rest };
11897
+ };
11898
+ const STACKED_MODAL_ID = "items_stacked_modal";
11899
+ const Items = () => {
11900
+ const { id } = reactRouterDom.useParams();
11901
+ const {
11902
+ order: preview,
11903
+ isPending: isPreviewPending,
11904
+ isError: isPreviewError,
11905
+ error: previewError
11906
+ } = useOrderPreview(id, void 0, {
11907
+ placeholderData: reactQuery.keepPreviousData
11908
+ });
11909
+ useInitiateOrderEdit({ preview });
11910
+ const { draft_order, isPending, isError, error } = useDraftOrder(
11911
+ id,
11912
+ {
11913
+ fields: "currency_code"
11914
+ },
11915
+ {
11916
+ enabled: !!id
11917
+ }
11918
+ );
11919
+ const { onCancel } = useCancelOrderEdit({ preview });
11920
+ if (isError) {
11921
+ throw error;
11922
+ }
11923
+ if (isPreviewError) {
11924
+ throw previewError;
11925
+ }
11926
+ const ready = !!preview && !isPreviewPending && !!draft_order && !isPending;
11927
+ return /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal, { onClose: onCancel, children: ready ? /* @__PURE__ */ jsxRuntime.jsx(ItemsForm, { preview, currencyCode: draft_order.currency_code }) : /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
11928
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit Items" }) }),
11929
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Loading data for the draft order, please wait..." }) })
11930
+ ] }) });
11931
+ };
11932
+ const ItemsForm = ({ preview, currencyCode }) => {
11933
+ var _a;
11934
+ const [isSubmitting, setIsSubmitting] = React.useState(false);
11935
+ const [modalContent, setModalContent] = React.useState(
11936
+ null
11937
+ );
11938
+ const { handleSuccess } = useRouteModal();
11939
+ const { searchValue, onSearchValueChange, query: query2 } = useDebouncedSearch();
11940
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11941
+ const { mutateAsync: requestOrderEdit } = useDraftOrderRequestEdit(preview.id);
11942
+ const itemCount = ((_a = preview.items) == null ? void 0 : _a.reduce((acc, item) => acc + item.quantity, 0)) || 0;
11943
+ const matches = React.useMemo(() => {
11944
+ return matchSorter.matchSorter(preview.items, query2, {
11945
+ keys: ["product_title", "variant_title", "variant_sku", "title"]
11946
+ });
11947
+ }, [preview.items, query2]);
11948
+ const onSubmit = async () => {
11949
+ setIsSubmitting(true);
11950
+ let requestSucceeded = false;
11951
+ await requestOrderEdit(void 0, {
11952
+ onError: (e) => {
11953
+ ui.toast.error(`Failed to request order edit: ${e.message}`);
11954
+ },
11955
+ onSuccess: () => {
11956
+ requestSucceeded = true;
11957
+ }
11958
+ });
11959
+ if (!requestSucceeded) {
11960
+ setIsSubmitting(false);
11961
+ return;
11962
+ }
11963
+ await confirmOrderEdit(void 0, {
11964
+ onError: (e) => {
11965
+ ui.toast.error(`Failed to confirm order edit: ${e.message}`);
11966
+ },
11967
+ onSuccess: () => {
11968
+ handleSuccess();
11969
+ },
11970
+ onSettled: () => {
11971
+ setIsSubmitting(false);
11972
+ }
11973
+ });
11974
+ };
11975
+ const onKeyDown = React.useCallback(
11976
+ (e) => {
11977
+ if (e.key === "Enter" && (e.ctrlKey || e.metaKey)) {
11978
+ if (modalContent || isSubmitting) {
11979
+ return;
11980
+ }
11981
+ onSubmit();
11982
+ }
11983
+ },
11984
+ [modalContent, isSubmitting, onSubmit]
11985
+ );
11986
+ React.useEffect(() => {
11987
+ document.addEventListener("keydown", onKeyDown);
11988
+ return () => {
11989
+ document.removeEventListener("keydown", onKeyDown);
11990
+ };
11991
+ }, [onKeyDown]);
11992
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex h-full flex-col overflow-hidden", children: [
11993
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Header, {}),
11994
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Body, { className: "flex flex-1 flex-col overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsxs(
11995
+ StackedFocusModal,
11996
+ {
11997
+ id: STACKED_MODAL_ID,
11998
+ onOpenChangeCallback: (open) => {
11999
+ if (!open) {
12000
+ setModalContent(null);
12722
12001
  }
12723
- ) }),
12724
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip1_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12725
- "path",
12726
- {
12727
- d: "M143.496 87.8055L150.881 87.8481L150.905 83.6383",
12728
- stroke: "#A1A1AA",
12729
- strokeWidth: "1.5",
12730
- strokeLinecap: "round",
12731
- strokeLinejoin: "round"
12002
+ },
12003
+ children: [
12004
+ /* @__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: [
12005
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12006
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Items" }) }),
12007
+ /* @__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" }) })
12008
+ ] }),
12009
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12010
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-6", children: [
12011
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 items-center gap-3", children: [
12012
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12013
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Items" }),
12014
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Choose items from the product catalog." })
12015
+ ] }),
12016
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-2", children: [
12017
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
12018
+ ui.Input,
12019
+ {
12020
+ type: "search",
12021
+ placeholder: "Search items",
12022
+ value: searchValue,
12023
+ onChange: (e) => onSearchValueChange(e.target.value)
12024
+ }
12025
+ ) }),
12026
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
12027
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Trigger, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { type: "button", children: /* @__PURE__ */ jsxRuntime.jsx(icons.Plus, {}) }) }),
12028
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
12029
+ /* @__PURE__ */ jsxRuntime.jsx(
12030
+ StackedModalTrigger,
12031
+ {
12032
+ type: "add-items",
12033
+ setModalContent
12034
+ }
12035
+ ),
12036
+ /* @__PURE__ */ jsxRuntime.jsx(
12037
+ StackedModalTrigger,
12038
+ {
12039
+ type: "add-custom-item",
12040
+ setModalContent
12041
+ }
12042
+ )
12043
+ ] })
12044
+ ] })
12045
+ ] })
12046
+ ] }),
12047
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle shadow-elevation-card-rest rounded-xl", children: [
12048
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "px-[5px]", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-ui-fg-muted grid grid-cols-[2fr_1fr_2fr_28px] gap-3 px-4 py-2", children: [
12049
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Item" }) }),
12050
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Quantity" }) }),
12051
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "text-right", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: "Price" }) }),
12052
+ /* @__PURE__ */ jsxRuntime.jsx("div", {})
12053
+ ] }) }),
12054
+ /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-col gap-y-1.5 px-[5px] pb-[5px]", children: itemCount <= 0 ? /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12055
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "There are no items in this order" }),
12056
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: "Add items to the order to get started." })
12057
+ ] }) : matches.length > 0 ? matches == null ? void 0 : matches.map((item) => /* @__PURE__ */ jsxRuntime.jsx(
12058
+ Item,
12059
+ {
12060
+ item,
12061
+ preview,
12062
+ currencyCode
12063
+ },
12064
+ item.id
12065
+ )) : /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest flex flex-col items-center justify-center gap-1 gap-x-3 rounded-lg p-4", children: [
12066
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "No items found" }),
12067
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.Text, { size: "small", className: "text-ui-fg-subtle", children: [
12068
+ 'No items found for "',
12069
+ query2,
12070
+ '".'
12071
+ ] })
12072
+ ] }) })
12073
+ ] })
12074
+ ] }),
12075
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12076
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-[1fr_0.5fr_0.5fr] gap-3", children: [
12077
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: "Subtotal" }) }),
12078
+ /* @__PURE__ */ jsxRuntime.jsx("div", { children: /* @__PURE__ */ jsxRuntime.jsxs(
12079
+ ui.Text,
12080
+ {
12081
+ size: "small",
12082
+ leading: "compact",
12083
+ className: "text-ui-fg-subtle",
12084
+ children: [
12085
+ itemCount,
12086
+ " ",
12087
+ itemCount === 1 ? "item" : "items"
12088
+ ]
12089
+ }
12090
+ ) }),
12091
+ /* @__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) }) })
12092
+ ] })
12093
+ ] }) }),
12094
+ modalContent && (modalContent === "add-items" ? /* @__PURE__ */ jsxRuntime.jsx(ExistingItemsForm, { orderId: preview.id, items: preview.items }) : modalContent === "add-custom-item" ? /* @__PURE__ */ jsxRuntime.jsx(
12095
+ CustomItemForm,
12096
+ {
12097
+ orderId: preview.id,
12098
+ currencyCode
12099
+ }
12100
+ ) : null)
12101
+ ]
12102
+ }
12103
+ ) }),
12104
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12105
+ /* @__PURE__ */ jsxRuntime.jsx(RouteFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12106
+ /* @__PURE__ */ jsxRuntime.jsx(
12107
+ ui.Button,
12108
+ {
12109
+ size: "small",
12110
+ type: "button",
12111
+ onClick: onSubmit,
12112
+ isLoading: isSubmitting,
12113
+ children: "Save"
12114
+ }
12115
+ )
12116
+ ] }) })
12117
+ ] });
12118
+ };
12119
+ const Item = ({ item, preview, currencyCode }) => {
12120
+ if (item.variant_id) {
12121
+ return /* @__PURE__ */ jsxRuntime.jsx(VariantItem, { item, preview, currencyCode });
12122
+ }
12123
+ return /* @__PURE__ */ jsxRuntime.jsx(CustomItem, { item, preview, currencyCode });
12124
+ };
12125
+ const VariantItem = ({ item, preview, currencyCode }) => {
12126
+ const [editing, setEditing] = React.useState(false);
12127
+ const form = reactHookForm.useForm({
12128
+ defaultValues: {
12129
+ quantity: item.quantity,
12130
+ unit_price: item.unit_price
12131
+ },
12132
+ resolver: zod.zodResolver(variantItemSchema)
12133
+ });
12134
+ const actionId = React.useMemo(() => {
12135
+ var _a, _b;
12136
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12137
+ }, [item]);
12138
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12139
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12140
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12141
+ const onSubmit = form.handleSubmit(async (data) => {
12142
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity) {
12143
+ setEditing(false);
12144
+ return;
12145
+ }
12146
+ if (!actionId) {
12147
+ await updateOriginalItem(
12148
+ {
12149
+ item_id: item.id,
12150
+ quantity: data.quantity,
12151
+ unit_price: convertNumber(data.unit_price)
12152
+ },
12153
+ {
12154
+ onSuccess: () => {
12155
+ setEditing(false);
12156
+ },
12157
+ onError: (e) => {
12158
+ ui.toast.error(e.message);
12732
12159
  }
12733
- ) }),
12734
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip2_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12735
- "path",
12160
+ }
12161
+ );
12162
+ return;
12163
+ }
12164
+ await updateActionItem(
12165
+ {
12166
+ action_id: actionId,
12167
+ quantity: data.quantity,
12168
+ unit_price: convertNumber(data.unit_price)
12169
+ },
12170
+ {
12171
+ onSuccess: () => {
12172
+ setEditing(false);
12173
+ },
12174
+ onError: (e) => {
12175
+ ui.toast.error(e.message);
12176
+ }
12177
+ }
12178
+ );
12179
+ });
12180
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
12181
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex w-full items-center gap-x-3", children: [
12182
+ /* @__PURE__ */ jsxRuntime.jsx(
12183
+ Thumbnail,
12184
+ {
12185
+ thumbnail: item.thumbnail,
12186
+ alt: item.product_title ?? void 0
12187
+ }
12188
+ ),
12189
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col", children: [
12190
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-1", children: [
12191
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", leading: "compact", children: item.product_title }),
12192
+ /* @__PURE__ */ jsxRuntime.jsxs(
12193
+ ui.Text,
12194
+ {
12195
+ size: "small",
12196
+ leading: "compact",
12197
+ className: "text-ui-fg-subtle",
12198
+ children: [
12199
+ "(",
12200
+ item.variant_title,
12201
+ ")"
12202
+ ]
12203
+ }
12204
+ )
12205
+ ] }),
12206
+ /* @__PURE__ */ jsxRuntime.jsx(
12207
+ ui.Text,
12736
12208
  {
12737
- d: "M153.887 93.8088L161.271 93.8514L161.295 89.6416",
12738
- stroke: "#A1A1AA",
12739
- strokeWidth: "1.5",
12740
- strokeLinecap: "round",
12741
- strokeLinejoin: "round"
12209
+ size: "small",
12210
+ leading: "compact",
12211
+ className: "text-ui-fg-subtle",
12212
+ children: item.variant_sku
12742
12213
  }
12743
- ) }),
12744
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip3_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12745
- "path",
12746
- {
12747
- d: "M126.114 89.1912L118.729 89.1486L118.705 93.3584",
12748
- stroke: "#A1A1AA",
12749
- strokeWidth: "1.5",
12750
- strokeLinecap: "round",
12751
- strokeLinejoin: "round"
12214
+ )
12215
+ ] })
12216
+ ] }),
12217
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
12218
+ Form$2.Field,
12219
+ {
12220
+ control: form.control,
12221
+ name: "quantity",
12222
+ render: ({ field }) => {
12223
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
12224
+ }
12225
+ }
12226
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }) }),
12227
+ editing ? /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(
12228
+ Form$2.Field,
12229
+ {
12230
+ control: form.control,
12231
+ name: "unit_price",
12232
+ render: ({ field: { onChange, ...field } }) => {
12233
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12234
+ ui.CurrencyInput,
12235
+ {
12236
+ ...field,
12237
+ symbol: getNativeSymbol(currencyCode),
12238
+ code: currencyCode,
12239
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12240
+ }
12241
+ ) }) });
12242
+ }
12243
+ }
12244
+ ) }) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex w-full flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12245
+ /* @__PURE__ */ jsxRuntime.jsx(
12246
+ ui.IconButton,
12247
+ {
12248
+ type: "button",
12249
+ size: "small",
12250
+ onClick: editing ? onSubmit : () => {
12251
+ setEditing(true);
12252
+ },
12253
+ disabled: isPending,
12254
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
12255
+ }
12256
+ )
12257
+ ] }) }) });
12258
+ };
12259
+ const variantItemSchema = objectType({
12260
+ quantity: numberType(),
12261
+ unit_price: unionType([numberType(), stringType()])
12262
+ });
12263
+ const CustomItem = ({ item, preview, currencyCode }) => {
12264
+ const [editing, setEditing] = React.useState(false);
12265
+ const { quantity, unit_price, title } = item;
12266
+ const form = reactHookForm.useForm({
12267
+ defaultValues: {
12268
+ title,
12269
+ quantity,
12270
+ unit_price
12271
+ },
12272
+ resolver: zod.zodResolver(customItemSchema)
12273
+ });
12274
+ React.useEffect(() => {
12275
+ form.reset({
12276
+ title,
12277
+ quantity,
12278
+ unit_price
12279
+ });
12280
+ }, [form, title, quantity, unit_price]);
12281
+ const actionId = React.useMemo(() => {
12282
+ var _a, _b;
12283
+ return (_b = (_a = item.actions) == null ? void 0 : _a.find((a) => a.action === "ITEM_ADD")) == null ? void 0 : _b.id;
12284
+ }, [item]);
12285
+ const { mutateAsync: updateActionItem, isPending: isUpdatingActionItem } = useDraftOrderUpdateActionItem(preview.id);
12286
+ const { mutateAsync: removeActionItem, isPending: isRemovingActionItem } = useDraftOrderRemoveActionItem(preview.id);
12287
+ const { mutateAsync: updateOriginalItem, isPending: isUpdatingOriginalItem } = useDraftOrderUpdateItem(preview.id);
12288
+ const isPending = isUpdatingActionItem || isUpdatingOriginalItem;
12289
+ const onSubmit = form.handleSubmit(async (data) => {
12290
+ if (convertNumber(data.unit_price) === item.unit_price && data.quantity === item.quantity && data.title === item.title) {
12291
+ setEditing(false);
12292
+ return;
12293
+ }
12294
+ if (!actionId) {
12295
+ await updateOriginalItem(
12296
+ {
12297
+ item_id: item.id,
12298
+ quantity: data.quantity,
12299
+ unit_price: convertNumber(data.unit_price)
12300
+ },
12301
+ {
12302
+ onSuccess: () => {
12303
+ setEditing(false);
12304
+ },
12305
+ onError: (e) => {
12306
+ ui.toast.error(e.message);
12752
12307
  }
12753
- ) }),
12754
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip4_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12755
- "path",
12756
- {
12757
- d: "M136.504 95.1945L129.12 95.1519L129.095 99.3617",
12758
- stroke: "#A1A1AA",
12759
- strokeWidth: "1.5",
12760
- strokeLinecap: "round",
12761
- strokeLinejoin: "round"
12308
+ }
12309
+ );
12310
+ return;
12311
+ }
12312
+ if (data.quantity === 0) {
12313
+ await removeActionItem(actionId, {
12314
+ onSuccess: () => {
12315
+ setEditing(false);
12316
+ },
12317
+ onError: (e) => {
12318
+ ui.toast.error(e.message);
12319
+ }
12320
+ });
12321
+ return;
12322
+ }
12323
+ await updateActionItem(
12324
+ {
12325
+ action_id: actionId,
12326
+ quantity: data.quantity,
12327
+ unit_price: convertNumber(data.unit_price)
12328
+ },
12329
+ {
12330
+ onSuccess: () => {
12331
+ setEditing(false);
12332
+ },
12333
+ onError: (e) => {
12334
+ ui.toast.error(e.message);
12335
+ }
12336
+ }
12337
+ );
12338
+ });
12339
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx("form", { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-[minmax(0,2fr)_minmax(0,1fr)_minmax(0,2fr)_28px] items-center gap-3 rounded-lg px-4 py-2", children: [
12340
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-3", children: [
12341
+ /* @__PURE__ */ jsxRuntime.jsx(
12342
+ Thumbnail,
12343
+ {
12344
+ thumbnail: item.thumbnail,
12345
+ alt: item.title ?? void 0
12346
+ }
12347
+ ),
12348
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12349
+ Form$2.Field,
12350
+ {
12351
+ control: form.control,
12352
+ name: "title",
12353
+ render: ({ field }) => {
12354
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }) });
12762
12355
  }
12763
- ) }),
12764
- /* @__PURE__ */ jsxRuntime.jsx("g", { clipPath: "url(#clip5_20915_38670)", children: /* @__PURE__ */ jsxRuntime.jsx(
12765
- "path",
12356
+ }
12357
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.title })
12358
+ ] }),
12359
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12360
+ Form$2.Field,
12361
+ {
12362
+ control: form.control,
12363
+ name: "quantity",
12364
+ render: ({ field }) => {
12365
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field }) }) });
12366
+ }
12367
+ }
12368
+ ) : /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: item.quantity }),
12369
+ editing ? /* @__PURE__ */ jsxRuntime.jsx(
12370
+ Form$2.Field,
12371
+ {
12372
+ control: form.control,
12373
+ name: "unit_price",
12374
+ render: ({ field: { onChange, ...field } }) => {
12375
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12376
+ ui.CurrencyInput,
12377
+ {
12378
+ ...field,
12379
+ symbol: getNativeSymbol(currencyCode),
12380
+ code: currencyCode,
12381
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value)
12382
+ }
12383
+ ) }) });
12384
+ }
12385
+ }
12386
+ ) : /* @__PURE__ */ jsxRuntime.jsx("div", { className: "flex flex-1 items-center justify-end", children: /* @__PURE__ */ jsxRuntime.jsx(ui.Text, { size: "small", weight: "plus", children: getLocaleAmount(item.unit_price, currencyCode) }) }),
12387
+ /* @__PURE__ */ jsxRuntime.jsx(
12388
+ ui.IconButton,
12389
+ {
12390
+ type: "button",
12391
+ size: "small",
12392
+ onClick: editing ? onSubmit : () => {
12393
+ setEditing(true);
12394
+ },
12395
+ disabled: isPending,
12396
+ children: editing ? /* @__PURE__ */ jsxRuntime.jsx(icons.Check, {}) : /* @__PURE__ */ jsxRuntime.jsx(icons.PencilSquare, {})
12397
+ }
12398
+ )
12399
+ ] }) }) });
12400
+ };
12401
+ const StackedModalTrigger = ({
12402
+ type,
12403
+ setModalContent
12404
+ }) => {
12405
+ const { setIsOpen } = useStackedModal();
12406
+ const onClick = React.useCallback(() => {
12407
+ setModalContent(type);
12408
+ setIsOpen(STACKED_MODAL_ID, true);
12409
+ }, [setModalContent, setIsOpen, type]);
12410
+ 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" }) });
12411
+ };
12412
+ const VARIANT_PREFIX = "items";
12413
+ const LIMIT = 50;
12414
+ const ExistingItemsForm = ({ orderId, items }) => {
12415
+ const { setIsOpen } = useStackedModal();
12416
+ const [rowSelection, setRowSelection] = React.useState(
12417
+ items.reduce((acc, item) => {
12418
+ acc[item.variant_id] = true;
12419
+ return acc;
12420
+ }, {})
12421
+ );
12422
+ React.useEffect(() => {
12423
+ setRowSelection(
12424
+ items.reduce((acc, item) => {
12425
+ if (item.variant_id) {
12426
+ acc[item.variant_id] = true;
12427
+ }
12428
+ return acc;
12429
+ }, {})
12430
+ );
12431
+ }, [items]);
12432
+ const { q, order, offset } = useQueryParams(
12433
+ ["q", "order", "offset"],
12434
+ VARIANT_PREFIX
12435
+ );
12436
+ const { variants, count, isPending, isError, error } = useProductVariants(
12437
+ {
12438
+ q,
12439
+ order,
12440
+ offset: offset ? parseInt(offset) : void 0,
12441
+ limit: LIMIT
12442
+ },
12443
+ {
12444
+ placeholderData: reactQuery.keepPreviousData
12445
+ }
12446
+ );
12447
+ const columns = useColumns();
12448
+ const { mutateAsync } = useDraftOrderAddItems(orderId);
12449
+ const onSubmit = async () => {
12450
+ const ids = Object.keys(rowSelection).filter(
12451
+ (id) => !items.find((i) => i.variant_id === id)
12452
+ );
12453
+ await mutateAsync(
12454
+ {
12455
+ items: ids.map((id) => ({
12456
+ variant_id: id,
12457
+ quantity: 1
12458
+ }))
12459
+ },
12460
+ {
12461
+ onSuccess: () => {
12462
+ setRowSelection({});
12463
+ setIsOpen(STACKED_MODAL_ID, false);
12464
+ },
12465
+ onError: (e) => {
12466
+ ui.toast.error(e.message);
12467
+ }
12468
+ }
12469
+ );
12470
+ };
12471
+ if (isError) {
12472
+ throw error;
12473
+ }
12474
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12475
+ StackedFocusModal.Content,
12476
+ {
12477
+ onOpenAutoFocus: (e) => {
12478
+ e.preventDefault();
12479
+ const searchInput = document.querySelector(
12480
+ "[data-modal-id='modal-search-input']"
12481
+ );
12482
+ if (searchInput) {
12483
+ searchInput.focus();
12484
+ }
12485
+ },
12486
+ children: [
12487
+ /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Header, { children: [
12488
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Product Variants" }) }),
12489
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Choose product variants to add to the order." }) })
12490
+ ] }),
12491
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Body, { className: "flex-1 overflow-hidden", children: /* @__PURE__ */ jsxRuntime.jsx(
12492
+ DataTable,
12766
12493
  {
12767
- d: "M146.894 101.198L139.51 101.155L139.486 105.365",
12768
- stroke: "#A1A1AA",
12769
- strokeWidth: "1.5",
12770
- strokeLinecap: "round",
12771
- strokeLinejoin: "round"
12494
+ data: variants,
12495
+ columns,
12496
+ isLoading: isPending,
12497
+ getRowId: (row) => row.id,
12498
+ rowCount: count,
12499
+ prefix: VARIANT_PREFIX,
12500
+ layout: "fill",
12501
+ rowSelection: {
12502
+ state: rowSelection,
12503
+ onRowSelectionChange: setRowSelection,
12504
+ enableRowSelection: (row) => {
12505
+ return !items.find((i) => i.variant_id === row.original.id);
12506
+ }
12507
+ },
12508
+ autoFocusSearch: true
12772
12509
  }
12773
12510
  ) }),
12774
- /* @__PURE__ */ jsxRuntime.jsxs("defs", { children: [
12775
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip0_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12776
- "rect",
12777
- {
12778
- width: "12",
12779
- height: "12",
12780
- fill: "white",
12781
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 138.36 74.6508)"
12782
- }
12783
- ) }),
12784
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip1_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12785
- "rect",
12786
- {
12787
- width: "12",
12788
- height: "12",
12789
- fill: "white",
12790
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 148.75 80.6541)"
12791
- }
12792
- ) }),
12793
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip2_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12794
- "rect",
12795
- {
12796
- width: "12",
12797
- height: "12",
12798
- fill: "white",
12799
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 159.141 86.6575)"
12800
- }
12801
- ) }),
12802
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip3_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12803
- "rect",
12511
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12512
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12513
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Update items" })
12514
+ ] }) })
12515
+ ]
12516
+ }
12517
+ );
12518
+ };
12519
+ const columnHelper = ui.createDataTableColumnHelper();
12520
+ const useColumns = () => {
12521
+ return React.useMemo(() => {
12522
+ return [
12523
+ columnHelper.select(),
12524
+ columnHelper.accessor("product.title", {
12525
+ header: "Product",
12526
+ cell: ({ row }) => {
12527
+ var _a, _b, _c;
12528
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center gap-x-2", children: [
12529
+ /* @__PURE__ */ jsxRuntime.jsx(
12530
+ Thumbnail,
12531
+ {
12532
+ thumbnail: (_a = row.original.product) == null ? void 0 : _a.thumbnail,
12533
+ alt: (_b = row.original.product) == null ? void 0 : _b.title
12534
+ }
12535
+ ),
12536
+ /* @__PURE__ */ jsxRuntime.jsx("span", { children: (_c = row.original.product) == null ? void 0 : _c.title })
12537
+ ] });
12538
+ },
12539
+ enableSorting: true
12540
+ }),
12541
+ columnHelper.accessor("title", {
12542
+ header: "Variant",
12543
+ enableSorting: true
12544
+ }),
12545
+ columnHelper.accessor("sku", {
12546
+ header: "SKU",
12547
+ cell: ({ getValue }) => {
12548
+ return getValue() ?? "-";
12549
+ },
12550
+ enableSorting: true
12551
+ }),
12552
+ columnHelper.accessor("updated_at", {
12553
+ header: "Updated",
12554
+ cell: ({ getValue }) => {
12555
+ return /* @__PURE__ */ jsxRuntime.jsx(
12556
+ ui.Tooltip,
12804
12557
  {
12805
- width: "12",
12806
- height: "12",
12807
- fill: "white",
12808
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 120.928 84.4561)"
12558
+ content: getFullDate({ date: getValue(), includeTime: true }),
12559
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
12809
12560
  }
12810
- ) }),
12811
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip4_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12812
- "rect",
12561
+ );
12562
+ },
12563
+ enableSorting: true,
12564
+ sortAscLabel: "Oldest first",
12565
+ sortDescLabel: "Newest first"
12566
+ }),
12567
+ columnHelper.accessor("created_at", {
12568
+ header: "Created",
12569
+ cell: ({ getValue }) => {
12570
+ return /* @__PURE__ */ jsxRuntime.jsx(
12571
+ ui.Tooltip,
12813
12572
  {
12814
- width: "12",
12815
- height: "12",
12816
- fill: "white",
12817
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 131.318 90.4594)"
12573
+ content: getFullDate({ date: getValue(), includeTime: true }),
12574
+ children: /* @__PURE__ */ jsxRuntime.jsx("span", { children: getFullDate({ date: getValue() }) })
12818
12575
  }
12819
- ) }),
12820
- /* @__PURE__ */ jsxRuntime.jsx("clipPath", { id: "clip5_20915_38670", children: /* @__PURE__ */ jsxRuntime.jsx(
12821
- "rect",
12576
+ );
12577
+ },
12578
+ enableSorting: true,
12579
+ sortAscLabel: "Oldest first",
12580
+ sortDescLabel: "Newest first"
12581
+ })
12582
+ ];
12583
+ }, []);
12584
+ };
12585
+ const CustomItemForm = ({ orderId, currencyCode }) => {
12586
+ const { setIsOpen } = useStackedModal();
12587
+ const { mutateAsync: addItems } = useDraftOrderAddItems(orderId);
12588
+ const form = reactHookForm.useForm({
12589
+ defaultValues: {
12590
+ title: "",
12591
+ quantity: 1,
12592
+ unit_price: ""
12593
+ },
12594
+ resolver: zod.zodResolver(customItemSchema)
12595
+ });
12596
+ const onSubmit = form.handleSubmit(async (data) => {
12597
+ await addItems(
12598
+ {
12599
+ items: [
12600
+ {
12601
+ title: data.title,
12602
+ quantity: data.quantity,
12603
+ unit_price: convertNumber(data.unit_price)
12604
+ }
12605
+ ]
12606
+ },
12607
+ {
12608
+ onSuccess: () => {
12609
+ setIsOpen(STACKED_MODAL_ID, false);
12610
+ },
12611
+ onError: (e) => {
12612
+ ui.toast.error(e.message);
12613
+ }
12614
+ }
12615
+ );
12616
+ });
12617
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2, { ...form, children: /* @__PURE__ */ jsxRuntime.jsx(KeyboundForm, { onSubmit, children: /* @__PURE__ */ jsxRuntime.jsxs(StackedFocusModal.Content, { children: [
12618
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Header, {}),
12619
+ /* @__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: [
12620
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12621
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Add custom item" }) }),
12622
+ /* @__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." }) })
12623
+ ] }),
12624
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12625
+ /* @__PURE__ */ jsxRuntime.jsx(
12626
+ Form$2.Field,
12627
+ {
12628
+ control: form.control,
12629
+ name: "title",
12630
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12631
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12632
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Title" }),
12633
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the title of the item" })
12634
+ ] }),
12635
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12636
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12637
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12638
+ ] })
12639
+ ] }) })
12640
+ }
12641
+ ),
12642
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12643
+ /* @__PURE__ */ jsxRuntime.jsx(
12644
+ Form$2.Field,
12645
+ {
12646
+ control: form.control,
12647
+ name: "unit_price",
12648
+ render: ({ field: { onChange, ...field } }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12649
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12650
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Unit price" }),
12651
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the unit price of the item" })
12652
+ ] }),
12653
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12654
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12655
+ ui.CurrencyInput,
12656
+ {
12657
+ symbol: getNativeSymbol(currencyCode),
12658
+ code: currencyCode,
12659
+ onValueChange: (_value, _name, values) => onChange(values == null ? void 0 : values.value),
12660
+ ...field
12661
+ }
12662
+ ) }),
12663
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12664
+ ] })
12665
+ ] }) })
12666
+ }
12667
+ ),
12668
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Divider, { variant: "dashed" }),
12669
+ /* @__PURE__ */ jsxRuntime.jsx(
12670
+ Form$2.Field,
12671
+ {
12672
+ control: form.control,
12673
+ name: "quantity",
12674
+ render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-x-3", children: [
12675
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { children: [
12676
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Quantity" }),
12677
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Hint, { children: "Enter the quantity of the item" })
12678
+ ] }),
12679
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "w-full flex-1", children: [
12680
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx("div", { className: "w-full flex-1", children: /* @__PURE__ */ jsxRuntime.jsx(NumberInput, { ...field, className: "w-full" }) }) }),
12681
+ /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12682
+ ] })
12683
+ ] }) })
12684
+ }
12685
+ )
12686
+ ] }) }) }),
12687
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12688
+ /* @__PURE__ */ jsxRuntime.jsx(StackedFocusModal.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
12689
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "button", onClick: onSubmit, children: "Add item" })
12690
+ ] }) })
12691
+ ] }) }) });
12692
+ };
12693
+ const customItemSchema = objectType({
12694
+ title: stringType().min(1),
12695
+ quantity: numberType(),
12696
+ unit_price: unionType([numberType(), stringType()])
12697
+ });
12698
+ const InlineTip = React.forwardRef(
12699
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
12700
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
12701
+ return /* @__PURE__ */ jsxRuntime.jsxs(
12702
+ "div",
12703
+ {
12704
+ ref,
12705
+ className: ui.clx(
12706
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
12707
+ className
12708
+ ),
12709
+ ...props,
12710
+ children: [
12711
+ /* @__PURE__ */ jsxRuntime.jsx(
12712
+ "div",
12822
12713
  {
12823
- width: "12",
12824
- height: "12",
12825
- fill: "white",
12826
- transform: "matrix(0.865865 0.500278 -0.871576 0.490261 141.709 96.4627)"
12714
+ role: "presentation",
12715
+ className: ui.clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
12716
+ "bg-ui-tag-orange-icon": variant === "warning"
12717
+ })
12827
12718
  }
12828
- ) })
12829
- ] })
12830
- ]
12831
- }
12832
- );
12833
- };
12834
- const schema$2 = objectType({
12835
- customer_id: stringType().min(1)
12719
+ ),
12720
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "text-pretty", children: [
12721
+ /* @__PURE__ */ jsxRuntime.jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
12722
+ labelValue,
12723
+ ":"
12724
+ ] }),
12725
+ " ",
12726
+ children
12727
+ ] })
12728
+ ]
12729
+ }
12730
+ );
12731
+ }
12732
+ );
12733
+ InlineTip.displayName = "InlineTip";
12734
+ const MetadataFieldSchema = objectType({
12735
+ key: stringType(),
12736
+ disabled: booleanType().optional(),
12737
+ value: anyType()
12836
12738
  });
12837
- const BillingAddress = () => {
12739
+ const MetadataSchema = objectType({
12740
+ metadata: arrayType(MetadataFieldSchema)
12741
+ });
12742
+ const Metadata = () => {
12838
12743
  const { id } = reactRouterDom.useParams();
12839
12744
  const { order, isPending, isError, error } = useOrder(id, {
12840
- fields: "+billing_address"
12745
+ fields: "metadata"
12841
12746
  });
12842
12747
  if (isError) {
12843
12748
  throw error;
@@ -12845,36 +12750,33 @@ const BillingAddress = () => {
12845
12750
  const isReady = !isPending && !!order;
12846
12751
  return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
12847
12752
  /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Header, { children: [
12848
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Billing Address" }) }),
12849
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
12753
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Metadata" }) }),
12754
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
12850
12755
  ] }),
12851
- isReady && /* @__PURE__ */ jsxRuntime.jsx(BillingAddressForm, { order })
12756
+ !isReady ? /* @__PURE__ */ jsxRuntime.jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsxRuntime.jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
12852
12757
  ] });
12853
12758
  };
12854
- const BillingAddressForm = ({ order }) => {
12855
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12759
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
12760
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
12761
+ const MetadataForm = ({ orderId, metadata }) => {
12762
+ const { handleSuccess } = useRouteModal();
12763
+ const hasUneditableRows = getHasUneditableRows(metadata);
12764
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
12856
12765
  const form = reactHookForm.useForm({
12857
12766
  defaultValues: {
12858
- first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
12859
- last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
12860
- company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
12861
- address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
12862
- address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
12863
- city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
12864
- province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
12865
- country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
12866
- postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
12867
- phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
12767
+ metadata: getDefaultValues(metadata)
12868
12768
  },
12869
- resolver: zod.zodResolver(schema$1)
12769
+ resolver: zod.zodResolver(MetadataSchema)
12870
12770
  });
12871
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12872
- const { handleSuccess } = useRouteModal();
12873
- const onSubmit = form.handleSubmit(async (data) => {
12771
+ const handleSubmit = form.handleSubmit(async (data) => {
12772
+ const parsedData = parseValues(data);
12874
12773
  await mutateAsync(
12875
- { billing_address: data },
12774
+ {
12775
+ metadata: parsedData
12776
+ },
12876
12777
  {
12877
12778
  onSuccess: () => {
12779
+ ui.toast.success("Metadata updated");
12878
12780
  handleSuccess();
12879
12781
  },
12880
12782
  onError: (error) => {
@@ -12883,168 +12785,266 @@ const BillingAddressForm = ({ order }) => {
12883
12785
  }
12884
12786
  );
12885
12787
  });
12788
+ const { fields, insert, remove } = reactHookForm.useFieldArray({
12789
+ control: form.control,
12790
+ name: "metadata"
12791
+ });
12792
+ function deleteRow(index) {
12793
+ remove(index);
12794
+ if (fields.length === 1) {
12795
+ insert(0, {
12796
+ key: "",
12797
+ value: "",
12798
+ disabled: false
12799
+ });
12800
+ }
12801
+ }
12802
+ function insertRow(index, position) {
12803
+ insert(index + (position === "above" ? 0 : 1), {
12804
+ key: "",
12805
+ value: "",
12806
+ disabled: false
12807
+ });
12808
+ }
12886
12809
  return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(
12887
12810
  KeyboundForm,
12888
12811
  {
12812
+ onSubmit: handleSubmit,
12889
12813
  className: "flex flex-1 flex-col overflow-hidden",
12890
- onSubmit,
12891
12814
  children: [
12892
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-col gap-y-4", children: [
12893
- /* @__PURE__ */ jsxRuntime.jsx(
12894
- Form$2.Field,
12895
- {
12896
- control: form.control,
12897
- name: "country_code",
12898
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12899
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Country" }),
12900
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(CountrySelect, { ...field }) }),
12901
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12902
- ] })
12903
- }
12904
- ),
12905
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12906
- /* @__PURE__ */ jsxRuntime.jsx(
12907
- Form$2.Field,
12908
- {
12909
- control: form.control,
12910
- name: "first_name",
12911
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12912
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "First name" }),
12913
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12914
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12915
- ] })
12916
- }
12917
- ),
12918
- /* @__PURE__ */ jsxRuntime.jsx(
12919
- Form$2.Field,
12920
- {
12921
- control: form.control,
12922
- name: "last_name",
12923
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12924
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Last name" }),
12925
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12926
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12927
- ] })
12928
- }
12929
- )
12930
- ] }),
12931
- /* @__PURE__ */ jsxRuntime.jsx(
12932
- Form$2.Field,
12933
- {
12934
- control: form.control,
12935
- name: "company",
12936
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12937
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Company" }),
12938
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12939
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12940
- ] })
12941
- }
12942
- ),
12943
- /* @__PURE__ */ jsxRuntime.jsx(
12944
- Form$2.Field,
12945
- {
12946
- control: form.control,
12947
- name: "address_1",
12948
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12949
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Address" }),
12950
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12951
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12952
- ] })
12953
- }
12954
- ),
12955
- /* @__PURE__ */ jsxRuntime.jsx(
12956
- Form$2.Field,
12957
- {
12958
- control: form.control,
12959
- name: "address_2",
12960
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12961
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12962
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12963
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12964
- ] })
12965
- }
12966
- ),
12967
- /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12968
- /* @__PURE__ */ jsxRuntime.jsx(
12969
- Form$2.Field,
12970
- {
12971
- control: form.control,
12972
- name: "postal_code",
12973
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12974
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "Postal code" }),
12975
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12976
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12977
- ] })
12815
+ /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
12816
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
12817
+ /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
12818
+ /* @__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" }) }),
12819
+ /* @__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" }) })
12820
+ ] }),
12821
+ fields.map((field, index) => {
12822
+ const isDisabled = field.disabled || false;
12823
+ let placeholder = "-";
12824
+ if (typeof field.value === "object") {
12825
+ placeholder = "{ ... }";
12978
12826
  }
12979
- ),
12980
- /* @__PURE__ */ jsxRuntime.jsx(
12981
- Form$2.Field,
12982
- {
12983
- control: form.control,
12984
- name: "city",
12985
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12986
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { children: "City" }),
12987
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
12988
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
12989
- ] })
12827
+ if (Array.isArray(field.value)) {
12828
+ placeholder = "[ ... ]";
12990
12829
  }
12991
- )
12830
+ return /* @__PURE__ */ jsxRuntime.jsx(
12831
+ ConditionalTooltip,
12832
+ {
12833
+ showTooltip: isDisabled,
12834
+ content: "This row is disabled because it contains non-primitive data.",
12835
+ children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "group/table relative", children: [
12836
+ /* @__PURE__ */ jsxRuntime.jsxs(
12837
+ "div",
12838
+ {
12839
+ className: ui.clx("grid grid-cols-2 divide-x", {
12840
+ "overflow-hidden rounded-b-lg": index === fields.length - 1
12841
+ }),
12842
+ children: [
12843
+ /* @__PURE__ */ jsxRuntime.jsx(
12844
+ Form$2.Field,
12845
+ {
12846
+ control: form.control,
12847
+ name: `metadata.${index}.key`,
12848
+ render: ({ field: field2 }) => {
12849
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12850
+ GridInput,
12851
+ {
12852
+ "aria-labelledby": METADATA_KEY_LABEL_ID,
12853
+ ...field2,
12854
+ disabled: isDisabled,
12855
+ placeholder: "Key"
12856
+ }
12857
+ ) }) });
12858
+ }
12859
+ }
12860
+ ),
12861
+ /* @__PURE__ */ jsxRuntime.jsx(
12862
+ Form$2.Field,
12863
+ {
12864
+ control: form.control,
12865
+ name: `metadata.${index}.value`,
12866
+ render: ({ field: { value, ...field2 } }) => {
12867
+ return /* @__PURE__ */ jsxRuntime.jsx(Form$2.Item, { children: /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(
12868
+ GridInput,
12869
+ {
12870
+ "aria-labelledby": METADATA_VALUE_LABEL_ID,
12871
+ ...field2,
12872
+ value: isDisabled ? placeholder : value,
12873
+ disabled: isDisabled,
12874
+ placeholder: "Value"
12875
+ }
12876
+ ) }) });
12877
+ }
12878
+ }
12879
+ )
12880
+ ]
12881
+ }
12882
+ ),
12883
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu, { children: [
12884
+ /* @__PURE__ */ jsxRuntime.jsx(
12885
+ ui.DropdownMenu.Trigger,
12886
+ {
12887
+ className: ui.clx(
12888
+ "invisible absolute inset-y-0 -right-2.5 my-auto group-hover/table:visible data-[state='open']:visible",
12889
+ {
12890
+ hidden: isDisabled
12891
+ }
12892
+ ),
12893
+ disabled: isDisabled,
12894
+ asChild: true,
12895
+ children: /* @__PURE__ */ jsxRuntime.jsx(ui.IconButton, { size: "2xsmall", children: /* @__PURE__ */ jsxRuntime.jsx(icons.EllipsisVertical, {}) })
12896
+ }
12897
+ ),
12898
+ /* @__PURE__ */ jsxRuntime.jsxs(ui.DropdownMenu.Content, { children: [
12899
+ /* @__PURE__ */ jsxRuntime.jsxs(
12900
+ ui.DropdownMenu.Item,
12901
+ {
12902
+ className: "gap-x-2",
12903
+ onClick: () => insertRow(index, "above"),
12904
+ children: [
12905
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowUpMini, { className: "text-ui-fg-subtle" }),
12906
+ "Insert row above"
12907
+ ]
12908
+ }
12909
+ ),
12910
+ /* @__PURE__ */ jsxRuntime.jsxs(
12911
+ ui.DropdownMenu.Item,
12912
+ {
12913
+ className: "gap-x-2",
12914
+ onClick: () => insertRow(index, "below"),
12915
+ children: [
12916
+ /* @__PURE__ */ jsxRuntime.jsx(icons.ArrowDownMini, { className: "text-ui-fg-subtle" }),
12917
+ "Insert row below"
12918
+ ]
12919
+ }
12920
+ ),
12921
+ /* @__PURE__ */ jsxRuntime.jsx(ui.DropdownMenu.Separator, {}),
12922
+ /* @__PURE__ */ jsxRuntime.jsxs(
12923
+ ui.DropdownMenu.Item,
12924
+ {
12925
+ className: "gap-x-2",
12926
+ onClick: () => deleteRow(index),
12927
+ children: [
12928
+ /* @__PURE__ */ jsxRuntime.jsx(icons.Trash, { className: "text-ui-fg-subtle" }),
12929
+ "Delete row"
12930
+ ]
12931
+ }
12932
+ )
12933
+ ] })
12934
+ ] })
12935
+ ] })
12936
+ },
12937
+ field.id
12938
+ );
12939
+ })
12992
12940
  ] }),
12993
- /* @__PURE__ */ jsxRuntime.jsx(
12994
- Form$2.Field,
12995
- {
12996
- control: form.control,
12997
- name: "province",
12998
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
12999
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Province / State" }),
13000
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13001
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13002
- ] })
13003
- }
13004
- ),
13005
- /* @__PURE__ */ jsxRuntime.jsx(
13006
- Form$2.Field,
13007
- {
13008
- control: form.control,
13009
- name: "phone",
13010
- render: ({ field }) => /* @__PURE__ */ jsxRuntime.jsxs(Form$2.Item, { children: [
13011
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Label, { optional: true, children: "Phone" }),
13012
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.Control, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Input, { ...field }) }),
13013
- /* @__PURE__ */ jsxRuntime.jsx(Form$2.ErrorMessage, {})
13014
- ] })
13015
- }
13016
- )
13017
- ] }) }),
13018
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
13019
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
12941
+ 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." })
12942
+ ] }),
12943
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12944
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", type: "button", children: "Cancel" }) }),
13020
12945
  /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13021
12946
  ] }) })
13022
12947
  ]
13023
12948
  }
13024
12949
  ) });
13025
12950
  };
13026
- const schema$1 = addressSchema;
13027
- const CustomItems = () => {
13028
- return /* @__PURE__ */ jsxRuntime.jsxs(RouteDrawer, { children: [
13029
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Heading, { children: "Edit Custom Items" }) }) }),
13030
- /* @__PURE__ */ jsxRuntime.jsx(CustomItemsForm, {})
12951
+ const GridInput = React.forwardRef(({ className, ...props }, ref) => {
12952
+ return /* @__PURE__ */ jsxRuntime.jsx(
12953
+ "input",
12954
+ {
12955
+ ref,
12956
+ ...props,
12957
+ autoComplete: "off",
12958
+ className: ui.clx(
12959
+ "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",
12960
+ className
12961
+ )
12962
+ }
12963
+ );
12964
+ });
12965
+ GridInput.displayName = "MetadataForm.GridInput";
12966
+ const PlaceholderInner = () => {
12967
+ return /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex flex-1 flex-col overflow-hidden", children: [
12968
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-[148ox] w-full rounded-lg" }) }),
12969
+ /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex items-center justify-end gap-x-2", children: [
12970
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" }),
12971
+ /* @__PURE__ */ jsxRuntime.jsx(ui.Skeleton, { className: "h-7 w-12 rounded-md" })
12972
+ ] }) })
13031
12973
  ] });
13032
12974
  };
13033
- const CustomItemsForm = () => {
13034
- const form = reactHookForm.useForm({
13035
- resolver: zod.zodResolver(schema)
12975
+ const EDITABLE_TYPES = ["string", "number", "boolean"];
12976
+ function getDefaultValues(metadata) {
12977
+ if (!metadata || !Object.keys(metadata).length) {
12978
+ return [
12979
+ {
12980
+ key: "",
12981
+ value: "",
12982
+ disabled: false
12983
+ }
12984
+ ];
12985
+ }
12986
+ return Object.entries(metadata).map(([key, value]) => {
12987
+ if (!EDITABLE_TYPES.includes(typeof value)) {
12988
+ return {
12989
+ key,
12990
+ value,
12991
+ disabled: true
12992
+ };
12993
+ }
12994
+ let stringValue = value;
12995
+ if (typeof value !== "string") {
12996
+ stringValue = JSON.stringify(value);
12997
+ }
12998
+ return {
12999
+ key,
13000
+ value: stringValue,
13001
+ original_key: key
13002
+ };
13036
13003
  });
13037
- return /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxRuntime.jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
13038
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Body, {}),
13039
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxRuntime.jsxs("div", { className: "flex justify-end gap-2", children: [
13040
- /* @__PURE__ */ jsxRuntime.jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13041
- /* @__PURE__ */ jsxRuntime.jsx(ui.Button, { size: "small", type: "submit", children: "Save" })
13042
- ] }) })
13043
- ] }) });
13044
- };
13045
- const schema = objectType({
13046
- email: stringType().email()
13047
- });
13004
+ }
13005
+ function parseValues(values) {
13006
+ const metadata = values.metadata;
13007
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
13008
+ if (isEmpty) {
13009
+ return null;
13010
+ }
13011
+ const update = {};
13012
+ metadata.forEach((field) => {
13013
+ let key = field.key;
13014
+ let value = field.value;
13015
+ const disabled = field.disabled;
13016
+ if (!key || !value) {
13017
+ return;
13018
+ }
13019
+ if (disabled) {
13020
+ update[key] = value;
13021
+ return;
13022
+ }
13023
+ key = key.trim();
13024
+ value = value.trim();
13025
+ if (value === "true") {
13026
+ update[key] = true;
13027
+ } else if (value === "false") {
13028
+ update[key] = false;
13029
+ } else {
13030
+ const parsedNumber = parseFloat(value);
13031
+ if (!isNaN(parsedNumber)) {
13032
+ update[key] = parsedNumber;
13033
+ } else {
13034
+ update[key] = value;
13035
+ }
13036
+ }
13037
+ });
13038
+ return update;
13039
+ }
13040
+ function getHasUneditableRows(metadata) {
13041
+ if (!metadata) {
13042
+ return false;
13043
+ }
13044
+ return Object.values(metadata).some(
13045
+ (value) => !EDITABLE_TYPES.includes(typeof value)
13046
+ );
13047
+ }
13048
13048
  const widgetModule = { widgets: [] };
13049
13049
  const routeModule = {
13050
13050
  routes: [
@@ -13066,16 +13066,16 @@ const routeModule = {
13066
13066
  loader,
13067
13067
  children: [
13068
13068
  {
13069
- Component: Email,
13070
- path: "/draft-orders/:id/email"
13069
+ Component: CustomItems,
13070
+ path: "/draft-orders/:id/custom-items"
13071
13071
  },
13072
13072
  {
13073
- Component: Items,
13074
- path: "/draft-orders/:id/items"
13073
+ Component: BillingAddress,
13074
+ path: "/draft-orders/:id/billing-address"
13075
13075
  },
13076
13076
  {
13077
- Component: Metadata,
13078
- path: "/draft-orders/:id/metadata"
13077
+ Component: Email,
13078
+ path: "/draft-orders/:id/email"
13079
13079
  },
13080
13080
  {
13081
13081
  Component: Promotions,
@@ -13098,12 +13098,12 @@ const routeModule = {
13098
13098
  path: "/draft-orders/:id/transfer-ownership"
13099
13099
  },
13100
13100
  {
13101
- Component: BillingAddress,
13102
- path: "/draft-orders/:id/billing-address"
13101
+ Component: Items,
13102
+ path: "/draft-orders/:id/items"
13103
13103
  },
13104
13104
  {
13105
- Component: CustomItems,
13106
- path: "/draft-orders/:id/custom-items"
13105
+ Component: Metadata,
13106
+ path: "/draft-orders/:id/metadata"
13107
13107
  }
13108
13108
  ]
13109
13109
  }