Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Composing selectors - selector
import { createSelector } from '@ngrx/store';
import * as fromFeature from '../reducers';
import * as fromEvents from '../reducers/events.reducer';
import * as fromSettingsSelectors from '../../../settings/store/selectors';
import { eventEntityAdapter } from '../reducers/events.reducer';
import { filterEventEntitiesByUserSettings } from '../../utils/notification-utils';
import { filter, size } from 'lodash-es';
// This is a basic use case to slice feature state
export const getEventsState = createSelector(
fromFeature.getNotificationsState,
(state: fromFeature.NotificationsState): fromEvents.EventsState => state.events,
);
// Following selectors come free when you use @ngrx/entity
export const { selectIds, selectEntities, selectAll, selectTotal } = eventEntityAdapter.getSelectors(getEventsState);
// As discussed earlier, you can compose multiple selectors
// In this example, it queries data from another feature's state for filtering in-app notifications
export const selectPreferredEvents = createSelector(
selectAll,
fromSettingsSelectors.getNotificationState,
filterEventEntitiesByUserSettings,
);
export const selectPreferredEventsUnreadTotal = createSelector(
selectAll,
fromSettingsSelectors.getNotificationState,
(entities, notificationSettings) =>
size(filter(filterEventEntitiesByUserSettings(entities, notificationSettings), { read: false })),
);
export const selectLoading = createSelector(getEventsState, fromEvents.getEventsLoading);
export const selectLoaded = createSelector(getEventsState, fromEvents.getEventsLoaded);
export const selectError = createSelector(getEventsState, fromEvents.getEventsError);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment