From 25f04c3d7f4dd15b318bda894626c8b4e62def23 Mon Sep 17 00:00:00 2001 From: rjwats Date: Sun, 19 Jul 2020 19:57:59 +0100 Subject: [PATCH] Retain configured hostname (#173) Retain configured hostname when selecting new network from network scanner --- interface/src/wifi/WiFiSettingsController.tsx | 27 +----------------- interface/src/wifi/WiFiSettingsForm.tsx | 28 +++++++++++++++++-- 2 files changed, 27 insertions(+), 28 deletions(-) diff --git a/interface/src/wifi/WiFiSettingsController.tsx b/interface/src/wifi/WiFiSettingsController.tsx index b5ad0f8..d0613f8 100644 --- a/interface/src/wifi/WiFiSettingsController.tsx +++ b/interface/src/wifi/WiFiSettingsController.tsx @@ -1,8 +1,7 @@ import React, { Component } from 'react'; -import {restController, RestControllerProps, RestFormLoader, SectionContent } from '../components'; +import { restController, RestControllerProps, RestFormLoader, SectionContent } from '../components'; import WiFiSettingsForm from './WiFiSettingsForm'; -import { WiFiConnectionContext } from './WiFiConnectionContext'; import { WIFI_SETTINGS_ENDPOINT } from '../api'; import { WiFiSettings } from './types'; @@ -10,39 +9,15 @@ type WiFiSettingsControllerProps = RestControllerProps; class WiFiSettingsController extends Component { - static contextType = WiFiConnectionContext; - context!: React.ContextType; - componentDidMount() { - const { selectedNetwork } = this.context; - if (selectedNetwork) { - const wifiSettings: WiFiSettings = { - ssid: selectedNetwork.ssid, - password: "", - hostname: "esp8266-react", - static_ip_config: false, - } - this.props.setData(wifiSettings); - } else { - this.props.loadData(); - } - } - - deselectNetworkAndLoadData = () => { - this.context.deselectNetwork(); this.props.loadData(); } - componentWillUnmount() { - this.context.deselectNetwork(); - } - render() { return ( } /> diff --git a/interface/src/wifi/WiFiSettingsForm.tsx b/interface/src/wifi/WiFiSettingsForm.tsx index f36e13f..80c7039 100644 --- a/interface/src/wifi/WiFiSettingsForm.tsx +++ b/interface/src/wifi/WiFiSettingsForm.tsx @@ -24,15 +24,39 @@ class WiFiSettingsForm extends React.Component { static contextType = WiFiConnectionContext; context!: React.ContextType; + constructor(props: WiFiStatusFormProps, context: WiFiConnectionContext) { + super(props); + + const { selectedNetwork } = context; + if (selectedNetwork) { + const wifiSettings: WiFiSettings = { + ssid: selectedNetwork.ssid, + password: "", + hostname: props.data.hostname, + static_ip_config: false, + } + props.setData(wifiSettings); + } + } + componentWillMount() { ValidatorForm.addValidationRule('isIP', isIP); ValidatorForm.addValidationRule('isHostname', isHostname); ValidatorForm.addValidationRule('isOptionalIP', optional(isIP)); } + deselectNetworkAndLoadData = () => { + this.context.deselectNetwork(); + this.props.loadData(); + } + + componentWillUnmount() { + this.context.deselectNetwork(); + } + render() { const { selectedNetwork, deselectNetwork } = this.context; - const { data, handleValueChange, saveData, loadData } = this.props; + const { data, handleValueChange, saveData } = this.props; return ( { @@ -167,7 +191,7 @@ class WiFiSettingsForm extends React.Component { } variant="contained" color="primary" type="submit"> Save - + Reset