Blame view

redux/reducers/userReducers.js 4.21 KB
jay committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
import {
  LOAD_USER_REQUEST,
  LOAD_USER_SUCCESS,
  LOAD_USER_FAIL,
  FORGOT_PASSWORD_REQUEST,
  FORGOT_PASSWORD_SUCCESS,
  FORGOT_PASSWORD_FAIL,
  RESET_PASSWORD_REQUEST,
  RESET_PASSWORD_SUCCESS,
  RESET_PASSWORD_FAIL,
  UPDATE_USER_PROFILE_REQUEST,
  UPDATE_USER_PROFILE_SUCCESS,
  UPDATE_USER_PROFILE_RESET,
  UPDATE_USER_PROFILE_FAIL,
  REGISTER_USER_REQUEST,
  REGISTER_USER_SUCCESS,
  REGISTER_USER_FAIL,
jaymehta committed
18 19 20 21
  CLEAR_ERRORS,
  GET_END_USER_FAIL,
  GET_END_USER_SUCCESS,
  GET_END_USER_REQUEST
jay committed
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
} from "../constants/userConstants";

// Auth reducer
export const authReducer = (state = { loading: false, success: false, user: null }, action) => {
  switch (action.type) {
    case REGISTER_USER_REQUEST:
      return {
        loading: true
      };

    case REGISTER_USER_SUCCESS:
      console.log(`Received dispatch REGISTER_USER_SUCCESS`);

      return {
        loading: false,
        success: true
      };

    case REGISTER_USER_FAIL:
      return {
        loading: false,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};

// Load user reducer
jaymehta committed
58
export const loadedUserReducer = (state = { loading: true, success: false, loadedUser: null }, action) => {
jay committed
59 60 61 62 63 64 65 66 67 68 69
  switch (action.type) {
    case LOAD_USER_REQUEST:
      return {
        loading: true,
        isAuthenticated: false
      };

    case LOAD_USER_SUCCESS:
      return {
        loading: false,
        isAuthenticated: true,
jaymehta committed
70
        loadedUser: action.payload
jay committed
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 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
      };

    case LOAD_USER_FAIL:
      return {
        loading: false,
        isAuthenticated: true,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};

// User reducer
export const userReducer = (state = {}, action) => {
  switch (action.type) {
    case UPDATE_USER_PROFILE_REQUEST:
      return {
        loading: true
      };

    case UPDATE_USER_PROFILE_SUCCESS:
      return {
        loading: false,
        isUpdated: action.payload
      };

    case UPDATE_USER_PROFILE_RESET:
      return {
        loading: false,
        isUpdated: false
      };

    case UPDATE_USER_PROFILE_FAIL:
      return {
        loading: false,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};

// Forgot password reducer
export const forgotPasswordReducer = (state = {}, action) => {
  switch (action.type) {
    case FORGOT_PASSWORD_REQUEST:
      return {
        loading: true
      };

    case FORGOT_PASSWORD_SUCCESS:
      return {
        loading: false,
        message: action.payload
      };

    case FORGOT_PASSWORD_FAIL:
      return {
        loading: false,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};

// Reset password reducer
export const resetPasswordReducer = (state = {}, action) => {
  switch (action.type) {
    case RESET_PASSWORD_REQUEST:
      return {
        loading: true
      };

    case RESET_PASSWORD_SUCCESS:
      return {
        loading: false,
        message: action.payload
      };

    case RESET_PASSWORD_FAIL:
      return {
        loading: false,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};
jaymehta committed
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223


// Load user reducer
export const getEndUserReducer = (state = { loading: true, success: false, endUser: null }, action) => {
  switch (action.type) {
    case GET_END_USER_REQUEST:
      return {
        loading: true,
        isAuthenticated: false
      };

    case GET_END_USER_SUCCESS:
      return {
        loading: false,
        isAuthenticated: true,
        endUser: action.payload
      };

    case GET_END_USER_FAIL:
      return {
        loading: false,
        isAuthenticated: true,
        error: action.payload.error.message
      };

    case CLEAR_ERRORS:
      return {
        ...state,
        error: null
      };

    default:
      return state;
  }
};