Last active
April 10, 2018 21:56
-
-
Save kmaraz/b5a838bdac6acb8b9bb9e4d5879b22e8 to your computer and use it in GitHub Desktop.
This file contains 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
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