import { Country } from "@medusajs/medusa" import { StockLocationAddressDTO } from "@medusajs/types" import { useAdminRegions } from "medusa-react" import { useEffect, useMemo, useState } from "react" import { Controller, useWatch } from "react-hook-form" import { useTranslation } from "react-i18next" import InputField from "../../../../../components/molecules/input" import { NextSelect } from "../../../../../components/molecules/select/next-select" import FormValidator from "../../../../../utils/form-validator" import { NestedForm } from "../../../../../utils/nested-form" const AddressForm = ({ form, }: { form: NestedForm }) => { const { register, path, formState: { errors }, control, } = form const { t } = useTranslation() const { regions } = useAdminRegions() const watchedAddressForm = useWatch({ control, name: [ path("company"), path("address_1"), path("address_2"), path("postal_code"), path("city"), path("country_code"), ], }) const anyAddressFieldsFilled = watchedAddressForm.some((field) => !!field) const [addressFieldsRequired, setAddressFieldsRequired] = useState(false) useEffect(() => { setAddressFieldsRequired(!!anyAddressFieldsFilled) }, [anyAddressFieldsFilled]) const countries = useMemo(() => { if (!regions) { return [] } return regions .reduce( (countries, region) => [...countries, ...region.countries], [] as Country[] ) .map((country) => ({ label: country.display_name, value: country.iso_2, })) }, [regions]) return ( <> {t("address-form-address", "Address")}
{ let fieldValue: | string | { value: string; label: string } | undefined = value if (typeof fieldValue === "string") { fieldValue = countries.find( (country) => country.value === fieldValue ) } return ( ) }} />
) } export default AddressForm