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 interface State { | |
theme: 'dark' | 'light'; | |
} |
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 metaReducer(reducer: ActionReducer<any>) : ActionReducer<any> { | |
// a function with the exact same signature of a reducer | |
return function(state: any, action: Action) { | |
return reducer(state, action); | |
}; | |
} |
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 {ActionReducer, Action} from '@ngrx/store'; | |
import {merge, pick} from 'lodash-es'; | |
function setSavedState(state: any, localStorageKey: string) { | |
localStorage.setItem(localStorageKey, JSON.stringify(state)); | |
} | |
function getSavedState(localStorageKey: string): any { | |
return JSON.parse(localStorage.getItem(localStorageKey)); | |
} |
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 {StoreModule} from '@ngrx/store'; | |
import {storageMetaReducer} from './storage.metareducer'; | |
@NgModule({ | |
imports: [StoreModule.forRoot( | |
reducers, | |
{ | |
metaReducers: [storageMetaReducer] | |
} | |
)] |
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 {InjectionToken} from '@angular/core'; | |
// token for the state keys. | |
export const ROOT_STORAGE_KEYS = new InjectionToken<string[]>('StoreKeys'); | |
// token for the localStorage key. | |
export const ROOT_LOCAL_STORAGE_KEY = new InjectionToken<string[]>('appStorage'); |
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 {Injectable} from '@angular/core'; | |
@Injectable( { providedIn: 'root' } ) | |
export class LocalStorageService { | |
constructor() {} | |
setSavedState(state: any, localStorageKey: string) { | |
localStorage.setItem(localStorageKey, JSON.stringify(state)); | |
} |
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 {StoreModule, MetaReducer, META_REDUCERS} from '@ngrx/store'; | |
import {ROOT_STORAGE_KEYS, LOCAL_STORAGE_KEY} from './app.tokens'; | |
import {LocalStorageService} from './shared/local-storage.service'; | |
import {storageMetaReducer} from './storage.metareducer'; | |
// factory meta-reducer configuration function | |
export function getMetaReducers(saveKeys: string[], | |
localStorageKey: string, | |
storageService: LocalStorageService | |
): MetaReducer<any>[] { |
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 {ActionReducer, Action} from '@ngrx/store'; | |
import {merge, pick} from 'lodash-es'; | |
import {LocalStorageService} from './local-storage.service'; | |
export function storageMetaReducer<S, A extends Action = Action>(saveKeys: string[], | |
localStorageKey: string, | |
storageService: LocalStorageService | |
) { | |
let onInit = true; // after load/refresh… | |
return function(reducer: ActionReducer<S, A>) { |
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 enum ViewModes { | |
SingleCol = 'SINGLE', | |
MultiCol = 'MULTI', | |
} | |
export interface Course { | |
id: number; | |
description?: string; | |
iconUrl?: string; | |
courseListIcon?: string; |
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 {InjectionToken} from '@angular/core'; | |
import {StoreConfig} from '@ngrx/store/src/store_module'; | |
import * as fromReducer from './reducers/courses.reducer'; | |
import * as fromActions from './actions/courses.actions'; | |
export const COURSES_STORAGE_KEYS = | |
new InjectionToken<keyof fromReducer.State[]>('CoursesStorageKeys'); | |
export const COURSES_LOCAL_STORAGE_KEY = | |
new InjectionToken<string[]>('CoursesStorage'); | |
export const COURSES_CONFIG_TOKEN = |
OlderNewer