Created
September 19, 2018 20:25
-
-
Save onderceylan/d5efa8f4e90af1c66c22a10caa3ce347 to your computer and use it in GitHub Desktop.
Persistent storage of your store - reducer
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
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