diff --git a/interface/src/components/RestComponent.js b/interface/src/components/RestComponent.js new file mode 100644 index 0000000..1aad357 --- /dev/null +++ b/interface/src/components/RestComponent.js @@ -0,0 +1,96 @@ +import React from 'react'; +import {withNotifier} from '../components/SnackbarNotification'; + +export const restComponent = (endpointUrl, FormComponent) => { + + return withNotifier( + class extends React.Component { + + constructor(props) { + super(props); + + this.setState = this.setState.bind(this); + this.loadData = this.loadData.bind(this); + this.saveData = this.saveData.bind(this); + this.setData = this.setData.bind(this); + } + + setData(data) { + this.setState({ + data:data, + fetched: true, + errorMessage:null + }); + } + + loadData() { + this.setState({ + data:null, + fetched: false, + errorMessage:null + }); + fetch(endpointUrl) + .then(response => { + if (response.status === 200) { + return response.json(); + } + throw Error("Invalid status code: " + response.status); + }) + .then(json => {this.setState({data: json, fetched:true})}) + .catch(error =>{ + this.props.raiseNotification("Problem fetching: " + error.message); + this.setState({data: null, fetched:true, errorMessage:error.message}); + }); + } + + saveData(e) { + this.setState({fetched: false}); + fetch(endpointUrl, { + method: 'POST', + body: JSON.stringify(this.statedata), + headers: new Headers({ + 'Content-Type': 'application/json' + }) + }) + .then(response => { + if (response.status === 200) { + return response.json(); + } + throw Error("Invalid status code: " + response.status); + }) + .then(json => { + this.props.raiseNotification("Changes successfully applied."); + this.setState({data: json, fetched:true}); + }).catch(error => { + this.props.raiseNotification("Problem saving: " + error.message); + this.setState({data: null, fetched:true, errorMessage:error.message}); + }); + } + + valueChange = name => event => { + const { data } = this.state; + data[name] = event.target.value; + this.setState({data}); + }; + + checkboxChange = name => event => { + const { data } = this.state; + data[name] = event.target.checked; + this.setState({data}); + } + + render() { + return ; + } + + } + ); +} diff --git a/interface/src/containers/APStatus.js b/interface/src/containers/APStatus.js index ce74b23..5313b61 100644 --- a/interface/src/containers/APStatus.js +++ b/interface/src/containers/APStatus.js @@ -11,7 +11,7 @@ import SettingsInputAntennaIcon from 'material-ui-icons/SettingsInputAntenna'; import DeviceHubIcon from 'material-ui-icons/DeviceHub'; import ComputerIcon from 'material-ui-icons/Computer'; -import {withNotifier} from '../components/SnackbarNotification'; +import {restComponent} from '../components/RestComponent'; import SectionContent from '../components/SectionContent' import * as Highlight from '../constants/Highlight'; @@ -37,50 +37,29 @@ const styles = theme => ({ class APStatus extends Component { - constructor(props) { - super(props); - - this.state = { - status:null, - fetched: false, - errorMessage:null - }; - - this.setState = this.setState.bind(this); - this.loadAPStatus = this.loadAPStatus.bind(this); - } - componentDidMount() { - this.loadAPStatus(); + this.props.loadData(); } - loadAPStatus() { - simpleGet( - AP_STATUS_ENDPOINT, - this.setState, - this.props.raiseNotification - ); + apStatusHighlight(data){ + return data.active ? Highlight.SUCCESS : Highlight.IDLE; } - apStatusHighlight(status){ - return status.active ? Highlight.SUCCESS : Highlight.IDLE; - } - - apStatus(status){ - return status.active ? "Active" : "Inactive"; + apStatus(data){ + return data.active ? "Active" : "Inactive"; } // active, ip_address, mac_address, station_num - renderAPStatus(status, fullDetails, classes){ + renderAPStatus(data, fullDetails, classes){ const listItems = []; listItems.push( - + - + ); listItems.push(); @@ -88,7 +67,7 @@ class APStatus extends Component { listItems.push( IP - + ); listItems.push(); @@ -98,7 +77,7 @@ class APStatus extends Component { - + ); listItems.push(); @@ -108,7 +87,7 @@ class APStatus extends Component { - + ); listItems.push(); @@ -118,7 +97,7 @@ class APStatus extends Component { {listItems} - @@ -126,8 +105,7 @@ class APStatus extends Component { } render() { - const { status, fetched, errorMessage } = this.state; - const { classes, fullDetails } = this.props; + const { data, fetched, errorMessage, classes, fullDetails } = this.props; return ( @@ -140,13 +118,13 @@ class APStatus extends Component { : - status ? this.renderAPStatus(status, fullDetails, classes) + data ? this.renderAPStatus(data, fullDetails, classes) :
{errorMessage} -
@@ -156,4 +134,4 @@ class APStatus extends Component { } } -export default withNotifier(withStyles(styles)(APStatus)); +export default restComponent(AP_STATUS_ENDPOINT, withStyles(styles)(APStatus)); diff --git a/interface/src/containers/NTPStatus.js b/interface/src/containers/NTPStatus.js index 74344b9..a25c57b 100644 --- a/interface/src/containers/NTPStatus.js +++ b/interface/src/containers/NTPStatus.js @@ -19,11 +19,9 @@ import * as Highlight from '../constants/Highlight'; import { unixTimeToTimeAndDate } from '../constants/TimeFormat'; import { NTP_STATUS_ENDPOINT } from '../constants/Endpoints'; -import {withNotifier} from '../components/SnackbarNotification'; +import { restComponent } from '../components/RestComponent'; import SectionContent from '../components/SectionContent'; -import { simpleGet } from '../helpers/SimpleGet'; - import moment from 'moment'; const styles = theme => ({ @@ -48,51 +46,30 @@ const styles = theme => ({ class NTPStatus extends Component { - constructor(props) { - super(props); - - this.state = { - status:null, - fetched: false, - errorMessage:null - }; - - this.setState = this.setState.bind(this); - this.loadNTPStatus = this.loadNTPStatus.bind(this); - } - componentDidMount() { - this.loadNTPStatus(); + this.props.loadData(); } - loadNTPStatus() { - simpleGet( - NTP_STATUS_ENDPOINT, - this.setState, - this.props.raiseNotification - ); - } - - renderNTPStatus(status, fullDetails, classes){ + renderNTPStatus(data, fullDetails, classes){ const listItems = []; listItems.push( - + - + ); listItems.push(); - if (isSynchronized(status)) { + if (isSynchronized(data)) { listItems.push( - + ); listItems.push(); @@ -103,7 +80,7 @@ class NTPStatus extends Component { - 0 ? unixTimeToTimeAndDate(status.last_sync) : "never" } /> + 0 ? unixTimeToTimeAndDate(data.last_sync) : "never" } /> ); listItems.push(); @@ -113,7 +90,7 @@ class NTPStatus extends Component { - + ); listItems.push(); @@ -123,7 +100,7 @@ class NTPStatus extends Component { - + ); listItems.push(); @@ -133,7 +110,7 @@ class NTPStatus extends Component { - + ); @@ -142,7 +119,7 @@ class NTPStatus extends Component { {listItems} - @@ -150,8 +127,7 @@ class NTPStatus extends Component { } render() { - const { status, fetched, errorMessage } = this.state; - const { classes, fullDetails } = this.props; + const { data, fetched, errorMessage, classes, fullDetails } = this.props; return ( @@ -164,13 +140,13 @@ class NTPStatus extends Component { : - status ? this.renderNTPStatus(status, fullDetails, classes) + data ? this.renderNTPStatus(data, fullDetails, classes) :
{errorMessage} -
@@ -180,4 +156,4 @@ class NTPStatus extends Component { } } -export default withNotifier(withStyles(styles)(NTPStatus)); +export default restComponent(NTP_STATUS_ENDPOINT, withStyles(styles)(NTPStatus)); diff --git a/interface/src/containers/WiFiStatus.js b/interface/src/containers/WiFiStatus.js index 0a36d51..7103643 100644 --- a/interface/src/containers/WiFiStatus.js +++ b/interface/src/containers/WiFiStatus.js @@ -5,14 +5,13 @@ import Button from 'material-ui/Button'; import { LinearProgress } from 'material-ui/Progress'; import Typography from 'material-ui/Typography'; -import {withNotifier} from '../components/SnackbarNotification'; import SectionContent from '../components/SectionContent'; import { WIFI_STATUS_ENDPOINT } from '../constants/Endpoints'; import { isConnected, connectionStatus, connectionStatusHighlight } from '../constants/WiFiConnectionStatus'; import * as Highlight from '../constants/Highlight'; -import { simpleGet } from '../helpers/SimpleGet'; +import { restComponent } from '../components/RestComponent'; import List, { ListItem, ListItemText } from 'material-ui/List'; import Avatar from 'material-ui/Avatar'; @@ -47,21 +46,8 @@ const styles = theme => ({ class WiFiStatus extends Component { - constructor(props) { - super(props); - - this.state = { - status:null, - fetched: false, - errorMessage:null - }; - - this.setState = this.setState.bind(this); - this.loadWiFiStatus = this.loadWiFiStatus.bind(this); - } - componentDidMount() { - this.loadWiFiStatus(); + this.props.loadData(); } dnsServers(status) { @@ -71,27 +57,19 @@ class WiFiStatus extends Component { return status.dns_ip_1 + (status.dns_ip_2 ? ','+status.dns_ip_2 : ''); } - loadWiFiStatus() { - simpleGet( - WIFI_STATUS_ENDPOINT, - this.setState, - this.props.raiseNotification - ); - } - - renderWiFiStatus(status, fullDetails, classes) { + renderWiFiStatus(data, fullDetails, classes) { const listItems = []; listItems.push( - + - + ); - if (fullDetails && isConnected(status)) { + if (fullDetails && isConnected(data)) { listItems.push(); listItems.push( @@ -99,7 +77,7 @@ class WiFiStatus extends Component { - + ); listItems.push(); @@ -107,7 +85,7 @@ class WiFiStatus extends Component { listItems.push( IP - + ); listItems.push(); @@ -115,7 +93,7 @@ class WiFiStatus extends Component { listItems.push( # - + ); listItems.push(); @@ -125,7 +103,7 @@ class WiFiStatus extends Component { - + ); listItems.push(); @@ -135,7 +113,7 @@ class WiFiStatus extends Component { - + ); } @@ -145,7 +123,7 @@ class WiFiStatus extends Component { {listItems} - @@ -154,8 +132,7 @@ class WiFiStatus extends Component { } render() { - const { status, fetched, errorMessage } = this.state; - const { classes, fullDetails } = this.props; + const { data, fetched, errorMessage, classes, fullDetails } = this.props; return ( @@ -168,13 +145,13 @@ class WiFiStatus extends Component { : - status ? this.renderWiFiStatus(status, fullDetails, classes) + data ? this.renderWiFiStatus(data, fullDetails, classes) :
{errorMessage} -
@@ -184,4 +161,4 @@ class WiFiStatus extends Component { } } -export default withNotifier(withStyles(styles)(WiFiStatus)); +export default restComponent(WIFI_STATUS_ENDPOINT, withStyles(styles)(WiFiStatus));