import React from "react";
import {clearCompletedDownloads, clearFailedDownloads, deleteDownload, killDownload, restartDownload} from "../../api";
import {Link} from "react-router-dom";
import {
APIButton,
DisableDownloadsToggle,
ErrorMessage,
isoDatetimeToElapsedPopup,
secondsToFrequency,
textEllipsis,
useTitle,
WROLModeMessage
} from "../Common";
import {
Button as SButton,
Loader,
PlaceholderLine,
TableBody,
TableCell,
TableFooter,
TableHeader,
TableHeaderCell,
TableRow
} from "semantic-ui-react";
import {Button, Header, Modal, ModalActions, ModalContent, ModalHeader, Placeholder, Segment, Table} from "../Theme";
import {useDownloads} from "../../hooks/customHooks";
import {toast} from "react-semantic-toasts-2";
function ClearCompleteDownloads({callback}) {
async function localClearDownloads() {
try {
await clearCompletedDownloads();
} finally {
if (callback) {
callback()
}
}
}
return <>
Clear Completed
>
}
function ClearFailedDownloads({callback}) {
async function localDeleteFailed() {
try {
await clearFailedDownloads();
} finally {
if (callback) {
callback()
}
}
}
return
Clear Failed
}
class RecurringDownloadRow extends React.Component {
constructor(props) {
super(props);
this.state = {
errorModalOpen: false,
}
}
handleDelete = async () => {
const {id} = this.props;
try {
await deleteDownload(id);
} finally {
if (this.props.fetchDownloads) {
this.props.fetchDownloads();
}
}
}
handleRestart = async () => {
const {id} = this.props;
try {
const response = await restartDownload(id);
if (response.status !== 204) {
throw Error('Unable to restart download');
}
} catch (e) {
toast({
type: 'error',
title: 'Error',
description: 'Unable to restart download',
time: 5000,
})
throw e;
} finally {
if (this.props.fetchDownloads) {
this.props.fetchDownloads();
}
}
}
render() {
let {url, frequency, last_successful_download, status, location, next_download, error} = this.props;
const {errorModalOpen} = this.state;
const link = location ?
(text) => {text} :
(text) => {text};
const errorModal = this.setState({errorModalOpen: false})}
onOpen={() => this.setState({errorModalOpen: true})}
open={errorModalOpen}
trigger={}
>
Download Error
{error}
this.setState({errorModalOpen: false})}>Close
;
const deleteButton = <>
>;
const restartButton = <>
>;
// Show "now" if we have passed the next_download.
let next = 'now';
if (next_download) {
if (new Date() < new Date(next_download)) {
next = isoDatetimeToElapsedPopup(next_download);
}
}
return
{link(textEllipsis(url, 50))}
{secondsToFrequency(frequency)}
{last_successful_download ? isoDatetimeToElapsedPopup(last_successful_download) : null}
{status === 'pending' ? : null}
{next}
{error && errorModal}
{deleteButton}
{restartButton}
}
}
class OnceDownloadRow extends React.Component {
constructor(props) {
super(props);
}
handleDelete = async () => {
await deleteDownload(this.props.id);
await this.props.fetchDownloads();
};
handleStop = async () => {
await killDownload(this.props.id);
await this.props.fetchDownloads();
};
handleRestart = async () => {
try {
await restartDownload(this.props.id);
} catch (e) {
toast({
type: 'error',
title: 'Error',
description: 'Unable to restart download',
time: 5000,
})
throw e;
}
await this.props.fetchDownloads();
};
render() {
let {url, last_successful_download, status, location, error} = this.props;
// Open downloads (/download), or external links in an anchor.
const link = location && !location.startsWith('/download') ?
(text) => {text} :
(text) => {text};
let completedAtCell = last_successful_download ? isoDatetimeToElapsedPopup(last_successful_download) : null;
let buttonCell = ;
if (status === 'pending' || status === 'new') {
buttonCell = (
Stop
);
} else if (status === 'failed' || status === 'deferred') {
buttonCell = (
);
} else if (status === 'complete' && location) {
buttonCell = (
{link('View')}
);
}
if (error) {
completedAtCell = (
}
>
Download Error
{error}
)
}
return
{link(textEllipsis(url, 50))}
{completedAtCell}
{status === 'pending' ? : null}
{buttonCell}
}
}
export function OnceDownloadsTable({downloads, fetchDownloads}) {
if (downloads && downloads.length >= 1) {
return <>
URL
Completed At
Control
{downloads.map(i => )}
>
} else if (downloads) {
return No downloads are scheduled.
} else if (downloads === undefined) {
return Unable to fetch downloads
}
return
}
export function RecurringDownloadsTable({downloads, fetchDownloads}) {
if (downloads && downloads.length >= 1) {
return
URL
Download Frequency
Completed At
Next
Control
{downloads.map(i => )}
} else if (downloads) {
return No downloads are scheduled.
} else if (downloads === undefined) {
return Unable to fetch downloads
}
return
}
export function DownloadsPage() {
useTitle('Downloads');
const {onceDownloads, recurringDownloads, pendingOnceDownloads, fetchDownloads} = useDownloads();
const pendingOnceDownloadsSpan = pendingOnceDownloads > 0 ? ({pendingOnceDownloads}) : null;
return <>
Downloads {pendingOnceDownloadsSpan}
>
}