diff --git a/GrossesMitaines/grosses-mitaines-ui/src/pages/Formulaire.js b/GrossesMitaines/grosses-mitaines-ui/src/pages/Formulaire.js index f410c27..bb87e7d 100644 --- a/GrossesMitaines/grosses-mitaines-ui/src/pages/Formulaire.js +++ b/GrossesMitaines/grosses-mitaines-ui/src/pages/Formulaire.js @@ -7,23 +7,37 @@ import { Form } from "react-bootstrap"; import { useNavigate } from "react-router-dom"; export default function App() { - const { register, handleSubmit, setValue, watch, formState: { errors } } = useForm(); + const { register, handleSubmit, formState: { errors } } = useForm(); const cookies = new Cookies(); const [userAddresses, setUserAddresses] = useState([]); const navigate = useNavigate(); + const [currentAdress, setCurrentAdress] = useState({ + firstName: "", + lastName: "", + phoneNumber: "", + emailAddress: "", + civicNumber: "", + appartment: "", + street: "", + city: "", + province: "", + country: "", + postalCode: "" + }) + useEffect(() => { const userInfo = cookies.get("GMGM"); if (userInfo != null && userInfo.LoggedIn == true) { if (userInfo.firstName != null) - setValue("firstName", userInfo.firstName); + setCurrentAdress((e) => { return { ...e, firstName: userInfo.firstName } }); if (userInfo.lastName != null) - setValue("lastName", userInfo.lastName); + setCurrentAdress((e) => { return { ...e, lastName: userInfo.lastName } }); if (userInfo.phone != null) - setValue("phoneNumber", userInfo.phone); + setCurrentAdress((e) => { return { ...e, phoneNumber: userInfo.phone } }); if (userInfo.email != null) - setValue("emailAddress", userInfo.email); + setCurrentAdress((e) => { return { ...e, emailAddress: userInfo.email } }); fetch("https://localhost:7292/api/Address", { method: 'GET', @@ -33,22 +47,27 @@ export default function App() { if (response.ok) { const json = await response.json(); setUserAddresses(json); - if (json.length >= 1) + if (json.length >= 1) { setFormAddress(json[0]); + } } }); } }, []); const setFormAddress = (address) => { - - setValue("civicNumber", address.civicNumber); - setValue("appartment", address.appartment); - setValue("street", address.street); - setValue("city", address.city); - setValue("province", address.province); - setValue("country", address.country); - setValue("postalCode", address.postalCode); + setCurrentAdress((e) => { + return { + ...e, + civicNumber: address.civicNumber, + appartment: address.appartment, + street: address.street, + city: address.city, + province: address.province, + country: address.country, + postalCode: address.postalCode + } + }); } const onSubmit = async (data) => { @@ -68,7 +87,6 @@ export default function App() { } } - return (