add logout in AuthBlock and fix layout

This commit is contained in:
Никита Губарь 2024-01-10 16:28:33 +03:00
parent 0764002a60
commit 8d0cb1c831
4 changed files with 102 additions and 17 deletions

View File

@ -1,10 +1,73 @@
import React from "react"; import React, { useEffect, useRef, useState } from "react";
import { useDispatch, useSelector } from "react-redux";
import { useNavigate } from "react-router";
import { loading, selectIsLoading } from "@redux/loaderSlice";
import { auth, selectAuth, setUserInfo } from "@redux/outstaffingSlice";
import { setRole } from "@redux/roleSlice";
import { apiRequest } from "@api/request";
import { Loader } from "@components/Common/Loader/Loader";
import ModalErrorLogin from "@components/Modal/ModalErrorLogin/ModalErrorLogin";
import authImg from "assets/images/partnerProfile/authCandidateFormImg.png"; import authImg from "assets/images/partnerProfile/authCandidateFormImg.png";
import "./authBlock.scss"; import "./authBlock.scss";
export const AuthBlock = ({ title, description, img, resetModal }) => { export const AuthBlock = ({ title, description, img, resetModal }) => {
const dispatch = useDispatch();
const ref = useRef();
const navigate = useNavigate();
const isAuth = useSelector(selectAuth);
const isLoading = useSelector(selectIsLoading);
const [error, setError] = useState(null);
const [modalError, setModalError] = useState(false);
useEffect(() => {
if (!localStorage.getItem("auth_token")) {
dispatch(auth(false));
}
}, []);
useEffect(() => {
if (isAuth) {
navigate("/");
}
});
const submitHandler = () => {
let formData = new FormData(ref.current);
if (!isLoading) {
dispatch(loading(true));
apiRequest("/user/login", {
method: "POST",
data: formData
}).then((res) => {
if (!res.access_token) {
setError("Введены некорректные данные для входа");
setModalError(true);
dispatch(loading(false));
} else {
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(
"access_token_expired_at",
res.access_token_expired_at
);
dispatch(auth(true));
dispatch(setUserInfo(res));
dispatch(loading(false));
dispatch(setRole("ROLE_PARTNER"));
}
});
}
};
return ( return (
<div className="auth__wrapper"> <div className="auth__wrapper">
<div className="auth__info"> <div className="auth__info">
@ -12,9 +75,9 @@ export const AuthBlock = ({ title, description, img, resetModal }) => {
<img src={authImg} alt="img" /> <img src={authImg} alt="img" />
<p>{description}</p> <p>{description}</p>
</div> </div>
<form className="auth__form"> <form ref={ref} className="auth__form">
<label htmlFor="login">Ваш e-mail</label> <label htmlFor="login">Ваш e-mail</label>
<input id="login" type="text" name="username" placeholder="E-mail" /> <input id="login" type="email" name="email" placeholder="E-mail" />
<label htmlFor="password">Ваш пароль</label> <label htmlFor="password">Ваш пароль</label>
<input <input
@ -23,8 +86,27 @@ export const AuthBlock = ({ title, description, img, resetModal }) => {
name="password" name="password"
placeholder="Пароль" placeholder="Пароль"
/> />
{error && (
<div className="auth-box__form-error">
<ModalErrorLogin
active={modalError}
setActive={setModalError}
title={error}
/>
</div>
)}
<div className="auth__form__buttons"> <div className="auth__form__buttons">
<button onClick={(e) => e.preventDefault()}>Войти</button> <button
className="auth-box__form-btn"
onClick={(e) => {
e.preventDefault();
submitHandler();
}}
>
{isLoading ? <Loader /> : "Войти"}
</button>
<span onClick={() => resetModal(true)}>Восстановить пароль</span> <span onClick={() => resetModal(true)}>Восстановить пароль</span>
</div> </div>
</form> </form>

View File

@ -89,17 +89,19 @@
color: #000000; color: #000000;
} }
button { &__buttons {
background: #52b709; button {
border-radius: 44px; background: #52b709;
max-width: 130px; border-radius: 44px;
width: 100%; max-width: 130px;
border: none; width: 100%;
font-weight: 500; border: none;
font-size: 18px; font-weight: 500;
line-height: 32px; font-size: 18px;
color: white; line-height: 32px;
height: 45px; color: white;
height: 45px;
}
} }
&__buttons { &__buttons {

View File

@ -104,7 +104,6 @@ export const AuthBox = ({ title }) => {
alt="eye" alt="eye"
/> />
</div> </div>
{error && ( {error && (
<div className="auth-box__form-error"> <div className="auth-box__form-error">
<ModalErrorLogin <ModalErrorLogin
@ -114,7 +113,6 @@ export const AuthBox = ({ title }) => {
/> />
</div> </div>
)} )}
<div className="auth-box__form-buttons"> <div className="auth-box__form-buttons">
<button <button
className="auth-box__form-btn" className="auth-box__form-btn"

View File

@ -167,6 +167,9 @@
&__submit { &__submit {
width: 100%; width: 100%;
margin-top: 10px; margin-top: 10px;
align-items: center;
display: flex;
justify-content: center;
button { button {
border: none; border: none;