From 883f5dc801f5b0f13f6c845c962282a6ecefe191 Mon Sep 17 00:00:00 2001 From: n8detar Date: Sat, 23 Apr 2022 09:40:51 -0700 Subject: [PATCH] Added rescan subs and start download functionality --- src/lib/getDownloads.ts | 22 +++++++++++++++++++++- src/pages/download.tsx | 19 +++++++++++++++---- src/types/download.ts | 5 +++++ 3 files changed, 41 insertions(+), 5 deletions(-) diff --git a/src/lib/getDownloads.ts b/src/lib/getDownloads.ts index 02f9ead..3483416 100755 --- a/src/lib/getDownloads.ts +++ b/src/lib/getDownloads.ts @@ -1,4 +1,4 @@ -import { Download } from "../types/download"; +import { Download, Task } from "../types/download"; import { getTAUrl } from "./constants"; const TA_BASE_URL = getTAUrl(); @@ -106,4 +106,24 @@ export const sendMoveVideoQueuedIgnored = async (token: string, videoId: string, throw new Error("Error moving video to" + status + "."); } return response.json(); +}; + +export const sendTasks = async (token: string, task: string): Promise => { + var data = { + "run": task + }; + const response = await fetch(`${TA_BASE_URL.server}/api/task/`, { + body: JSON.stringify(data), + headers: { + Accept: "application/json", + "Content-Type": "application/json", + Authorization: `Token ${token}`, + mode: "no-cors", + }, + method: "POST" + }); + if (!response.ok) { + throw new Error("Error running task: " + task + "."); + } + return response.json(); }; \ No newline at end of file diff --git a/src/pages/download.tsx b/src/pages/download.tsx index 3682de5..eff38e3 100755 --- a/src/pages/download.tsx +++ b/src/pages/download.tsx @@ -5,8 +5,7 @@ import { dehydrate, QueryClient, useQuery } from "react-query"; import { CustomHead } from "../components/CustomHead"; import { Layout } from "../components/Layout"; import NextImage from "next/image"; -import { getDownloads, sendDeleteAllQueuedIgnored, sendDeleteVideoQueuedIgnored, sendMoveVideoQueuedIgnored } from "../lib/getDownloads"; -import { sendDownloads } from "../lib/getDownloads"; +import { getDownloads, sendDownloads, sendDeleteAllQueuedIgnored, sendDeleteVideoQueuedIgnored, sendMoveVideoQueuedIgnored, sendTasks } from "../lib/getDownloads"; import RescanIcon from "../images/icon-rescan.svg"; import DownloadIcon from "../images/icon-download.svg"; import AddIcon from "../images/icon-add.svg"; @@ -124,6 +123,18 @@ const Download: NextPage = () => { .catch(error => handleSetErrorMessage(error.message)); } + const handleSendTask = (session: string, task: string) => { + sendTasks(session, task).then((response) => { + if (response.success) { + handleSetErrorMessage(null); + } else { + handleSetErrorMessage("Error running task: " + response.task + "."); + } + + }) + .catch(error => handleSetErrorMessage(error.message)); + } + return ( <> @@ -202,7 +213,7 @@ const Download: NextPage = () => { alt="rescan-icon" title="Rescan subscriptions" // className="rotate-img" // Set when rescanning - onClick={() => console.log("rescanPending()")} + onClick={() => handleSendTask(session.ta_token.token, "rescan_pending")} /> {/* rescan-icon */}

Rescan subscriptions

@@ -215,7 +226,7 @@ const Download: NextPage = () => { alt="download-icon" title="Start download" // className="bounce-img" // Set when video is downloading - onClick={() => console.log("dlPending()")} + onClick={() => handleSendTask(session.ta_token.token, "download_pending")} /> {/* download-icon */}

Start download

diff --git a/src/types/download.ts b/src/types/download.ts index c14eadb..e0fb714 100755 --- a/src/types/download.ts +++ b/src/types/download.ts @@ -5,6 +5,11 @@ export interface Download { message: string; } +export interface Task { + success: boolean; + task: string; +} + export interface Paginate { page_size: number; page_from: number;