import React from 'react';
import {
Button,
Checkbox,
Confirm,
Container,
Divider,
Form,
Header,
Loader,
Placeholder,
Table
} from "semantic-ui-react";
import {clearDownloads, getDownloads, getSettings, killDownload, postDownload, saveSettings} from "../api";
import TimezoneSelect from 'react-timezone-select';
import {
DisableDownloadsToggle,
HotspotToggle,
PageContainer,
secondsToDate,
secondsToFrequency,
TabLinks,
textEllipsis,
ThrottleToggle,
WROLModeMessage
} from "./Common";
import {Route} from "react-router-dom";
class Settings extends React.Component {
constructor(props) {
super(props);
this.state = {
disabled: false,
ready: false,
download_on_startup: null,
hotspot_on_startup: null,
hotspot_status: null,
throttle_on_startup: null,
throttle_status: null,
timezone: {timezone: null},
}
this.handleSubmit = this.handleSubmit.bind(this);
}
async componentDidMount() {
const settings = await getSettings();
this.setState({
ready: true,
disabled: settings.wrol_mode,
download_on_startup: settings.download_on_startup,
hotspot_on_startup: settings.hotspot_on_startup,
hotspot_status: settings.hotspot_status,
throttle_on_startup: settings.throttle_on_startup,
throttle_status: settings.throttle_status,
timezone: {value: settings.timezone, label: settings.timezone},
});
}
async handleSubmit(e) {
e.preventDefault();
let settings = {
download_on_startup: this.state.download_on_startup,
hotspot_on_startup: this.state.hotspot_on_startup,
throttle_on_startup: this.state.throttle_on_startup,
timezone: this.state.timezone.value,
}
await saveSettings(settings);
}
handleTimezoneChange = async (timezone) => {
this.setState({timezone: timezone});
}
handleInputChange = async (e, name, checked) => {
e.preventDefault();
this.setState({[name]: checked});
}
render() {
if (this.state.ready === false) {
return
}
let {disabled, download_on_startup, hotspot_on_startup, throttle_on_startup, timezone} = this.state;
return (
The settings for your WROLPi.
Any changes will be written to config/wrolpi.yaml.
)
}
}
class WROLMode extends React.Component {
constructor(props) {
super(props);
this.state = {
WROLMode: false,
}
}
async componentDidMount() {
let settings = await getSettings();
this.setState({ready: true, WROLMode: settings.wrol_mode});
}
toggleWROLMode = async () => {
// Handle WROL Mode toggling by itself so that other settings are not modified.
let config = {
wrol_mode: !this.state.WROLMode,
}
await saveSettings(config);
this.setState({disabled: !this.state.WROLMode, WROLMode: !this.state.WROLMode});
}
render() {
if (this.state.ready === false) {
return
}
return (
Enable read-only mode. No content can be deleted or modified. Enable this when the SHTF and you
want to prevent any potential loss of data.
Note: User settings and favorites can still be modified.
)
}
}
function ClearCompleteDownloads() {
const [disabled, setDisabled] = React.useState(false);
async function localClearDownloads() {
setDisabled(true);
await clearDownloads();
setDisabled(false);
}
return <>
>
}
class DownloadRow extends React.Component {
render() {
let {url, frequency, last_successful_download, status, next_download} = this.props;
let positive = false;
if (status === 'pending') {
positive = true;
}
return (
{textEllipsis(url, 50)}
{secondsToFrequency(frequency)}
{last_successful_download ? secondsToDate(last_successful_download) : null}
{secondsToDate(next_download)}
{status}
);
}
}
class StoppableRow extends React.Component {
constructor(props) {
super(props);
this.state = {
stopOpen: false,
startOpen: false,
};
}
openStop = () => {
this.setState({stopOpen: true});
}
closeStop = () => {
this.setState({stopOpen: false});
}
openStart = () => {
this.setState({startOpen: true});
}
closeStart = () => {
this.setState({startOpen: false});
}
handleStop = async (e) => {
e.preventDefault();
await killDownload(this.props.id);
this.closeStop();
await this.props.fetchDownloads();
};
handleStart = async (e) => {
e.preventDefault();
let downloader = this.props.downloader || null;
await postDownload(`${this.props.url}`, downloader);
this.closeStart();
await this.props.fetchDownloads();
};
render() {
let {url, last_successful_download, status} = this.props;
let {stopOpen, startOpen} = this.state;
let buttonCell = ;
let positive = false;
let negative = false;
let warning = false;
if (status === 'pending' || status === 'new') {
positive = status === 'pending';
buttonCell = (
);
} else if (status === 'failed' || status === 'deferred') {
negative = status === 'failed';
warning = status === 'deferred';
buttonCell = (
);
}
return (
{textEllipsis(url, 50)}
{last_successful_download ? secondsToDate(last_successful_download) : null}
{status}
{buttonCell}
);
}
}
class Downloads extends React.Component {
constructor(props) {
super(props);
this.state = {
once_downloads: null,
recurring_downloads: null,
pending_downloads: null,
stopOpen: false,
disabled: false,
};
}
async componentDidMount() {
await this.fetchDownloads();
this.intervalId = setInterval(this.fetchDownloads, 1000 * 10);
}
componentWillUnmount() {
clearInterval(this.intervalId);
}
closeStop = () => {
this.setState({stopOpen: false});
}
openStop = () => {
this.setState({stopOpen: true});
}
fetchDownloads = async () => {
let data = await getDownloads();
this.setState({
once_downloads: data.once_downloads,
recurring_downloads: data.recurring_downloads,
pending_downloads: data.pending_downloads,
});
}
render() {
let tablePlaceholder = (
);
let stoppableHeader = (
URL
Completed At
Status
Control
);
let nonStoppableHeader = (
URL
Download Frequency
Last Successful Download
Next Download
Status
);
let onceTable = tablePlaceholder;
if (this.state.once_downloads !== null && this.state.once_downloads.length === 0) {
onceTable = No downloads are scheduled to be downloaded.
} else if (this.state.once_downloads !== null) {
onceTable = (
<>
{stoppableHeader}
{this.state.once_downloads.map((i) =>
)}
>);
}
let recurringTable = tablePlaceholder;
if (this.state.recurring_downloads !== null && this.state.recurring_downloads.length === 0) {
recurringTable = No downloads are scheduled to be downloaded.
} else if (this.state.recurring_downloads !== null) {
recurringTable = (
{nonStoppableHeader}
{this.state.recurring_downloads.map((i) => )}
);
}
return (
{onceTable}
{recurringTable}
);
}
}
export default function Admin(props) {
const links = [
{text: 'Downloads', to: '/admin', exact: true, key: 'admin'},
{text: 'Settings', to: '/admin/settings', key: 'settings'},
{text: 'WROL Mode', to: '/admin/wrol', key: 'wrol'},
];
return (
)
}