import { AuthActionEnum , SetAuthAction , SetErrorAction , SetIsLoadingAction , SetUserAction } from "./types" ; import { IUser } from "../../../models/IUser" ; import { AppDispatch } from "../../index" ; import axios from "axios" ; import UserService from "../../../api/UserService" ; export const AuthActionCreators = { setUser : ( user ) : SetUserAction => ( { type : AuthActionEnum . SET_USER , payload : user } ) , setIsAuth : ( auth ) : SetAuthAction => ( { type : AuthActionEnum . SET_AUTH , payload : auth } ) , setIsLoading : ( payload ) : SetIsLoadingAction => ( { type : AuthActionEnum . SET_IS_LOADING , payload } ) , setError : ( payload ) : SetErrorAction => ( { type : AuthActionEnum . SET_ERROR , payload } ) , login : ( username , password ) => async ( dispatch ) => { try { dispatch ( AuthActionCreators . setIsLoading ( true ) ) ; setTimeout ( async ( ) => { const response = await UserService . getUsers ( ) ; const mockUser = response . data . find ( user => user . username === username && user . password === password ) ; if ( mockUser ) { localStorage . setItem ( 'auth' , 'true' ) ; localStorage . setItem ( 'username' , mockUser . username ) ; dispatch ( AuthActionCreators . setUser ( mockUser ) ) ; dispatch ( AuthActionCreators . setIsAuth ( true ) ) ; } else { dispatch ( AuthActionCreators . setError ( 'Некорректный логин или пароль' ) ) ; } dispatch ( AuthActionCreators . setIsLoading ( false ) ) ; } , 1000 ) ; } catch ( e ) { dispatch ( AuthActionCreators . setError ( 'Произошла ошибка при логине' ) ) ; } } , logout : ( ) => async ( dispatch ) => { localStorage . removeItem ( 'auth' ) ; localStorage . removeItem ( 'username' ) ; dispatch ( AuthActionCreators . setUser ( { } as IUser ) ) ; dispatch ( AuthActionCreators . setIsAuth ( false ) ) ; } } ;