Skip to content

Instantly share code, notes, and snippets.

@wojtek1150
Last active June 1, 2018 07:39
Show Gist options
  • Save wojtek1150/0e9cc98016e98fd01879d5f2e6579e84 to your computer and use it in GitHub Desktop.
Save wojtek1150/0e9cc98016e98fd01879d5f2e6579e84 to your computer and use it in GitHub Desktop.
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