Last active
June 1, 2018 07:39
-
-
Save wojtek1150/0e9cc98016e98fd01879d5f2e6579e84 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import * as fromUsers from '../actions/users.action'; | |
import { Users } from 'resources/users/users'; | |
export interface UsersState { | |
entities: Users.Entities; | |
loaded: boolean, | |
loading: boolean | |
} | |
export const initialState: UsersState = { | |
entities: {}, | |
loaded: false, | |
loading: false | |
}; | |
export function reducer( | |
state = initialState, | |
action: fromUsers.UsersAction | |
): UsersState { | |
switch (action.type) { | |
case fromUsers.LOAD_USERS: { | |
return { | |
...state, | |
loading: true | |
}; | |
} | |
case fromUsers.LOAD_USERS_SUCCESS: { | |
const users = action.payload; | |
const entities = users.reduce((entities: Users.Entities, user) => { | |
return { | |
...entities, | |
[user.id]: user | |
}; | |
}, | |
{} | |
); | |
return { | |
...state, | |
loading: false, | |
loaded: true, | |
entities | |
}; | |
} | |
case fromUsers.LOAD_USERS_FAIL: { | |
return { | |
...state, | |
loading: false, | |
loaded: false | |
}; | |
} | |
} | |
return state; | |
} | |
export const getUsersEntities = (state: UsersState) => state.entities; | |
export const getUsersLoaded = (state: UsersState) => state.loaded; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment