Skip to content

Commit

Permalink
Refactor app to work with local storage
Browse files Browse the repository at this point in the history
Also simplifying TodoApp to remove code now replaced by Redux.
  • Loading branch information
ashleydb committed Feb 6, 2017
1 parent acec9f6 commit 8980040
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 35 deletions.
7 changes: 7 additions & 0 deletions app/actions/actions.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ export var addTodo = (text) => {
};
}

export var addTodos = (todos) => {
return {
type: 'ADD_TODOS',
todos
};
}

export var toggleShowCompleted = () => {
return {
type: 'TOGGLE_SHOW_COMPLETED'
Expand Down
8 changes: 7 additions & 1 deletion app/app.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,20 @@ var {Route, Router, IndexRoute, hashHistory} = require('react-router');

//Include our component dependencies
var TodoApp = require('TodoApp');
var TodoAPI = require('TodoAPI');

var actions = require('actions');
var store = require('configureStore').configure();

store.subscribe(() => {
console.log('New state', store.getState());
var state = store.getState();
console.log('New state', state);
TodoAPI.setTodos(state.todos);
});

var initialTodos = TodoAPI.getTodos();
store.dispatch(actions.addTodos(initialTodos));

//Use jQuery to start foundation
$(document).foundation();

Expand Down
32 changes: 2 additions & 30 deletions app/components/TodoApp.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,47 +6,19 @@ var moment = require('moment');
import TodoList from 'TodoList';
import AddTodo from 'AddTodo';
import TodoSearch from 'TodoSearch';
var TodoAPI = require('TodoAPI');

//Main component of this app, maintaining state
var TodoApp = React.createClass({
getInitialState: function() {
return {
showCompleted: false,
searchText: '',
todos: TodoAPI.getTodos()
}
},
componentDidUpdate: function() {
//This is a little over eager to update even if the filter changes, but it works
TodoAPI.setTodos(this.state.todos);
},
handleAddTodo: function(text) {
this.setState({
todos: [
...this.state.todos,
{id: UUID(), text: text, createdAt: moment().unix(), completedAt: undefined, complete: false}
]
});
},
handleFilterTodo: function(searchText, showCompleted) {
this.setState({
showCompleted: showCompleted,
searchText: searchText.toLowerCase()
});
},
render: function() {
var {todos, showCompleted, searchText} = this.state;
var filteredTodos = TodoAPI.filterTodos(todos, showCompleted, searchText);
return (
<div>
<h1 className="page-title">Todo App</h1>
<div className="row">
<div className="column small-centered small-11 medium-6 large-5">
<div className="container">
<TodoSearch onSearch={this.handleFilterTodo}/>
<TodoSearch/>
<TodoList/>
<AddTodo onNewTodo={this.handleAddTodo}/>
<AddTodo/>
</div>
</div>
</div>
Expand Down
6 changes: 6 additions & 0 deletions app/reducers/reducers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,12 @@ export var todosReducer = (state = [], action) => {
completedAt: undefined, complete: false}
];
break;
case 'ADD_TODOS':
return [
...state,
...action.todos
];
break;
case 'TOGGLE_TODO':
return state.map((todo) => {
if (action.id === todo.id) {
Expand Down
11 changes: 11 additions & 0 deletions app/tests/actions/actions.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,17 @@ describe('Actions', () => {
expect(res).toEqual(action);
});

it('should generate add todos action', () => {
var action = {
type: 'ADD_TODOS',
todos: [{id: 1, text: 'something', completed: false, completedAt: undefined, createdAt: 5000}]
};

var res = actions.addTodos(action.todos);

expect(res).toEqual(action);
});

it('should generate toggle show completed action', () => {
var action = {
type: 'TOGGLE_SHOW_COMPLETED'
Expand Down
10 changes: 10 additions & 0 deletions app/tests/reducers/reducers.test.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,16 @@ describe('Reducers', () => {
expect(res[0].text).toEqual(action.text);
});

it('should add new todos', () => {
var action = {
type: 'ADD_TODOS',
todos: [{id: 1, text: 'something', completed: false, completedAt: undefined, createdAt: 5000}]
};
var res = reducers.todosReducer(df([]), df(action));
expect(res.length).toEqual(1);
expect(res[0]).toEqual(action.todos[0]);
});

it('should toggle a todo', () => {
var state = [{id: 123, text: 'hello', createdAt: 123,
completedAt: 234, complete: true}];
Expand Down
8 changes: 4 additions & 4 deletions public/bundle.js

Large diffs are not rendered by default.

0 comments on commit 8980040

Please sign in to comment.