redux-devshare
Version:
[![NPM version][npm-image]][npm-url] [![NPM downloads][npm-downloads-image]][npm-url] [![Build Status][travis-image]][travis-url] [![Dependency Status][daviddm-image]][daviddm-url] [![Code Climate][climate-image]][climate-url] [![Code Coverage][coverage-i
91 lines (72 loc) • 2.1 kB
JavaScript
import {fromJS} from 'immutable'
import {
SET,
SET_PROFILE,
LOGIN,
LOGOUT,
LOGIN_ERROR,
NO_VALUE,
AUTHENTICATION_INIT_STARTED,
AUTHENTICATION_INIT_FINISHED,
UNAUTHORIZED_ERROR
} from './constants'
const emptyState = {
auth: undefined,
authError: undefined,
profile: undefined,
isInitializing: undefined,
data: {},
snapshot: {}
}
const initialState = fromJS(emptyState)
const pathToArr = path => path.split(/\//).filter(p => !!p)
export default (state = initialState, action = {}) => {
const {path} = action
let pathArr
let retVal
switch (action.type) {
case SET:
const { data } = action
pathArr = pathToArr(path)
retVal = (data !== undefined)
? state.setIn(['data', ...pathArr], fromJS(data))
: state.deleteIn(['data', ...pathArr])
return retVal
case NO_VALUE:
pathArr = pathToArr(path)
retVal = state.setIn(['data', ...pathArr], fromJS({}))
retVal = retVal.setIn(['snapshot', ...pathArr], fromJS({}))
return retVal
case SET_PROFILE:
const {profile} = action
return (profile !== undefined)
? state.setIn(['profile'], fromJS(profile))
: state.deleteIn(['profile'])
case LOGOUT:
return fromJS({
auth: null,
authError: null,
profile: null,
isLoading: false,
data: {},
snapshot: {}
})
case LOGIN:
return state.setIn(['auth'], fromJS(action.auth))
.setIn(['authError'], null)
case LOGIN_ERROR:
return state
.setIn(['authError'], action.authError)
.setIn(['auth'], null)
.setIn(['profile'], null)
case AUTHENTICATION_INIT_STARTED:
return initialState.setIn(['isInitializing'], true)
// return state.setIn(['isInitializing'], true) // throws state.setIn not a function error
case AUTHENTICATION_INIT_FINISHED:
return state.setIn(['isInitializing'], false)
case UNAUTHORIZED_ERROR:
return state.setIn(['authError'], action.authError)
default:
return state
}
}