Skip to content

Instantly share code, notes, and snippets.

@kmaraz
Last active April 10, 2018 21:56
Show Gist options
  • Save kmaraz/b5a838bdac6acb8b9bb9e4d5879b22e8 to your computer and use it in GitHub Desktop.
Save kmaraz/b5a838bdac6acb8b9bb9e4d5879b22e8 to your computer and use it in GitHub Desktop.
export function reducer(state: EventsState = initialState, action: Action): EventsState {
switch (action.type) {
case EventsActions.UPDATE: {
// Clone the oridinal state to be sure, not to update the object reference
const newState = cloneDeep(state);
newState.events = action.payload;
return newState;
}
case EventsActions.RESET: {
return cloneDeep(initialState);
}
default: {
return state;
}
}
}
// Bunch of selectors, just to provide the particular slices of state to the components
export const eventsReducer: ActionReducer<EventsState> = reducer;
export const selectFeature = createFeatureSelector<State>('Events');
export const selectFeatureState = createSelector(selectFeature, (state: State) => state.events);
export const selectEvents = createSelector(selectFeatureState,
(state: EventsState) => state.events.map((e) => new Event(e))
);
// Initial state for this reducer
export const initialState: EventsState = {
events: []
};
export interface EventsState {
events: Event[];
}
export interface State extends fromRoot.State {
events: EventsState;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment