client/src/reducers/authReducer.js
author Alexandre Segura <mex.zktk@gmail.com>
Wed, 28 Jun 2017 12:54:48 +0200
changeset 101 e165aa89ac82
parent 100 6fd752d98933
child 129 d48946d164c6
permissions -rw-r--r--
Add group dropdown, update session after group creation.

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

export const isAuthenticated = (state = false, action) => {
  switch (action.type) {
    case types.AUTH_DEAUTHENTICATE:
    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_DEAUTHENTICATE:
    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_DEAUTHENTICATE:
    case types.AUTH_LOGOUT:
      return null;
    case types.AUTH_STORE_TOKEN:
      return action.token;
    default:
      return state;
  }
}

export const login = (state = asyncRequest, 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,
        errorMessages: action.type === types.AUTH_LOGIN_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
      })
    default:
      return state
  }
}

export const register = (state = asyncRequest, action) => {
  switch (action.type) {
    case types.AUTH_REGISTER_REQUEST:
      return Immutable.Map({
        loading: true,
        success: false,
        error: false,
      })
    case types.AUTH_LOGIN_SUCCESS:
    case types.AUTH_REGISTER_ERROR:
      return Immutable.Map({
        loading: false,
        success: action.type === types.AUTH_LOGIN_SUCCESS,
        error: action.type === types.AUTH_REGISTER_ERROR,
        errorMessages: action.type === types.AUTH_REGISTER_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
      })
    default:
      return state
  }
}

export const groups = (state = Immutable.List([]), action) => {
  switch (action.type) {
    case types.GROUP_LOAD_SUCCESS:
      return Immutable.List(action.groups);
    case types.GROUP_CREATE_SUCCESS:
      return state.push(action.group);
    default:
      return state
  }
}

export const createGroup = (state = asyncRequest, action) => {
  switch (action.type) {
    case types.GROUP_CREATE_ASYNC:
      return Immutable.Map({
        loading: true,
        success: false,
        error: false,
      })
    case types.GROUP_CREATE_SUCCESS:
    case types.GROUP_CREATE_ERROR:
      return Immutable.Map({
        loading: false,
        success: action.type === types.GROUP_CREATE_SUCCESS,
        error: action.type === types.GROUP_CREATE_ERROR,
        errorMessages: action.type === types.GROUP_CREATE_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
      })
    default:
      return state
  }
}