129 lines
3.7 KiB
React
Raw Normal View History

2023-03-13 18:35:26 +03:00
import React, { useEffect, useRef, useState } from "react";
2023-05-24 13:49:09 +03:00
import { useNavigate } from "react-router-dom";
import { useDispatch, useSelector } from "react-redux";
2021-11-30 16:00:58 +02:00
2023-05-30 10:10:34 +03:00
import { auth, selectAuth, setUserInfo } from "@redux/outstaffingSlice";
import { loading, selectIsLoading } from "@redux/loaderSlice";
import { setRole } from "@redux/roleSlice";
import ModalRegistration from "@components/Modal/ModalRegistration/ModalRegistration";
import ModalErrorLogin from "@components/Modal/ModalErrorLogin/ModalErrorLogin";
import { Loader } from "@components/Loader/Loader";
import { apiRequest } from "@api/request";
2023-01-18 17:37:52 +03:00
2023-05-30 10:10:34 +03:00
import ellipse from "assets/icons/ellipse.png";
2021-11-30 16:00:58 +02:00
2023-03-13 18:35:26 +03:00
import "./authBox.scss";
2021-11-30 16:00:58 +02:00
2023-03-14 18:49:45 +03:00
export const AuthBox = ({ title }) => {
const dispatch = useDispatch();
const ref = useRef();
2023-01-18 17:37:52 +03:00
const navigate = useNavigate();
2023-01-18 17:37:52 +03:00
const isAuth = useSelector(selectAuth);
const isLoading = useSelector(selectIsLoading);
const [error, setError] = useState(null);
2023-03-22 15:33:12 +03:00
const [modalError, setModalError] = useState(false);
2023-03-28 20:42:18 +03:00
const [modalReg, setModalReg] = useState(false);
2023-01-18 17:37:52 +03:00
useEffect(() => {
2023-03-13 18:35:26 +03:00
if (!localStorage.getItem("auth_token")) {
dispatch(auth(false));
}
}, []);
useEffect(() => {
if (isAuth) {
2023-03-13 18:35:26 +03:00
navigate("/");
}
});
const submitHandler = () => {
let formData = new FormData(ref.current);
if (!isLoading) {
dispatch(loading(true));
2023-03-13 18:35:26 +03:00
apiRequest("/user/login", {
method: "POST",
data: formData,
}).then((res) => {
if (!res.access_token) {
setError("Введены некоректные данные для входа");
2023-03-22 15:33:12 +03:00
setModalError(true);
2023-03-13 18:35:26 +03:00
dispatch(loading(false));
} else {
2023-03-13 18:35:26 +03:00
localStorage.setItem("auth_token", res.access_token);
localStorage.setItem("id", res.id);
localStorage.setItem("cardId", res.card_id);
localStorage.setItem("role_status", res.status);
localStorage.setItem(
2023-03-13 18:35:26 +03:00
"access_token_expired_at",
res.access_token_expired_at
);
dispatch(auth(true));
dispatch(setUserInfo(res));
dispatch(loading(false));
2023-03-13 18:35:26 +03:00
dispatch(setRole("ROLE_PARTNER"));
}
2023-03-13 18:35:26 +03:00
});
}
};
2022-06-01 19:59:54 +03:00
2021-11-30 16:00:58 +02:00
return (
2023-03-13 18:35:26 +03:00
<div className="auth-box">
<h2 className="auth-box__header">
Войти в <span>систему</span>
</h2>
<div className="auth-box__title">
2023-05-24 13:49:09 +03:00
<img src={ellipse} alt="." />
2023-03-13 18:35:26 +03:00
<span>{title}</span>
</div>
2023-03-13 18:35:26 +03:00
<form ref={ref} className="auth-box__form">
<label htmlFor="login">Ваш логин:</label>
<input id="login" type="text" name="username" placeholder="Логин" />
<label htmlFor="password">Пароль:</label>
<input
id="password"
type="password"
name="password"
placeholder="Пароль"
/>
{error && (
<div className="auth-box__form-error">
2023-03-22 15:33:12 +03:00
<ModalErrorLogin
active={modalError}
setActive={setModalError}
title={error}
/>
2023-03-13 18:35:26 +03:00
</div>
)}
<div className="auth-box__form-buttons">
<button
className="auth-box__form-btn"
onClick={(e) => {
e.preventDefault();
submitHandler();
}}
>
{isLoading ? <Loader /> : "Войти"}
</button>
2023-03-28 20:42:18 +03:00
<ModalRegistration active={modalReg} setActive={setModalReg} />
2023-03-14 18:49:45 +03:00
<button
className="auth-box__form-btn--role auth-box__auth-link"
2023-03-22 15:33:12 +03:00
onClick={(e) => {
e.preventDefault();
2023-03-28 20:42:18 +03:00
setModalReg(true);
2023-03-22 15:33:12 +03:00
}}
2023-03-14 18:49:45 +03:00
>
Регистрация
</button>
2023-03-13 18:35:26 +03:00
</div>
</form>
</div>
);
};