esp8266-react-framework/interface/src/containers/WiFiSettings.js

77 lines
2.1 KiB
JavaScript
Raw Normal View History

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';
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){
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 });
deselectNetwork();
}else {
2018-03-04 17:36:04 +00:00
this.props.loadData();
}
}
2018-03-04 17:36:04 +00:00
deselectNetworkAndLoadData() {
this.deselectNetwork();
2018-03-04 17:36:04 +00:00
this.props.loadData();
}
2018-03-04 17:36:04 +00:00
deselectNetwork() {
this.setState({ selectedNetwork:null });
}
render() {
2018-03-04 17:36:04 +00:00
const { selectedNetwork } = this.state;
const { data, fetched, errorMessage } = this.props;
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}
/>
</SectionContent>
)
}
}
WiFiSettings.propTypes = {
deselectNetwork: PropTypes.func,
selectedNetwork: PropTypes.object
};
2018-03-04 17:36:04 +00:00
export default restComponent(WIFI_SETTINGS_ENDPOINT, WiFiSettings);