mirror of
https://github.com/hubHarmony/servii-frontend.git
synced 2024-11-17 21:40:30 +00:00
[+] Added maintenance toast
This commit is contained in:
parent
f52930bce4
commit
7edcbe3653
@ -59,54 +59,67 @@ const nonToastableCalls: string[] = [
|
|||||||
serviiRequest.fetchPlayersStatus,
|
serviiRequest.fetchPlayersStatus,
|
||||||
];
|
];
|
||||||
|
|
||||||
|
function toast_status(status: number, message: string) {
|
||||||
|
let toastType: 'success' | 'error' | 'info' | 'warning';
|
||||||
|
|
||||||
|
if (status >= 200 && status < 300) {
|
||||||
|
toastType = 'success';
|
||||||
|
} else if (status >= 400 && status < 600) {
|
||||||
|
toastType = 'error';
|
||||||
|
} else {
|
||||||
|
toastType = 'info';
|
||||||
|
}
|
||||||
|
if (status == 666) {
|
||||||
|
toastType = 'warning';
|
||||||
|
}
|
||||||
|
|
||||||
|
toast[toastType](message, {
|
||||||
|
position: "top-right",
|
||||||
|
autoClose: 3500,
|
||||||
|
hideProgressBar: false,
|
||||||
|
closeOnClick: true,
|
||||||
|
pauseOnHover: true,
|
||||||
|
draggable: true,
|
||||||
|
progress: undefined,
|
||||||
|
theme: "light",
|
||||||
|
transition: Bounce,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
class serviiApi {
|
class serviiApi {
|
||||||
constructor() {}
|
|
||||||
|
|
||||||
private static async call<T extends BaseRequest>(endpoint: serviiRequest, body: T): Promise<ApiResponse> {
|
private static async call<T extends BaseRequest>(endpoint: serviiRequest, body: T): Promise<ApiResponse> {
|
||||||
const response = await fetch(`${apiUrl}/${endpoint}`, {
|
const unreachable: string = "Couldn't find an available API";
|
||||||
method: 'POST',
|
try {
|
||||||
headers: {
|
const response = await fetch(`${apiUrl}/${endpoint}`, {
|
||||||
'Content-Type': 'application/json',
|
method: 'POST',
|
||||||
},
|
headers: {
|
||||||
body: JSON.stringify(body),
|
'Content-Type': 'application/json',
|
||||||
});
|
},
|
||||||
const status: number = response.status;
|
body: JSON.stringify(body),
|
||||||
const json = await response.json();
|
|
||||||
|
|
||||||
if (json.message === undefined) {
|
|
||||||
if (!(endpoint === serviiRequest.fetchServers)) {
|
|
||||||
return { return_code: status, message: "Couldn't find an available API" };
|
|
||||||
}
|
|
||||||
return { return_code: status, message: json };
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!nonToastableCalls.includes(endpoint)) {
|
|
||||||
|
|
||||||
let toastType: 'success' | 'error' | 'info';
|
|
||||||
|
|
||||||
if (status >= 200 && status < 300) {
|
|
||||||
toastType = 'success';
|
|
||||||
} else if (status >= 400 && status < 600) {
|
|
||||||
toastType = 'error';
|
|
||||||
} else {
|
|
||||||
toastType = 'info';
|
|
||||||
}
|
|
||||||
|
|
||||||
toast[toastType](json.message, {
|
|
||||||
position: "top-right",
|
|
||||||
autoClose: 3500,
|
|
||||||
hideProgressBar: false,
|
|
||||||
closeOnClick: true,
|
|
||||||
pauseOnHover: true,
|
|
||||||
draggable: true,
|
|
||||||
progress: undefined,
|
|
||||||
theme: "light",
|
|
||||||
transition: Bounce,
|
|
||||||
});
|
});
|
||||||
|
const status: number = response.status;
|
||||||
|
const json = await response.json();
|
||||||
|
const message = json.message;
|
||||||
|
|
||||||
|
if (message === undefined) {
|
||||||
|
if (!(endpoint === serviiRequest.fetchServers)) {
|
||||||
|
return {return_code: 503, message: "Couldn't find an available API"};
|
||||||
|
}
|
||||||
|
return {return_code: status, message: json};
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!nonToastableCalls.includes(endpoint)) {
|
||||||
|
toast_status(status, message);
|
||||||
|
}
|
||||||
|
|
||||||
|
return {return_code: status, message: json.message};
|
||||||
|
} catch (error) {
|
||||||
|
toast_status(666, unreachable);
|
||||||
|
return {return_code: 503, message: "Couldn't find an available API"}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return { return_code: status, message: json.message };
|
constructor() {}
|
||||||
}
|
|
||||||
|
|
||||||
private static token(): string {
|
private static token(): string {
|
||||||
const currentUser = getAuth().currentUser;
|
const currentUser = getAuth().currentUser;
|
||||||
|
Loading…
Reference in New Issue
Block a user