Skip to content

Commit 2fc915a

Browse files
author
yinm
committed
ToDoの更新機能を追加
1 parent c7ff879 commit 2fc915a

File tree

3 files changed

+26
-3
lines changed

3 files changed

+26
-3
lines changed

app/assets/javascripts/Task.js

+10-1
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,23 @@
11
import React from "react";
22

33
export default class Task extends React.Component {
4+
handleUpdate(e) {
5+
e.preventDefault();
6+
this.props.onTaskUpdate({task: {id: this.props.id, status: e.target.value}});
7+
}
8+
49
render() {
510
return (
611
<tr key={this.props.id}>
712
<td>
813
{this.props.content}
914
</td>
1015
<td>
11-
{this.props.status}
16+
<select defaultValue={this.props.status} onChange={this.handleUpdate.bind(this)} >
17+
<option value="todo" key="todo">todo</option>
18+
<option value="doing" key="doing">doing</option>
19+
<option value="done" key="done">done</option>
20+
</select>
1221
</td>
1322
</tr>
1423
);

app/assets/javascripts/TaskApp.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,20 @@ export default class TaskApp extends React.Component {
4343
});
4444
}
4545

46+
taskUpdate(task) {
47+
request
48+
.patch(this.props.url + '/' + task.task.id)
49+
.accept('application/json')
50+
.send(task)
51+
.end((err, res) => {
52+
if (err || !res.ok) {
53+
console.error(this.props.url, status, err.toString());
54+
} else {
55+
this.setState({data: res.body});
56+
}
57+
});
58+
}
59+
4660
componentDidMount() {
4761
this.loadTaskFromServer();
4862
setInterval(this.loadTaskFromServer.bind(this),
@@ -62,7 +76,7 @@ export default class TaskApp extends React.Component {
6276
<th colSpan="3"></th>
6377
</tr>
6478
</thead>
65-
<TaskList data={this.state.data} />
79+
<TaskList data={this.state.data} onTaskUpdate={this.taskUpdate.bind(this)} />
6680
</table>
6781
</div>
6882
);

app/assets/javascripts/TaskList.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ export default class TaskList extends React.Component {
66
var tasks = this.props.data.map((task) => {
77
return (
88
<Task key={task.id} id={task.id}
9-
content={task.content} status={task.status}>
9+
content={task.content} status={task.status} onTaskUpdate={this.props.onTaskUpdate} >
1010
</Task>
1111
);
1212
});

0 commit comments

Comments
 (0)