-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[react-todo@1.0.2] created auxiliary files for configuration, saved i…
…nfo using local storage
- Loading branch information
Alexey_Berezin
committed
Dec 11, 2017
1 parent
e04fa37
commit ddb5bea
Showing
10 changed files
with
95 additions
and
18 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import { createStore } from 'redux'; | ||
|
||
import rootReducer from './js/reducers' | ||
|
||
import { loadState, saveState } from './js/helpers/local-storage'; | ||
import throttle from './js/helpers/throttle'; | ||
|
||
const configureStore = () => { | ||
const persistedState = loadState(); | ||
|
||
let store = createStore( | ||
rootReducer, | ||
persistedState, | ||
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__() | ||
); | ||
|
||
store.subscribe(throttle(() =>{ | ||
saveState({ | ||
todos: store.getState().todos | ||
}); | ||
})); | ||
|
||
return store; | ||
}; | ||
|
||
|
||
|
||
export default configureStore; |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,4 +14,4 @@ const App = () => ( | |
</div> | ||
); | ||
|
||
export default App | ||
export default App; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React from 'react'; | ||
import { Provider } from 'react-redux'; | ||
|
||
import App from './App'; | ||
|
||
const Root = ({ store }) => ( | ||
<Provider store={store}> | ||
<App/> | ||
</Provider> | ||
); | ||
|
||
export default Root; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const loadState = () => { | ||
try { | ||
const persistedState = localStorage.getItem('state'); | ||
if (persistedState === null) { | ||
return undefined; | ||
} | ||
return JSON.parse(persistedState); | ||
} catch (error) { | ||
return undefined; | ||
} | ||
} | ||
|
||
export const saveState = (state) => { | ||
try { | ||
const persistedState = JSON.stringify(state); | ||
localStorage.setItem('state', persistedState); | ||
} catch (error) { | ||
// TODO: add logging | ||
} | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
// 400 ms | ||
const DEFAULT_THROTTLE_TIME = 400; | ||
|
||
const throttle = (fn, time = DEFAULT_THROTTLE_TIME) => { | ||
let running = false; | ||
return (...args) => { | ||
if (running) { | ||
return Promise.reject(); | ||
} | ||
|
||
running = true; | ||
setTimeout(() => { | ||
running = false; | ||
}); | ||
return Promise.resolve().then(fn(...args)); | ||
}; | ||
}; | ||
|
||
export default throttle; |