From 678755b3ef835890141e8d41443fd8f2c7e27a52 Mon Sep 17 00:00:00 2001 From: Mitchell Date: Sun, 28 Aug 2016 23:34:18 -0500 Subject: [PATCH] Starting on sensor stuff --- client/js/components/Sidebar.js | 7 ++- client/js/components/sensors/SensorList.js | 60 ++++++++++++++++++++++ client/js/redux/actions.js | 1 + 3 files changed, 66 insertions(+), 2 deletions(-) create mode 100644 client/js/components/sensors/SensorList.js diff --git a/client/js/components/Sidebar.js b/client/js/components/Sidebar.js index a4a8572..a0026c2 100644 --- a/client/js/components/Sidebar.js +++ b/client/js/components/Sidebar.js @@ -1,6 +1,9 @@ import React from 'react'; -//assest +//components +import SensorList from './sensors/SensorList'; + +//assets import me from '../../assets/images/me.jpg'; import '../../assets/scss/Sidebar.scss'; @@ -60,7 +63,7 @@ export default class Sidebar extends React.Component{

Sensors


- + ); } diff --git a/client/js/components/sensors/SensorList.js b/client/js/components/sensors/SensorList.js new file mode 100644 index 0000000..f13a7fc --- /dev/null +++ b/client/js/components/sensors/SensorList.js @@ -0,0 +1,60 @@ +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 ( +
+

{sensor.location}

+

{sensor.temperature}

+

{date.toString()}

+
+ ); + } + render(){ + console.log(this.state); + return ( +
+ {this.state.fetched ? this.state.sensors.map(this.insertSensorData) : null} +
+ ) + } +} diff --git a/client/js/redux/actions.js b/client/js/redux/actions.js index 80fc555..47adfdd 100644 --- a/client/js/redux/actions.js +++ b/client/js/redux/actions.js @@ -1,5 +1,6 @@ import * as types from "./constants"; import marked from 'marked'; +import 'whatwg-fetch'; function initPreview(posts) { return {