Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alex-okrushko
Created December 12, 2018 03:55
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 alex-okrushko/8facf2d104ba0de1652cc0b651ccf321 to your computer and use it in GitHub Desktop.
Save alex-okrushko/8facf2d104ba0de1652cc0b651ccf321 to your computer and use it in GitHub Desktop.
export function productSync(reducer: ActionReducer<{ product: ProductState }>) {
return (state, action) => {
let reducedState = reducer(state, action);
if (action.type === INIT) {
const data = window.localStorage.getItem('productData');
if (data) {
reducedState = {
...reducedState,
product: JSON.parse(data),
};
}
} else if (action.type !== UPDATE) {
window.localStorage.setItem(
'productData',
JSON.stringify(reducedState.product)
);
}
return reducedState;
};
}
@NgModule({
imports: [
StoreModule.forRoot(
{ product: productReducer },
{ metaReducers: [productSync] }
),
],
})
export class SomeModule {}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment