'use strict'; import 'whatwg-fetch'; import React from 'react'; export default class WifiStep extends React.Component { constructor (props) { super(props); this.state = { loading: false, networks: { networks: [] }, buttonDisabled: true, selectedSsid: null, showSsidInput: false, showPasswordInput: false, showPassword: false }; } componentDidMount () { let interval; let done = false; let networks = () => { window.fetch(`${this.props.baseApi}/networks`).then((res) => { if (res.ok && !done) { done = true; window.clearInterval(interval); return res.json(); } }).then((json) => { this.setState({ loading: false, networks: json }); }); }; interval = window.setInterval(networks, 5 * 1000); networks(); } handleSelectChange (e) { if (e.target.value === 'select') { this.setState({ showSsidInput: false, showPasswordInput: false, selectedSsid: null, buttonDisabled: true }); } else if (e.target.value === 'other') { this.setState({ showSsidInput: true, showPasswordInput: true, selectedSsid: null, buttonDisabled: false }); } else { let data = e.target.options[e.target.selectedIndex].dataset; this.setState({ showSsidInput: false, showPasswordInput: data.open === 'no', selectedSsid: data.ssid, buttonDisabled: false }); } } handleHiddenChange (e) { this.setState({ showPassword: e.target.checked }); } handleFormSubmit (e) { e.preventDefault(); let creds = {}; if (this.state.selectedSsid) { creds.ssid = this.state.selectedSsid; } else { creds.ssid = this.refs.ssid.value; } creds.password = this.refs.password.value; this.props.setWifiCreds(creds); this.props.nextStep(); } render () { return (
Select the Wi-Fi network to connect to: