From a1c748adcbea872de7714dbca11f7ab52ec60d1a Mon Sep 17 00:00:00 2001 From: Frida Jonsson Date: Fri, 21 Feb 2020 13:31:25 +0100 Subject: [PATCH] Failures visible in frontend --- frontend/src/components/RegisterForm.js | 27 ++++++++++++++++++++----- 1 file changed, 22 insertions(+), 5 deletions(-) diff --git a/frontend/src/components/RegisterForm.js b/frontend/src/components/RegisterForm.js index 820184f84..db159e207 100644 --- a/frontend/src/components/RegisterForm.js +++ b/frontend/src/components/RegisterForm.js @@ -9,26 +9,43 @@ const [name, setName] = useState("") const [password, setPassword] = useState("") const [email, setEmail] = useState("") const [registred, setRegistred] = useState(false) +const [failure, setFailure] = useState(false) const handleSubmit = event => { event.preventDefault() fetch(url, { method: 'POST', body: JSON.stringify({ name, email, password }), - headers: { 'Content-Type': 'application/json'} + headers: { 'Content-Type': 'application/json' } }) - .then(res => res.json()) - .then(setRegistred(true)) + //Här händer en callback funktion + .then(res => { + // console.log(res.json()) + if (res.status !== 201) { + return res.json().then(json => console.log("hej", json.message)), + setFailure(true) + } else { + return setRegistred(true) + } + }) .catch(err => console.log('Error:', err)) } +// .catch (err => { +// setRegistred(false) +// console.log('Fetch Error', err); +// }); return ( {!registred && ( - Register form + {!failure && ( + Register form + )} + { failure && ( + User not registred. Try using another name or email! + )}
-