123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354 |
- import {
- actionLoginSuccess,
- actionLoginReject,
- actionLogoutSuccess,
- actionLogoutReject,
- } from '../action';
- import { actionLoading } from '../../loading/action';
- import { loginGQL, registerGQL } from '../../../api-data';
- const asyncLogin = (login: string, password: string) => async (
- dispatch: any,
- ) => {
- try {
- dispatch(actionLoading(true));
- const token = await loginGQL<string>(login, password);
- if (!token) throw new Error('Wrong credentials');
- localStorage.token = token;
- dispatch(actionLoginSuccess({ login, token }));
- } catch (e) {
- dispatch(actionLoginReject());
- } finally {
- dispatch(actionLoading(false));
- }
- };
- const asyncLogout = () => async (dispatch: any) => {
- try {
- dispatch(actionLoading(true));
- dispatch(actionLogoutSuccess({ login: '', token: '' }));
- localStorage.token = null;
- } catch (e) {
- dispatch(actionLogoutReject());
- } finally {
- dispatch(actionLoading(false));
- }
- };
- const asyncCreateUser = (login: string, password: string) => async (
- dispatch: any,
- ) => {
- try {
- dispatch(actionLoading(true));
- await registerGQL(login, password);
- const token = await loginGQL<string>(login, password);
- if (!token) throw new Error('Server error');
- dispatch(actionLoginSuccess({ login, token }));
- } catch (e) {
- dispatch(actionLoginReject());
- } finally {
- dispatch(actionLoading(false));
- }
- };
- export { asyncLogin, asyncLogout, asyncCreateUser };
|