2018-02-26 00:11:31 +00:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
|
|
|
|
import { WIFI_SETTINGS_ENDPOINT } from '../constants/Endpoints';
|
2018-03-04 17:36:04 +00:00
|
|
|
import { restComponent } from '../components/RestComponent';
|
2018-02-26 00:11:31 +00:00
|
|
|
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,
|
|
|
|
};
|
2018-03-04 17:43:01 +00:00
|
|
|
this.deselectNetworkAndLoadData = this.deselectNetworkAndLoadData.bind(this);
|
2018-02-26 00:11:31 +00:00
|
|
|
this.deselectNetwork = this.deselectNetwork.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
const { selectedNetwork, deselectNetwork } = this.props;
|
|
|
|
if (selectedNetwork){
|
|
|
|
var wifiSettings = {
|
|
|
|
ssid:selectedNetwork.ssid,
|
|
|
|
password:"",
|
|
|
|
hostname:"esp8266-react",
|
|
|
|
static_ip_config:false,
|
|
|
|
}
|
2018-03-04 17:36:04 +00:00
|
|
|
this.props.setData(wifiSettings);
|
|
|
|
this.setState({ selectedNetwork });
|
2018-02-26 00:11:31 +00:00
|
|
|
deselectNetwork();
|
|
|
|
}else {
|
2018-03-04 17:36:04 +00:00
|
|
|
this.props.loadData();
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-04 17:36:04 +00:00
|
|
|
deselectNetworkAndLoadData() {
|
2018-02-26 00:11:31 +00:00
|
|
|
this.deselectNetwork();
|
2018-03-04 17:36:04 +00:00
|
|
|
this.props.loadData();
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
2018-03-04 17:36:04 +00:00
|
|
|
deselectNetwork() {
|
|
|
|
this.setState({ selectedNetwork:null });
|
2018-02-26 00:11:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2018-03-04 17:36:04 +00:00
|
|
|
const { selectedNetwork } = this.state;
|
|
|
|
const { data, fetched, errorMessage } = this.props;
|
2018-02-26 00:11:31 +00:00
|
|
|
return (
|
|
|
|
<SectionContent title="WiFi Settings">
|
2018-03-04 17:36:04 +00:00
|
|
|
<WiFiSettingsForm
|
|
|
|
wifiSettings={data}
|
|
|
|
wifiSettingsFetched={fetched}
|
|
|
|
errorMessage={errorMessage}
|
|
|
|
selectedNetwork={selectedNetwork}
|
|
|
|
deselectNetwork={this.deselectNetwork}
|
|
|
|
onSubmit={this.props.saveData}
|
|
|
|
onReset={this.deselectNetworkAndLoadData}
|
|
|
|
handleValueChange={this.props.handleValueChange}
|
|
|
|
handleCheckboxChange={this.props.handleCheckboxChange}
|
|
|
|
/>
|
2018-02-26 00:11:31 +00:00
|
|
|
</SectionContent>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
WiFiSettings.propTypes = {
|
|
|
|
deselectNetwork: PropTypes.func,
|
|
|
|
selectedNetwork: PropTypes.object
|
|
|
|
};
|
|
|
|
|
2018-03-04 17:36:04 +00:00
|
|
|
export default restComponent(WIFI_SETTINGS_ENDPOINT, WiFiSettings);
|