import React, {useContext} from "react";
import {clearCompletedDownloads, clearFailedDownloads, deleteDownload, killDownload, postDownload} from "../../api";
import {Link} from "react-router-dom";
import {
DisableDownloadsToggle,
isoDatetimeToElapsedPopup,
secondsToFrequency,
textEllipsis,
useTitle,
WROLModeMessage
} from "../Common";
import {
Button as SemanticButton,
Confirm,
Loader,
Modal,
PlaceholderLine,
TableBody,
TableCell,
TableHeader,
TableHeaderCell,
TableRow
} from "semantic-ui-react";
import {Button, Header, Placeholder, Segment, Table} from "../Theme";
import {ThemeContext} from "../../contexts/contexts";
import {useDownloads} from "../../hooks/customHooks";
import _ from "lodash";
function ClearCompleteDownloads({callback}) {
const [disabled, setDisabled] = React.useState(false);
async function localClearDownloads() {
setDisabled(true);
try {
await clearCompletedDownloads();
} finally {
setDisabled(false);
if (callback) {
callback()
}
}
}
return <>
Clear Completed
>
}
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 <>
setOpen(true)}
disabled={disabled}
color='red'
>Clear Failed
setOpen(false)}
onConfirm={localDeleteFailed}
/>
>
}
class DownloadRow extends React.Component {
constructor(props) {
super(props);
this.state = {
errorModalOpen: false,
deleteOpen: false,
}
}
openDelete = (e) => {
if (e) {
e.preventDefault();
}
this.setState({deleteOpen: true});
}
closeDelete = (e) => {
if (e) {
e.preventDefault();
}
this.setState({deleteOpen: false});
}
handleDelete = async (e) => {
if (e) {
e.preventDefault();
}
const {id} = this.props;
try {
await deleteDownload(id);
} finally {
this.closeDelete();
if (this.props.fetchDownloads) {
this.props.fetchDownloads();
}
}
}
render() {
let {url, frequency, last_successful_download, status, location, next_download, error} = this.props;
const {errorModalOpen, deleteOpen} = 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 = <>
>;
return
{link(textEllipsis(url, 50))}
{secondsToFrequency(frequency)}
{last_successful_download ? isoDatetimeToElapsedPopup(last_successful_download) : null}
{status === 'pending' ? : null}
{next_download ? isoDatetimeToElapsedPopup(next_download) : null}
{error && errorModal}
{deleteButton}
}
}
class StoppableRow extends React.Component {
constructor(props) {
super(props);
this.state = {
stopOpen: false,
startOpen: false,
errorModalOpen: 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, location, error} = this.props;
let {stopOpen, startOpen, errorModalOpen} = this.state;
const link = location ?
(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 = (
Stop
Retry
);
} else if (status === 'complete' && location) {
buttonCell = (
{link('View')}
);
}
if (error) {
completedAtCell = (
this.setState({errorModalOpen: false})}
onOpen={() => this.setState({errorModalOpen: true})}
open={errorModalOpen}
trigger={ }
>
Download Error
{error}
this.setState({errorModalOpen: false})}>Close
)
}
return (
{link(textEllipsis(url, 50))}
{completedAtCell}
{status === 'pending' ? : null}
{buttonCell}
);
}
}
export function Downloads() {
useTitle('Downloads');
const {t} = useContext(ThemeContext);
const {onceDownloads, recurringDownloads, fetchDownloads} = useDownloads();
const tablePlaceholder =
;
const stoppableHeader = (
URL
Completed At
Control
);
const nonStoppableHeader = (
URL
Download Frequency
Completed At
Next
Control
);
let onceTable = tablePlaceholder;
if (_.isEmpty(onceDownloads)) {
onceTable = No downloads are scheduled.
} else if (onceDownloads) {
onceTable = (
<>
{stoppableHeader}
{onceDownloads.map((i) =>
)}
>);
}
let recurringTable = tablePlaceholder;
if (_.isEmpty(recurringDownloads)) {
recurringTable = No recurring downloads are scheduled.
} else if (recurringDownloads) {
recurringTable =
{nonStoppableHeader}
{recurringDownloads.map((i) => )}
;
}
return <>
{onceTable}
{recurringTable}
>
}