@medusajs/draft-order 2.10.4-preview-20251011000317 → 2.10.4-preview-20251011030926

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.
@@ -9567,6 +9567,196 @@ const ID = () => {
9567
9567
  /* @__PURE__ */ jsx(Outlet, {})
9568
9568
  ] });
9569
9569
  };
9570
+ const BillingAddress = () => {
9571
+ const { id } = useParams();
9572
+ const { order, isPending, isError, error } = useOrder(id, {
9573
+ fields: "+billing_address"
9574
+ });
9575
+ if (isError) {
9576
+ throw error;
9577
+ }
9578
+ const isReady = !isPending && !!order;
9579
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9580
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
9581
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Billing Address" }) }),
9582
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
9583
+ ] }),
9584
+ isReady && /* @__PURE__ */ jsx(BillingAddressForm, { order })
9585
+ ] });
9586
+ };
9587
+ const BillingAddressForm = ({ order }) => {
9588
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
9589
+ const form = useForm({
9590
+ defaultValues: {
9591
+ first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
9592
+ last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
9593
+ company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
9594
+ address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
9595
+ address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
9596
+ city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
9597
+ province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
9598
+ country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
9599
+ postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
9600
+ phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
9601
+ },
9602
+ resolver: zodResolver(schema$5)
9603
+ });
9604
+ const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9605
+ const { handleSuccess } = useRouteModal();
9606
+ const onSubmit = form.handleSubmit(async (data) => {
9607
+ await mutateAsync(
9608
+ { billing_address: data },
9609
+ {
9610
+ onSuccess: () => {
9611
+ handleSuccess();
9612
+ },
9613
+ onError: (error) => {
9614
+ toast.error(error.message);
9615
+ }
9616
+ }
9617
+ );
9618
+ });
9619
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
9620
+ KeyboundForm,
9621
+ {
9622
+ className: "flex flex-1 flex-col overflow-hidden",
9623
+ onSubmit,
9624
+ children: [
9625
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
9626
+ /* @__PURE__ */ jsx(
9627
+ Form$2.Field,
9628
+ {
9629
+ control: form.control,
9630
+ name: "country_code",
9631
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9632
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
9633
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
9634
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9635
+ ] })
9636
+ }
9637
+ ),
9638
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9639
+ /* @__PURE__ */ jsx(
9640
+ Form$2.Field,
9641
+ {
9642
+ control: form.control,
9643
+ name: "first_name",
9644
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9645
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
9646
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9647
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9648
+ ] })
9649
+ }
9650
+ ),
9651
+ /* @__PURE__ */ jsx(
9652
+ Form$2.Field,
9653
+ {
9654
+ control: form.control,
9655
+ name: "last_name",
9656
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9657
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
9658
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9659
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9660
+ ] })
9661
+ }
9662
+ )
9663
+ ] }),
9664
+ /* @__PURE__ */ jsx(
9665
+ Form$2.Field,
9666
+ {
9667
+ control: form.control,
9668
+ name: "company",
9669
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9670
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
9671
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9672
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9673
+ ] })
9674
+ }
9675
+ ),
9676
+ /* @__PURE__ */ jsx(
9677
+ Form$2.Field,
9678
+ {
9679
+ control: form.control,
9680
+ name: "address_1",
9681
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9682
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
9683
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9684
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9685
+ ] })
9686
+ }
9687
+ ),
9688
+ /* @__PURE__ */ jsx(
9689
+ Form$2.Field,
9690
+ {
9691
+ control: form.control,
9692
+ name: "address_2",
9693
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9694
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
9695
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9696
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9697
+ ] })
9698
+ }
9699
+ ),
9700
+ /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
9701
+ /* @__PURE__ */ jsx(
9702
+ Form$2.Field,
9703
+ {
9704
+ control: form.control,
9705
+ name: "postal_code",
9706
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9707
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
9708
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9709
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9710
+ ] })
9711
+ }
9712
+ ),
9713
+ /* @__PURE__ */ jsx(
9714
+ Form$2.Field,
9715
+ {
9716
+ control: form.control,
9717
+ name: "city",
9718
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9719
+ /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
9720
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9721
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9722
+ ] })
9723
+ }
9724
+ )
9725
+ ] }),
9726
+ /* @__PURE__ */ jsx(
9727
+ Form$2.Field,
9728
+ {
9729
+ control: form.control,
9730
+ name: "province",
9731
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9732
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
9733
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9734
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9735
+ ] })
9736
+ }
9737
+ ),
9738
+ /* @__PURE__ */ jsx(
9739
+ Form$2.Field,
9740
+ {
9741
+ control: form.control,
9742
+ name: "phone",
9743
+ render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
9744
+ /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
9745
+ /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
9746
+ /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
9747
+ ] })
9748
+ }
9749
+ )
9750
+ ] }) }),
9751
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
9752
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
9753
+ /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
9754
+ ] }) })
9755
+ ]
9756
+ }
9757
+ ) });
9758
+ };
9759
+ const schema$5 = addressSchema;
9570
9760
  const CustomItems = () => {
9571
9761
  return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
9572
9762
  /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Custom Items" }) }) }),
@@ -9575,7 +9765,7 @@ const CustomItems = () => {
9575
9765
  };
9576
9766
  const CustomItemsForm = () => {
9577
9767
  const form = useForm({
9578
- resolver: zodResolver(schema$5)
9768
+ resolver: zodResolver(schema$4)
9579
9769
  });
9580
9770
  return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", children: [
9581
9771
  /* @__PURE__ */ jsx(RouteDrawer.Body, {}),
@@ -9585,7 +9775,7 @@ const CustomItemsForm = () => {
9585
9775
  ] }) })
9586
9776
  ] }) });
9587
9777
  };
9588
- const schema$5 = objectType({
9778
+ const schema$4 = objectType({
9589
9779
  email: stringType().email()
9590
9780
  });
9591
9781
  const Email = () => {
@@ -9610,7 +9800,7 @@ const EmailForm = ({ order }) => {
9610
9800
  defaultValues: {
9611
9801
  email: order.email ?? ""
9612
9802
  },
9613
- resolver: zodResolver(schema$4)
9803
+ resolver: zodResolver(schema$3)
9614
9804
  });
9615
9805
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
9616
9806
  const { handleSuccess } = useRouteModal();
@@ -9653,7 +9843,7 @@ const EmailForm = ({ order }) => {
9653
9843
  }
9654
9844
  ) });
9655
9845
  };
9656
- const schema$4 = objectType({
9846
+ const schema$3 = objectType({
9657
9847
  email: stringType().email()
9658
9848
  });
9659
9849
  const NumberInput = forwardRef(
@@ -10630,148 +10820,425 @@ const customItemSchema = objectType({
10630
10820
  quantity: numberType(),
10631
10821
  unit_price: unionType([numberType(), stringType()])
10632
10822
  });
10633
- const InlineTip = forwardRef(
10634
- ({ variant = "tip", label, className, children, ...props }, ref) => {
10635
- const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
10636
- return /* @__PURE__ */ jsxs(
10637
- "div",
10638
- {
10639
- ref,
10640
- className: clx(
10641
- "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
10642
- className
10643
- ),
10644
- ...props,
10645
- children: [
10646
- /* @__PURE__ */ jsx(
10647
- "div",
10648
- {
10649
- role: "presentation",
10650
- className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
10651
- "bg-ui-tag-orange-icon": variant === "warning"
10652
- })
10653
- }
10654
- ),
10655
- /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
10656
- /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
10657
- labelValue,
10658
- ":"
10659
- ] }),
10660
- " ",
10661
- children
10662
- ] })
10663
- ]
10664
- }
10665
- );
10666
- }
10667
- );
10668
- InlineTip.displayName = "InlineTip";
10669
- const MetadataFieldSchema = objectType({
10670
- key: stringType(),
10671
- disabled: booleanType().optional(),
10672
- value: anyType()
10673
- });
10674
- const MetadataSchema = objectType({
10675
- metadata: arrayType(MetadataFieldSchema)
10676
- });
10677
- const Metadata = () => {
10678
- const { id } = useParams();
10679
- const { order, isPending, isError, error } = useOrder(id, {
10680
- fields: "metadata"
10681
- });
10682
- if (isError) {
10683
- throw error;
10684
- }
10685
- const isReady = !isPending && !!order;
10686
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
10687
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
10688
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
10689
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
10690
- ] }),
10691
- !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
10692
- ] });
10823
+ const PROMOTION_QUERY_KEY = "promotions";
10824
+ const promotionsQueryKeys = {
10825
+ list: (query2) => [
10826
+ PROMOTION_QUERY_KEY,
10827
+ query2 ? query2 : void 0
10828
+ ],
10829
+ detail: (id, query2) => [
10830
+ PROMOTION_QUERY_KEY,
10831
+ id,
10832
+ query2 ? query2 : void 0
10833
+ ]
10693
10834
  };
10694
- const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
10695
- const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
10696
- const MetadataForm = ({ orderId, metadata }) => {
10835
+ const usePromotions = (query2, options) => {
10836
+ const { data, ...rest } = useQuery({
10837
+ queryKey: promotionsQueryKeys.list(query2),
10838
+ queryFn: async () => sdk.admin.promotion.list(query2),
10839
+ ...options
10840
+ });
10841
+ return { ...data, ...rest };
10842
+ };
10843
+ const Promotions = () => {
10844
+ const { id } = useParams();
10845
+ const {
10846
+ order: preview,
10847
+ isError: isPreviewError,
10848
+ error: previewError
10849
+ } = useOrderPreview(id, void 0);
10850
+ useInitiateOrderEdit({ preview });
10851
+ const { onCancel } = useCancelOrderEdit({ preview });
10852
+ if (isPreviewError) {
10853
+ throw previewError;
10854
+ }
10855
+ const isReady = !!preview;
10856
+ return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
10857
+ /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
10858
+ isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
10859
+ ] });
10860
+ };
10861
+ const PromotionForm = ({ preview }) => {
10862
+ const { items, shipping_methods } = preview;
10863
+ const [isSubmitting, setIsSubmitting] = useState(false);
10864
+ const [comboboxValue, setComboboxValue] = useState("");
10697
10865
  const { handleSuccess } = useRouteModal();
10698
- const hasUneditableRows = getHasUneditableRows(metadata);
10699
- const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
10700
- const form = useForm({
10701
- defaultValues: {
10702
- metadata: getDefaultValues(metadata)
10866
+ const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
10867
+ const promoIds = getPromotionIds(items, shipping_methods);
10868
+ const { promotions, isPending, isError, error } = usePromotions(
10869
+ {
10870
+ id: promoIds
10703
10871
  },
10704
- resolver: zodResolver(MetadataSchema)
10872
+ {
10873
+ enabled: !!promoIds.length
10874
+ }
10875
+ );
10876
+ const comboboxData = useComboboxData({
10877
+ queryKey: ["promotions", "combobox", promoIds],
10878
+ queryFn: async (params) => {
10879
+ return await sdk.admin.promotion.list({
10880
+ ...params,
10881
+ id: {
10882
+ $nin: promoIds
10883
+ }
10884
+ });
10885
+ },
10886
+ getOptions: (data) => {
10887
+ return data.promotions.map((promotion) => ({
10888
+ label: promotion.code,
10889
+ value: promotion.code
10890
+ }));
10891
+ }
10705
10892
  });
10706
- const handleSubmit = form.handleSubmit(async (data) => {
10707
- const parsedData = parseValues(data);
10708
- await mutateAsync(
10893
+ const add = async (value) => {
10894
+ if (!value) {
10895
+ return;
10896
+ }
10897
+ addPromotions(
10709
10898
  {
10710
- metadata: parsedData
10899
+ promo_codes: [value]
10711
10900
  },
10712
10901
  {
10713
- onSuccess: () => {
10714
- toast.success("Metadata updated");
10715
- handleSuccess();
10902
+ onError: (e) => {
10903
+ toast.error(e.message);
10904
+ comboboxData.onSearchValueChange("");
10905
+ setComboboxValue("");
10716
10906
  },
10717
- onError: (error) => {
10718
- toast.error(error.message);
10907
+ onSuccess: () => {
10908
+ comboboxData.onSearchValueChange("");
10909
+ setComboboxValue("");
10719
10910
  }
10720
10911
  }
10721
10912
  );
10722
- });
10723
- const { fields, insert, remove } = useFieldArray({
10724
- control: form.control,
10725
- name: "metadata"
10726
- });
10727
- function deleteRow(index) {
10728
- remove(index);
10729
- if (fields.length === 1) {
10730
- insert(0, {
10731
- key: "",
10732
- value: "",
10733
- disabled: false
10734
- });
10913
+ };
10914
+ const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
10915
+ const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
10916
+ const onSubmit = async () => {
10917
+ setIsSubmitting(true);
10918
+ let requestSucceeded = false;
10919
+ await requestOrderEdit(void 0, {
10920
+ onError: (e) => {
10921
+ toast.error(e.message);
10922
+ },
10923
+ onSuccess: () => {
10924
+ requestSucceeded = true;
10925
+ }
10926
+ });
10927
+ if (!requestSucceeded) {
10928
+ setIsSubmitting(false);
10929
+ return;
10735
10930
  }
10736
- }
10737
- function insertRow(index, position) {
10738
- insert(index + (position === "above" ? 0 : 1), {
10739
- key: "",
10740
- value: "",
10741
- disabled: false
10931
+ await confirmOrderEdit(void 0, {
10932
+ onError: (e) => {
10933
+ toast.error(e.message);
10934
+ },
10935
+ onSuccess: () => {
10936
+ handleSuccess();
10937
+ },
10938
+ onSettled: () => {
10939
+ setIsSubmitting(false);
10940
+ }
10742
10941
  });
10942
+ };
10943
+ if (isError) {
10944
+ throw error;
10743
10945
  }
10744
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
10745
- KeyboundForm,
10946
+ return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
10947
+ /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
10948
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
10949
+ /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
10950
+ /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
10951
+ /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
10952
+ ] }),
10953
+ /* @__PURE__ */ jsx(
10954
+ Combobox,
10955
+ {
10956
+ id: "promotion-combobox",
10957
+ "aria-describedby": "promotion-combobox-hint",
10958
+ isFetchingNextPage: comboboxData.isFetchingNextPage,
10959
+ fetchNextPage: comboboxData.fetchNextPage,
10960
+ options: comboboxData.options,
10961
+ onSearchValueChange: comboboxData.onSearchValueChange,
10962
+ searchValue: comboboxData.searchValue,
10963
+ disabled: comboboxData.disabled || isAddingPromotions,
10964
+ onChange: add,
10965
+ value: comboboxValue
10966
+ }
10967
+ )
10968
+ ] }),
10969
+ /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
10970
+ /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
10971
+ PromotionItem,
10972
+ {
10973
+ promotion,
10974
+ orderId: preview.id,
10975
+ isLoading: isPending
10976
+ },
10977
+ promotion.id
10978
+ )) })
10979
+ ] }) }),
10980
+ /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
10981
+ /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
10982
+ /* @__PURE__ */ jsx(
10983
+ Button,
10984
+ {
10985
+ size: "small",
10986
+ type: "submit",
10987
+ isLoading: isSubmitting || isAddingPromotions,
10988
+ children: "Save"
10989
+ }
10990
+ )
10991
+ ] }) })
10992
+ ] });
10993
+ };
10994
+ const PromotionItem = ({
10995
+ promotion,
10996
+ orderId,
10997
+ isLoading
10998
+ }) => {
10999
+ var _a;
11000
+ const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11001
+ const onRemove = async () => {
11002
+ removePromotions(
11003
+ {
11004
+ promo_codes: [promotion.code]
11005
+ },
11006
+ {
11007
+ onError: (e) => {
11008
+ toast.error(e.message);
11009
+ }
11010
+ }
11011
+ );
11012
+ };
11013
+ const displayValue = getDisplayValue(promotion);
11014
+ return /* @__PURE__ */ jsxs(
11015
+ "div",
10746
11016
  {
10747
- onSubmit: handleSubmit,
10748
- className: "flex flex-1 flex-col overflow-hidden",
11017
+ className: clx(
11018
+ "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11019
+ {
11020
+ "animate-pulse": isLoading
11021
+ }
11022
+ ),
10749
11023
  children: [
10750
- /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
10751
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
10752
- /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
10753
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
10754
- /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
11024
+ /* @__PURE__ */ jsxs("div", { children: [
11025
+ /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11026
+ /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11027
+ displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11028
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11029
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
10755
11030
  ] }),
10756
- fields.map((field, index) => {
10757
- const isDisabled = field.disabled || false;
10758
- let placeholder = "-";
10759
- if (typeof field.value === "object") {
10760
- placeholder = "{ ... }";
10761
- }
10762
- if (Array.isArray(field.value)) {
10763
- placeholder = "[ ... ]";
10764
- }
10765
- return /* @__PURE__ */ jsx(
10766
- ConditionalTooltip,
10767
- {
10768
- showTooltip: isDisabled,
10769
- content: "This row is disabled because it contains non-primitive data.",
10770
- children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
10771
- /* @__PURE__ */ jsxs(
10772
- "div",
10773
- {
10774
- className: clx("grid grid-cols-2 divide-x", {
11031
+ /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11032
+ ] })
11033
+ ] }),
11034
+ /* @__PURE__ */ jsx(
11035
+ IconButton,
11036
+ {
11037
+ size: "small",
11038
+ type: "button",
11039
+ variant: "transparent",
11040
+ onClick: onRemove,
11041
+ isLoading: isPending || isLoading,
11042
+ children: /* @__PURE__ */ jsx(XMark, {})
11043
+ }
11044
+ )
11045
+ ]
11046
+ },
11047
+ promotion.id
11048
+ );
11049
+ };
11050
+ function getDisplayValue(promotion) {
11051
+ var _a, _b, _c, _d;
11052
+ const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11053
+ if (!value) {
11054
+ return null;
11055
+ }
11056
+ if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11057
+ const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11058
+ if (!currency) {
11059
+ return null;
11060
+ }
11061
+ return getLocaleAmount(value, currency);
11062
+ } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11063
+ return formatPercentage(value);
11064
+ }
11065
+ return null;
11066
+ }
11067
+ const formatter = new Intl.NumberFormat([], {
11068
+ style: "percent",
11069
+ minimumFractionDigits: 2
11070
+ });
11071
+ const formatPercentage = (value, isPercentageValue = false) => {
11072
+ let val = value || 0;
11073
+ if (!isPercentageValue) {
11074
+ val = val / 100;
11075
+ }
11076
+ return formatter.format(val);
11077
+ };
11078
+ function getPromotionIds(items, shippingMethods) {
11079
+ const promotionIds = /* @__PURE__ */ new Set();
11080
+ for (const item of items) {
11081
+ if (item.adjustments) {
11082
+ for (const adjustment of item.adjustments) {
11083
+ if (adjustment.promotion_id) {
11084
+ promotionIds.add(adjustment.promotion_id);
11085
+ }
11086
+ }
11087
+ }
11088
+ }
11089
+ for (const shippingMethod of shippingMethods) {
11090
+ if (shippingMethod.adjustments) {
11091
+ for (const adjustment of shippingMethod.adjustments) {
11092
+ if (adjustment.promotion_id) {
11093
+ promotionIds.add(adjustment.promotion_id);
11094
+ }
11095
+ }
11096
+ }
11097
+ }
11098
+ return Array.from(promotionIds);
11099
+ }
11100
+ const InlineTip = forwardRef(
11101
+ ({ variant = "tip", label, className, children, ...props }, ref) => {
11102
+ const labelValue = label || (variant === "warning" ? "Warning" : "Tip");
11103
+ return /* @__PURE__ */ jsxs(
11104
+ "div",
11105
+ {
11106
+ ref,
11107
+ className: clx(
11108
+ "bg-ui-bg-component txt-small text-ui-fg-subtle grid grid-cols-[4px_1fr] items-start gap-3 rounded-lg border p-3",
11109
+ className
11110
+ ),
11111
+ ...props,
11112
+ children: [
11113
+ /* @__PURE__ */ jsx(
11114
+ "div",
11115
+ {
11116
+ role: "presentation",
11117
+ className: clx("w-4px bg-ui-tag-neutral-icon h-full rounded-full", {
11118
+ "bg-ui-tag-orange-icon": variant === "warning"
11119
+ })
11120
+ }
11121
+ ),
11122
+ /* @__PURE__ */ jsxs("div", { className: "text-pretty", children: [
11123
+ /* @__PURE__ */ jsxs("strong", { className: "txt-small-plus text-ui-fg-base", children: [
11124
+ labelValue,
11125
+ ":"
11126
+ ] }),
11127
+ " ",
11128
+ children
11129
+ ] })
11130
+ ]
11131
+ }
11132
+ );
11133
+ }
11134
+ );
11135
+ InlineTip.displayName = "InlineTip";
11136
+ const MetadataFieldSchema = objectType({
11137
+ key: stringType(),
11138
+ disabled: booleanType().optional(),
11139
+ value: anyType()
11140
+ });
11141
+ const MetadataSchema = objectType({
11142
+ metadata: arrayType(MetadataFieldSchema)
11143
+ });
11144
+ const Metadata = () => {
11145
+ const { id } = useParams();
11146
+ const { order, isPending, isError, error } = useOrder(id, {
11147
+ fields: "metadata"
11148
+ });
11149
+ if (isError) {
11150
+ throw error;
11151
+ }
11152
+ const isReady = !isPending && !!order;
11153
+ return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
11154
+ /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
11155
+ /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Metadata" }) }),
11156
+ /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Add metadata to the draft order." }) })
11157
+ ] }),
11158
+ !isReady ? /* @__PURE__ */ jsx(PlaceholderInner, {}) : /* @__PURE__ */ jsx(MetadataForm, { orderId: id, metadata: order == null ? void 0 : order.metadata })
11159
+ ] });
11160
+ };
11161
+ const METADATA_KEY_LABEL_ID = "metadata-form-key-label";
11162
+ const METADATA_VALUE_LABEL_ID = "metadata-form-value-label";
11163
+ const MetadataForm = ({ orderId, metadata }) => {
11164
+ const { handleSuccess } = useRouteModal();
11165
+ const hasUneditableRows = getHasUneditableRows(metadata);
11166
+ const { mutateAsync, isPending } = useUpdateDraftOrder(orderId);
11167
+ const form = useForm({
11168
+ defaultValues: {
11169
+ metadata: getDefaultValues(metadata)
11170
+ },
11171
+ resolver: zodResolver(MetadataSchema)
11172
+ });
11173
+ const handleSubmit = form.handleSubmit(async (data) => {
11174
+ const parsedData = parseValues(data);
11175
+ await mutateAsync(
11176
+ {
11177
+ metadata: parsedData
11178
+ },
11179
+ {
11180
+ onSuccess: () => {
11181
+ toast.success("Metadata updated");
11182
+ handleSuccess();
11183
+ },
11184
+ onError: (error) => {
11185
+ toast.error(error.message);
11186
+ }
11187
+ }
11188
+ );
11189
+ });
11190
+ const { fields, insert, remove } = useFieldArray({
11191
+ control: form.control,
11192
+ name: "metadata"
11193
+ });
11194
+ function deleteRow(index) {
11195
+ remove(index);
11196
+ if (fields.length === 1) {
11197
+ insert(0, {
11198
+ key: "",
11199
+ value: "",
11200
+ disabled: false
11201
+ });
11202
+ }
11203
+ }
11204
+ function insertRow(index, position) {
11205
+ insert(index + (position === "above" ? 0 : 1), {
11206
+ key: "",
11207
+ value: "",
11208
+ disabled: false
11209
+ });
11210
+ }
11211
+ return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
11212
+ KeyboundForm,
11213
+ {
11214
+ onSubmit: handleSubmit,
11215
+ className: "flex flex-1 flex-col overflow-hidden",
11216
+ children: [
11217
+ /* @__PURE__ */ jsxs(RouteDrawer.Body, { className: "flex flex-1 flex-col gap-y-8 overflow-y-auto", children: [
11218
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-base shadow-elevation-card-rest grid grid-cols-1 divide-y rounded-lg", children: [
11219
+ /* @__PURE__ */ jsxs("div", { className: "bg-ui-bg-subtle grid grid-cols-2 divide-x rounded-t-lg", children: [
11220
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_KEY_LABEL_ID, children: "Key" }) }),
11221
+ /* @__PURE__ */ jsx("div", { className: "txt-compact-small-plus text-ui-fg-subtle px-2 py-1.5", children: /* @__PURE__ */ jsx("label", { id: METADATA_VALUE_LABEL_ID, children: "Value" }) })
11222
+ ] }),
11223
+ fields.map((field, index) => {
11224
+ const isDisabled = field.disabled || false;
11225
+ let placeholder = "-";
11226
+ if (typeof field.value === "object") {
11227
+ placeholder = "{ ... }";
11228
+ }
11229
+ if (Array.isArray(field.value)) {
11230
+ placeholder = "[ ... ]";
11231
+ }
11232
+ return /* @__PURE__ */ jsx(
11233
+ ConditionalTooltip,
11234
+ {
11235
+ showTooltip: isDisabled,
11236
+ content: "This row is disabled because it contains non-primitive data.",
11237
+ children: /* @__PURE__ */ jsxs("div", { className: "group/table relative", children: [
11238
+ /* @__PURE__ */ jsxs(
11239
+ "div",
11240
+ {
11241
+ className: clx("grid grid-cols-2 divide-x", {
10775
11242
  "overflow-hidden rounded-b-lg": index === fields.length - 1
10776
11243
  }),
10777
11244
  children: [
@@ -10938,324 +11405,47 @@ function getDefaultValues(metadata) {
10938
11405
  });
10939
11406
  }
10940
11407
  function parseValues(values) {
10941
- const metadata = values.metadata;
10942
- const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
10943
- if (isEmpty) {
10944
- return null;
10945
- }
10946
- const update = {};
10947
- metadata.forEach((field) => {
10948
- let key = field.key;
10949
- let value = field.value;
10950
- const disabled = field.disabled;
10951
- if (!key || !value) {
10952
- return;
10953
- }
10954
- if (disabled) {
10955
- update[key] = value;
10956
- return;
10957
- }
10958
- key = key.trim();
10959
- value = value.trim();
10960
- if (value === "true") {
10961
- update[key] = true;
10962
- } else if (value === "false") {
10963
- update[key] = false;
10964
- } else {
10965
- const parsedNumber = parseFloat(value);
10966
- if (!isNaN(parsedNumber)) {
10967
- update[key] = parsedNumber;
10968
- } else {
10969
- update[key] = value;
10970
- }
10971
- }
10972
- });
10973
- return update;
10974
- }
10975
- function getHasUneditableRows(metadata) {
10976
- if (!metadata) {
10977
- return false;
10978
- }
10979
- return Object.values(metadata).some(
10980
- (value) => !EDITABLE_TYPES.includes(typeof value)
10981
- );
10982
- }
10983
- const PROMOTION_QUERY_KEY = "promotions";
10984
- const promotionsQueryKeys = {
10985
- list: (query2) => [
10986
- PROMOTION_QUERY_KEY,
10987
- query2 ? query2 : void 0
10988
- ],
10989
- detail: (id, query2) => [
10990
- PROMOTION_QUERY_KEY,
10991
- id,
10992
- query2 ? query2 : void 0
10993
- ]
10994
- };
10995
- const usePromotions = (query2, options) => {
10996
- const { data, ...rest } = useQuery({
10997
- queryKey: promotionsQueryKeys.list(query2),
10998
- queryFn: async () => sdk.admin.promotion.list(query2),
10999
- ...options
11000
- });
11001
- return { ...data, ...rest };
11002
- };
11003
- const Promotions = () => {
11004
- const { id } = useParams();
11005
- const {
11006
- order: preview,
11007
- isError: isPreviewError,
11008
- error: previewError
11009
- } = useOrderPreview(id, void 0);
11010
- useInitiateOrderEdit({ preview });
11011
- const { onCancel } = useCancelOrderEdit({ preview });
11012
- if (isPreviewError) {
11013
- throw previewError;
11014
- }
11015
- const isReady = !!preview;
11016
- return /* @__PURE__ */ jsxs(RouteDrawer, { onClose: onCancel, children: [
11017
- /* @__PURE__ */ jsx(RouteDrawer.Header, { children: /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Promotions" }) }) }),
11018
- isReady && /* @__PURE__ */ jsx(PromotionForm, { preview })
11019
- ] });
11020
- };
11021
- const PromotionForm = ({ preview }) => {
11022
- const { items, shipping_methods } = preview;
11023
- const [isSubmitting, setIsSubmitting] = useState(false);
11024
- const [comboboxValue, setComboboxValue] = useState("");
11025
- const { handleSuccess } = useRouteModal();
11026
- const { mutateAsync: addPromotions, isPending: isAddingPromotions } = useDraftOrderAddPromotions(preview.id);
11027
- const promoIds = getPromotionIds(items, shipping_methods);
11028
- const { promotions, isPending, isError, error } = usePromotions(
11029
- {
11030
- id: promoIds
11031
- },
11032
- {
11033
- enabled: !!promoIds.length
11034
- }
11035
- );
11036
- const comboboxData = useComboboxData({
11037
- queryKey: ["promotions", "combobox", promoIds],
11038
- queryFn: async (params) => {
11039
- return await sdk.admin.promotion.list({
11040
- ...params,
11041
- id: {
11042
- $nin: promoIds
11043
- }
11044
- });
11045
- },
11046
- getOptions: (data) => {
11047
- return data.promotions.map((promotion) => ({
11048
- label: promotion.code,
11049
- value: promotion.code
11050
- }));
11051
- }
11052
- });
11053
- const add = async (value) => {
11054
- if (!value) {
11055
- return;
11056
- }
11057
- addPromotions(
11058
- {
11059
- promo_codes: [value]
11060
- },
11061
- {
11062
- onError: (e) => {
11063
- toast.error(e.message);
11064
- comboboxData.onSearchValueChange("");
11065
- setComboboxValue("");
11066
- },
11067
- onSuccess: () => {
11068
- comboboxData.onSearchValueChange("");
11069
- setComboboxValue("");
11070
- }
11071
- }
11072
- );
11073
- };
11074
- const { mutateAsync: confirmOrderEdit } = useDraftOrderConfirmEdit(preview.id);
11075
- const { mutateAsync: requestOrderEdit } = useOrderEditRequest(preview.id);
11076
- const onSubmit = async () => {
11077
- setIsSubmitting(true);
11078
- let requestSucceeded = false;
11079
- await requestOrderEdit(void 0, {
11080
- onError: (e) => {
11081
- toast.error(e.message);
11082
- },
11083
- onSuccess: () => {
11084
- requestSucceeded = true;
11085
- }
11086
- });
11087
- if (!requestSucceeded) {
11088
- setIsSubmitting(false);
11089
- return;
11090
- }
11091
- await confirmOrderEdit(void 0, {
11092
- onError: (e) => {
11093
- toast.error(e.message);
11094
- },
11095
- onSuccess: () => {
11096
- handleSuccess();
11097
- },
11098
- onSettled: () => {
11099
- setIsSubmitting(false);
11100
- }
11101
- });
11102
- };
11103
- if (isError) {
11104
- throw error;
11105
- }
11106
- return /* @__PURE__ */ jsxs(KeyboundForm, { className: "flex flex-1 flex-col", onSubmit, children: [
11107
- /* @__PURE__ */ jsx(RouteDrawer.Body, { children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-4", children: [
11108
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-3", children: [
11109
- /* @__PURE__ */ jsxs("div", { className: "flex flex-col", children: [
11110
- /* @__PURE__ */ jsx(Label$1, { size: "small", weight: "plus", htmlFor: "promotion-combobox", children: "Apply promotions" }),
11111
- /* @__PURE__ */ jsx(Hint$1, { id: "promotion-combobox-hint", children: "Manage promotions that should be applied to the order." })
11112
- ] }),
11113
- /* @__PURE__ */ jsx(
11114
- Combobox,
11115
- {
11116
- id: "promotion-combobox",
11117
- "aria-describedby": "promotion-combobox-hint",
11118
- isFetchingNextPage: comboboxData.isFetchingNextPage,
11119
- fetchNextPage: comboboxData.fetchNextPage,
11120
- options: comboboxData.options,
11121
- onSearchValueChange: comboboxData.onSearchValueChange,
11122
- searchValue: comboboxData.searchValue,
11123
- disabled: comboboxData.disabled || isAddingPromotions,
11124
- onChange: add,
11125
- value: comboboxValue
11126
- }
11127
- )
11128
- ] }),
11129
- /* @__PURE__ */ jsx(Divider, { variant: "dashed" }),
11130
- /* @__PURE__ */ jsx("div", { className: "flex flex-col gap-2", children: promotions == null ? void 0 : promotions.map((promotion) => /* @__PURE__ */ jsx(
11131
- PromotionItem,
11132
- {
11133
- promotion,
11134
- orderId: preview.id,
11135
- isLoading: isPending
11136
- },
11137
- promotion.id
11138
- )) })
11139
- ] }) }),
11140
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
11141
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
11142
- /* @__PURE__ */ jsx(
11143
- Button,
11144
- {
11145
- size: "small",
11146
- type: "submit",
11147
- isLoading: isSubmitting || isAddingPromotions,
11148
- children: "Save"
11149
- }
11150
- )
11151
- ] }) })
11152
- ] });
11153
- };
11154
- const PromotionItem = ({
11155
- promotion,
11156
- orderId,
11157
- isLoading
11158
- }) => {
11159
- var _a;
11160
- const { mutateAsync: removePromotions, isPending } = useDraftOrderRemovePromotions(orderId);
11161
- const onRemove = async () => {
11162
- removePromotions(
11163
- {
11164
- promo_codes: [promotion.code]
11165
- },
11166
- {
11167
- onError: (e) => {
11168
- toast.error(e.message);
11169
- }
11170
- }
11171
- );
11172
- };
11173
- const displayValue = getDisplayValue(promotion);
11174
- return /* @__PURE__ */ jsxs(
11175
- "div",
11176
- {
11177
- className: clx(
11178
- "bg-ui-bg-component shadow-elevation-card-rest flex items-center justify-between rounded-lg px-3 py-2",
11179
- {
11180
- "animate-pulse": isLoading
11181
- }
11182
- ),
11183
- children: [
11184
- /* @__PURE__ */ jsxs("div", { children: [
11185
- /* @__PURE__ */ jsx(Text, { size: "small", weight: "plus", leading: "compact", children: promotion.code }),
11186
- /* @__PURE__ */ jsxs("div", { className: "text-ui-fg-subtle flex items-center gap-1.5", children: [
11187
- displayValue && /* @__PURE__ */ jsxs("div", { className: "flex items-center gap-1.5", children: [
11188
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: displayValue }),
11189
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", children: "·" })
11190
- ] }),
11191
- /* @__PURE__ */ jsx(Text, { size: "small", leading: "compact", className: "capitalize", children: (_a = promotion.application_method) == null ? void 0 : _a.allocation })
11192
- ] })
11193
- ] }),
11194
- /* @__PURE__ */ jsx(
11195
- IconButton,
11196
- {
11197
- size: "small",
11198
- type: "button",
11199
- variant: "transparent",
11200
- onClick: onRemove,
11201
- isLoading: isPending || isLoading,
11202
- children: /* @__PURE__ */ jsx(XMark, {})
11203
- }
11204
- )
11205
- ]
11206
- },
11207
- promotion.id
11208
- );
11209
- };
11210
- function getDisplayValue(promotion) {
11211
- var _a, _b, _c, _d;
11212
- const value = (_a = promotion.application_method) == null ? void 0 : _a.value;
11213
- if (!value) {
11214
- return null;
11215
- }
11216
- if (((_b = promotion.application_method) == null ? void 0 : _b.type) === "fixed") {
11217
- const currency = (_c = promotion.application_method) == null ? void 0 : _c.currency_code;
11218
- if (!currency) {
11219
- return null;
11220
- }
11221
- return getLocaleAmount(value, currency);
11222
- } else if (((_d = promotion.application_method) == null ? void 0 : _d.type) === "percentage") {
11223
- return formatPercentage(value);
11224
- }
11225
- return null;
11226
- }
11227
- const formatter = new Intl.NumberFormat([], {
11228
- style: "percent",
11229
- minimumFractionDigits: 2
11230
- });
11231
- const formatPercentage = (value, isPercentageValue = false) => {
11232
- let val = value || 0;
11233
- if (!isPercentageValue) {
11234
- val = val / 100;
11235
- }
11236
- return formatter.format(val);
11237
- };
11238
- function getPromotionIds(items, shippingMethods) {
11239
- const promotionIds = /* @__PURE__ */ new Set();
11240
- for (const item of items) {
11241
- if (item.adjustments) {
11242
- for (const adjustment of item.adjustments) {
11243
- if (adjustment.promotion_id) {
11244
- promotionIds.add(adjustment.promotion_id);
11245
- }
11246
- }
11247
- }
11408
+ const metadata = values.metadata;
11409
+ const isEmpty = !metadata.length || metadata.length === 1 && !metadata[0].key && !metadata[0].value;
11410
+ if (isEmpty) {
11411
+ return null;
11248
11412
  }
11249
- for (const shippingMethod of shippingMethods) {
11250
- if (shippingMethod.adjustments) {
11251
- for (const adjustment of shippingMethod.adjustments) {
11252
- if (adjustment.promotion_id) {
11253
- promotionIds.add(adjustment.promotion_id);
11254
- }
11413
+ const update = {};
11414
+ metadata.forEach((field) => {
11415
+ let key = field.key;
11416
+ let value = field.value;
11417
+ const disabled = field.disabled;
11418
+ if (!key || !value) {
11419
+ return;
11420
+ }
11421
+ if (disabled) {
11422
+ update[key] = value;
11423
+ return;
11424
+ }
11425
+ key = key.trim();
11426
+ value = value.trim();
11427
+ if (value === "true") {
11428
+ update[key] = true;
11429
+ } else if (value === "false") {
11430
+ update[key] = false;
11431
+ } else {
11432
+ const parsedNumber = parseFloat(value);
11433
+ if (!isNaN(parsedNumber)) {
11434
+ update[key] = parsedNumber;
11435
+ } else {
11436
+ update[key] = value;
11255
11437
  }
11256
11438
  }
11439
+ });
11440
+ return update;
11441
+ }
11442
+ function getHasUneditableRows(metadata) {
11443
+ if (!metadata) {
11444
+ return false;
11257
11445
  }
11258
- return Array.from(promotionIds);
11446
+ return Object.values(metadata).some(
11447
+ (value) => !EDITABLE_TYPES.includes(typeof value)
11448
+ );
11259
11449
  }
11260
11450
  const SalesChannel = () => {
11261
11451
  const { id } = useParams();
@@ -11285,7 +11475,7 @@ const SalesChannelForm = ({ order }) => {
11285
11475
  defaultValues: {
11286
11476
  sales_channel_id: order.sales_channel_id || ""
11287
11477
  },
11288
- resolver: zodResolver(schema$3)
11478
+ resolver: zodResolver(schema$2)
11289
11479
  });
11290
11480
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
11291
11481
  const { handleSuccess } = useRouteModal();
@@ -11360,7 +11550,7 @@ const SalesChannelField = ({ control, order }) => {
11360
11550
  }
11361
11551
  );
11362
11552
  };
11363
- const schema$3 = objectType({
11553
+ const schema$2 = objectType({
11364
11554
  sales_channel_id: stringType().min(1)
11365
11555
  });
11366
11556
  const STACKED_FOCUS_MODAL_ID = "shipping-form";
@@ -12202,7 +12392,7 @@ const ShippingAddressForm = ({ order }) => {
12202
12392
  postal_code: ((_i = order.shipping_address) == null ? void 0 : _i.postal_code) ?? "",
12203
12393
  phone: ((_j = order.shipping_address) == null ? void 0 : _j.phone) ?? ""
12204
12394
  },
12205
- resolver: zodResolver(schema$2)
12395
+ resolver: zodResolver(schema$1)
12206
12396
  });
12207
12397
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12208
12398
  const { handleSuccess } = useRouteModal();
@@ -12372,7 +12562,7 @@ const ShippingAddressForm = ({ order }) => {
12372
12562
  }
12373
12563
  ) });
12374
12564
  };
12375
- const schema$2 = addressSchema;
12565
+ const schema$1 = addressSchema;
12376
12566
  const TransferOwnership = () => {
12377
12567
  const { id } = useParams();
12378
12568
  const { draft_order, isPending, isError, error } = useDraftOrder(id, {
@@ -12396,7 +12586,7 @@ const TransferOwnershipForm = ({ order }) => {
12396
12586
  defaultValues: {
12397
12587
  customer_id: order.customer_id || ""
12398
12588
  },
12399
- resolver: zodResolver(schema$1)
12589
+ resolver: zodResolver(schema)
12400
12590
  });
12401
12591
  const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12402
12592
  const { handleSuccess } = useRouteModal();
@@ -12846,199 +13036,9 @@ const Illustration = () => {
12846
13036
  }
12847
13037
  );
12848
13038
  };
12849
- const schema$1 = objectType({
13039
+ const schema = objectType({
12850
13040
  customer_id: stringType().min(1)
12851
13041
  });
12852
- const BillingAddress = () => {
12853
- const { id } = useParams();
12854
- const { order, isPending, isError, error } = useOrder(id, {
12855
- fields: "+billing_address"
12856
- });
12857
- if (isError) {
12858
- throw error;
12859
- }
12860
- const isReady = !isPending && !!order;
12861
- return /* @__PURE__ */ jsxs(RouteDrawer, { children: [
12862
- /* @__PURE__ */ jsxs(RouteDrawer.Header, { children: [
12863
- /* @__PURE__ */ jsx(RouteDrawer.Title, { asChild: true, children: /* @__PURE__ */ jsx(Heading, { children: "Edit Billing Address" }) }),
12864
- /* @__PURE__ */ jsx(RouteDrawer.Description, { asChild: true, children: /* @__PURE__ */ jsx("span", { className: "sr-only", children: "Edit the billing address for the draft order" }) })
12865
- ] }),
12866
- isReady && /* @__PURE__ */ jsx(BillingAddressForm, { order })
12867
- ] });
12868
- };
12869
- const BillingAddressForm = ({ order }) => {
12870
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
12871
- const form = useForm({
12872
- defaultValues: {
12873
- first_name: ((_a = order.billing_address) == null ? void 0 : _a.first_name) ?? "",
12874
- last_name: ((_b = order.billing_address) == null ? void 0 : _b.last_name) ?? "",
12875
- company: ((_c = order.billing_address) == null ? void 0 : _c.company) ?? "",
12876
- address_1: ((_d = order.billing_address) == null ? void 0 : _d.address_1) ?? "",
12877
- address_2: ((_e = order.billing_address) == null ? void 0 : _e.address_2) ?? "",
12878
- city: ((_f = order.billing_address) == null ? void 0 : _f.city) ?? "",
12879
- province: ((_g = order.billing_address) == null ? void 0 : _g.province) ?? "",
12880
- country_code: ((_h = order.billing_address) == null ? void 0 : _h.country_code) ?? "",
12881
- postal_code: ((_i = order.billing_address) == null ? void 0 : _i.postal_code) ?? "",
12882
- phone: ((_j = order.billing_address) == null ? void 0 : _j.phone) ?? ""
12883
- },
12884
- resolver: zodResolver(schema)
12885
- });
12886
- const { mutateAsync, isPending } = useUpdateDraftOrder(order.id);
12887
- const { handleSuccess } = useRouteModal();
12888
- const onSubmit = form.handleSubmit(async (data) => {
12889
- await mutateAsync(
12890
- { billing_address: data },
12891
- {
12892
- onSuccess: () => {
12893
- handleSuccess();
12894
- },
12895
- onError: (error) => {
12896
- toast.error(error.message);
12897
- }
12898
- }
12899
- );
12900
- });
12901
- return /* @__PURE__ */ jsx(RouteDrawer.Form, { form, children: /* @__PURE__ */ jsxs(
12902
- KeyboundForm,
12903
- {
12904
- className: "flex flex-1 flex-col overflow-hidden",
12905
- onSubmit,
12906
- children: [
12907
- /* @__PURE__ */ jsx(RouteDrawer.Body, { className: "flex flex-col gap-y-6 overflow-y-auto", children: /* @__PURE__ */ jsxs("div", { className: "flex flex-col gap-y-4", children: [
12908
- /* @__PURE__ */ jsx(
12909
- Form$2.Field,
12910
- {
12911
- control: form.control,
12912
- name: "country_code",
12913
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12914
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Country" }),
12915
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(CountrySelect, { ...field }) }),
12916
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12917
- ] })
12918
- }
12919
- ),
12920
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12921
- /* @__PURE__ */ jsx(
12922
- Form$2.Field,
12923
- {
12924
- control: form.control,
12925
- name: "first_name",
12926
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12927
- /* @__PURE__ */ jsx(Form$2.Label, { children: "First name" }),
12928
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12929
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12930
- ] })
12931
- }
12932
- ),
12933
- /* @__PURE__ */ jsx(
12934
- Form$2.Field,
12935
- {
12936
- control: form.control,
12937
- name: "last_name",
12938
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12939
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Last name" }),
12940
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12941
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12942
- ] })
12943
- }
12944
- )
12945
- ] }),
12946
- /* @__PURE__ */ jsx(
12947
- Form$2.Field,
12948
- {
12949
- control: form.control,
12950
- name: "company",
12951
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12952
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Company" }),
12953
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12954
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12955
- ] })
12956
- }
12957
- ),
12958
- /* @__PURE__ */ jsx(
12959
- Form$2.Field,
12960
- {
12961
- control: form.control,
12962
- name: "address_1",
12963
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12964
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Address" }),
12965
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12966
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12967
- ] })
12968
- }
12969
- ),
12970
- /* @__PURE__ */ jsx(
12971
- Form$2.Field,
12972
- {
12973
- control: form.control,
12974
- name: "address_2",
12975
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12976
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Apartment, suite, etc." }),
12977
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12978
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12979
- ] })
12980
- }
12981
- ),
12982
- /* @__PURE__ */ jsxs("div", { className: "grid grid-cols-2 gap-4", children: [
12983
- /* @__PURE__ */ jsx(
12984
- Form$2.Field,
12985
- {
12986
- control: form.control,
12987
- name: "postal_code",
12988
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
12989
- /* @__PURE__ */ jsx(Form$2.Label, { children: "Postal code" }),
12990
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
12991
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
12992
- ] })
12993
- }
12994
- ),
12995
- /* @__PURE__ */ jsx(
12996
- Form$2.Field,
12997
- {
12998
- control: form.control,
12999
- name: "city",
13000
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13001
- /* @__PURE__ */ jsx(Form$2.Label, { children: "City" }),
13002
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13003
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13004
- ] })
13005
- }
13006
- )
13007
- ] }),
13008
- /* @__PURE__ */ jsx(
13009
- Form$2.Field,
13010
- {
13011
- control: form.control,
13012
- name: "province",
13013
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13014
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Province / State" }),
13015
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13016
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13017
- ] })
13018
- }
13019
- ),
13020
- /* @__PURE__ */ jsx(
13021
- Form$2.Field,
13022
- {
13023
- control: form.control,
13024
- name: "phone",
13025
- render: ({ field }) => /* @__PURE__ */ jsxs(Form$2.Item, { children: [
13026
- /* @__PURE__ */ jsx(Form$2.Label, { optional: true, children: "Phone" }),
13027
- /* @__PURE__ */ jsx(Form$2.Control, { children: /* @__PURE__ */ jsx(Input, { ...field }) }),
13028
- /* @__PURE__ */ jsx(Form$2.ErrorMessage, {})
13029
- ] })
13030
- }
13031
- )
13032
- ] }) }),
13033
- /* @__PURE__ */ jsx(RouteDrawer.Footer, { children: /* @__PURE__ */ jsxs("div", { className: "flex justify-end gap-2", children: [
13034
- /* @__PURE__ */ jsx(RouteDrawer.Close, { asChild: true, children: /* @__PURE__ */ jsx(Button, { size: "small", variant: "secondary", children: "Cancel" }) }),
13035
- /* @__PURE__ */ jsx(Button, { size: "small", type: "submit", isLoading: isPending, children: "Save" })
13036
- ] }) })
13037
- ]
13038
- }
13039
- ) });
13040
- };
13041
- const schema = addressSchema;
13042
13042
  const widgetModule = { widgets: [] };
13043
13043
  const routeModule = {
13044
13044
  routes: [
@@ -13059,6 +13059,10 @@ const routeModule = {
13059
13059
  handle,
13060
13060
  loader,
13061
13061
  children: [
13062
+ {
13063
+ Component: BillingAddress,
13064
+ path: "/draft-orders/:id/billing-address"
13065
+ },
13062
13066
  {
13063
13067
  Component: CustomItems,
13064
13068
  path: "/draft-orders/:id/custom-items"
@@ -13071,14 +13075,14 @@ const routeModule = {
13071
13075
  Component: Items,
13072
13076
  path: "/draft-orders/:id/items"
13073
13077
  },
13074
- {
13075
- Component: Metadata,
13076
- path: "/draft-orders/:id/metadata"
13077
- },
13078
13078
  {
13079
13079
  Component: Promotions,
13080
13080
  path: "/draft-orders/:id/promotions"
13081
13081
  },
13082
+ {
13083
+ Component: Metadata,
13084
+ path: "/draft-orders/:id/metadata"
13085
+ },
13082
13086
  {
13083
13087
  Component: SalesChannel,
13084
13088
  path: "/draft-orders/:id/sales-channel"
@@ -13094,10 +13098,6 @@ const routeModule = {
13094
13098
  {
13095
13099
  Component: TransferOwnership,
13096
13100
  path: "/draft-orders/:id/transfer-ownership"
13097
- },
13098
- {
13099
- Component: BillingAddress,
13100
- path: "/draft-orders/:id/billing-address"
13101
13101
  }
13102
13102
  ]
13103
13103
  }