2023-03-12 02:32:56 +00:00
|
|
|
|
import { useNavigate } from "react-router-dom";
|
2023-04-17 12:24:09 +00:00
|
|
|
|
import { enqueueSnackbar } from "notistack";
|
|
|
|
|
import { useTheme } from "@mui/material/styles";
|
2023-05-21 11:05:19 +00:00
|
|
|
|
import { Formik, Field, Form, FormikHelpers } from "formik";
|
2023-04-17 12:24:09 +00:00
|
|
|
|
import { Link } from "react-router-dom";
|
2023-09-01 13:17:24 +00:00
|
|
|
|
import {
|
|
|
|
|
Box,
|
|
|
|
|
Checkbox,
|
|
|
|
|
Typography,
|
|
|
|
|
FormControlLabel,
|
|
|
|
|
Button,
|
|
|
|
|
} from "@mui/material";
|
2023-04-17 12:24:09 +00:00
|
|
|
|
import Logo from "@pages/Logo";
|
|
|
|
|
import OutlinedInput from "@kitUI/outlinedInput";
|
2023-03-12 00:16:15 +00:00
|
|
|
|
import EmailOutlinedIcon from "@mui/icons-material/EmailOutlined";
|
2023-04-17 12:24:09 +00:00
|
|
|
|
import LockOutlinedIcon from "@mui/icons-material/LockOutlined";
|
2023-09-01 13:17:24 +00:00
|
|
|
|
import { signin } from "@root/api/auth";
|
2023-03-12 00:16:15 +00:00
|
|
|
|
|
2023-03-12 02:32:56 +00:00
|
|
|
|
interface Values {
|
2023-06-07 12:46:56 +00:00
|
|
|
|
email: string;
|
|
|
|
|
password: string;
|
2023-03-12 02:32:56 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
function validate(values: Values) {
|
2023-06-07 12:46:56 +00:00
|
|
|
|
const errors = {} as any;
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
2023-06-07 12:46:56 +00:00
|
|
|
|
if (!values.email) {
|
2023-07-25 18:48:52 +00:00
|
|
|
|
errors.email = "Обязательное поле";
|
|
|
|
|
} else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(values.email)) {
|
|
|
|
|
errors.email = "Неверный формат эл. почты";
|
2023-06-07 12:46:56 +00:00
|
|
|
|
}
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
2023-06-07 12:46:56 +00:00
|
|
|
|
if (!values.password) {
|
2023-07-25 18:48:52 +00:00
|
|
|
|
errors.password = "Введите пароль";
|
2023-06-07 12:46:56 +00:00
|
|
|
|
}
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
2023-06-07 12:46:56 +00:00
|
|
|
|
if (values.password && !/^[\S]{8,25}$/i.test(values.password)) {
|
|
|
|
|
errors.password = "Invalid password";
|
|
|
|
|
}
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
2023-06-07 12:46:56 +00:00
|
|
|
|
return errors;
|
2023-03-12 02:32:56 +00:00
|
|
|
|
}
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
|
|
|
|
const SigninForm = () => {
|
2023-06-07 12:46:56 +00:00
|
|
|
|
const theme = useTheme();
|
|
|
|
|
const navigate = useNavigate();
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
2023-06-07 12:46:56 +00:00
|
|
|
|
const initialValues: Values = {
|
|
|
|
|
email: "",
|
|
|
|
|
password: "",
|
|
|
|
|
};
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
2023-09-01 13:17:24 +00:00
|
|
|
|
const onSignFormSubmit = async (
|
|
|
|
|
values: Values,
|
|
|
|
|
formikHelpers: FormikHelpers<Values>
|
|
|
|
|
) => {
|
2023-06-07 12:46:56 +00:00
|
|
|
|
formikHelpers.setSubmitting(true);
|
2023-09-01 13:17:24 +00:00
|
|
|
|
|
|
|
|
|
const [_, signinError] = await signin(values.email, values.password);
|
|
|
|
|
|
|
|
|
|
formikHelpers.setSubmitting(false);
|
|
|
|
|
|
|
|
|
|
if (signinError) {
|
|
|
|
|
return enqueueSnackbar(signinError);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
navigate("/users");
|
2023-06-07 12:46:56 +00:00
|
|
|
|
};
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
2023-06-07 12:46:56 +00:00
|
|
|
|
return (
|
2023-09-01 13:17:24 +00:00
|
|
|
|
<Formik
|
|
|
|
|
initialValues={initialValues}
|
|
|
|
|
validate={validate}
|
|
|
|
|
onSubmit={onSignFormSubmit}
|
|
|
|
|
>
|
2023-06-07 12:46:56 +00:00
|
|
|
|
{(props) => (
|
|
|
|
|
<Form>
|
|
|
|
|
<Box
|
|
|
|
|
component="section"
|
|
|
|
|
sx={{
|
|
|
|
|
minHeight: "100vh",
|
|
|
|
|
height: "100%",
|
|
|
|
|
width: "100%",
|
|
|
|
|
backgroundColor: theme.palette.content.main,
|
|
|
|
|
display: "flex",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
padding: "15px 0",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Box
|
|
|
|
|
component="article"
|
|
|
|
|
sx={{
|
|
|
|
|
width: "350px",
|
|
|
|
|
backgroundColor: theme.palette.content.main,
|
|
|
|
|
display: "flex",
|
|
|
|
|
flexDirection: "column",
|
|
|
|
|
justifyContent: "center",
|
|
|
|
|
"> *": {
|
|
|
|
|
marginTop: "15px",
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<Logo />
|
|
|
|
|
<Box>
|
|
|
|
|
<Typography variant="h5" color={theme.palette.secondary.main}>
|
|
|
|
|
Добро пожаловать
|
|
|
|
|
</Typography>
|
|
|
|
|
<Typography variant="h6" color={theme.palette.secondary.main}>
|
|
|
|
|
Мы рады что вы выбрали нас!
|
|
|
|
|
</Typography>
|
|
|
|
|
</Box>
|
2023-09-01 13:17:24 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
marginTop: "15px",
|
|
|
|
|
"> *": { marginRight: "10px" },
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-06-07 12:46:56 +00:00
|
|
|
|
<EmailOutlinedIcon htmlColor={theme.palette.golden.main} />
|
2023-07-25 18:48:52 +00:00
|
|
|
|
<Field
|
|
|
|
|
as={OutlinedInput}
|
|
|
|
|
name="email"
|
|
|
|
|
variant="filled"
|
|
|
|
|
label="Эл. почта"
|
|
|
|
|
error={props.touched.email && !!props.errors.email}
|
|
|
|
|
helperText={
|
|
|
|
|
<Typography sx={{ fontSize: "12px", width: "200px" }}>
|
|
|
|
|
{props.touched.email && props.errors.email}
|
|
|
|
|
</Typography>
|
|
|
|
|
}
|
|
|
|
|
/>
|
2023-06-07 12:46:56 +00:00
|
|
|
|
</Box>
|
2023-09-01 13:17:24 +00:00
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
marginTop: "15px",
|
|
|
|
|
"> *": { marginRight: "10px" },
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-06-07 12:46:56 +00:00
|
|
|
|
<LockOutlinedIcon htmlColor={theme.palette.golden.main} />
|
2023-07-25 18:48:52 +00:00
|
|
|
|
<Field
|
|
|
|
|
as={OutlinedInput}
|
|
|
|
|
type="password"
|
|
|
|
|
name="password"
|
|
|
|
|
variant="filled"
|
|
|
|
|
label="Пароль"
|
|
|
|
|
error={props.touched.password && !!props.errors.password}
|
|
|
|
|
helperText={
|
|
|
|
|
<Typography sx={{ fontSize: "12px", width: "200px" }}>
|
|
|
|
|
{props.touched.password && props.errors.password}
|
|
|
|
|
</Typography>
|
|
|
|
|
}
|
|
|
|
|
/>
|
2023-06-07 12:46:56 +00:00
|
|
|
|
</Box>
|
|
|
|
|
<Box
|
|
|
|
|
component="article"
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
alignItems: "center",
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
<FormControlLabel
|
|
|
|
|
sx={{ color: "white" }}
|
|
|
|
|
control={
|
|
|
|
|
<Checkbox
|
|
|
|
|
value="checkedA"
|
|
|
|
|
inputProps={{ "aria-label": "Checkbox A" }}
|
|
|
|
|
sx={{
|
|
|
|
|
color: "white",
|
|
|
|
|
transform: "scale(1.5)",
|
|
|
|
|
"&.Mui-checked": {
|
|
|
|
|
color: "white",
|
|
|
|
|
},
|
|
|
|
|
"&.MuiFormControlLabel-root": {
|
|
|
|
|
color: "white",
|
|
|
|
|
},
|
|
|
|
|
}}
|
|
|
|
|
/>
|
|
|
|
|
}
|
|
|
|
|
label="Запомнить этот компьютер"
|
|
|
|
|
/>
|
|
|
|
|
</Box>
|
|
|
|
|
<Link to="/restore" style={{ textDecoration: "none" }}>
|
2023-09-01 13:17:24 +00:00
|
|
|
|
<Typography color={theme.palette.golden.main}>
|
|
|
|
|
Забыли пароль?
|
|
|
|
|
</Typography>
|
2023-06-07 12:46:56 +00:00
|
|
|
|
</Link>
|
|
|
|
|
<Button
|
|
|
|
|
type="submit"
|
|
|
|
|
disabled={props.isSubmitting}
|
|
|
|
|
sx={{
|
|
|
|
|
width: "250px",
|
|
|
|
|
margin: "15px auto",
|
|
|
|
|
padding: "20px 30px",
|
|
|
|
|
fontSize: 18,
|
|
|
|
|
}}
|
|
|
|
|
>
|
|
|
|
|
Войти
|
|
|
|
|
</Button>
|
|
|
|
|
<Box
|
|
|
|
|
sx={{
|
|
|
|
|
display: "flex",
|
|
|
|
|
}}
|
|
|
|
|
>
|
2023-09-01 13:17:24 +00:00
|
|
|
|
<Typography color={theme.palette.secondary.main}>
|
|
|
|
|
У вас нет аккаунта?
|
|
|
|
|
</Typography>
|
2023-06-07 12:46:56 +00:00
|
|
|
|
<Link to="/signup" style={{ textDecoration: "none" }}>
|
2023-09-01 13:17:24 +00:00
|
|
|
|
<Typography color={theme.palette.golden.main}>
|
|
|
|
|
Зарегестрируйтесь
|
|
|
|
|
</Typography>
|
2023-06-07 12:46:56 +00:00
|
|
|
|
</Link>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</Box>
|
|
|
|
|
</Form>
|
|
|
|
|
)}
|
|
|
|
|
</Formik>
|
|
|
|
|
);
|
2023-04-17 12:24:09 +00:00
|
|
|
|
};
|
2023-04-18 10:48:29 +00:00
|
|
|
|
|
2023-11-14 11:07:48 +00:00
|
|
|
|
export default SigninForm;
|