diff --git a/app/src/api.js b/app/src/api.js index 44e4091c..a0b98618 100644 --- a/app/src/api.js +++ b/app/src/api.js @@ -1284,6 +1284,33 @@ export async function retryOnceDownloads() { return response } +export async function batchDeleteDownloads(downloadIds) { + const response = await apiPost(`${API_URI}/download/batch/delete`, {download_ids: downloadIds}); + if (!response.ok) { + const message = await getErrorMessage(response, 'Could not delete downloads! See server logs.'); + toast({type: 'error', title: 'Downloads Error', description: message, time: 5000}); + } + return response +} + +export async function batchRetryDownloads(downloadIds) { + const response = await apiPost(`${API_URI}/download/batch/retry`, {download_ids: downloadIds}); + if (!response.ok) { + const message = await getErrorMessage(response, 'Could not retry downloads! See server logs.'); + toast({type: 'error', title: 'Downloads Error', description: message, time: 5000}); + } + return response +} + +export async function batchClearDownloads(downloadIds) { + const response = await apiPost(`${API_URI}/download/batch/clear`, {download_ids: downloadIds}); + if (!response.ok) { + const message = await getErrorMessage(response, 'Could not clear downloads! See server logs.'); + toast({type: 'error', title: 'Downloads Error', description: message, time: 5000}); + } + return response +} + export async function getStatistics() { const response = await apiGet(`${API_URI}/statistics`); if (response.ok) { diff --git a/app/src/components/admin/Downloads.js b/app/src/components/admin/Downloads.js index fa87f23e..43c6baa2 100644 --- a/app/src/components/admin/Downloads.js +++ b/app/src/components/admin/Downloads.js @@ -1,5 +1,8 @@ import React from "react"; import { + batchClearDownloads, + batchDeleteDownloads, + batchRetryDownloads, clearCompletedDownloads, deleteDownload, deleteOnceDownloads, @@ -20,6 +23,7 @@ import { import { Button as SButton, ButtonGroup, + Checkbox, Label, Loader, PlaceholderLine, @@ -41,11 +45,19 @@ import { EditZimDownloadForm } from "../Download"; import {Downloaders} from "../Vars"; +import {SortableTable} from "../SortableTable"; + +function ClearDownloadsButton({callback, selectedIds, clearSelection}) { + const hasSelection = selectedIds && selectedIds.length > 0; -function ClearDownloadsButton({callback}) { async function localClearDownloads() { try { - await clearCompletedDownloads(); + if (hasSelection) { + await batchClearDownloads(selectedIds); + if (clearSelection) clearSelection(); + } else { + await clearCompletedDownloads(); + } } finally { if (callback) { callback() @@ -53,19 +65,28 @@ function ClearDownloadsButton({callback}) { } } + const label = hasSelection ? `Clear (${selectedIds.length})` : 'Clear'; + return <> Clear + >{label} } -function RetryDownloadsButton({callback}) { +function RetryDownloadsButton({callback, selectedIds, clearSelection}) { + const hasSelection = selectedIds && selectedIds.length > 0; + async function localRetryOnce() { try { - await retryOnceDownloads(); + if (hasSelection) { + await batchRetryDownloads(selectedIds); + if (clearSelection) clearSelection(); + } else { + await retryOnceDownloads(); + } } finally { if (callback) { callback() @@ -73,17 +94,26 @@ function RetryDownloadsButton({callback}) { } } + const label = hasSelection ? `Retry (${selectedIds.length})` : 'Retry'; + return Retry + >{label} } -function DeleteOnceDownloadsButton({callback}) { +function DeleteOnceDownloadsButton({callback, selectedIds, clearSelection}) { + const hasSelection = selectedIds && selectedIds.length > 0; + async function localDeleteOnce() { try { - await deleteOnceDownloads(); + if (hasSelection) { + await batchDeleteDownloads(selectedIds); + if (clearSelection) clearSelection(); + } else { + await deleteOnceDownloads(); + } } finally { if (callback) { callback() @@ -91,13 +121,18 @@ function DeleteOnceDownloadsButton({callback}) { } } + const label = hasSelection ? `Delete (${selectedIds.length})` : 'Delete'; + const confirmContent = hasSelection + ? `Are you sure you want to delete ${selectedIds.length} download(s)?` + : 'Are you sure you want to delete all downloads? Some may be retried!'; + return Delete + >{label} } @@ -246,213 +281,267 @@ function RecurringDownloadRow({download, fetchDownloads, onDelete}) { } -class OnceDownloadRow extends React.Component { - constructor(props) { - super(props); - this.state = { - editModalOpen: false - }; - } +function OnceDownloadRow({download, fetchDownloads, isSelected, onSelect}) { + const [editModalOpen, setEditModalOpen] = React.useState(false); - handleDelete = async () => { + const {url, last_successful_download, status, location, error, downloader, settings, id} = download; + + const handleDelete = async () => { try { - await deleteDownload(this.props.id); + await deleteDownload(id); } finally { - await this.props.fetchDownloads(); + await fetchDownloads(); } }; - handleStop = async () => { + const handleStop = async () => { try { - await killDownload(this.props.id); + await killDownload(id); } finally { - await this.props.fetchDownloads(); + await fetchDownloads(); } }; - handleRestart = async () => { + const handleRestart = async () => { try { - await restartDownload(this.props.id); + await restartDownload(id); } finally { - await this.props.fetchDownloads(); + await fetchDownloads(); } }; - handleEditOpen = () => { - this.setState({editModalOpen: true}); + const handleEditOpen = () => setEditModalOpen(true); + const handleEditClose = () => setEditModalOpen(false); + + const handleEditSuccess = async () => { + await fetchDownloads(); + handleEditClose(); }; - handleEditClose = () => { - this.setState({editModalOpen: false}); - }; + // Open downloads (/download), or external links in an anchor. + const link = location && !location.startsWith('/download') ? + (text) => {text} : + (text) => {text}; - handleEditSuccess = async () => { - await this.props.fetchDownloads(); - this.handleEditClose(); - }; - - render() { - let {url, last_successful_download, status, location, error, downloader, settings, id} = 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 = ( - + 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 - - ); - } else if (status === 'failed' || status === 'deferred') { - buttonCell = ( - - - - + {(downloader === Downloaders.Video || downloader === Downloaders.Archive) && status !== 'pending' && ( +