File size: 3,156 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
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
import * as SessionAPIUtil from '../util/session_api_util';

export const RECEIVE_USER = 'RECEIVE_USER';
export const LOGOUT_CURRENT_USER = 'LOGOUT_CURRENT_USER';
export const REMOVE_LOGIN_ERRORS = 'REMOVE_LOGIN_ERRORS';
export const REMOVE_SIGNUP_ERRORS = 'REMOVE_SIGNUP_ERRORS';
export const REMOVE_LOGOUT_ERRORS = 'REMOVE_LOGOUT_ERRORS';
export const REMOVE_ERRORS = 'REMOVE_ERRORS';
export const RECEIVE_ALL_USERS = 'RECEIVE_ALL_USERS';
export const RECEIVE_SEARCH = 'RECEIVE_SEARCH';
export const CLEAR_SEARCH_RESULTS = 'CLEAR_SEARCH_RESULTS';
export const RECEIVE_CURRENT_USER = 'RECEIVE_CURRENT_USER';
export const receiveCurrentUser = user => ({
  type: RECEIVE_CURRENT_USER,
  currentUser: user
});

export const receiveUser = user => ({
  type: RECEIVE_USER,
  user
});

export const receiveAllUsers = (users) => ({
  type: RECEIVE_ALL_USERS,
  users
});


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

export const receiveLoginErrors = (errors) => ({
  type: REMOVE_LOGIN_ERRORS,
  errors
});

export const receiveSignupErrors = (errors) => ({
  type: REMOVE_SIGNUP_ERRORS,
  errors
});

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

export const receiveLogoutErrors = (errors) => ({
  type: REMOVE_LOGOUT_ERRORS,
  errors
});


export const login = (user) => dispatch => {
  return SessionAPIUtil.login(user)
  .then(newUser => {
    dispatch(receiveCurrentUser(newUser));
  }, (err => {
    dispatch(receiveLoginErrors(err.responseJSON));
  }));
};

export const demologin = () => dispatch => {
  return SessionAPIUtil.demologin().then(user=> {
    return dispatch(receiveCurrentUser(user));
  });
};



export const signup = (user) => dispatch => {
  return SessionAPIUtil.signup(user)
  .then(newUser => {
    return dispatch(receiveCurrentUser(newUser));
  }, (err => {
    dispatch(receiveSignupErrors(err.responseJSON));
  }));
};

export const logout = () => dispatch => {
  return SessionAPIUtil.logout()
  .then(()=> {
    dispatch(logoutCurrentUser());
  }, (err) => {
    dispatch(receiveLogoutErrors(err.responseJSON));
  });
};

export const fetchUser = (id) => {
  return (dispatch) => {
    return SessionAPIUtil.fetchUser(id).then(
      (success) => dispatch(receiveUser(success)),
      (err) => dispatch(receiveErrors(err))
    );
  };
};

export const fetchAllUsers = () => {
  return (dispatch) => {
    return SessionAPIUtil.fetchAllUsers().then(
      (users) => dispatch(receiveAllUsers(users)),
      (err) => dispatch(receiveErrors(err))
    );
  };
};

export const updateUser = (data) => {
  return (dispatch) => {
    return SessionAPIUtil.updateUser(data).then(
      (success) => dispatch(receiveUser(success)),
      (err) => dispatch(receiveErrors(err))
    );
  };
};

const receiveSearch = (results) => {
  return {
    type: RECEIVE_SEARCH,
    results
  };
};

export const searchUsers = (query) => (dispatch) => {
  return SessionAPIUtil.searchUsers(query).then((users) =>
  dispatch(receiveSearch(users)), (err) => dispatch(receiveErrors(err)));
};
export const clearSearchResults = () => {
  return {
    type: CLEAR_SEARCH_RESULTS
  };
};