@medusajs/draft-order 2.10.2-preview-20250903180151 → 2.10.2-preview-20250903210146

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