File tree Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Expand file tree Collapse file tree 5 files changed +102
-0
lines changed Original file line number Diff line number Diff line change 1+
2+ import { fromJS } from 'immutable'
3+
4+ import Store from '../../Store'
5+ import actions from '../actions'
6+ import constants from './constants'
7+ import utilLocalStorage from '../../utility/utilLocalStorage'
8+
9+ const init = async ( ) => {
10+ // load preference from localStorage
11+ const preferenceJs = utilLocalStorage . get ( constants . localStoragePath )
12+ if ( preferenceJs ) {
13+ const preferenceImm = fromJS ( preferenceJs )
14+
15+ Store . dispatch ( {
16+ type :'PREFERENCE_SET' ,
17+ payload :{
18+ preference : preferenceImm ,
19+ }
20+ } )
21+
22+ return
23+ }
24+ }
25+
26+ const localBackup = async ( ) => {
27+ const state = Store . getState ( )
28+ const js = state . preference . options . toJS ( )
29+
30+ utilLocalStorage . set ( constants . localStoragePath , js )
31+ }
32+
33+ const setIn = async ( { path, value} ) => {
34+ if ( ! path ) throw new Error ( 'preference.actions.setIn: no path defined' )
35+
36+ Store . dispatch ( {
37+ type :'PREFERENCE_SETIN' ,
38+ payload :{
39+ path,
40+ value,
41+ }
42+ } )
43+
44+ await localBackup ( )
45+ }
46+
47+ actions . subscribe ( 'preference' , {
48+ setIn,
49+ } )
50+
51+ export default {
52+ init,
53+ setIn,
54+ }
Original file line number Diff line number Diff line change 1+
2+ const localStoragePath = 'frescoPreferenceStore'
3+
4+ export default {
5+ localStoragePath,
6+ }
Original file line number Diff line number Diff line change 1+ import actions from './actions'
2+ import constants from './constants'
3+ import selectors from './selectors'
4+
5+ export default {
6+ actions,
7+ constants,
8+ selectors,
9+ }
Original file line number Diff line number Diff line change 1+ import { Map } from 'immutable'
2+
3+ const state = {
4+ options : Map ( { } ) ,
5+ }
6+
7+ export const reducer = ( st = state , action ) => {
8+ switch ( action . type ) {
9+ case 'PREFERENCE_SETIN' :{
10+ const { path, value} = action . payload
11+ const options = st . options . setIn ( path , value )
12+
13+ return {
14+ ...st ,
15+ options,
16+ }
17+ }
18+ case 'PREFERENCE_SET' :{
19+ const { preference} = action . payload
20+ return {
21+ ...st ,
22+ options : preference
23+ }
24+ }
25+ default :
26+ return st
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ export default {
2+ getIn : ( state , { path} ) => {
3+ return state . preference . options . getIn ( path )
4+ } ,
5+ }
You can’t perform that action at this time.
0 commit comments