import React, {useState, useEffect} from 'react'; import { useParams } from 'react-router'; import { HashRouter as Router, Switch, Route, Link } from "react-router-dom"; const People = () => { const [people, setPeople] = useState([]); const [loading, setLoading] = useState(true); const [error, setError] = useState(''); const get_people = () => { return fetch('http://localhost:8000/person') .then(response => { if (response.ok) { return response; } else { return response.text() .then((text) => { throw new Error(text); }); } }) .then(response => { return response.json(); }) .catch(err => { console.log(err.message); }); }; const add_person = (nom, prenom) => { return fetch("http://localhost:8000/person", { method:"POST", body: JSON.stringify({lastname: nom, firstname: prenom}), headers: { 'Content-Type': 'application/json' } }) .then(response => { if (response.ok) { return response; } else { return response.text() .then((text) => { throw new Error(text); }); } }) .then(response => { return response.json(); }) .catch(err => { console.log(err.message); }); } const remove_person = (id) => { return fetch("http://localhost:8000/person/"+id, { method:"DELETE" }) .then(response => { if (response.ok) { return response; } else { return response.text() .then((text) => { throw new Error(text); }); } }) .then(response => { setPeople(people => people.filter((i) => i.id != id)); return response.json(); }) .catch(err => { console.log(err.message); }); } const get_mails = (id) => { return fetch('http://localhost:8000/person/'+id+'/mailAddress') .then(response => { if (response.ok) { return response; } else { return response.text() .then((text) => { throw new Error(text); }); } }) .then(response => { return response.json(); }) .catch(err => { console.log(err.message); }); } const get_phones = (id) => { return fetch('http://localhost:8000/person/'+id+'/phone') .then(response => { if (response.ok) { return response; } else { return response.text() .then((text) => { throw new Error(text); }); } }) .then(response => { return response.json(); }) .catch(err => { console.log(err.message); }); } const Details = () => { const {id} = useParams(); console.log("qs"); const [phones, setPhones] = useState([]); const [mails, setMails] = useState([]); useEffect( () => { Promise.all([get_mails(id), get_phones(id)]) .then(values => { setMails(values[0]); setPhones(values[1]); }); },[id]) return <>