From 5aaf0854fea41c4a18e66f53140946fa3e70694c Mon Sep 17 00:00:00 2001 From: schroda <50052685+schroda@users.noreply.github.com> Date: Mon, 15 May 2023 18:15:28 +0200 Subject: [PATCH] Fix/download queue staying stopped when removing download (#299) * [Cleanup] Use async/await * Show download queue removal error toast * Optionally restart download queue after removing download When removing a download while the download queue was running, the queue was never started again. --- src/i18n/locale/en.json | 5 +++++ src/screens/DownloadQueue.tsx | 27 +++++++++++++++++++++------ 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/src/i18n/locale/en.json b/src/i18n/locale/en.json index 5f7f14ad..13762ef6 100644 --- a/src/i18n/locale/en.json +++ b/src/i18n/locale/en.json @@ -142,6 +142,11 @@ "label": { "no_downloads": "No downloads" }, + "error": { + "label": { + "failed_to_remove": "Could not remove download from queue." + } + }, "title": "Download Queue" }, "state": { diff --git a/src/screens/DownloadQueue.tsx b/src/screens/DownloadQueue.tsx index ce52853e..dd1d8442 100644 --- a/src/screens/DownloadQueue.tsx +++ b/src/screens/DownloadQueue.tsx @@ -26,6 +26,7 @@ import { Link } from 'react-router-dom'; import { BACK } from 'util/useBackTo'; import { useTranslation } from 'react-i18next'; import { IChapter, IQueue } from 'typings'; +import makeToast from 'components/util/Toast'; const initialQueue = { status: 'Stopped', @@ -59,17 +60,31 @@ const DownloadQueue: React.FC = () => { return ; } - const handleDelete = (chapter: IChapter) => { - // required to stop before deleting otherwise the download kept going. Server issue? - client.get('/api/v1/downloads/stop').then(() => - Promise.all([ + const handleDelete = async (chapter: IChapter) => { + const isRunning = status === 'Started'; + + try { + if (isRunning) { + // required to stop before deleting otherwise the download kept going. Server issue? + await client.get('/api/v1/downloads/stop'); + } + + await Promise.all([ // remove from download queue client.delete(`/api/v1/download/${chapter.mangaId}/chapter/${chapter.index}`), // delete partial download, should be handle server side? // bug: The folder and the last image downloaded are not deleted client.delete(`/api/v1/manga/${chapter.mangaId}/chapter/${chapter.index}`), - ]), - ); + ]); + } catch (error) { + makeToast(t('download.queue.error.label.failed_to_remove'), 'error'); + } + + if (!isRunning) { + return; + } + + client.get('/api/v1/downloads/start').catch(() => {}); }; return (