| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- /* eslint-disable */
- import React, { useEffect, useState } from "react";
- import PropTypes from "prop-types";
- import { useFormik } from "formik";
- import { useDispatch, useSelector } from "react-redux";
- import { NavLink } from "react-router-dom";
- import { useTranslation } from "react-i18next";
- import {
- clearLoginErrors,
- fetchUser,
- } from "../../store/actions/login/loginActions";
- import { selectLoginError } from "../../store/selectors/loginSelectors";
- import { FORGOT_PASSWORD_PAGE, HOME_PAGE } from "../../constants/pages";
- import {
- Box,
- Button,
- Container,
- Grid,
- IconButton,
- InputAdornment,
- Link,
- TextField,
- Typography,
- } from "@mui/material";
- import { Visibility, VisibilityOff } from "@mui/icons-material";
- import Backdrop from "../../components/MUI/BackdropComponent";
- import ErrorMessage from "../../components/MUI/ErrorMessageComponent";
- import { selectIsLoadingByActionType } from "../../store/selectors/loadingSelectors";
- import {
- LOGIN_USER_SCOPE,
- } from "../../store/actions/login/loginActionConstants";
- import loginValidation from "../../validations/loginValidation";
- import loginInitialValues from "../../initialValues/loginInitialValues";
-
- const LoginPage = ({ history }) => {
- const dispatch = useDispatch();
- const { t } = useTranslation();
- const error = useSelector(selectLoginError);
-
- const [showPassword, setShowPassword] = useState(false);
- const handleClickShowPassword = () => setShowPassword(!showPassword);
- const handleMouseDownPassword = () => setShowPassword(!showPassword);
-
- // Clear login errors when user firstly enters the page
- useEffect(() => {
- dispatch(clearLoginErrors());
- }, []);
-
- const isLoading = useSelector(selectIsLoadingByActionType(LOGIN_USER_SCOPE));
-
- const handleApiResponseSuccess = () => {
- history.push({
- pathname: HOME_PAGE,
- state: {
- from: history.location.pathname,
- },
- });
- };
-
- const handleSubmit = (values) => {
- const { email, password} = values;
- dispatch(clearLoginErrors());
- dispatch(
- fetchUser({
- identifier: email,
- password,
- handleApiResponseSuccess,
- })
- );
- };
-
- const formik = useFormik({
- initialValues: loginInitialValues,
- validationSchema: loginValidation,
- onSubmit: handleSubmit,
- validateOnBlur: true,
- enableReinitialize: true,
- });
-
- return (
- <Container component="main" maxWidth="md">
- <Box
- sx={{
- marginTop: 32,
- display: "flex",
- flexDirection: "column",
- alignItems: "center",
- }}
- >
- <Typography component="h1" variant="h5">
- {t("login.logInTitle")}
- </Typography>
- {error && <ErrorMessage error={error} />}
- <Box
- component="form"
- onSubmit={formik.handleSubmit}
- sx={{ position: "relative", mt: 1, p: 1 }}
- >
- <Backdrop position="absolute" isLoading={isLoading} />
- <TextField
- name="email"
- label={t("common.labelEmail")}
- margin="normal"
- value={formik.values.email}
- onChange={formik.handleChange}
- error={formik.touched.email && Boolean(formik.errors.email)}
- helperText={formik.touched.email && formik.errors.email}
- autoFocus
- fullWidth
- />
- <TextField
- name="password"
- label={t("common.labelPassword")}
- margin="normal"
- type={showPassword ? "text" : "password"}
- value={formik.values.password}
- onChange={formik.handleChange}
- error={formik.touched.password && Boolean(formik.errors.password)}
- helperText={formik.touched.password && formik.errors.password}
- fullWidth
- InputProps={{
- endAdornment: (
- <InputAdornment position="end">
- <IconButton
- onClick={handleClickShowPassword}
- onMouseDown={handleMouseDownPassword}
- >
- {showPassword ? <Visibility /> : <VisibilityOff />}
- </IconButton>
- </InputAdornment>
- ),
- }}
- />
- <Button
- type="submit"
- variant="contained"
- sx={{ mt: 3, mb: 2 }}
- fullWidth
- >
- {t("login.logIn")}
- </Button>
- <Grid container>
- <Grid
- item
- xs={12}
- md={6}
- sx={{ textAlign: { xs: "center", md: "left" } }}
- >
- <Link
- to={FORGOT_PASSWORD_PAGE}
- component={NavLink}
- variant="body2"
- underline="hover"
- >
- {t("login.forgotYourPassword")}
- </Link>
- </Grid>
- <Grid
- item
- xs={12}
- md={6}
- sx={{ textAlign: { xs: "center", md: "right" } }}
- >
- <Link
- to="#"
- component={NavLink}
- variant="body2"
- underline="hover"
- >
- {t("login.dontHaveAccount")}
- </Link>
- </Grid>
- </Grid>
- </Box>
- </Box>
- </Container>
- );
- };
-
- LoginPage.propTypes = {
- history: PropTypes.shape({
- replace: PropTypes.func,
- push: PropTypes.func,
- location: PropTypes.shape({
- pathname: PropTypes.string,
- }),
- }),
- };
- export default LoginPage;
|