/** * Reducer: Defines store structure and state updates for this module. * * Manages both transient (temporary) and persistent (saved) state. * The initial state must define all properties, as dynamic additions are not supported. * * @file */ import { createReducer, createReducerSetters } from '../utils'; import ACTION_TYPES from './action-types'; // Store structure. // Transient: Values that are _not_ saved to the DB (like app lifecycle-flags). const defaultTransient = Object.freeze( { isReady: false, } ); // Persistent: Values that are loaded from the DB. const defaultPersistent = Object.freeze( { // TODO: Add real DB properties here. sampleValue: 'foo', } ); // Reducer logic. const [ changeTransient, changePersistent ] = createReducerSetters( defaultTransient, defaultPersistent ); const reducer = createReducer( defaultTransient, defaultPersistent, { [ ACTION_TYPES.SET_TRANSIENT ]: ( state, payload ) => changeTransient( state, payload ), [ ACTION_TYPES.SET_PERSISTENT ]: ( state, payload ) => changePersistent( state, payload ), [ ACTION_TYPES.RESET ]: ( state ) => { const cleanState = changeTransient( changePersistent( state, defaultPersistent ), defaultTransient ); // Keep "read-only" details and initialization flags. cleanState.isReady = true; return cleanState; }, [ ACTION_TYPES.HYDRATE ]: ( state, payload ) => changePersistent( state, payload.data ), } ); export default reducer;