modpack api calls

This commit is contained in:
AntoninoP 2024-09-22 18:50:07 +02:00
parent 1e64fc0677
commit c9ad1a372a
2 changed files with 53 additions and 5 deletions

View File

@ -1,12 +1,38 @@
import React, { useEffect, useState } from 'react';
import styles from './modpack.module.scss';
import PropTypes from "prop-types";
import Navbar from '../../../components/navbar/Navbar';
import { useNavigate } from 'react-router-dom';
import serviiApi from '../../../service/api';
const Modpack = ({ user }) => {
const navigate = useNavigate();
const [modpacks, setModpacks] = useState([]);
const [error, setError] = useState('');
useEffect(() => {
const fetchModpacks = async () => {
if (!user) {
setError("Vous devez être connecté pour voir les modpacks.");
return;
}
try {
const response = await serviiApi.fetchModpacks();
console.log(response);
if (response.return_code === 200) {
setModpacks(response.message);
} else {
setError(response.message);
}
} catch (error) {
console.error(error);
setError("Erreur lors du chargement des modpacks.");
}
};
fetchModpacks();
}, [user]);
return (
<div className={styles.Container}>
@ -18,7 +44,13 @@ const Modpack = ({ user }) => {
onBackClick={() => navigate('/CreateServer')}
/>
<div className={styles.hey}>
<h1>Prochainement disponible</h1>
{error ? <h2>{error}</h2> : (
<ul>
{modpacks.map((modpack, index) => (
<li key={index}>{modpack.name}</li>
))}
</ul>
)}
</div>
</div>
);

View File

@ -88,7 +88,23 @@ function toast_status(status: number, message: string) {
});
}
class serviiApi {
public static async fetchModpacks(): Promise<ApiResponse> {
try {
const response = await fetch(`${apiUrl}/modpacks`, {
method: 'GET',
});
const json = await response.json();
return { return_code: response.status, message: json };
} catch (error) {
toast_status(666, "Couldn't fetch modpacks");
console.error(error);
return { return_code: 503, message: "Couldn't fetch modpacks" };
}
}
private static async call<T extends BaseRequest>(endpoint: serviiRequest, body: T, token: string): Promise<ApiResponse> {
const unreachable: string = "Couldn't find an available API";
try {