Skip to content

Instantly share code, notes, and snippets.

View itayod's full-sized avatar

Itay Oded itayod

  • Tel Aviv
View GitHub Profile
export interface State {
theme: 'dark' | 'light';
}
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);
};
}
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));
}
import {StoreModule} from '@ngrx/store';
import {storageMetaReducer} from './storage.metareducer';
@NgModule({
imports: [StoreModule.forRoot(
reducers,
{
metaReducers: [storageMetaReducer]
}
)]
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');
import {Injectable} from '@angular/core';
@Injectable( { providedIn: 'root' } )
export class LocalStorageService {
constructor() {}
setSavedState(state: any, localStorageKey: string) {
localStorage.setItem(localStorageKey, JSON.stringify(state));
}
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>[] {
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>) {
export enum ViewModes {
SingleCol = 'SINGLE',
MultiCol = 'MULTI',
}
export interface Course {
id: number;
description?: string;
iconUrl?: string;
courseListIcon?: string;
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 =