-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathindex.jsx
More file actions
113 lines (97 loc) · 2.65 KB
/
index.jsx
File metadata and controls
113 lines (97 loc) · 2.65 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
const React = require('react');
const ReactDOM = require('react-dom');
class App extends React.Component {
constructor(){
super();
this.state = { todos: []};
dpd.todos.get()
.then(todos => this.setState({ todos }));
}
addTask(e){
e.preventDefault();
const todos = this.state.todos;
const title = this.input.value;
if (title) {
dpd.todos.post({ title }, (result, error) => {
if (error) throw new Error(error);
this.setState({
todos: [
...todos,
{
id: result.id,
title: this.input.value,
done: false
}
]
});
this.input.value = '';
});
}
}
toggleTask(i){
const todos = this.state.todos;
const updatedTodo = Object.assign({}, todos[i], {done: !todos[i].done});
dpd.todos.put(updatedTodo, (result, error) => {
this.setState({
todos: [
...todos.slice(0, i),
result,
...todos.slice(i+1)
]
});
});
}
removeDoneTasks(e){
e.preventDefault();
const todos = this.state.todos.filter(t => t.done);
todos.forEach(todo => {
dpd.todos.del(todo.id, (_, error) => {
if (error) throw new Error(error);
this.setState({ todos: this.state.todos.filter(t => t !== todo) });
});
});
}
render(){
var noTasks;
if (!this.state.todos.length)
noTasks = (<p id="empty">You don't have any todos! Add one now:</p>);
return (
<div className="container">
<h1>Deployd Todos</h1>
{ noTasks }
<ul id="todos" className="unstyled">
{
this.state.todos.map((todo, index) =>
{
return (
<li key={todo.id}>
<label className="checkbox">
<input type="checkbox" onClick={ this.toggleTask.bind(this, index) } checked={todo.done}/>
<span>{ todo.title }</span>
</label>
</li>
);
}
)
}
</ul>
<form className="form-inline">
<input
id="todo-title"
type="text"
ref={ (node) => this.input = node }
/>
<button
id="add-btn"
className="btn btn-success"
onClick={ this.addTask.bind(this) }
>Add</button>
</form>
<p>
<a href id="remove-completed-btn" onClick={ this.removeDoneTasks.bind(this) }>Remove completed</a>
</p>
</div>
);
}
}
ReactDOM.render(<App/>, document.getElementById('app'));