Last active
November 3, 2019 13:25
-
-
Save pubudu-ranasinghe/bf755d92b336285f4ee1f8b59e1bde00 to your computer and use it in GitHub Desktop.
react-context-example-19
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, { useState } from "react"; | |
import { | |
useAuthContext, | |
loginSuccess, | |
loginFail | |
} from "../contexts/AuthContext"; | |
import { apiLogin } from "../api/auth"; | |
import { Redirect } from "react-router-dom"; | |
export default function Login() { | |
const [name, setName] = useState(""); | |
const [loading, setLoading] = useState(false); | |
const { auth, dispatch } = useAuthContext(); | |
// Handle login will call the login API and dispatch actions | |
// depending on the result | |
async function handleLogin() { | |
setLoading(true); | |
try { | |
await apiLogin(name); | |
setLoading(false); | |
dispatch(loginSuccess(name)); | |
} catch (error) { | |
setLoading(false); | |
dispatch(loginFail(error.message)); | |
} | |
} | |
// If user is already logged in redirect to Todos page | |
if (auth.isLoggedIn) return <Redirect to="/todos" />; | |
if (loading) return <p>Loading..</p>; | |
return ( | |
<header className="App-header"> | |
<h2>Login</h2> | |
<div className="Item"> | |
<input | |
type="text" | |
placeholder="Name" | |
value={name} | |
onChange={e => setName(e.target.value)} | |
></input> | |
<button onClick={handleLogin}>Login</button> | |
</div> | |
{auth.error && <p>{auth.error}</p>} | |
</header> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment