diff --git a/src/components/modules/downloads/DownloadsModule.tsx b/src/components/modules/downloads/DownloadsModule.tsx index 042ad4b74..03eefef29 100644 --- a/src/components/modules/downloads/DownloadsModule.tsx +++ b/src/components/modules/downloads/DownloadsModule.tsx @@ -59,7 +59,7 @@ export default function DownloadComponent() { setIsLoading(false); }); }, 5000); - }, [config.services]); + }, []); if (downloadServices.length === 0) { return ( diff --git a/src/pages/api/modules/downloads.ts b/src/pages/api/modules/downloads.ts index 87de10437..a38dd55f8 100644 --- a/src/pages/api/modules/downloads.ts +++ b/src/pages/api/modules/downloads.ts @@ -7,14 +7,12 @@ import { Config } from '../../../tools/types'; async function Post(req: NextApiRequest, res: NextApiResponse) { // Get the type of service from the request url - const torrents: NormalizedTorrent[] = []; const { config }: { config: Config } = req.body; - const qBittorrentServices = config.services - .filter((service) => service.type === 'qBittorrent'); - + const qBittorrentServices = config.services.filter((service) => service.type === 'qBittorrent'); const delugeServices = config.services.filter((service) => service.type === 'Deluge'); - const transmissionServices = config.services - .filter((service) => service.type === 'Transmission'); + const transmissionServices = config.services.filter((service) => service.type === 'Transmission'); + + const torrents: NormalizedTorrent[] = []; if (!qBittorrentServices && !delugeServices && !transmissionServices) { return res.status(500).json({ @@ -22,44 +20,39 @@ async function Post(req: NextApiRequest, res: NextApiResponse) { message: 'Missing services', }); } - if (qBittorrentServices) { - for (const service of qBittorrentServices) { - torrents.push( - ...( - await new QBittorrent({ - baseUrl: service.url, - username: service.username, - password: service.password, - }).getAllData() - ).torrents - ); - } - } - if (delugeServices) { - for (const service of delugeServices) { - torrents.push( - ...( - await new Deluge({ - baseUrl: service.url, - password: 'password' in service ? service.password : '', - }).getAllData() - ).torrents - ) - } - } - if (transmissionServices) { - for (const service of transmissionServices) { - torrents.push( - ...( - await new Transmission({ - baseUrl: service.url, - username: 'username' in service ? service.username : '', - password: 'password' in service ? service.password : '', - }).getAllData() - ).torrents - ); - } - } + await Promise.all( + qBittorrentServices.map((service) => + new QBittorrent({ + baseUrl: service.url, + username: service.username, + password: service.password, + }) + .getAllData() + .then((e) => torrents.push(...e.torrents)) + ) + ); + await Promise.all( + delugeServices.map((service) => + new Deluge({ + baseUrl: service.url, + password: 'password' in service ? service.password : '', + }) + .getAllData() + .then((e) => torrents.push(...e.torrents)) + ) + ); + // Map transmissionServices + await Promise.all( + transmissionServices.map((service) => + new Transmission({ + baseUrl: service.url, + username: 'username' in service ? service.username : '', + password: 'password' in service ? service.password : '', + }) + .getAllData() + .then((e) => torrents.push(...e.torrents)) + ) + ); res.status(200).json(torrents); }