license import { Reducer } from 'redux' ; import { createSelector } from 'reselect' ; import { REQUEST_USER , RECEIVE_USER , FAIL_USER } from '../actions/users.js' ; import { idSelector } from './app.js' ; import { RootAction , RootState } from '../store.js' ; export interface UsersState { [ k ] : UserState ; } ; export interface UserState { created ? : string ; failure ? : boolean ; id ? : string ; isFetching ? : boolean ; karma ? : string ; } ; const users = ( state = { } , action ) => { switch ( action . type ) { case REQUEST_USER : case RECEIVE_USER : case FAIL_USER : const userId = action . userId ; return { ... state , [ userId ] : user ( state [ userId ] , action ) } ; default : return state ; } } ; const user = ( state = { } , action ) => { switch ( action . type ) { case REQUEST_USER : return { ... state , id : action . userId , failure : false , isFetching : true } ; case RECEIVE_USER : return { ... state , failure : false , isFetching : false , ... action . data } ; case FAIL_USER : return { ... state , failure : true , isFetching : false } ; default : return state ; } } ; export default users ; const usersSelector = ( state ) => state . users ; export const currentUserSelector = createSelector ( usersSelector , idSelector , ( users , id ) => { return id ? users [ id ] || { id } : null ; } ) ;