Skip to content

Instantly share code, notes, and snippets.

@onderceylan
Created September 19, 2018 20:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save onderceylan/d5efa8f4e90af1c66c22a10caa3ce347 to your computer and use it in GitHub Desktop.
Save onderceylan/d5efa8f4e90af1c66c22a10caa3ce347 to your computer and use it in GitHub Desktop.
Persistent storage of your store - reducer
import { Action, ActionReducer, ActionReducerMap, createFeatureSelector, MetaReducer } from '@ngrx/store';
import * as fromTimezone from './timezone.reducer';
import * as fromNotifications from './notification.reducer';
import { localStorageSync } from 'ngrx-store-localstorage';
export const FEATURE_NAME = 'settings';
const STORE_KEYS_TO_PERSIST = ['timezone', 'notifications'];
export interface SettingsState {
timezone: fromTimezone.TimezoneState;
notifications: fromNotifications.NotificationState;
}
export const reducers: ActionReducerMap<SettingsState> = {
timezone: fromTimezone.timezoneReducer,
notifications: fromNotifications.notificationReducer,
};
export const getSettingsState = createFeatureSelector<SettingsState>(FEATURE_NAME);
export function localStorageSyncReducer(reducer: ActionReducer<SettingsState>): ActionReducer<SettingsState> {
return localStorageSync({
keys: STORE_KEYS_TO_PERSIST,
rehydrate: true,
})(reducer);
}
export const metaReducers: Array<MetaReducer<SettingsState, Action>> = [localStorageSyncReducer];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment