Created
December 12, 2018 03:55
-
-
Save alex-okrushko/8facf2d104ba0de1652cc0b651ccf321 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 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