client/src/reducers/authReducer.js
changeset 168 ea92f4fe783d
parent 134 be36eed5e6e0
child 199 c78d579f4b55
equal deleted inserted replaced
167:1f340f3597a8 168:ea92f4fe783d
     1 import Immutable from 'immutable';
     1 import * as R from 'ramda';
     2 import * as types from '../constants/actionTypes';
     2 import * as types from '../constants/actionTypes';
     3 import UserRecord from '../store/userRecord';
     3 import UserRecord from '../store/userRecord';
     4 import GroupRecord from '../store/groupRecord';
     4 import GroupRecord from '../store/groupRecord';
     5 import asyncRequest from '../constants/asyncRequest';
     5 import asyncRequest from '../constants/asyncRequest';
     6 import uuidV4 from 'uuid/v4';
     6 import uuidV4 from 'uuid/v4';
     7 import _ from 'lodash';
     7 
     8 
     8 
     9 export const isAuthenticated = (state = false, action) => {
     9 export const isAuthenticated = (state = false, action) => {
    10   switch (action.type) {
    10   switch (action.type) {
    11     case types.AUTH_DEAUTHENTICATE:
    11     case types.AUTH_DEAUTHENTICATE:
    12     case types.AUTH_LOGOUT:
    12     case types.AUTH_LOGOUT:
    24     case types.AUTH_LOGOUT:
    24     case types.AUTH_LOGOUT:
    25       return null;
    25       return null;
    26     case types.AUTH_LOGIN_SUCCESS:
    26     case types.AUTH_LOGIN_SUCCESS:
    27       return new UserRecord(action.user);
    27       return new UserRecord(action.user);
    28     case types.USER_UPDATE_SETTINGS:
    28     case types.USER_UPDATE_SETTINGS:
    29       return state.merge({
    29       return R.merge(state,{
    30         first_name: action.firstname,
    30         first_name: action.firstname,
    31         last_name: action.lastname
    31         last_name: action.lastname
    32       });
    32       });
    33     default:
    33     default:
    34       return state;
    34       return state;
    82 }
    82 }
    83 
    83 
    84 export const login = (state = asyncRequest, action) => {
    84 export const login = (state = asyncRequest, action) => {
    85   switch (action.type) {
    85   switch (action.type) {
    86     case types.AUTH_LOGIN_REQUEST:
    86     case types.AUTH_LOGIN_REQUEST:
    87       return Immutable.Map({
    87       return {
    88         loading: true,
    88         loading: true,
    89         success: false,
    89         success: false,
    90         error: false,
    90         error: false,
    91       })
    91       }
    92     case types.AUTH_LOGIN_SUCCESS:
    92     case types.AUTH_LOGIN_SUCCESS:
    93     case types.AUTH_LOGIN_ERROR:
    93     case types.AUTH_LOGIN_ERROR:
    94       return Immutable.Map({
    94       return {
    95         loading: false,
    95         loading: false,
    96         success: action.type === types.AUTH_LOGIN_SUCCESS,
    96         success: action.type === types.AUTH_LOGIN_SUCCESS,
    97         error: action.type === types.AUTH_LOGIN_ERROR,
    97         error: action.type === types.AUTH_LOGIN_ERROR,
    98         errorMessages: action.type === types.AUTH_LOGIN_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
    98         errorMessages: action.type === types.AUTH_LOGIN_ERROR ? action.error : {}
    99       })
    99       }
   100     default:
   100     default:
   101       return state
   101       return state
   102   }
   102   }
   103 }
   103 }
   104 
   104 
   105 export const register = (state = asyncRequest, action) => {
   105 export const register = (state = asyncRequest, action) => {
   106   switch (action.type) {
   106   switch (action.type) {
   107     case types.AUTH_REGISTER_REQUEST:
   107     case types.AUTH_REGISTER_REQUEST:
   108       return Immutable.Map({
   108       return {
   109         loading: true,
   109         loading: true,
   110         success: false,
   110         success: false,
   111         error: false,
   111         error: false,
   112       })
   112       }
   113     case types.AUTH_LOGIN_SUCCESS:
   113     case types.AUTH_LOGIN_SUCCESS:
   114     case types.AUTH_REGISTER_ERROR:
   114     case types.AUTH_REGISTER_ERROR:
   115       return Immutable.Map({
   115       return {
   116         loading: false,
   116         loading: false,
   117         success: action.type === types.AUTH_LOGIN_SUCCESS,
   117         success: action.type === types.AUTH_LOGIN_SUCCESS,
   118         error: action.type === types.AUTH_REGISTER_ERROR,
   118         error: action.type === types.AUTH_REGISTER_ERROR,
   119         errorMessages: action.type === types.AUTH_REGISTER_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
   119         errorMessages: action.type === types.AUTH_REGISTER_ERROR ? action.error : {}
   120       })
   120       }
   121     default:
   121     default:
   122       return state
   122       return state
   123   }
   123   }
   124 }
   124 }
   125 
   125 
   126 export const groups = (state = Immutable.List([]), action) => {
   126 const groupTransformIsPersonal = R.compose(
       
   127   GroupRecord,
       
   128   R.converge(
       
   129     R.merge,
       
   130     [
       
   131       R.omit('is_personal'),
       
   132       R.compose(
       
   133         R.objOf('isPersonal'),
       
   134         R.propOr(false, 'is_personal')
       
   135       )
       
   136     ]
       
   137   )
       
   138 );
       
   139 
       
   140 export const groups = (state = [], action) => {
   127   switch (action.type) {
   141   switch (action.type) {
   128     case types.GROUP_LOAD_SUCCESS:
   142     case types.GROUP_LOAD_SUCCESS:
   129       return Immutable.List(
   143       return R.map(groupTransformIsPersonal, action.groups)
   130         action.groups.map((group) => GroupRecord({
       
   131           ...(_.omit(group, 'is_personal')),
       
   132           isPersonal: group['is_personal']
       
   133         }))
       
   134       );
       
   135     case types.GROUP_CREATE_SUCCESS:
   144     case types.GROUP_CREATE_SUCCESS:
   136       return state.push(GroupRecord({
   145       return R.append(groupTransformIsPersonal(action.group), state);
   137           ...(_.omit(action.group, 'is_personal')),
       
   138           isPersonal: action.group['is_personal']
       
   139         }));
       
   140     case types.AUTH_LOGOUT: {
   146     case types.AUTH_LOGOUT: {
   141       return Immutable.List(); // empty note list on logout
   147       return []; // empty note list on logout
   142     }
   148     }
   143     default:
   149     default:
   144       return state
   150       return state
   145   }
   151   }
   146 }
   152 }
   147 
   153 
   148 export const createGroup = (state = asyncRequest, action) => {
   154 export const createGroup = (state = asyncRequest, action) => {
   149   switch (action.type) {
   155   switch (action.type) {
   150     case types.GROUP_CREATE_ASYNC:
   156     case types.GROUP_CREATE_ASYNC:
   151       return Immutable.Map({
   157       return {
   152         loading: true,
   158         loading: true,
   153         success: false,
   159         success: false,
   154         error: false,
   160         error: false,
   155       })
   161       }
   156     case types.GROUP_CREATE_SUCCESS:
   162     case types.GROUP_CREATE_SUCCESS:
   157     case types.GROUP_CREATE_ERROR:
   163     case types.GROUP_CREATE_ERROR:
   158       return Immutable.Map({
   164       return {
   159         loading: false,
   165         loading: false,
   160         success: action.type === types.GROUP_CREATE_SUCCESS,
   166         success: action.type === types.GROUP_CREATE_SUCCESS,
   161         error: action.type === types.GROUP_CREATE_ERROR,
   167         error: action.type === types.GROUP_CREATE_ERROR,
   162         errorMessages: action.type === types.GROUP_CREATE_ERROR ? Immutable.Map(action.error) : Immutable.Map({})
   168         errorMessages: action.type === types.GROUP_CREATE_ERROR ? action.error : {}
   163       })
   169       }
   164     default:
   170     default:
   165       return state
   171       return state
   166   }
   172   }
   167 }
   173 }