File size: 989 Bytes
f5071ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import * as ApiUtil from '../util/session_api_util';

export const RECEIVE_CURRENT_USER = 'RECEIVE_CURRENT_USER';
export const LOGOUT_CURRENT_USER = 'LOGOUT_CURRENT_USER';
export const RECEIVE_SESSION_ERRORS = 'RECEIVE_SESSION_ERRORS';

const receiveCurrentUser = currentUser => ({
  type: RECEIVE_CURRENT_USER,
  currentUser
});

const logoutCurrentUser = () => ({
  type: LOGOUT_CURRENT_USER
});

const receiveErrors = errors => ({
  type: RECEIVE_SESSION_ERRORS,
  errors
});

export const login = user => dispatch => (
  ApiUtil.login(user).then(
    res => dispatch(receiveCurrentUser(res)),
    errors => dispatch(receiveErrors(errors))
  )
);

export const logout = () => dispatch => (
  ApiUtil.logout().then(
    res => dispatch(logoutCurrentUser(res)),
    errors => dispatch(receiveErrors(errors))
  )
);

export const signup = user => dispatch => (
  ApiUtil.signup(user).then(
    res => dispatch(receiveCurrentUser(res)),
    errors => dispatch(receiveErrors(errors))
  )
);