url-minify/frontend/components/Login/Login.jsx

111 lines
2.9 KiB
React
Raw Normal View History

2022-02-15 17:18:05 +00:00
import React from 'react'
import LoginStyle from './Login.style'
2022-03-23 09:41:57 +00:00
// import AccountCircleIcon from '@mui/icons-material/AccountCircle'
2022-02-15 17:18:05 +00:00
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
2022-03-23 09:41:57 +00:00
import { faEnvelope, faLock } from '@fortawesome/free-solid-svg-icons'
import UserContext from '../../helpers/user/usercontext'
2022-02-15 17:18:05 +00:00
import { useState } from 'react'
import Link from 'next/link'
import { useContext } from 'react'
2022-03-23 09:41:57 +00:00
import { useRouter } from 'next/router'
2022-02-15 14:49:59 +00:00
import {PopupMain} from "../Pop_up/Popup.jsx"
import style from "../../styles/popup_style.module.css"
2022-02-15 14:49:59 +00:00
function Login() {
2022-03-23 09:41:57 +00:00
const router = useRouter()
const user = useContext(UserContext)
2022-02-15 14:49:59 +00:00
const [userData, setUserData] = useState({
2022-02-15 17:18:05 +00:00
email: '',
password: '',
})
2022-02-15 14:49:59 +00:00
const handleInput = (event) => {
2022-02-15 17:18:05 +00:00
const name = event.target.name
const value = event.target.value
setUserData({ ...userData, [name]: value })
}
2022-02-15 14:49:59 +00:00
const handleLogin =async (evt) => {
2022-03-23 09:41:57 +00:00
evt.preventDefault()
await user.login(userData)
user.popupHandler(true)
2022-03-23 09:41:57 +00:00
}
// disable submit button is any input has not been filled
const disabledSubmitBtn = Object.values(userData).some((val) => val === '')
2022-03-23 09:41:57 +00:00
if (user.user) {
setTimeout(() => {
router.push('/dashboard')
}, 4400);
2022-03-23 09:41:57 +00:00
}
return <>
<div className={style["container-modal-main"]}>
{
(user.showPopUp) ? (user.user) ? <PopupMain message="Successfully Logged In" mode="accept" /> :
<PopupMain message="Invalid Credentials" mode="error" /> : ""
}
</div>
2022-02-15 14:49:59 +00:00
<LoginStyle>
2022-03-23 09:41:57 +00:00
<form className="form-wrapper" onSubmit={handleLogin}>
2022-02-15 14:49:59 +00:00
<p className="reg-title">Sign in</p>
<img src="/images/user.png"></img>
2022-02-15 14:49:59 +00:00
<div className="reg-field">
<div className="reg-label">
<FontAwesomeIcon icon={faEnvelope} />
</div>
<input
className="reg-input"
name="email"
autoComplete="off"
onChange={handleInput}
type="text"
value={userData.email}
placeholder="Email Address"
/>
</div>
2022-02-15 14:49:59 +00:00
<div className="reg-field">
<div className="reg-label">
<FontAwesomeIcon icon={faLock} />
</div>
<input
className="reg-input"
name="password"
autoComplete="off"
onChange={handleInput}
type="password"
value={userData.password}
placeholder="Password"
/>
</div>
<button
type="submit"
className="submit-button"
disabled={disabledSubmitBtn}
>
2022-03-23 09:41:57 +00:00
Submit
</button>
2022-02-15 14:49:59 +00:00
<p className="foot-text">
New here?&nbsp;
<Link href="/signup" exact className="foot-text underline">
Create an account
</Link>
</p>
</form>
</LoginStyle>
</>
2022-02-15 14:49:59 +00:00
}
2022-02-15 17:18:05 +00:00
export default Login