Skip to content

Commit

Permalink
Failures visible in frontend
Browse files Browse the repository at this point in the history
  • Loading branch information
annafridamaria committed Feb 21, 2020
1 parent 47a94ff commit a1c748a
Showing 1 changed file with 22 additions and 5 deletions.
27 changes: 22 additions & 5 deletions frontend/src/components/RegisterForm.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (

<FieldContainer>
{!registred && (
<FieldContainer>
<Heading>Register form</Heading>
{!failure && (
<Heading>Register form</Heading>
)}
{ failure && (
<Heading>User not registred. Try using another name or email!</Heading>
)}
<Form onSubmit={handleSubmit}>

<Label>
<Input
type="text"
Expand Down

0 comments on commit a1c748a

Please sign in to comment.