2020-04-06 16:46:03 +02:00
|
|
|
import { Fragment, useContext, useState } from 'react';
|
|
|
|
import { useRouter } from 'next/router';
|
|
|
|
import Link from 'next/link';
|
|
|
|
import htmlParser from 'html-react-parser';
|
2020-04-23 17:45:21 +02:00
|
|
|
import Loader from '../../components/Loader';
|
|
|
|
import HeadTag from '../../components/HeadTag';
|
|
|
|
import { UserContext } from '../../contexts/UserContext';
|
|
|
|
import withoutAuth from '../../hoc/withoutAuth';
|
|
|
|
import '../../public/css/pages/register-login.css';
|
2020-03-31 07:48:00 +02:00
|
|
|
|
|
|
|
const Login = () => {
|
|
|
|
|
2020-04-08 15:26:18 +02:00
|
|
|
const router = useRouter();
|
|
|
|
const [inputState, setInputState] = useState({});
|
|
|
|
const { loginUser, messageLogin, loginLoading, isAuth } = useContext(UserContext);
|
2020-04-06 16:46:03 +02:00
|
|
|
|
2020-04-08 00:47:24 +02:00
|
|
|
const handleChange = (event) => {
|
2020-04-06 16:46:03 +02:00
|
|
|
const inputStateNew = { ...inputState };
|
|
|
|
inputStateNew[event.target.name] = event.target.value;
|
|
|
|
setInputState(inputStateNew);
|
|
|
|
}
|
|
|
|
|
2020-04-08 15:26:18 +02:00
|
|
|
const handleSubmit = async (event) => {
|
2020-04-06 16:46:03 +02:00
|
|
|
event.preventDefault();
|
2020-04-08 15:26:18 +02:00
|
|
|
if (!isAuth) {
|
2020-04-12 12:40:56 +02:00
|
|
|
await loginUser(inputState);
|
2020-04-08 15:26:18 +02:00
|
|
|
}
|
2020-04-06 16:46:03 +02:00
|
|
|
}
|
|
|
|
|
2020-03-31 07:48:00 +02:00
|
|
|
return (
|
|
|
|
<Fragment>
|
|
|
|
<HeadTag
|
2020-04-07 13:56:56 +02:00
|
|
|
title="Se connecter - FunctionProject"
|
2020-03-31 07:48:00 +02:00
|
|
|
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>
|
2020-04-06 16:46:03 +02:00
|
|
|
<form onSubmit={handleSubmit}>
|
2020-03-31 07:48:00 +02:00
|
|
|
<div className="form-group">
|
2020-04-06 16:46:03 +02:00
|
|
|
<label className="form-label" htmlFor="email">Email :</label>
|
|
|
|
<input onChange={handleChange} type="email" name="email" id="email" className="form-control" placeholder="email@gmail.com" />
|
2020-03-31 07:48:00 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-group">
|
2020-04-06 16:46:03 +02:00
|
|
|
<label className="form-label" htmlFor="password">Mot de passe :</label>
|
|
|
|
<input onChange={handleChange} type="password" name="password" id="password" className="form-control" placeholder="******" />
|
|
|
|
<p>
|
2020-04-07 13:56:56 +02:00
|
|
|
<Link href={"/forgotPassword"}>
|
2020-04-06 16:46:03 +02:00
|
|
|
<a className="Register-Login__Forgot-password">Mot de passe oublié ?</a>
|
|
|
|
</Link>
|
|
|
|
</p>
|
2020-03-31 07:48:00 +02:00
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="form-group text-center">
|
|
|
|
<button type="submit" className="btn btn-dark">Envoyer</button>
|
|
|
|
</div>
|
|
|
|
</form>
|
2020-04-06 16:46:03 +02:00
|
|
|
<div className="form-result text-center">
|
2020-04-08 00:47:24 +02:00
|
|
|
{(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>}
|
2020-04-06 16:46:03 +02:00
|
|
|
{
|
|
|
|
(loginLoading) ?
|
|
|
|
<Loader />
|
|
|
|
:
|
|
|
|
htmlParser(messageLogin)
|
|
|
|
}
|
|
|
|
</div>
|
2020-03-31 07:48:00 +02:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</Fragment>
|
|
|
|
);
|
|
|
|
}
|
2020-04-06 16:46:03 +02:00
|
|
|
|
2020-04-12 12:40:56 +02:00
|
|
|
export default withoutAuth(Login);
|