remove redundant code and refrences

This commit is contained in:
rjwats@gmail.com 2018-03-04 17:53:57 +00:00
parent 036c9de75c
commit 2c732d3281
6 changed files with 18 additions and 108 deletions

View File

@ -4,8 +4,6 @@ import { NTP_SETTINGS_ENDPOINT } from '../constants/Endpoints';
import {restComponent} from '../components/RestComponent'; import {restComponent} from '../components/RestComponent';
import SectionContent from '../components/SectionContent'; import SectionContent from '../components/SectionContent';
import NTPSettingsForm from '../forms/NTPSettingsForm'; import NTPSettingsForm from '../forms/NTPSettingsForm';
import { simpleGet } from '../helpers/SimpleGet';
import { simplePost } from '../helpers/SimplePost';
class NTPSettings extends Component { class NTPSettings extends Component {

View File

@ -4,8 +4,6 @@ import { OTA_SETTINGS_ENDPOINT } from '../constants/Endpoints';
import {restComponent} from '../components/RestComponent'; import {restComponent} from '../components/RestComponent';
import SectionContent from '../components/SectionContent'; import SectionContent from '../components/SectionContent';
import OTASettingsForm from '../forms/OTASettingsForm'; import OTASettingsForm from '../forms/OTASettingsForm';
import { simpleGet } from '../helpers/SimpleGet';
import { simplePost } from '../helpers/SimplePost';
class OTASettings extends Component { class OTASettings extends Component {

View File

@ -5,23 +5,17 @@ import { WIFI_SETTINGS_ENDPOINT } from '../constants/Endpoints';
import { restComponent } from '../components/RestComponent'; import { restComponent } from '../components/RestComponent';
import SectionContent from '../components/SectionContent'; import SectionContent from '../components/SectionContent';
import WiFiSettingsForm from '../forms/WiFiSettingsForm'; import WiFiSettingsForm from '../forms/WiFiSettingsForm';
import { simpleGet } from '../helpers/SimpleGet';
import { simplePost } from '../helpers/SimplePost';
class WiFiSettings extends Component { class WiFiSettings extends Component {
constructor(props) { constructor(props) {
super(props); super(props);
this.state = {
selectedNetwork: null,
};
this.deselectNetworkAndLoadData = this.deselectNetworkAndLoadData.bind(this); this.deselectNetworkAndLoadData = this.deselectNetworkAndLoadData.bind(this);
this.deselectNetwork = this.deselectNetwork.bind(this);
} }
componentDidMount() { componentDidMount() {
const { selectedNetwork, deselectNetwork } = this.props; const { selectedNetwork } = this.props;
if (selectedNetwork) { if (selectedNetwork) {
var wifiSettings = { var wifiSettings = {
ssid:selectedNetwork.ssid, ssid:selectedNetwork.ssid,
@ -30,25 +24,18 @@ class WiFiSettings extends Component {
static_ip_config:false, static_ip_config:false,
} }
this.props.setData(wifiSettings); this.props.setData(wifiSettings);
this.setState({ selectedNetwork });
deselectNetwork();
} else { } else {
this.props.loadData(); this.props.loadData();
} }
} }
deselectNetworkAndLoadData() { deselectNetworkAndLoadData() {
this.deselectNetwork(); this.props.deselectNetwork();
this.props.loadData(); this.props.loadData();
} }
deselectNetwork() {
this.setState({ selectedNetwork:null });
}
render() { render() {
const { selectedNetwork } = this.state; const { data, fetched, errorMessage, selectedNetwork } = this.props;
const { data, fetched, errorMessage } = this.props;
return ( return (
<SectionContent title="WiFi Settings"> <SectionContent title="WiFi Settings">
<WiFiSettingsForm <WiFiSettingsForm
@ -56,7 +43,7 @@ class WiFiSettings extends Component {
wifiSettingsFetched={fetched} wifiSettingsFetched={fetched}
errorMessage={errorMessage} errorMessage={errorMessage}
selectedNetwork={selectedNetwork} selectedNetwork={selectedNetwork}
deselectNetwork={this.deselectNetwork} deselectNetwork={this.props.deselectNetwork}
onSubmit={this.props.saveData} onSubmit={this.props.saveData}
onReset={this.deselectNetworkAndLoadData} onReset={this.deselectNetworkAndLoadData}
handleValueChange={this.props.handleValueChange} handleValueChange={this.props.handleValueChange}

View File

@ -1,4 +1,4 @@
import React from 'react'; import React, {Fragment} from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import { withStyles } from 'material-ui/styles'; import { withStyles } from 'material-ui/styles';
@ -142,8 +142,8 @@ class WiFiSettingsForm extends React.Component {
{ {
wifiSettings.static_ip_config && wifiSettings.static_ip_config &&
[ <Fragment>
<TextValidator key="local_ip" <TextValidator
validators={['required', 'isIP']} validators={['required', 'isIP']}
errorMessages={['Local IP is required', 'Must be an IP address']} errorMessages={['Local IP is required', 'Must be an IP address']}
name="local_ip" name="local_ip"
@ -152,8 +152,8 @@ class WiFiSettingsForm extends React.Component {
value={wifiSettings.local_ip} value={wifiSettings.local_ip}
onChange={handleValueChange('local_ip')} onChange={handleValueChange('local_ip')}
margin="normal" margin="normal"
/>, />
<TextValidator key="gateway_ip" <TextValidator
validators={['required', 'isIP']} validators={['required', 'isIP']}
errorMessages={['Gateway IP is required', 'Must be an IP address']} errorMessages={['Gateway IP is required', 'Must be an IP address']}
name="gateway_ip" name="gateway_ip"
@ -162,8 +162,8 @@ class WiFiSettingsForm extends React.Component {
value={wifiSettings.gateway_ip} value={wifiSettings.gateway_ip}
onChange={handleValueChange('gateway_ip')} onChange={handleValueChange('gateway_ip')}
margin="normal" margin="normal"
/>, />
<TextValidator key="subnet_mask" <TextValidator
validators={['required', 'isIP']} validators={['required', 'isIP']}
errorMessages={['Subnet mask is required', 'Must be an IP address']} errorMessages={['Subnet mask is required', 'Must be an IP address']}
name="subnet_mask" name="subnet_mask"
@ -172,8 +172,8 @@ class WiFiSettingsForm extends React.Component {
value={wifiSettings.subnet_mask} value={wifiSettings.subnet_mask}
onChange={handleValueChange('subnet_mask')} onChange={handleValueChange('subnet_mask')}
margin="normal" margin="normal"
/>, />
<TextValidator key="dns_ip_1" <TextValidator
validators={['isOptionalIP']} validators={['isOptionalIP']}
errorMessages={['Must be an IP address']} errorMessages={['Must be an IP address']}
name="dns_ip_1" name="dns_ip_1"
@ -182,8 +182,8 @@ class WiFiSettingsForm extends React.Component {
value={wifiSettings.dns_ip_1} value={wifiSettings.dns_ip_1}
onChange={handleValueChange('dns_ip_1')} onChange={handleValueChange('dns_ip_1')}
margin="normal" margin="normal"
/>, />
<TextValidator key="dns_ip_2" <TextValidator
validators={['isOptionalIP']} validators={['isOptionalIP']}
errorMessages={['Must be an IP address']} errorMessages={['Must be an IP address']}
name="dns_ip_2" name="dns_ip_2"
@ -193,7 +193,7 @@ class WiFiSettingsForm extends React.Component {
onChange={handleValueChange('dns_ip_2')} onChange={handleValueChange('dns_ip_2')}
margin="normal" margin="normal"
/> />
] </Fragment>
} }
<Button variant="raised" color="primary" className={classes.button} type="submit"> <Button variant="raised" color="primary" className={classes.button} type="submit">

View File

@ -1,35 +0,0 @@
/**
* Executes a get request for an endpoint, updating the local state of the calling
* component. The calling component must bind setState before using this
* function.
*
* This is designed for re-use in simple situations, we arn't using redux here!
*/
export const simpleGet = (
endpointUrl,
setState,
raiseNotification = null,
dataKey="status",
fetchedKey="fetched",
errorMessageKey = "errorMessage"
) => {
setState({
[dataKey]:null,
[fetchedKey]: false,
[errorMessageKey]:null
});
fetch(endpointUrl)
.then(response => {
if (response.status === 200) {
return response.json();
}
throw Error("Invalid status code: " + response.status);
})
.then(json => {setState({[dataKey]: json, [fetchedKey]:true})})
.catch(error =>{
if (raiseNotification) {
raiseNotification("Problem fetching: " + error.message);
}
setState({[dataKey]: null, [fetchedKey]:true, [errorMessageKey]:error.message});
});
}

View File

@ -1,38 +0,0 @@
/**
* Executes a post request for saving data to an endpoint, updating the local
* state with the response. The calling component must bind setState before
* using this function.
*
* This is designed for re-use in simple situations, we arn't using redux here!
*/
export const simplePost = (
endpointUrl,
state,
setState,
raiseNotification = null,
dataKey="settings",
fetchedKey="fetched",
errorMessageKey = "errorMessage"
) => {
setState({[fetchedKey]: false});
fetch(endpointUrl, {
method: 'POST',
body: JSON.stringify(state[dataKey]),
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 => {
raiseNotification("Changes successfully applied.");
setState({[dataKey]: json, [fetchedKey]:true});
}).catch(error => {
raiseNotification("Problem saving: " + error.message);
setState({[dataKey]: null, [fetchedKey]:true, [errorMessageKey]:error.message});
});
}