generated from Open-Science-Community-Saudi-Arabia/Template
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bd2a003
commit cc7ebcd
Showing
3 changed files
with
134 additions
and
0 deletions.
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import React from 'react' | ||
|
||
export default function index() { | ||
return ( | ||
<div>Admin dashboard</div> | ||
) | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { useState } from "react"; | ||
import "../style.scss"; | ||
import { toast } from "react-toastify"; | ||
import { MdOutlineVisibilityOff, MdOutlineVisibility } from "react-icons/md"; | ||
import { useNavigate } from "react-router-dom"; | ||
import { login } from "../../../utils/api/auth"; | ||
import Spinner from "../../../components/Spinner"; | ||
import { LoginInRequestPayload } from "../../../types"; | ||
import { setToken } from "../../../utils"; | ||
import LanguageToggle from "../../../components/LanguageToggle"; | ||
import { Trans, t } from "@lingui/macro"; | ||
|
||
/** | ||
* @category Client App | ||
* @subcategory Pages | ||
* @module Login | ||
* @description Users login with email and password, including Google signup option. | ||
* @component | ||
* @example | ||
<Route path="admin/login" element={<AdminLogin />} /> | ||
*/ | ||
|
||
const Login = () => { | ||
const [toggleVisibility, setToggleVisibility] = useState(false); | ||
const [isError, setError] = useState(false); | ||
const [isLoading, setLoading] = useState(false); | ||
const navigate = useNavigate(); | ||
|
||
const loginHandler = async (event: any) => { | ||
setError(false); | ||
event.preventDefault(); | ||
try { | ||
const formData: LoginInRequestPayload = { | ||
email: event.target.email.value, | ||
password: event.target.password.value, | ||
}; | ||
setLoading(true); | ||
let response = await login(formData); | ||
if (response.success) { | ||
setToken(response.data.access_token); | ||
console.log(response) | ||
} | ||
} catch (error: any) { | ||
setError(true); | ||
toast.error(error.message, { | ||
position: toast.POSITION.TOP_CENTER, | ||
autoClose: 5000, | ||
theme: "colored", | ||
}); | ||
} finally { | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
return ( | ||
<> | ||
<section className="login-signup"> | ||
<div className="login-signup__languageToggle"> | ||
{" "} | ||
<LanguageToggle /> | ||
</div> | ||
|
||
<h1 className="login-signup__heading"> | ||
<Trans>Login As Admin</Trans> | ||
</h1> | ||
|
||
<form | ||
className="login-signup__form" | ||
onSubmit={loginHandler} | ||
method="POST" | ||
> | ||
<div className="field"> | ||
<label className="sr-only" htmlFor="email"> | ||
<Trans> Email</Trans> | ||
</label> | ||
<input | ||
type="email" | ||
name="email" | ||
placeholder={t`Email`} | ||
required | ||
className={`${isError && "error-input"}`} | ||
/> | ||
</div> | ||
|
||
<div className="field"> | ||
<label className="sr-only" htmlFor="password"> | ||
Password | ||
</label> | ||
<input | ||
type={toggleVisibility ? "text" : "password"} | ||
placeholder={t`Password`} | ||
required | ||
name="password" | ||
className={`${isError && "error-input"}`} | ||
/> | ||
<button | ||
type="button" | ||
aria-label="toggle password" | ||
className="icon-button eye-icon" | ||
onClick={() => setToggleVisibility(!toggleVisibility)} | ||
> | ||
{toggleVisibility ? ( | ||
<MdOutlineVisibility /> | ||
) : ( | ||
<MdOutlineVisibilityOff /> | ||
)} | ||
</button> | ||
</div> | ||
|
||
<div className="field button-field"> | ||
<button> | ||
{isLoading ? ( | ||
<Spinner width="30px" height="30px" color="#fff" /> | ||
) : ( | ||
t`Login` | ||
)} | ||
</button> | ||
</div> | ||
</form> | ||
</section> | ||
</> | ||
); | ||
}; | ||
|
||
export default Login; |