Skip to content

Commit eb7b4f7

Browse files
baseline for ep26
1 parent 09adec4 commit eb7b4f7

File tree

12 files changed

+621
-0
lines changed

12 files changed

+621
-0
lines changed

ep26-enhance-todo-store/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#### Setting up the application
2+
3+
```
4+
npm install
5+
npm start
6+
```
7+
8+
Visit http://localhost:8080 in browser.
9+
10+
#### Notes
11+
12+
* `npm install random-key --save`
13+
* [Source code](...)
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
var api = require("../utils/api");
2+
var TodoStore = require("../stores/TodoStore");
3+
4+
var TodoActions = {
5+
6+
addTodo: (todo) => {
7+
console.log("adding TODO");
8+
api.addTodo(todo)
9+
.then( () => {
10+
console.log("Added TODO successfully");
11+
TodoActions.getAllTodos();
12+
})
13+
},
14+
15+
deleteTodo: (todo) => {
16+
console.log("Deleting TODO");
17+
api.deleteTodo(todo.id)
18+
.then( () => {
19+
console.log("Deleted TODO successfully");
20+
TodoActions.getAllTodos();
21+
})
22+
},
23+
24+
markTodoDone: (todo) => {
25+
console.log("Marking TODO as done");
26+
api.markTodoDone(todo)
27+
.then( () => {
28+
console.log("marked TODO as done successfully");
29+
TodoActions.getAllTodos();
30+
})
31+
},
32+
33+
markTodoUnDone: (todo) => {
34+
console.log("Marking TODO as undone");
35+
api.markTodoUnDone(todo)
36+
.then( () => {
37+
console.log("marked TODO as undone successfully");
38+
TodoActions.getAllTodos();
39+
})
40+
},
41+
42+
getAllTodos: () => {
43+
api.getTodos()
44+
.then( (responseData) => {
45+
var todos = responseData.todos;
46+
console.log("new todos", todos);
47+
TodoStore.setTodos(todos);
48+
})
49+
}
50+
51+
}
52+
53+
module.exports = TodoActions;
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
import React from 'react';
2+
import DisplayList from './DisplayList';
3+
4+
var rand = require('random-key');
5+
var api = require("../utils/api");
6+
var TodoStore = require("../stores/TodoStore");
7+
var TodoActions = require("../actions/TodoActions");
8+
9+
export default class App extends React.Component {
10+
11+
constructor () {
12+
super();
13+
this.state = { title: '', todos: [] };
14+
15+
this.getAllTodos();
16+
}
17+
18+
componentDidMount () {
19+
var storeIsTellingUsThatDataHasChanged = (todos) => {
20+
console.log("Store is telling us that data has change");
21+
this.setState({todos: todos});
22+
}
23+
TodoStore.addChangeListener(storeIsTellingUsThatDataHasChanged);
24+
}
25+
26+
getAllTodos () {
27+
api.getTodos()
28+
.then( (responseData) => this.setState({todos: responseData.todos} ))
29+
}
30+
31+
handleSubmit (event) {
32+
event.preventDefault();
33+
34+
var newTodo = { title: this.state.title, done: false };
35+
36+
TodoActions.addTodo(newTodo);
37+
this.setState({ title: '' });
38+
}
39+
40+
handleChange (event) {
41+
var title = event.target.value;
42+
this.setState({ title: title });
43+
}
44+
45+
handleClearCompleted (event) {
46+
var newTodos = this.state.todos.filter((todo) => { return !todo.done});
47+
this.setState({ todos: newTodos });
48+
}
49+
50+
render () {
51+
return <div>
52+
<h1> TODO </h1>
53+
<form onSubmit={this.handleSubmit.bind(this)}>
54+
<input type="text"
55+
onChange={this.handleChange.bind(this)}
56+
value={this.state.title} />
57+
</form>
58+
59+
<DisplayList
60+
todos={this.state.todos} />
61+
62+
<footer>
63+
All: ({ this.state.todos.length }) |
64+
Completed: ({ this.state.todos.filter((todo) => { return todo.done }).length }) |
65+
Pending: ({ this.state.todos.filter((todo) => { return !todo.done }).length }) |
66+
<a href='#' onClick={this.handleClearCompleted.bind(this)}>Clear Completed</a>
67+
</footer>
68+
</div>;
69+
}
70+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
import React from 'react';
2+
3+
var TodoActions = require('../actions/TodoActions');
4+
5+
export default class DisplayItem extends React.Component {
6+
7+
constructor () {
8+
super();
9+
this.state = { editing: false }
10+
}
11+
12+
componentDidMount () {
13+
this.setState({ changedText: this.props.todo.title });
14+
}
15+
16+
handleEditing (event) {
17+
this.setState({ editing: true, changedText: this.props.todo.title });
18+
}
19+
20+
handleEditingDone (event) {
21+
if (event.keyCode === 13 ) { // submit
22+
this.setState({ editing: false });
23+
}
24+
}
25+
26+
handleEditingChange (event) {
27+
var _changedText = event.target.value;
28+
this.setState({ changedText: _changedText });
29+
}
30+
31+
toggleDone (todo) {
32+
if (todo.done) {
33+
TodoActions.markTodoUnDone(todo);
34+
} else {
35+
TodoActions.markTodoDone(todo);
36+
}
37+
}
38+
39+
handleDeleteTodoClick (todo) {
40+
TodoActions.deleteTodo(todo);
41+
}
42+
43+
render () {
44+
var todo = this.props.todo;
45+
46+
var viewStyle = {};
47+
var editStyle = {};
48+
49+
if (this.state.editing) {
50+
viewStyle.display = 'none';
51+
} else {
52+
editStyle.display = 'none';
53+
}
54+
55+
return <li className={ todo.done ? 'done' : '' }>
56+
<div style={viewStyle} onDoubleClick={this.handleEditing.bind(this)}>
57+
<input
58+
checked={todo.done}
59+
onChange={this.toggleDone.bind(this, todo)}
60+
type="checkbox"
61+
style={{ fontSize: 'x-large' }} />
62+
63+
<label>
64+
{ this.state.changedText }
65+
</label>
66+
67+
<a href='#'
68+
className="destroy"
69+
onClick={ this.handleDeleteTodoClick.bind(this, todo) }>
70+
[x]
71+
</a>
72+
</div>
73+
74+
<input type="text"
75+
onKeyDown={this.handleEditingDone.bind(this)}
76+
onChange={this.handleEditingChange.bind(this)}
77+
style={editStyle}
78+
value={this.state.changedText} />
79+
</li>
80+
}
81+
82+
}
83+
84+
DisplayItem.propTypes = {
85+
todo: React.PropTypes.object.isRequired
86+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import React from 'react';
2+
import DisplayItem from './DisplayItem';
3+
4+
export default class DisplayList extends React.Component {
5+
6+
render () {
7+
return <ul id="todo-list">
8+
{ this.props.todos.map((todo, i) => {
9+
return <section id="main" key={todo.id}>
10+
<DisplayItem
11+
todo={todo} />
12+
</section>
13+
}) }
14+
</ul>
15+
}
16+
17+
}
18+
19+
DisplayList.propTypes = {
20+
todos: React.PropTypes.array.isRequired
21+
}

ep26-enhance-todo-store/app/main.jsx

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import './stylesheets/main.css';
2+
3+
import React from 'react';
4+
import App from './components/App';
5+
6+
main();
7+
8+
function main() {
9+
var div = document.createElement('div');
10+
div.setAttribute("id", "todoapp");
11+
document.body.appendChild(div);
12+
13+
React.render(<App />, div);
14+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
var _todos = {};
2+
var _callback;
3+
4+
var TodoStore = {
5+
6+
setTodos: (todos) => {
7+
_todos = todos;
8+
console.log("TodoStore", TodoStore.getTodos());
9+
_callback(todos);
10+
},
11+
12+
getTodos: () => {
13+
return _todos;
14+
},
15+
16+
addChangeListener: function (callback) {
17+
console.log("registering callback for changelistener");
18+
_callback = callback;
19+
}
20+
}
21+
22+
module.exports = TodoStore;

0 commit comments

Comments
 (0)