Handle no API endpoint and API failures.

This commit is contained in:
n8detar 2022-04-20 11:33:58 -07:00
parent db148dcdf3
commit d7c45ba966
2 changed files with 24 additions and 10 deletions

View File

@ -3,8 +3,8 @@ import { getTAUrl } from "./constants";
const TA_BASE_URL = getTAUrl();
export const getDownloads = async (token: string, filter: boolean): Promise<Download> => {
const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter ? 'ignore' : 'pending'}`, {
export const getDownloads = async (token: string, filter: boolean, page: number): Promise<Download> => {
const response = await fetch(`${TA_BASE_URL.server}/api/download/?filter=${filter ? 'ignore' : 'pending'}&page=${page}`, {
headers: {
Accept: "application/json",
"Content-Type": "application/json",
@ -12,10 +12,19 @@ export const getDownloads = async (token: string, filter: boolean): Promise<Down
mode: "no-cors",
},
});
if (!response.ok) {
throw new Error("Error getting download queue information");
}
if (response.ok) {
return response.json();
} else {
var error: Download = {
data: null,
config: null,
paginate: null,
message: response.statusText + " (" + response.status + ")",
}
// error = response.statusText + " (" + response.status + ");
// throw new Error(response.statusText + " (" + response.status + ")");
return error;
}
};
export const sendDownloads = async (token: string, input: string): Promise<Download> => {

View File

@ -40,7 +40,7 @@ export const getServerSideProps: GetServerSideProps = async (context) => {
}
await queryClient.prefetchQuery(["downloads", session.ta_token.token, false], () =>
getDownloads(session.ta_token.token, false)
getDownloads(session.ta_token.token, false, 1)
);
return {
@ -64,7 +64,7 @@ const Download: NextPage = () => {
refetch,
} = useQuery(
["downloads", session.ta_token.token, ignoredStatus],
() => getDownloads(session.ta_token.token, ignoredStatus),
() => getDownloads(session.ta_token.token, ignoredStatus, page),
{
enabled: !!session?.ta_token?.token,
refetchInterval: 1500,
@ -116,6 +116,12 @@ const Download: NextPage = () => {
<h1>Downloads</h1>
</div>
<div id="notifications">
{downloads?.message &&
<div className="error notification">
<h3>{downloads?.message}</h3>
<p></p>
</div>
}
{errorMessage &&
<div className="error notification">
<h3>Failed to extract links.</h3>
@ -268,9 +274,9 @@ const Download: NextPage = () => {
<button onClick={() => sendDeleteAllQueuedIgnored(session.ta_token.token, "pending")} title="Delete all pending videos from the queue">Delete all queued</button>
</div>
}
<h3>Total videos: {downloads?.data?.length} {!downloads?.data?.length && !ignoredStatus && <p>No videos queued for download. Press rescan subscriptions to check if there are any new videos.</p>}</h3>
<h3>Total videos: {downloads?.data?.length ? downloads?.data?.length : 'N/A'} {!downloads?.data?.length && !downloads?.message && !ignoredStatus && <p>No videos queued for download. Press rescan subscriptions to check if there are any new videos.</p>}</h3>
<div className={`dl-list ${viewStyle}`}>
{!isLoading && !error && downloads?.data &&
{!isLoading && !error && !downloads?.message &&
downloads?.data?.map((video) => {
return (
<div key={video?.youtube_id} className={`dl-item ${viewStyle}`}>
@ -350,7 +356,6 @@ const Download: NextPage = () => {
{/* </div> */}
{/* {% endfor %} */}
{/* {% endif %} */}
</div>
</div>
<div className="boxed-content">