/
App.js
63 lines (51 loc) · 1.41 KB
/
App.js
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
import React, { Component } from 'react';
import { ActionCableConsumer } from 'react-actioncable-provider';
import ItemsList from './ItemsList';
import { API_ROOT, HEADERS } from '../utils/constants';
class App extends Component {
constructor(props) {
super(props);
this.state = {
itemInput: '',
items: []
};
}
componentDidMount = () => {
fetch(`${API_ROOT}/items`)
.then(res => res.json())
.then(items => this.setState({ items }));
console.info("didMount: ", this.state)
};
onChangeHandler = (event) => {
this.setState({ itemInput: event.target.value });
}
onSubmitHandler = (event) => {
event.preventDefault();
fetch(`${API_ROOT}/items`, {
method: 'POST',
headers: HEADERS,
body: JSON.stringify({description: this.state.itemInput})
});
handleReceivedItems = response => {
const { item } = response;
this.setState({
items: [...this.state.items, item]
});
};
render() {
return (
<div>
<ActionCableConsumer
channel={{ channel: 'ItemsChannel' }}
onReceived={this.handleReceivedItems}
/>
<ItemsList items={this.state.items} />
<form className="App" onSubmit={this.onSubmitHandler}>
<input value={this.state.itemInput} onChange={this.onChangeHandler} />
<button>Submit</button>
</form>
</div>
);
}
}
export default App;