-
Notifications
You must be signed in to change notification settings - Fork 0
/
sessionActions.js
70 lines (60 loc) · 1.87 KB
/
sessionActions.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
70
import * as APIUtil from '../util/sessionAPIUtil';
import { setRedirect } from './uiActions';
import { processResponse } from '../util/APIRequestHelpers';
export const RECEIVE_CURRENT_USER = 'RECEIVE_CURRENT_USER';
export const LOGOUT_CURRENT_USER = 'LOGOUT_CURRENT_USER';
export const RECEIVE_SESSION_ERRORS = 'RECEIVE_SESSION_ERRORS';
export const RESET_SESSION_ERRORS = 'RESET_SESSION_ERRORS';
export const receiveCurrentUser = currentUser => ({
type: RECEIVE_CURRENT_USER,
currentUser,
});
export const logoutCurrentUser = () => ({
type: LOGOUT_CURRENT_USER,
});
export const receiveSessionErrors = errors => ({
type: RECEIVE_SESSION_ERRORS,
errors,
});
export const resetSessionErrors = () => ({
type: RESET_SESSION_ERRORS,
});
export const signUp = user => dispatch => (
APIUtil.signUp(user).then(response => processResponse(response))
.then(user => {
dispatch(receiveCurrentUser(user));
dispatch(setRedirect('/artworks'));
}).catch(errors => (
dispatch(receiveSessionErrors(errors))
))
);
export const editProfile = user => dispatch => (
APIUtil.editProfile(user).then(response => processResponse(response))
.then(user => {
dispatch(receiveCurrentUser(user))
dispatch(setRedirect(`/users/${user.id}`))
}).catch(errors => (
dispatch(receiveSessionErrors(errors))
))
);
export const login = user => dispatch => (
APIUtil.login(user).then(response => processResponse(response))
.then(user => {
dispatch(receiveCurrentUser(user));
dispatch(setRedirect('/artworks'));
}).catch(errors => (
dispatch(receiveSessionErrors(errors))
))
);
export const logout = () => dispatch => (
APIUtil.logout().then(response => response.json())
.then(user => (
dispatch(logoutCurrentUser())
))
);
export const deleteAccount = id => dispatch => (
APIUtil.deleteAccount(id)
.then(response => (
dispatch(logoutCurrentUser())
))
);