forked from jhu-oose/todoose
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathItemsComponents.js
More file actions
86 lines (73 loc) · 2.25 KB
/
ItemsComponents.js
File metadata and controls
86 lines (73 loc) · 2.25 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
class PlusButton extends React.Component {
handleClick() {
fetch("/items", { method: "POST" });
}
render() {
return <button className={this.props.className} onClick={() => { this.handleClick(); }}>+</button>;
}
}
class ItemList extends React.Component {
constructor(props) {
super(props);
this.state = { items: [] };
}
async getDataFromServer() {
this.setState({ items: await (await fetch("/items")).json() });
window.setTimeout(() => { this.getDataFromServer(); }, 200);
}
componentDidMount() {
this.getDataFromServer();
}
render() {
return <ul>{this.state.items.map(item => <Item key={item.identifier} item={item}/>)}</ul>;
}
}
class Item extends React.Component {
render() {
return (
<li>
<MarkItemAsDoneCheckbox item={this.props.item}/>
<ItemDescription item={this.props.item}/>
</li>
);
}
}
class MarkItemAsDoneCheckbox extends React.Component {
handleChange() {
fetch(`/items/${this.props.item.identifier}`, { method: "DELETE" });
}
render() {
return <input type="checkbox" onChange={() => { this.handleChange(); } } />
}
}
class ItemDescription extends React.Component {
constructor(props) {
super(props);
this.state = null;
}
handleFocus() {
this.setState({ description: this.props.item.description });
}
handleChange(event) {
this.setState({ description: event.target.value });
}
async handleBlur() {
const formData = new FormData();
formData.append("description", this.state.description);
await fetch(`/items/${this.props.item.identifier}`, { method: "PUT", body: formData });
this.setState(null);
}
render() {
return (
<input
type="text"
name="description"
autoComplete="off"
value={this.state === null ? this.props.item.description : this.state.description}
onFocus={() => { this.handleFocus(); }}
onChange={event => { this.handleChange(event); }}
onBlur={() => { this.handleBlur(); }}
/>
);
}
}