I am using NGRX and Angular 7.
I have a store that is only used for the user settings (user preferences)
Here is a short version =>
import { Action } from '@ngrx/store';
import * as featureModels from './models';
export enum ActionTypes {
SetSettings = '[SETTINGS] Set Settings',
SetNavigationSettings = '[SETTINGS] Set Navigation Settings',
}
export class SetNavigationSettings implements Action {
public readonly type = ActionTypes.SetNavigationSettings;
constructor(public payload: {settings: Partial<featureModels.INavigationSettings>}) {}
}
export class SetSettings implements Action {
public readonly type = ActionTypes.SetSettings;
constructor(public payload: {settings: featureModels.ISettings}) {}
}
export type Actions = SetNavigationSettings |
SetSettings;
After any setting has changed, I would like to execute an effect, that will store the current settings in the local storage:
At the moment in my effect, I am just using a selector like this that trigger after any state changes (so it works ok) =>
export class SettingsEffects {
constructor(
private actions$: Actions,
private dataService: SettingsDataService,
private localStorageService: LocalStorageService,
private store$: Store<IAppState>
) {
this.store$.pipe(select(featureSelector.selectSettingsState)).subscribe((settings) => {
//save
});
}
@Effect()
init$ = this.actions$.pipe(
ofType(ROOT_EFFECTS_INIT),
switchMap(action => {
const settings = this.localStorageService.retrieve('settings');
console.log(settings)
if (settings) {
return of(new featureActions.SetSettings({settings}));
} else {
return EMPTY;
}
})
);
But, this will be executed on initialization, so before my INIT Effect, and it will always override the localStorage value with the store initial state. This will make my Init effect to just retrieve the initial state from the localstorage.
I can put the store selection inside the Init Effect (and it works fine)
But I was wondering if there is a way without using a selector/subscription, and just using an effect. So that every time the user trigger an action, it will save.