remove redundant code and refrences
This commit is contained in:
parent
036c9de75c
commit
2c732d3281
@ -4,8 +4,6 @@ import { NTP_SETTINGS_ENDPOINT } from '../constants/Endpoints';
|
||||
import {restComponent} from '../components/RestComponent';
|
||||
import SectionContent from '../components/SectionContent';
|
||||
import NTPSettingsForm from '../forms/NTPSettingsForm';
|
||||
import { simpleGet } from '../helpers/SimpleGet';
|
||||
import { simplePost } from '../helpers/SimplePost';
|
||||
|
||||
class NTPSettings extends Component {
|
||||
|
||||
|
@ -4,8 +4,6 @@ import { OTA_SETTINGS_ENDPOINT } from '../constants/Endpoints';
|
||||
import {restComponent} from '../components/RestComponent';
|
||||
import SectionContent from '../components/SectionContent';
|
||||
import OTASettingsForm from '../forms/OTASettingsForm';
|
||||
import { simpleGet } from '../helpers/SimpleGet';
|
||||
import { simplePost } from '../helpers/SimplePost';
|
||||
|
||||
class OTASettings extends Component {
|
||||
|
||||
|
@ -5,24 +5,18 @@ import { WIFI_SETTINGS_ENDPOINT } from '../constants/Endpoints';
|
||||
import { restComponent } from '../components/RestComponent';
|
||||
import SectionContent from '../components/SectionContent';
|
||||
import WiFiSettingsForm from '../forms/WiFiSettingsForm';
|
||||
import { simpleGet } from '../helpers/SimpleGet';
|
||||
import { simplePost } from '../helpers/SimplePost';
|
||||
|
||||
class WiFiSettings extends Component {
|
||||
|
||||
constructor(props) {
|
||||
super(props);
|
||||
|
||||
this.state = {
|
||||
selectedNetwork: null,
|
||||
};
|
||||
this.deselectNetworkAndLoadData = this.deselectNetworkAndLoadData.bind(this);
|
||||
this.deselectNetwork = this.deselectNetwork.bind(this);
|
||||
}
|
||||
|
||||
componentDidMount() {
|
||||
const { selectedNetwork, deselectNetwork } = this.props;
|
||||
if (selectedNetwork){
|
||||
const { selectedNetwork } = this.props;
|
||||
if (selectedNetwork) {
|
||||
var wifiSettings = {
|
||||
ssid:selectedNetwork.ssid,
|
||||
password:"",
|
||||
@ -30,25 +24,18 @@ class WiFiSettings extends Component {
|
||||
static_ip_config:false,
|
||||
}
|
||||
this.props.setData(wifiSettings);
|
||||
this.setState({ selectedNetwork });
|
||||
deselectNetwork();
|
||||
}else {
|
||||
} else {
|
||||
this.props.loadData();
|
||||
}
|
||||
}
|
||||
|
||||
deselectNetworkAndLoadData() {
|
||||
this.deselectNetwork();
|
||||
this.props.deselectNetwork();
|
||||
this.props.loadData();
|
||||
}
|
||||
|
||||
deselectNetwork() {
|
||||
this.setState({ selectedNetwork:null });
|
||||
}
|
||||
|
||||
render() {
|
||||
const { selectedNetwork } = this.state;
|
||||
const { data, fetched, errorMessage } = this.props;
|
||||
const { data, fetched, errorMessage, selectedNetwork } = this.props;
|
||||
return (
|
||||
<SectionContent title="WiFi Settings">
|
||||
<WiFiSettingsForm
|
||||
@ -56,7 +43,7 @@ class WiFiSettings extends Component {
|
||||
wifiSettingsFetched={fetched}
|
||||
errorMessage={errorMessage}
|
||||
selectedNetwork={selectedNetwork}
|
||||
deselectNetwork={this.deselectNetwork}
|
||||
deselectNetwork={this.props.deselectNetwork}
|
||||
onSubmit={this.props.saveData}
|
||||
onReset={this.deselectNetworkAndLoadData}
|
||||
handleValueChange={this.props.handleValueChange}
|
||||
|
@ -1,4 +1,4 @@
|
||||
import React from 'react';
|
||||
import React, {Fragment} from 'react';
|
||||
import PropTypes from 'prop-types';
|
||||
|
||||
import { withStyles } from 'material-ui/styles';
|
||||
@ -142,8 +142,8 @@ class WiFiSettingsForm extends React.Component {
|
||||
|
||||
{
|
||||
wifiSettings.static_ip_config &&
|
||||
[
|
||||
<TextValidator key="local_ip"
|
||||
<Fragment>
|
||||
<TextValidator
|
||||
validators={['required', 'isIP']}
|
||||
errorMessages={['Local IP is required', 'Must be an IP address']}
|
||||
name="local_ip"
|
||||
@ -152,8 +152,8 @@ class WiFiSettingsForm extends React.Component {
|
||||
value={wifiSettings.local_ip}
|
||||
onChange={handleValueChange('local_ip')}
|
||||
margin="normal"
|
||||
/>,
|
||||
<TextValidator key="gateway_ip"
|
||||
/>
|
||||
<TextValidator
|
||||
validators={['required', 'isIP']}
|
||||
errorMessages={['Gateway IP is required', 'Must be an IP address']}
|
||||
name="gateway_ip"
|
||||
@ -162,8 +162,8 @@ class WiFiSettingsForm extends React.Component {
|
||||
value={wifiSettings.gateway_ip}
|
||||
onChange={handleValueChange('gateway_ip')}
|
||||
margin="normal"
|
||||
/>,
|
||||
<TextValidator key="subnet_mask"
|
||||
/>
|
||||
<TextValidator
|
||||
validators={['required', 'isIP']}
|
||||
errorMessages={['Subnet mask is required', 'Must be an IP address']}
|
||||
name="subnet_mask"
|
||||
@ -172,8 +172,8 @@ class WiFiSettingsForm extends React.Component {
|
||||
value={wifiSettings.subnet_mask}
|
||||
onChange={handleValueChange('subnet_mask')}
|
||||
margin="normal"
|
||||
/>,
|
||||
<TextValidator key="dns_ip_1"
|
||||
/>
|
||||
<TextValidator
|
||||
validators={['isOptionalIP']}
|
||||
errorMessages={['Must be an IP address']}
|
||||
name="dns_ip_1"
|
||||
@ -182,8 +182,8 @@ class WiFiSettingsForm extends React.Component {
|
||||
value={wifiSettings.dns_ip_1}
|
||||
onChange={handleValueChange('dns_ip_1')}
|
||||
margin="normal"
|
||||
/>,
|
||||
<TextValidator key="dns_ip_2"
|
||||
/>
|
||||
<TextValidator
|
||||
validators={['isOptionalIP']}
|
||||
errorMessages={['Must be an IP address']}
|
||||
name="dns_ip_2"
|
||||
@ -193,7 +193,7 @@ class WiFiSettingsForm extends React.Component {
|
||||
onChange={handleValueChange('dns_ip_2')}
|
||||
margin="normal"
|
||||
/>
|
||||
]
|
||||
</Fragment>
|
||||
}
|
||||
|
||||
<Button variant="raised" color="primary" className={classes.button} type="submit">
|
||||
|
@ -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});
|
||||
});
|
||||
}
|
@ -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});
|
||||
});
|
||||
}
|
Loading…
Reference in New Issue
Block a user