-
Notifications
You must be signed in to change notification settings - Fork 71
/
Copy pathapp.jsx
188 lines (151 loc) · 4.05 KB
/
app.jsx
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
class App extends React.Component {
render() {
return (
<div>
<nav class="teal top-nav z-depth-0">
<div class="container">
<div class="nav-wrapper">
<span class="page-title">webxcel-react-todo</span>
</div>
</div>
</nav>
<div class="container">
<TodoList />
</div>
</div>
)
}
};
class ItemAddForm extends React.Component {
constructor(props) {
super(props);
this.state = {
value: ""
};
this.onAddButtonClicked = this.onAddButtonClicked.bind(this);
this.handleInput = this.handleInput.bind(this);
}
onAddButtonClicked() {
const value = this.state.value.trim();
if (value.length > 0) {
this.props.onItemAdded(value);
this.setState({ value: "" });
}
}
handleInput(event) {
this.setState({ value: event.target.value });
}
render() {
return (
<div class="row">
<div class="input-field col s8">
<input placeholder="Type an item here and add it" value={this.state.value} onChange={this.handleInput} type="text" />
</div>
<div class="col s2 l3">
<br />
<a class="btn" onClick={this.onAddButtonClicked}>
<i class="material-icons">add</i>
</a>
</div>
</div>
);
}
}
class TodoList extends React.Component {
constructor(props) {
super(props);
this.state = {
items: []
};
this.addItem = this.addItem.bind(this);
this.itemDeleted = this.itemDeleted.bind(this);
}
componentDidMount() {
axios.get("/workbook/todo")
.then(response => this.setState({
items: response.data.map(item => {
item.checked = item.checked == "TRUE";
return item;
})
}));
}
addItem(title) {
const id = uuid(),
item = {
id,
title,
checked: false
};
axios.post("/workbook/todo", item);
this.state.items.push(item);
this.setState(this.state);
}
itemCheckedChanged(id, checked) {
axios.put(`/workbook/todo/${id}`, {
id,
checked
});
}
itemDeleted(id) {
const items = this.state.items.filter(item => item.id != id);
this.setState({ items });
axios.delete(`/workbook/todo/${id}`);
}
render() {
const items = this.state.items.map(item => (
<TodoItem id={item.id} title={item.title} checked={item.checked} onCheckedChanged={this.itemCheckedChanged} onDeleted={this.itemDeleted} />
));
return (
<div>
<ItemAddForm onItemAdded={this.addItem} />
<br />
<div class="row">
<div class="col s12">
{items}
</div>
</div>
</div>
);
}
}
class TodoItem extends React.Component {
constructor(props) {
super(props);
this.state = {
checked: props.checked
};
this.onChange = this.onChange.bind(this);
this.onDeleted = this.onDeleted.bind(this);
}
onChange() {
const id = this.props.id,
checked = !this.state.checked;
this.setState({ checked });
this.props.onCheckedChanged(id, checked);
}
onDeleted() {
const id = this.props.id;
this.props.onDeleted(id);
}
render() {
return (
<div class="card">
<div class="card-content row">
<div class="col s10">
<input type="checkbox" id={this.props.id} checked={this.state.checked} onChange={this.onChange} />
<label for={this.props.id} class="black-text">
<div class="title">{this.props.title}</div>
</label>
</div>
<div class="col s2" onClick={this.onDeleted}>
<i class="material-icons">delete</i>
</div>
</div>
</div>
);
}
}
ReactDOM.render(
<App />,
document.getElementById("app")
);