2020-08-03 12:04:07 +02:00
|
|
|
import { useContext, useState } from 'react'
|
|
|
|
import { useRouter } from 'next/router'
|
|
|
|
import Link from 'next/link'
|
|
|
|
import htmlParser from 'html-react-parser'
|
|
|
|
import Loader from '../../components/Loader'
|
|
|
|
import HeadTag from '../../components/HeadTag'
|
|
|
|
import { UserContext } from '../../contexts/UserContext'
|
|
|
|
import withoutAuth from '../../hoc/withoutAuth'
|
2020-03-31 07:48:00 +02:00
|
|
|
|
|
|
|
const Login = () => {
|
2020-08-03 12:04:07 +02:00
|
|
|
const router = useRouter()
|
|
|
|
const [inputState, setInputState] = useState({})
|
|
|
|
const { loginUser, messageLogin, loginLoading, isAuth } = useContext(UserContext)
|
2020-03-31 07:48:00 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const handleChange = (event) => {
|
|
|
|
const inputStateNew = { ...inputState }
|
|
|
|
inputStateNew[event.target.name] = event.target.value
|
|
|
|
setInputState(inputStateNew)
|
|
|
|
}
|
2020-04-06 16:46:03 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
const handleSubmit = async (event) => {
|
|
|
|
event.preventDefault()
|
|
|
|
if (!isAuth) {
|
|
|
|
await loginUser(inputState)
|
2020-04-06 16:46:03 +02:00
|
|
|
}
|
2020-08-03 12:04:07 +02:00
|
|
|
}
|
2020-04-06 16:46:03 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<HeadTag
|
|
|
|
title='Se connecter - FunctionProject'
|
|
|
|
description='Connexion à FunctionProject.'
|
|
|
|
/>
|
|
|
|
<div className='container Register-Login__container'>
|
|
|
|
<div className='row Register-Login__row justify-content-center'>
|
|
|
|
<div className='col-20'>
|
|
|
|
<h1 className='Register-Login__title'>Se connecter</h1>
|
|
|
|
<form onSubmit={handleSubmit}>
|
|
|
|
<div className='form-group'>
|
|
|
|
<label className='form-label' htmlFor='email'>Email :</label>
|
|
|
|
<input onChange={handleChange} type='email' name='email' id='email' className='form-control' placeholder='email@gmail.com' />
|
|
|
|
</div>
|
2020-03-31 07:48:00 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='form-group'>
|
|
|
|
<label className='form-label' htmlFor='password'>Mot de passe :</label>
|
|
|
|
<input onChange={handleChange} type='password' name='password' id='password' className='form-control' placeholder='******' />
|
|
|
|
<p>
|
|
|
|
<Link href='/users/forgotPassword'>
|
|
|
|
<a className='Register-Login__Forgot-password'>Mot de passe oublié ?</a>
|
|
|
|
</Link>
|
|
|
|
</p>
|
|
|
|
</div>
|
2020-03-31 07:48:00 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
<div className='form-group text-center'>
|
|
|
|
<button type='submit' className='btn btn-dark'>Envoyer</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
|
|
|
<div className='form-result text-center'>
|
|
|
|
{(router.query.isConfirmed !== undefined && messageLogin === '') && <p className='form-success'><b>Succès:</b> Votre compte a bien été confirmé, vous pouvez maintenant vous connectez!</p>}
|
|
|
|
{(router.query.isSuccessEdit !== undefined && messageLogin === '') && <p className='form-success'><b>Succès:</b> Votre profil a bien été modifié, vous pouvez maintenant vous connectez!</p>}
|
|
|
|
{
|
|
|
|
(loginLoading)
|
|
|
|
? <Loader />
|
|
|
|
: htmlParser(messageLogin)
|
|
|
|
}
|
2020-03-31 07:48:00 +02:00
|
|
|
</div>
|
2020-08-03 12:04:07 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
)
|
2020-03-31 07:48:00 +02:00
|
|
|
}
|
2020-04-06 16:46:03 +02:00
|
|
|
|
2020-08-03 12:04:07 +02:00
|
|
|
export default withoutAuth(Login)
|