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

140 lines
3.7 KiB
React
Raw Normal View History

2022-02-15 15:14:21 +00:00
import React from 'react'
import RegStyle from './Reg.style'
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'
2022-03-07 10:26:12 +00:00
import { faUser, faEnvelope, faLock } from '@fortawesome/free-solid-svg-icons'
import { useState, useContext } from 'react'
2022-02-15 15:14:21 +00:00
import { Link } from '@mui/material'
2022-03-07 10:26:12 +00:00
import UserAuth from 'helpers/user/usercontext'
2022-03-23 09:41:57 +00:00
import { useRouter } from 'next/router'
import {PopupMain} from "../Pop_up/Popup.jsx"
import style from "../../styles/popup_style.module.css"
2022-02-08 15:21:06 +00:00
2022-02-15 14:49:59 +00:00
function Reg() {
2022-03-23 09:41:57 +00:00
const router = useRouter()
const context = useContext(UserAuth)
2022-02-15 14:49:59 +00:00
const [userData, setUserData] = useState({
name: '',
2022-02-15 15:14:21 +00:00
email: '',
password: '',
repassword: '',
})
2022-02-08 15:21:06 +00:00
2022-02-15 14:49:59 +00:00
const handleInput = (event) => {
2022-02-15 15:14:21 +00:00
const name = event.target.name
const value = event.target.value
setUserData({ ...userData, [name]: value })
}
const handleSubmit = async (e) => {
2022-03-07 10:26:12 +00:00
e.preventDefault()
setUserData(userData)
await context.createAcc(userData)
context.popupHandler(true) //* state change to spwan a popUp
2022-03-07 10:26:12 +00:00
}
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 === '')
if (context.user ) {
setTimeout(() => {
router.push('/dashboard')
}, 4400);
2022-03-23 09:41:57 +00:00
}
return <>
<div className={style["container-modal-main"]}>
{
(context.showPopUp) ? (context.user) ? <PopupMain message="Successfully Registered" mode="accept" /> :
<PopupMain message="Invalid Credentials" mode="error" /> : ""
}
</div>
2022-02-15 14:49:59 +00:00
<RegStyle>
2022-03-07 10:26:12 +00:00
<form onSubmit={handleSubmit} className="form-wrapper">
2022-02-15 14:49:59 +00:00
<p className="reg-title">Sign Up</p>
2022-02-15 14:49:59 +00:00
<div className="reg-field">
<div className="reg-label">
<FontAwesomeIcon icon={faUser} />
</div>
<input
className="reg-input"
name="name"
2022-02-15 14:49:59 +00:00
autoComplete="off"
type="text"
value={userData.name}
2022-02-15 14:49:59 +00:00
onChange={handleInput}
placeholder="Full Name"
/>
</div>
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>
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="repassword"
autoComplete="off"
onChange={handleInput}
type="password"
value={userData.repassword}
placeholder="Confirm Password"
/>
</div>
<button
type="submit"
className="submit-button"
disabled={disabledSubmitBtn}
>
2022-02-15 14:49:59 +00:00
Submit
</button>
2022-02-15 14:49:59 +00:00
<p className="foot-text">
Already registered? Login&nbsp;
<Link href="/login" exact className="foot-text underline">
here
</Link>
</p>
</form>
</RegStyle>
</>
2022-02-08 15:21:06 +00:00
}
2022-02-15 15:14:21 +00:00
export default Reg