Creating shutdown and restart buttons.

This commit is contained in:
Roland 2023-08-17 20:07:02 -06:00
parent 9dbd43cfdd
commit 2537017039
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
11 changed files with 198 additions and 14 deletions

View file

@ -6,7 +6,7 @@ import {Route, Routes} from "react-router-dom";
import {ThemeContext} from "../../contexts/contexts";
import {Header, Loader, Segment} from "../Theme";
import {DownloadsPage} from "./Downloads";
import {Settings} from "./Settings";
import {SettingsPage} from "./Settings";
import {Status} from "./Status";
class WROLMode extends React.Component {
@ -78,7 +78,7 @@ export default function AdminRoute() {
<TabLinks links={links}/>
<Routes>
<Route path='/' exact element={<DownloadsPage/>}/>
<Route path='settings' exact element={<Settings/>}/>
<Route path='settings' exact element={<SettingsPage/>}/>
<Route path='status' exact element={<Status/>}/>
<Route path='wrol' exact element={<WROLMode/>}/>
</Routes>

View file

@ -1,12 +1,102 @@
import React from "react";
import {getSettings, saveSettings} from "../../api";
import {getSettings, postRestart, postShutdown, saveSettings} from "../../api";
import {Button, Form, FormGroup, FormInput, Header, Loader, Modal, ModalContent, ModalHeader, Segment} from "../Theme";
import {Container, Dimmer, Icon} from "semantic-ui-react";
import {ThemeContext} from "../../contexts/contexts";
import {APIButton, HelpPopup, HotspotToggle, ThrottleToggle, Toggle, WROLModeMessage} from "../Common";
import QRCode from "react-qr-code";
import {toast} from "react-semantic-toasts-2";
import {useDockerized} from "../../hooks/customHooks";
export class Settings extends React.Component {
export function ShutdownButton() {
const dockerized = useDockerized();
const handleShutdown = async () => {
try {
const response = await postShutdown();
if (response && response['code'] === 'SHUTDOWN_FAILED') {
toast({
title: 'Shutdown Failed',
description: response['error'],
time: 5000,
type: "error",
})
} else if (response !== null) {
toast({
title: 'Shutdown Failed',
description: 'Unknown error when attempting shutdown',
time: 5000,
type: "error",
})
}
} catch (e) {
toast({
title: 'Shutdown Failed',
description: 'Failed to request WROLPi shutdown',
time: 5000,
type: "error",
})
throw e;
}
}
return <APIButton
size='huge'
color='red'
onClick={handleShutdown}
confirmContent='Are you sure you want to turn off your WROLPi?'
confirmButton='Shutdown'
disabled={dockerized}
>
Shutdown
</APIButton>
}
export function RestartButton() {
const dockerized = useDockerized();
const handleRestart = async () => {
try {
const response = await postRestart();
if (response && response['code'] === 'RESTART_FAILED') {
toast({
title: 'Restart Failed',
description: response['error'],
time: 5000,
type: "error",
})
} else if (response !== null) {
toast({
title: 'Restart Failed',
description: 'Unknown error when attempting restart',
time: 5000,
type: "error",
})
}
} catch (e) {
toast({
title: 'Restart Failed',
description: 'Failed to request WROLPi restart',
time: 5000,
type: "error",
})
throw e;
}
}
return <APIButton
size='huge'
color='yellow'
onClick={handleRestart}
confirmContent='Are you sure you want to restart your WROLPi?'
confirmButton='Restart'
disabled={dockerized}
>
Restart
</APIButton>
}
export class SettingsPage extends React.Component {
constructor(props) {
super(props);
@ -21,7 +111,6 @@ export class Settings extends React.Component {
download_on_startup: null,
download_timeout: null,
hotspot_device: null,
hotspot_on_startup: null,
hotspot_password: null,
hotspot_ssid: null,
hotspot_status: null,
@ -43,7 +132,6 @@ export class Settings extends React.Component {
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,
@ -65,7 +153,6 @@ export class Settings extends React.Component {
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,
ignore_outdated_zims: this.state.ignore_outdated_zims,
@ -113,7 +200,6 @@ export class Settings extends React.Component {
download_on_startup,
download_timeout,
hotspot_device,
hotspot_on_startup,
hotspot_password,
hotspot_ssid,
ignore_outdated_zims,
@ -235,6 +321,11 @@ export class Settings extends React.Component {
</Form>
</Segment>
<Segment>
<RestartButton/>
<ShutdownButton/>
</Segment>
</Container>}
</ThemeContext.Consumer>
}