Last active
December 4, 2019 06:40
-
-
Save nk-gears/97c4eee24d2e08d0cf0acc3284dc9bd8 to your computer and use it in GitHub Desktop.
Created with Copy to Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from "react"; | |
import logo from "../logo.svg"; | |
import { AuthContext } from "../App"; | |
export const Login = () => { | |
const { dispatch } = React.useContext(AuthContext); | |
const initialState = { | |
email: "", | |
password: "", | |
isSubmitting: false, | |
errorMessage: null | |
}; | |
const [data, setData] = React.useState(initialState); | |
const handleInputChange = event => { | |
setData({ | |
...data, | |
[event.target.name]: event.target.value | |
}); | |
}; | |
const handleFormSubmit = event => { | |
event.preventDefault(); | |
setData({ | |
...data, | |
isSubmitting: true, | |
errorMessage: null | |
}); | |
fetch("https://hookedbe.herokuapp.com/api/login", { | |
method: "post", | |
headers: { | |
"Content-Type": "application/json" | |
}, | |
body: JSON.stringify({ | |
username: data.email, | |
password: data.password | |
}) | |
}) | |
.then(res => { | |
if (res.ok) { | |
return res.json(); | |
} | |
throw res; | |
}) | |
.then(resJson => { | |
dispatch({ | |
type: "LOGIN", | |
payload: resJson | |
}) | |
}) | |
.catch(error => { | |
setData({ | |
...data, | |
isSubmitting: false, | |
errorMessage: error.message || error.statusText | |
}); | |
}); | |
}; | |
return ( | |
<div className="login-container"> | |
<div className="card"> | |
<div className="container"> | |
<form onSubmit={handleFormSubmit}> | |
<h1>Login</h1> | |
<label htmlFor="email"> | |
Email Address | |
<input | |
type="text" | |
value={data.email} | |
onChange={handleInputChange} | |
name="email" | |
id="email" | |
/> | |
</label> | |
<label htmlFor="password"> | |
Password | |
<input | |
type="password" | |
value={data.password} | |
onChange={handleInputChange} | |
name="password" | |
id="password" | |
/> | |
</label> | |
{data.errorMessage && ( | |
<span className="form-error">{data.errorMessage}</span> | |
)} | |
<button disabled={data.isSubmitting}> | |
{data.isSubmitting ? ( | |
<img className="spinner" src={logo} alt="loading icon" /> | |
) : ( | |
"Login" | |
)} | |
</button> | |
</form> | |
</div> | |
</div> | |
</div> | |
); | |
}; | |
export default Login; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment