Defined request interface & updated expected request status response
This commit is contained in:
15
src/api.ts
15
src/api.ts
@@ -1,4 +1,8 @@
|
|||||||
import { IList, IMediaCredits, IPersonCredits } from "./interfaces/IList";
|
import { IList, IMediaCredits, IPersonCredits } from "./interfaces/IList";
|
||||||
|
import type {
|
||||||
|
IRequestStatusResponse,
|
||||||
|
IRequestSubmitResponse
|
||||||
|
} from "./interfaces/IRequestResponse";
|
||||||
|
|
||||||
const { ELASTIC, ELASTIC_INDEX } = process.env;
|
const { ELASTIC, ELASTIC_INDEX } = process.env;
|
||||||
const API_HOSTNAME = window.location.origin;
|
const API_HOSTNAME = window.location.origin;
|
||||||
@@ -259,7 +263,7 @@ const addMagnet = (magnet: string, name: string, tmdbId: number | null) => {
|
|||||||
* @param {string} type Movie or show type
|
* @param {string} type Movie or show type
|
||||||
* @returns {object} Success/Failure response
|
* @returns {object} Success/Failure response
|
||||||
*/
|
*/
|
||||||
const request = (id, type) => {
|
const request = (id, type): Promise<IRequestSubmitResponse> => {
|
||||||
const url = new URL("/api/v2/request", API_HOSTNAME);
|
const url = new URL("/api/v2/request", API_HOSTNAME);
|
||||||
|
|
||||||
const options = {
|
const options = {
|
||||||
@@ -282,18 +286,13 @@ const request = (id, type) => {
|
|||||||
* @param {string} type
|
* @param {string} type
|
||||||
* @returns {object} Success/Failure response
|
* @returns {object} Success/Failure response
|
||||||
*/
|
*/
|
||||||
const getRequestStatus = (id, type = undefined) => {
|
const getRequestStatus = (id, type = null): Promise<IRequestStatusResponse> => {
|
||||||
const url = new URL("/api/v2/request", API_HOSTNAME);
|
const url = new URL("/api/v2/request", API_HOSTNAME);
|
||||||
url.pathname = `${url.pathname}/${id.toString()}`;
|
url.pathname = `${url.pathname}/${id.toString()}`;
|
||||||
url.searchParams.append("type", type);
|
url.searchParams.append("type", type);
|
||||||
|
|
||||||
return fetch(url.href)
|
return fetch(url.href)
|
||||||
.then(resp => {
|
.then(resp => resp.json())
|
||||||
const { status } = resp;
|
|
||||||
if (status === 200) return true;
|
|
||||||
|
|
||||||
return false;
|
|
||||||
})
|
|
||||||
.catch(err => Promise.reject(err));
|
.catch(err => Promise.reject(err));
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
@@ -190,6 +190,10 @@
|
|||||||
IMediaCredits,
|
IMediaCredits,
|
||||||
ICast
|
ICast
|
||||||
} from "../../interfaces/IList";
|
} from "../../interfaces/IList";
|
||||||
|
import type {
|
||||||
|
IRequestStatusResponse,
|
||||||
|
IRequestSubmitResponse
|
||||||
|
} from "../../interfaces/IRequestResponse";
|
||||||
import { MediaTypes } from "../../interfaces/IList";
|
import { MediaTypes } from "../../interfaces/IList";
|
||||||
|
|
||||||
import { humanMinutes } from "../../utils";
|
import { humanMinutes } from "../../utils";
|
||||||
@@ -241,8 +245,15 @@
|
|||||||
function setCast(_cast: ICast[]) {
|
function setCast(_cast: ICast[]) {
|
||||||
cast.value = _cast;
|
cast.value = _cast;
|
||||||
}
|
}
|
||||||
function setRequested(status: boolean) {
|
function setRequested(
|
||||||
requested.value = status;
|
requestResponse: IRequestStatusResponse | IRequestSubmitResponse
|
||||||
|
) {
|
||||||
|
if (requestResponse?.success) {
|
||||||
|
requested.value = requestResponse?.success;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
requested.value = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
function setBackdrop(): void {
|
function setBackdrop(): void {
|
||||||
@@ -301,13 +312,13 @@
|
|||||||
.then(() => getCredits(props.type))
|
.then(() => getCredits(props.type))
|
||||||
.then(credits => setCast(credits?.cast || []))
|
.then(credits => setCast(credits?.cast || []))
|
||||||
.then(() => getRequestStatus(props.id, props.type))
|
.then(() => getRequestStatus(props.id, props.type))
|
||||||
.then(requestStatus => setRequested(requestStatus || false))
|
.then(requestResponse => setRequested(requestResponse))
|
||||||
.then(setBackdrop);
|
.then(setBackdrop);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sendRequest() {
|
function sendRequest() {
|
||||||
request(props.id, props.type).then(resp =>
|
request(props.id, props.type).then(requestResponse =>
|
||||||
setRequested(resp?.success || false)
|
setRequested(requestResponse)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
19
src/interfaces/IRequestResponse.ts
Normal file
19
src/interfaces/IRequestResponse.ts
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
export interface Result {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
year: number;
|
||||||
|
type: string;
|
||||||
|
status: string;
|
||||||
|
requested_date: Date;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IRequestStatusResponse {
|
||||||
|
success: boolean;
|
||||||
|
message?: string;
|
||||||
|
result?: Result;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IRequestSubmitResponse {
|
||||||
|
success: boolean;
|
||||||
|
message: string;
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user