import React from "react"; import {getSettings, saveSettings} from "../../api"; import {Button, Form, FormGroup, FormInput, Header, Loader, Segment} from "../Theme"; import {Container, Dimmer, Icon, Modal} from "semantic-ui-react"; import {ThemeContext} from "../../contexts/contexts"; import {HelpPopup, HotspotToggle, ThrottleToggle, Toggle, WROLModeMessage} from "../Common"; import QRCode from "react-qr-code"; export class Settings extends React.Component { constructor(props) { super(props); this.state = { disabled: false, hotspot_encryption: 'WPA', pending: false, qrCodeValue: '', qrOpen: false, ready: false, download_on_startup: null, download_timeout: null, hotspot_device: null, hotspot_on_startup: null, hotspot_password: null, hotspot_ssid: null, hotspot_status: null, throttle_on_startup: null, throttle_status: null, } this.handleSubmit = this.handleSubmit.bind(this); this.handleHotspotChange = this.handleHotspotChange.bind(this); } async componentDidMount() { try { const settings = await getSettings(); this.setState({ ready: true, disabled: settings.wrol_mode, download_on_startup: settings.download_on_startup, download_timeout: settings.download_timeout || '', hotspot_device: settings.hotspot_device, hotspot_on_startup: settings.hotspot_on_startup, hotspot_password: settings.hotspot_password, hotspot_ssid: settings.hotspot_ssid, hotspot_status: settings.hotspot_status, throttle_on_startup: settings.throttle_on_startup, throttle_status: settings.throttle_status, }, this.handleHotspotChange); } catch (e) { console.error(e); } } async handleSubmit(e) { e.preventDefault(); this.setState({disabled: true, pending: true}); let settings = { download_on_startup: this.state.download_on_startup, download_timeout: this.state.download_timeout ? parseInt(this.state.download_timeout) : 0, hotspot_device: this.state.hotspot_device, hotspot_on_startup: this.state.hotspot_on_startup, hotspot_password: this.state.hotspot_password, hotspot_ssid: this.state.hotspot_ssid, throttle_on_startup: this.state.throttle_on_startup, } await saveSettings(settings); this.setState({disabled: false, pending: false}); } handleInputChange = async (e, name, value) => { if (e) { e.preventDefault() } this.setState({[name]: value}); } handleHotspotChange = async () => { let {hotspot_ssid, hotspot_encryption, hotspot_password} = this.state; // Special string which allows a mobile device to connect to a specific Wi-Fi. let qrCodeValue = `WIFI:S:${hotspot_ssid};T:${hotspot_encryption};P:${hotspot_password};;`; this.setState({qrCodeValue}); } handleTimeoutChange = async (e, name, value) => { if (e) { e.preventDefault() } // Restrict timeout to numbers. value = value.replace(/[^\d]/, ''); this.setState({[name]: value}); } handleQrOpen = async (e) => { e.preventDefault(); this.setState({qrOpen: true}); } render() { if (this.state.ready === false) { return } let { disabled, download_on_startup, download_timeout, hotspot_device, hotspot_on_startup, hotspot_password, hotspot_ssid, pending, qrCodeValue, throttle_on_startup, } = this.state; const qrButton = ; return {({i, t}) =>
Settings

Any changes will be written to config/wrolpi.yaml.

this.handleInputChange(null, 'download_on_startup', checked)} />
this.handleInputChange(null, 'hotspot_on_startup', checked)} />
this.handleInputChange(null, 'throttle_on_startup', checked)} />
Download Timeout } value={download_timeout} disabled={disabled || download_timeout === null} onChange={(e, d) => this.handleTimeoutChange(e, 'download_timeout', d.value)} /> this.setState({hotspot_ssid: d.value}, this.handleHotspotChange)} /> this.setState({hotspot_password: d.value}, this.handleHotspotChange)} /> this.handleInputChange(e, 'hotspot_device', d.value)} /> this.setState({qrOpen: false})} onOpen={this.handleQrOpen} open={this.state.qrOpen} trigger={qrButton} > Scan this code to join the hotspot
}
} }