import React, {useState} from 'react' import {Link} from 'react-router-dom' import {useDispatch, useSelector} from 'react-redux' import {withSwalInstance} from 'sweetalert2-react' import swal from 'sweetalert2' import {Loader} from '../Loader/Loader' import {auth, setUserInfo} from '../../redux/outstaffingSlice' import {loading} from '../../redux/loaderSlice' import {setRole} from '../../redux/roleSlice' import {selectIsLoading} from '../../redux/loaderSlice' import ellipse from '../../images/ellipse.png' import './authBox.scss' import {useRequest} from "../../hooks/useRequest"; const SweetAlert = withSwalInstance(swal); export const AuthBox = ({title, altTitle, roleChangeLink}) => { const dispatch = useDispatch(); const {apiRequest} = useRequest(); const isLoading = useSelector(selectIsLoading); const [username, setUsername] = useState(''); const [password, setPassword] = useState(''); const [error, setError] = useState(null); const submitHandler = () => { if (!isLoading) { dispatch(loading(true)); apiRequest('/user/login', { method: 'POST', data: JSON.stringify({ username, password }) }).then((res) => { if (!res.access_token) { setError('Некорректные данные для входа'); dispatch(loading(false)) } else { localStorage.setItem('auth_token', res.access_token); localStorage.setItem('id', res.id); localStorage.setItem('cardId', res.card_id); 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 (