mirror of
https://github.com/mgerb/mywebsite
synced 2026-01-11 18:32:50 +00:00
handling sensors with redux
This commit is contained in:
@@ -3,41 +3,6 @@ import 'whatwg-fetch';
|
||||
|
||||
export default class SensorList extends React.Component {
|
||||
|
||||
constructor() {
|
||||
super();
|
||||
|
||||
this.state = {
|
||||
sensors: {},
|
||||
fetching: false,
|
||||
fetched: false
|
||||
}
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
this.loadSensorData();
|
||||
}
|
||||
|
||||
loadSensorData() {
|
||||
this.setState({
|
||||
fetching: true
|
||||
});
|
||||
|
||||
fetch('/api/allsensors')
|
||||
.then((response) => {
|
||||
return response.json()
|
||||
})
|
||||
.then((json) => {
|
||||
this.setState({
|
||||
sensors: json,
|
||||
fetching: false,
|
||||
fetched: true
|
||||
});
|
||||
})
|
||||
.catch((e) => {
|
||||
console.log('Loading sensors failed', e)
|
||||
});
|
||||
}
|
||||
|
||||
insertSensorData = (sensor, index) => {
|
||||
const date = new Date(sensor.updated);
|
||||
|
||||
@@ -50,9 +15,11 @@ export default class SensorList extends React.Component {
|
||||
);
|
||||
}
|
||||
render() {
|
||||
const list = this.props.list;
|
||||
|
||||
return (
|
||||
<div>
|
||||
{this.state.fetched ? this.state.sensors.map(this.insertSensorData) : null}
|
||||
{list.map(this.insertSensorData)}
|
||||
</div>
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user