diff --git a/src/model/preference/actions.js b/src/model/preference/actions.js new file mode 100644 index 0000000..dc7cdf3 --- /dev/null +++ b/src/model/preference/actions.js @@ -0,0 +1,54 @@ + +import {fromJS} from 'immutable' + +import Store from '../../Store' +import actions from '../actions' +import constants from './constants' +import utilLocalStorage from '../../utility/utilLocalStorage' + +const init = async ()=>{ + // load preference from localStorage + const preferenceJs = utilLocalStorage.get(constants.localStoragePath) + if (preferenceJs){ + const preferenceImm = fromJS(preferenceJs) + + Store.dispatch({ + type:'PREFERENCE_SET', + payload:{ + preference: preferenceImm, + } + }) + + return + } +} + +const localBackup = async ()=>{ + const state = Store.getState() + const js = state.preference.options.toJS() + + utilLocalStorage.set(constants.localStoragePath, js) +} + +const setIn = async({path, value})=>{ + if (!path) throw new Error('preference.actions.setIn: no path defined') + + Store.dispatch({ + type:'PREFERENCE_SETIN', + payload:{ + path, + value, + } + }) + + await localBackup() +} + +actions.subscribe('preference',{ + setIn, +}) + +export default { + init, + setIn, +} \ No newline at end of file diff --git a/src/model/preference/constants.js b/src/model/preference/constants.js new file mode 100644 index 0000000..acc9b47 --- /dev/null +++ b/src/model/preference/constants.js @@ -0,0 +1,6 @@ + +const localStoragePath = 'frescoPreferenceStore' + +export default { + localStoragePath, +} \ No newline at end of file diff --git a/src/model/preference/index.js b/src/model/preference/index.js new file mode 100644 index 0000000..9fb150c --- /dev/null +++ b/src/model/preference/index.js @@ -0,0 +1,9 @@ +import actions from './actions' +import constants from './constants' +import selectors from './selectors' + +export default { + actions, + constants, + selectors, +} \ No newline at end of file diff --git a/src/model/preference/reducer.js b/src/model/preference/reducer.js new file mode 100644 index 0000000..4bb330c --- /dev/null +++ b/src/model/preference/reducer.js @@ -0,0 +1,28 @@ +import {Map} from 'immutable' + +const state = { + options: Map({}), +} + +export const reducer = (st = state, action)=>{ + switch (action.type){ + case 'PREFERENCE_SETIN':{ + const {path, value} = action.payload + const options = st.options.setIn(path, value) + + return { + ...st, + options, + } + } + case 'PREFERENCE_SET':{ + const {preference} = action.payload + return { + ...st, + options: preference + } + } + default: + return st + } +} \ No newline at end of file diff --git a/src/model/preference/selectors.js b/src/model/preference/selectors.js new file mode 100644 index 0000000..5dd226c --- /dev/null +++ b/src/model/preference/selectors.js @@ -0,0 +1,5 @@ +export default { + getIn: (state, {path})=>{ + return state.preference.options.getIn(path) + }, +} \ No newline at end of file