2023-02-02 18:10:44 +03:00
|
|
|
import React, {useEffect, useRef, useState} from 'react'
|
2023-01-18 17:37:52 +03:00
|
|
|
import {Link, useNavigate} from 'react-router-dom'
|
2023-01-13 13:02:48 +03:00
|
|
|
import {useDispatch, useSelector} from 'react-redux'
|
2021-11-30 16:00:58 +02:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
import {Loader} from '../Loader/Loader'
|
2023-01-25 16:50:36 +03:00
|
|
|
import ErrorBoundary from "../../hoc/ErrorBoundary";
|
2023-01-13 13:02:48 +03:00
|
|
|
|
2023-01-18 17:37:52 +03:00
|
|
|
import {auth, selectAuth, setUserInfo} from '../../redux/outstaffingSlice'
|
2023-01-13 13:02:48 +03:00
|
|
|
import {loading} from '../../redux/loaderSlice'
|
|
|
|
import {setRole} from '../../redux/roleSlice'
|
|
|
|
import {selectIsLoading} from '../../redux/loaderSlice'
|
2021-11-30 16:00:58 +02:00
|
|
|
|
2023-01-20 16:20:06 +03:00
|
|
|
import {apiRequest} from "../../api/request";
|
2023-01-18 17:37:52 +03:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
import ellipse from '../../images/ellipse.png'
|
2021-11-30 16:00:58 +02:00
|
|
|
|
|
|
|
import './authBox.scss'
|
|
|
|
|
2023-02-02 18:10:44 +03:00
|
|
|
import Swal from 'sweetalert2'
|
|
|
|
import withReactContent from 'sweetalert2-react-content'
|
2023-01-25 16:50:36 +03:00
|
|
|
|
2023-02-02 18:10:44 +03:00
|
|
|
const SweetAlert = withReactContent(Swal);
|
2021-11-30 16:00:58 +02:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
|
|
|
|
export const AuthBox = ({title, altTitle, roleChangeLink}) => {
|
2023-02-02 18:10:44 +03:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
const dispatch = useDispatch();
|
2023-01-25 16:50:36 +03:00
|
|
|
const ref = useRef();
|
2023-01-18 17:37:52 +03:00
|
|
|
const navigate = useNavigate();
|
2023-01-13 13:02:48 +03:00
|
|
|
|
2023-01-18 17:37:52 +03:00
|
|
|
const isAuth = useSelector(selectAuth);
|
2023-01-13 13:02:48 +03:00
|
|
|
const isLoading = useSelector(selectIsLoading);
|
|
|
|
|
|
|
|
const [error, setError] = useState(null);
|
|
|
|
|
2023-02-02 18:10:44 +03:00
|
|
|
const handleModalError = (error) => {
|
|
|
|
SweetAlert.fire({
|
|
|
|
title: 'Ошибка',
|
|
|
|
text: error
|
|
|
|
});
|
|
|
|
|
|
|
|
setError(null)
|
|
|
|
|
|
|
|
};
|
2023-01-18 17:37:52 +03:00
|
|
|
|
2023-02-02 18:10:44 +03:00
|
|
|
useEffect(() => {
|
2023-01-20 16:20:06 +03:00
|
|
|
if (!localStorage.getItem('auth_token')) {
|
|
|
|
dispatch(auth(false))
|
|
|
|
}
|
|
|
|
}, []);
|
|
|
|
|
2023-02-02 18:10:44 +03:00
|
|
|
useEffect(() => {
|
|
|
|
if (isAuth) {
|
|
|
|
navigate('/')
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
const submitHandler = () => {
|
|
|
|
|
2023-02-02 18:10:44 +03:00
|
|
|
let formData = new FormData(ref.current);
|
2023-01-13 13:02:48 +03:00
|
|
|
if (!isLoading) {
|
|
|
|
dispatch(loading(true));
|
|
|
|
apiRequest('/user/login',
|
|
|
|
{
|
|
|
|
method: 'POST',
|
2023-01-25 16:50:36 +03:00
|
|
|
data: formData
|
2023-01-13 13:02:48 +03:00
|
|
|
}).then((res) => {
|
|
|
|
|
|
|
|
if (!res.access_token) {
|
2022-06-01 19:59:54 +03:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
setError('Некорректные данные для входа');
|
|
|
|
dispatch(loading(false))
|
2022-06-01 19:59:54 +03:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
} else {
|
|
|
|
|
|
|
|
localStorage.setItem('auth_token', res.access_token);
|
|
|
|
localStorage.setItem('id', res.id);
|
|
|
|
localStorage.setItem('cardId', res.card_id);
|
2023-02-27 16:50:32 +03:00
|
|
|
localStorage.setItem('role_status', res.status);
|
2023-01-13 13:02:48 +03:00
|
|
|
localStorage.setItem(
|
|
|
|
'access_token_expired_at',
|
|
|
|
res.access_token_expired_at
|
|
|
|
);
|
|
|
|
dispatch(auth(true));
|
|
|
|
dispatch(setUserInfo(res));
|
|
|
|
dispatch(loading(false));
|
|
|
|
dispatch(setRole('ROLE_PARTNER'))
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
};
|
2022-06-01 19:59:54 +03:00
|
|
|
|
2021-11-30 16:00:58 +02:00
|
|
|
return (
|
2023-01-13 13:02:48 +03:00
|
|
|
<div className='auth-box'>
|
|
|
|
<h2 className='auth-box__header'>
|
|
|
|
Войти в <span>систему</span>
|
|
|
|
</h2>
|
|
|
|
<div className='auth-box__title'>
|
|
|
|
<img src={ellipse} alt=''/>
|
|
|
|
<span>{title}</span>
|
2021-11-30 16:00:58 +02:00
|
|
|
</div>
|
2023-01-25 16:50:36 +03:00
|
|
|
<form ref={ref} className='auth-box__form'>
|
2023-01-13 13:02:48 +03:00
|
|
|
<label htmlFor='login'>Ваш логин:</label>
|
|
|
|
<input
|
|
|
|
id='login'
|
|
|
|
type='text'
|
2023-01-25 16:50:36 +03:00
|
|
|
name='username'
|
2023-01-13 13:02:48 +03:00
|
|
|
placeholder='Логин'
|
|
|
|
/>
|
|
|
|
|
|
|
|
<label htmlFor='password'>Пароль:</label>
|
|
|
|
<input
|
|
|
|
id='password'
|
|
|
|
type='password'
|
2023-01-25 16:50:36 +03:00
|
|
|
name='password'
|
2023-01-13 13:02:48 +03:00
|
|
|
placeholder='Пароль'
|
|
|
|
/>
|
|
|
|
|
|
|
|
{error && (
|
|
|
|
<div className='auth-box__form-error'>
|
2023-01-18 17:37:52 +03:00
|
|
|
<ErrorBoundary>
|
2023-02-02 18:10:44 +03:00
|
|
|
|
|
|
|
|
|
|
|
{
|
|
|
|
handleModalError(error)
|
|
|
|
}
|
|
|
|
{/*<SweetAlert*/}
|
|
|
|
{/* show={!!error}*/}
|
|
|
|
{/* title='Ошибка'*/}
|
|
|
|
{/* text={error}*/}
|
|
|
|
{/* onConfirm={() => setError(null)}*/}
|
|
|
|
{/*/>*/}
|
2023-01-18 17:37:52 +03:00
|
|
|
</ErrorBoundary>
|
2023-01-13 13:02:48 +03:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
|
|
|
|
<div className='auth-box__form-buttons'>
|
|
|
|
<button
|
|
|
|
className='auth-box__form-btn'
|
|
|
|
onClick={(e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
submitHandler()
|
|
|
|
}}
|
|
|
|
>
|
|
|
|
{isLoading ? <Loader/> : 'Войти'}
|
|
|
|
</button>
|
|
|
|
|
|
|
|
<Link to={roleChangeLink}>
|
|
|
|
<button className='auth-box__form-btn--role auth-box__auth-link'>
|
|
|
|
{altTitle}
|
|
|
|
</button>
|
|
|
|
</Link>
|
|
|
|
</div>
|
|
|
|
</form>
|
2023-01-18 17:37:52 +03:00
|
|
|
|
2023-01-13 13:02:48 +03:00
|
|
|
</div>
|
2021-11-30 16:00:58 +02:00
|
|
|
)
|
2023-01-13 13:02:48 +03:00
|
|
|
};
|