1
0
mirror of https://github.com/mgerb/mywebsite synced 2026-01-10 18:02:51 +00:00
Files
mywebsite/client/js/components/sensors/SensorList.js
2016-08-28 23:34:18 -05:00

61 lines
1.1 KiB
JavaScript

import React from 'react';
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);
return (
<div key={index}>
<h3>{sensor.location}</h3>
<p>{sensor.temperature}</p>
<p>{date.toString()}</p>
</div>
);
}
render(){
console.log(this.state);
return (
<div>
{this.state.fetched ? this.state.sensors.map(this.insertSensorData) : null}
</div>
)
}
}