client/src/reducers/authReducer.js
author ymh <ymh.work@gmail.com>
Tue, 20 Jun 2017 21:56:42 +0200
changeset 67 9206af01f5e5
parent 62 b2514a9bcd49
child 88 2a861fed6bde
permissions -rw-r--r--
Change to UserRecord

import Immutable from 'immutable';
import * as types from '../constants/actionTypes';
import UserRecord from '../store/userRecord';

export const isAuthenticated = (state = false, action) => {
  switch (action.type) {
    case types.AUTH_LOGOUT:
      return false;
    case types.AUTH_LOGIN_SUCCESS:
      return true;
    default:
      return state;
  }
}

export const currentUser = (state = null, action) => {
  switch (action.type) {
    case types.AUTH_LOGOUT:
      return null;
    case types.AUTH_LOGIN_SUCCESS:
      return new UserRecord(action.user);
    case types.USER_UPDATE_SETTINGS:
      return state.merge({
        first_name: action.firstname,
        last_name: action.lastname
      });
    default:
      return state;
  }
}

export const token = (state = null, action) => {
  switch (action.type) {
    case types.AUTH_LOGOUT:
      return null;
    case types.AUTH_STORE_TOKEN:
      return action.token;
    default:
      return state;
  }
}

const loginState = Immutable.Map({
  loading: false,
  success: false,
  error: false,
});

export const login = (state = loginState, action) => {
  switch (action.type) {
    case types.AUTH_LOGIN_REQUEST:
      return Immutable.Map({
        loading: true,
        success: false,
        error: false,
      })
    case types.AUTH_LOGIN_SUCCESS:
    case types.AUTH_LOGIN_ERROR:
      return Immutable.Map({
        loading: false,
        success: action.type === types.AUTH_LOGIN_SUCCESS,
        error: action.type === types.AUTH_LOGIN_ERROR,
      })
    default:
      return state
  }
}