Skip to content

Commit

Permalink
[react-todo@1.0.2] created auxiliary files for configuration, saved i…
Browse files Browse the repository at this point in the history
…nfo using local storage
  • Loading branch information
Alexey_Berezin committed Dec 11, 2017
1 parent e04fa37 commit ddb5bea
Show file tree
Hide file tree
Showing 10 changed files with 95 additions and 18 deletions.
7 changes: 6 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
{
"name": "react-todo",
"version": "1.0.1",
"version": "1.0.2",
"private": true,
"homepage": "http://beraliv.github.io/react-todo",
"dependencies": {
"classnames": "^2.2.5",
"node-uuid": "^1.4.8",
"prop-types": "^15.5.10",
"react": "^15.6.1",
"react-dom": "^15.6.1",
Expand Down
28 changes: 28 additions & 0 deletions src/configureStore.js
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;
16 changes: 4 additions & 12 deletions src/index.js
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
import React from 'react';
import { render } from 'react-dom';
import { Provider } from 'react-redux';
import { createStore } from 'redux';

import App from './js/components/App';
import rootReducer from './js/reducers'

import registerServiceWorker from './registerServiceWorker';
import configureStore from './configureStore';

import Root from './js/components';
import './index.css';

let store = createStore(
rootReducer,
window.__REDUX_DEVTOOLS_EXTENSION__ && window.__REDUX_DEVTOOLS_EXTENSION__()
);
const store = configureStore();

render(
<Provider store={store}>
<App />
</Provider>,
<Root store={store}/>,
document.getElementById('root')
);

Expand Down
4 changes: 2 additions & 2 deletions src/js/actionCreators/todos.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
let todoId = 0;
import { v4 } from 'node-uuid';

export const ADD_TODO = 'ADD_TODO';
export const TOGGLE_TODO = 'TOGGLE_TODO';
Expand All @@ -10,7 +10,7 @@ export const TOGGLE_ALL_TODO = 'TOGGLE_ALL_TODO';

export const addTodo = title => ({
type: ADD_TODO,
id: todoId++,
id: v4(),
title
});

Expand Down
2 changes: 1 addition & 1 deletion src/js/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ const App = () => (
</div>
);

export default App
export default App;
2 changes: 1 addition & 1 deletion src/js/components/TodoList.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const TodoList = ({ todos, actions: { toggleTodo, removeTodo } }) => (
TodoList.propTypes = {
todos: PropTypes.arrayOf(
PropTypes.shape({
id: PropTypes.number.isRequired,
id: PropTypes.string.isRequired,
title: PropTypes.string.isRequired,
isCompleted: PropTypes.bool.isRequired
}).isRequired
Expand Down
12 changes: 12 additions & 0 deletions src/js/components/index.js
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;
20 changes: 20 additions & 0 deletions src/js/helpers/local-storage.js
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
}
};
19 changes: 19 additions & 0 deletions src/js/helpers/throttle.js
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;

0 comments on commit ddb5bea

Please sign in to comment.