Replacing icon property when icon has been requested.

Creating <APIButton/> which handles success/failure/pending during API calls.
This commit is contained in:
Roland 2023-07-21 15:14:09 -06:00
parent 237b429cbf
commit 59c4ab2502
No known key found for this signature in database
GPG key ID: 9D6DBD3ACF11AB93
24 changed files with 483 additions and 334 deletions

View file

@ -5,7 +5,7 @@ import {PageContainer, TabLinks, Toggle} from "../Common";
import {Route, Routes} from "react-router-dom";
import {ThemeContext} from "../../contexts/contexts";
import {Header, Loader, Segment} from "../Theme";
import {Downloads} from "./Downloads";
import {DownloadsPage} from "./Downloads";
import {Settings} from "./Settings";
import {Status} from "./Status";
@ -65,7 +65,7 @@ class WROLMode extends React.Component {
}
}
export default function Admin() {
export default function AdminRoute() {
const links = [
{text: 'Downloads', to: '/admin', key: 'admin', end: true},
@ -77,7 +77,7 @@ export default function Admin() {
return <PageContainer>
<TabLinks links={links}/>
<Routes>
<Route path='/' exact element={<Downloads/>}/>
<Route path='/' exact element={<DownloadsPage/>}/>
<Route path='settings' exact element={<Settings/>}/>
<Route path='status' exact element={<Status/>}/>
<Route path='wrol' exact element={<WROLMode/>}/>

View file

@ -2,6 +2,7 @@ import React from "react";
import {clearCompletedDownloads, clearFailedDownloads, deleteDownload, killDownload, postDownload} from "../../api";
import {Link} from "react-router-dom";
import {
APIButton,
DisableDownloadsToggle,
isoDatetimeToElapsedPopup,
secondsToFrequency,
@ -40,45 +41,35 @@ function ClearCompleteDownloads({callback}) {
}
return <>
<Button
<APIButton
onClick={localClearDownloads}
disabled={disabled}
color='yellow'
>Clear Completed</Button>
obeyWROLMode={true}
>Clear Completed</APIButton>
</>
}
function ClearFailedDownloads({callback}) {
const [open, setOpen] = React.useState(false);
const [disabled, setDisabled] = React.useState(false);
async function localDeleteFailed() {
setDisabled(true);
try {
await clearFailedDownloads();
} finally {
setDisabled(false);
setOpen(false);
if (callback) {
callback()
}
}
}
return <>
<Button
onClick={() => setOpen(true)}
disabled={disabled}
color='red'
>Clear Failed</Button>
<Confirm
open={open}
content='Are you sure you want to delete failed downloads? They will not be retried.'
confirmButton='Delete'
onCancel={() => setOpen(false)}
onConfirm={localDeleteFailed}
/>
</>
return <APIButton
color='red'
onClick={localDeleteFailed}
confirmContent='Are you sure you want to delete failed downloads? They will not be retried.'
confirmButton='Delete'
obeyWROLMode={true}
>
Clear Failed
</APIButton>
}
class DownloadRow extends React.Component {
@ -104,10 +95,7 @@ class DownloadRow extends React.Component {
this.setState({deleteOpen: false});
}
handleDelete = async (e) => {
if (e) {
e.preventDefault();
}
handleDelete = async () => {
const {id} = this.props;
try {
await deleteDownload(id);
@ -144,13 +132,13 @@ class DownloadRow extends React.Component {
</Modal>;
const deleteButton = <>
<Button icon='trash' onClick={this.openDelete} color='red'/>
<Confirm
open={deleteOpen}
content='Are you sure you want to delete this download?'
<APIButton
color='red'
icon='trash'
confirmContent='Are you sure you want to delete this download?'
confirmButton='Delete'
onCancel={this.closeDelete}
onConfirm={this.handleDelete}
onClick={this.handleDelete}
obeyWROLMode={true}
/>
</>;
@ -184,62 +172,29 @@ class StoppableRow extends React.Component {
constructor(props) {
super(props);
this.state = {
stopOpen: false,
startOpen: false,
errorModalOpen: false,
deleteOpen: false,
};
}
openDelete = () => {
this.setState({deleteOpen: true});
}
closeDelete = () => {
this.setState({deleteOpen: false});
}
openStop = () => {
this.setState({stopOpen: true});
}
closeStop = () => {
this.setState({stopOpen: false});
}
openStart = () => {
this.setState({startOpen: true});
}
closeStart = () => {
this.setState({startOpen: false});
}
handleDelete = async (e) => {
e.preventDefault();
handleDelete = async () => {
await deleteDownload(this.props.id);
this.closeDelete();
await this.props.fetchDownloads();
};
handleStop = async (e) => {
e.preventDefault();
handleStop = async () => {
await killDownload(this.props.id);
this.closeStop();
await this.props.fetchDownloads();
};
handleStart = async (e) => {
e.preventDefault();
handleStart = async () => {
let downloader = this.props.downloader;
await postDownload(`${this.props.url}`, downloader);
this.closeStart();
await this.props.fetchDownloads();
};
render() {
let {url, last_successful_download, status, location, error} = this.props;
let {stopOpen, startOpen, errorModalOpen, deleteOpen} = this.state;
let {errorModalOpen} = this.state;
// Open downloads (/download), or external links in an anchor.
const link = location && !location.startsWith('/download') ?
@ -251,38 +206,33 @@ class StoppableRow extends React.Component {
if (status === 'pending' || status === 'new') {
buttonCell = (
<TableCell>
<Button
onClick={this.openStop}
<APIButton
color='red'
>Stop</Button>
<Confirm
open={stopOpen}
content='Are you sure you want to stop this download? It will not be retried.'
onClick={this.handleStop}
confirmContent='Are you sure you want to stop this download? It will not be retried.'
confirmButton='Stop'
onCancel={this.closeStop}
onConfirm={this.handleStop}
/>
obeyWROLMode={true}
>Stop</APIButton>
</TableCell>
);
} else if (status === 'failed' || status === 'deferred') {
buttonCell = (
<TableCell>
<Button icon='trash' onClick={this.openDelete} color='red'/>
<Confirm
open={deleteOpen}
content='Are you sure you want to delete this download?'
<APIButton
color='red'
icon='trash'
onClick={this.handleDelete}
confirmContent='Are you sure you want to delete this download?'
confirmButton='Delete'
onCancel={this.closeDelete}
onConfirm={this.handleDelete}
obeyWROLMode={true}
/>
<Button onClick={this.openStart} color='green'>Retry</Button>
<Confirm
open={startOpen}
content='Are you sure you want to restart this download?'
<APIButton
color='green'
confirmContent='Are you sure you want to restart this download?'
confirmButton='Start'
onCancel={this.closeStart}
onConfirm={this.handleStart}
/>
onClick={this.handleStart}
obeyWROLMode={true}
>Retry</APIButton>
</TableCell>
);
} else if (status === 'complete' && location) {
@ -296,18 +246,12 @@ class StoppableRow extends React.Component {
completedAtCell = (
<Modal
closeIcon
onClose={() => this.setState({errorModalOpen: false})}
onOpen={() => this.setState({errorModalOpen: true})}
open={errorModalOpen}
trigger={<Button icon='exclamation circle' color='red'/>}
>
<ModalHeader>Download Error</ModalHeader>
<ModalContent>
<pre style={{overflowX: 'scroll'}}>{error}</pre>
</ModalContent>
<ModalActions>
<SButton onClick={() => this.setState({errorModalOpen: false})}>Close</SButton>
</ModalActions>
</Modal>
)
}
@ -379,7 +323,7 @@ export function RecurringDownloadsTable({downloads, fetchDownloads}) {
}
}
export function Downloads() {
export function DownloadsPage() {
useTitle('Downloads');
const {onceDownloads, recurringDownloads, fetchDownloads} = useDownloads();

View file

@ -3,7 +3,7 @@ import {getSettings, 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 {HelpPopup, HotspotToggle, ThrottleToggle, Toggle, WROLModeMessage} from "../Common";
import {APIButton, HelpPopup, HotspotToggle, ThrottleToggle, Toggle, WROLModeMessage} from "../Common";
import QRCode from "react-qr-code";
export class Settings extends React.Component {
@ -57,7 +57,9 @@ export class Settings extends React.Component {
}
async handleSubmit(e) {
e.preventDefault();
if (e) {
e.preventDefault();
}
this.setState({disabled: true, pending: true});
let settings = {
download_on_startup: this.state.download_on_startup,
@ -228,9 +230,13 @@ export class Settings extends React.Component {
<br/>
<Button color='violet' type="submit" disabled={disabled} size='large'>
Save
</Button>
<APIButton
color='violet'
size='big'
onClick={this.handleSubmit}
obeyWROLMode={true}
disabled={disabled}
>Save</APIButton>
<Dimmer active={pending}>
<Loader active={pending} size='large'/>