import { effects } from "redux-saga" ; import { SettingActions , SettingsActionTypes } from "./SettingsActions" ; import { ISettingsAwareState , ISettingsState } from "./SettingsContracts" ; export function * settingsRootSaga ( ) { if ( typeof Storage === "undefined" ) { return ; } yield effects . takeEvery ( SettingsActionTypes . LOAD_PREFERENCES , loadPreferences ) ; yield effects . takeEvery ( SettingsActionTypes . SET_PREFERENCES , storePreferencs ) ; } function * loadPreferences ( ) { const settingsStr = yield effects . call ( [ localStorage , localStorage . getItem ] , "userPreferences" ) ; if ( settingsStr ) { const settings = JSON . parse ( settingsStr ) ; yield effects . put ( SettingActions . setPreferences ( settings ) ) ; } } function * storePreferencs ( ) { const settingsState = yield effects . select ( ( state ) => state . settingsState ) ; yield effects . call ( [ localStorage , localStorage . setItem ] , "userPreferences" , JSON . stringify ( settingsState . userPreferences ) ) ; }