This repository has been archived by the owner on Sep 29, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
user.actions.js
69 lines (59 loc) · 1.69 KB
/
user.actions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
import { push } from 'connected-react-router'
import { tokensConstants, userConstants } from '../constants'
import { usersService } from '../services'
import { alertActions } from './'
const requestTokenPair = () => ({
type: userConstants.REFRESH_TOKENS
})
const login = (login, password) => async dispatch => {
const request = () => ({ type: userConstants.LOGIN_REQUEST })
const success = user => ({ type: userConstants.LOGIN_SUCCESS, user })
const failure = () => ({ type: userConstants.LOGIN_FAILURE })
try {
dispatch(request())
const { user, refreshToken, accessToken } = await usersService.login(login, password)
dispatch(success(user))
dispatch({
type: tokensConstants.TOKENS_SET,
refreshToken,
accessToken
})
dispatch(alertActions.clear())
dispatch(push('/'))
} catch (err) {
dispatch(failure())
dispatch(alertActions.error(err.statusText))
}
}
const logout = () => dispatch => {
dispatch(logoutWithoutRedirect())
dispatch(push('/'))
}
const logoutWithoutRedirect = () => {
usersService.logout()
return { type: userConstants.LOGOUT }
}
const register = (email, login, password) => async dispatch => {
const request = () => ({ type: userConstants.REGISTER_REQUEST })
const success = async () => {
await usersService.register(email, login, password)
return { type: userConstants.REGISTER_SUCCESS }
}
const failure = () => ({ type: userConstants.REGISTER_FAILURE })
try {
dispatch(request())
await dispatch(success())
dispatch(alertActions.clear())
dispatch(push('/'))
} catch (err) {
dispatch(failure())
dispatch(alertActions.error(err.statusText))
}
}
export const userActions = {
login,
logout,
logoutWithoutRedirect,
register,
requestTokenPair
}