2022-09-29 12:02:09 -06:00
|
|
|
import React from "react";
|
|
|
|
|
import {getSettings, saveSettings} from "../../api";
|
2023-04-18 22:03:45 -06:00
|
|
|
import {Button, Form, FormGroup, FormInput, Header, Loader, Modal, ModalContent, ModalHeader, Segment} from "../Theme";
|
|
|
|
|
import {Container, Dimmer, Icon} from "semantic-ui-react";
|
2022-09-29 12:02:09 -06:00
|
|
|
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});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 14:58:16 -06:00
|
|
|
handleQrOpen = async (e) => {
|
|
|
|
|
e.preventDefault();
|
|
|
|
|
this.setState({qrOpen: true});
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-29 12:02:09 -06:00
|
|
|
render() {
|
|
|
|
|
if (this.state.ready === false) {
|
|
|
|
|
return <Loader active inline='centered'/>
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 = <Button icon style={{marginBottom: '1em'}}><Icon name='qrcode' size='big'/></Button>;
|
|
|
|
|
|
|
|
|
|
return <ThemeContext.Consumer>
|
|
|
|
|
{({i, t}) => <Container fluid>
|
|
|
|
|
<WROLModeMessage content='Settings are disabled because WROL Mode is enabled.'/>
|
|
|
|
|
|
2022-12-02 11:23:05 -07:00
|
|
|
<Segment>
|
|
|
|
|
<HotspotToggle/>
|
|
|
|
|
<ThrottleToggle/>
|
|
|
|
|
</Segment>
|
|
|
|
|
|
|
|
|
|
<Segment>
|
|
|
|
|
<Header as='h2'>
|
|
|
|
|
Settings
|
|
|
|
|
</Header>
|
|
|
|
|
<p {...t}>
|
|
|
|
|
Any changes will be written to <i>config/wrolpi.yaml</i>.
|
|
|
|
|
</p>
|
|
|
|
|
|
|
|
|
|
<Form id="settings" onSubmit={this.handleSubmit}>
|
|
|
|
|
<div style={{margin: '0.5em'}}>
|
|
|
|
|
<Toggle
|
|
|
|
|
label='Download on Startup'
|
|
|
|
|
disabled={disabled || download_on_startup === null}
|
|
|
|
|
checked={download_on_startup === true}
|
|
|
|
|
onChange={checked => this.handleInputChange(null, 'download_on_startup', checked)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div style={{margin: '0.5em'}}>
|
|
|
|
|
<Toggle
|
|
|
|
|
label='WiFi Hotspot on Startup'
|
|
|
|
|
disabled={disabled || hotspot_on_startup === null}
|
|
|
|
|
checked={hotspot_on_startup === true}
|
|
|
|
|
onChange={checked => this.handleInputChange(null, 'hotspot_on_startup', checked)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<div style={{margin: '0.5em'}}>
|
|
|
|
|
<Toggle
|
|
|
|
|
label='CPU Power-save on Startup'
|
|
|
|
|
disabled={disabled || throttle_on_startup === null}
|
|
|
|
|
checked={throttle_on_startup === true}
|
|
|
|
|
onChange={checked => this.handleInputChange(null, 'throttle_on_startup', checked)}
|
|
|
|
|
/>
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
<FormGroup inline>
|
|
|
|
|
<FormInput
|
|
|
|
|
label={<>
|
|
|
|
|
<b {...t}>Download Timeout</b>
|
|
|
|
|
<HelpPopup content='Downloads will be stopped after this many seconds have elapsed.
|
2022-09-29 12:02:09 -06:00
|
|
|
Downloads will never timeout if this is empty.'/>
|
2022-12-02 11:23:05 -07:00
|
|
|
</>}
|
|
|
|
|
value={download_timeout}
|
|
|
|
|
disabled={disabled || download_timeout === null}
|
|
|
|
|
onChange={(e, d) => this.handleTimeoutChange(e, 'download_timeout', d.value)}
|
|
|
|
|
/>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
|
|
|
|
|
<FormGroup inline>
|
|
|
|
|
<FormInput
|
|
|
|
|
label='Hotspot SSID'
|
|
|
|
|
value={hotspot_ssid}
|
|
|
|
|
disabled={disabled || hotspot_ssid === null}
|
2023-01-07 18:31:39 -07:00
|
|
|
onChange={(e, d) => this.setState({hotspot_ssid: d.value}, this.handleHotspotChange)}
|
2022-12-02 11:23:05 -07:00
|
|
|
/>
|
|
|
|
|
<FormInput
|
|
|
|
|
label='Hotspot Password'
|
|
|
|
|
disabled={disabled || hotspot_password === null}
|
|
|
|
|
value={hotspot_password}
|
2023-01-07 18:31:39 -07:00
|
|
|
onChange={(e, d) => this.setState({hotspot_password: d.value}, this.handleHotspotChange)}
|
2022-12-02 11:23:05 -07:00
|
|
|
/>
|
|
|
|
|
<FormInput
|
|
|
|
|
label='Hotspot Device'
|
|
|
|
|
disabled={disabled || hotspot_password === null}
|
|
|
|
|
value={hotspot_device}
|
|
|
|
|
onChange={(e, d) => this.handleInputChange(e, 'hotspot_device', d.value)}
|
|
|
|
|
/>
|
|
|
|
|
</FormGroup>
|
|
|
|
|
|
|
|
|
|
<Modal closeIcon
|
|
|
|
|
onClose={() => this.setState({qrOpen: false})}
|
|
|
|
|
onOpen={this.handleQrOpen}
|
|
|
|
|
open={this.state.qrOpen}
|
|
|
|
|
trigger={qrButton}
|
|
|
|
|
>
|
2023-04-18 22:03:45 -06:00
|
|
|
<ModalHeader>
|
2022-12-02 11:23:05 -07:00
|
|
|
Scan this code to join the hotspot
|
2023-04-18 22:03:45 -06:00
|
|
|
</ModalHeader>
|
|
|
|
|
<ModalContent>
|
|
|
|
|
<div style={{display: 'inline-block', backgroundColor: '#ffffff', padding: '1em'}}>
|
|
|
|
|
<QRCode value={qrCodeValue} size={300}/>
|
|
|
|
|
</div>
|
|
|
|
|
</ModalContent>
|
2022-12-02 11:23:05 -07:00
|
|
|
</Modal>
|
|
|
|
|
|
2023-01-07 18:31:39 -07:00
|
|
|
<br/>
|
2022-12-02 11:23:05 -07:00
|
|
|
|
2023-04-05 10:18:52 -06:00
|
|
|
<Button color='violet' type="submit" disabled={disabled} size='large'>
|
2022-12-02 11:23:05 -07:00
|
|
|
Save
|
|
|
|
|
</Button>
|
|
|
|
|
|
|
|
|
|
<Dimmer active={pending}>
|
|
|
|
|
<Loader active={pending} size='large'/>
|
|
|
|
|
</Dimmer>
|
|
|
|
|
|
|
|
|
|
</Form>
|
|
|
|
|
</Segment>
|
2023-01-07 18:31:39 -07:00
|
|
|
</Container>}
|
2022-09-29 12:02:09 -06:00
|
|
|
</ThemeContext.Consumer>
|
|
|
|
|
}
|
|
|
|
|
}
|