From c9ad1a372a5e61c0f4c07cb5851044887ca8bcbe Mon Sep 17 00:00:00 2001 From: AntoninoP Date: Sun, 22 Sep 2024 18:50:07 +0200 Subject: [PATCH] modpack api calls --- src/pages/CreateServer/modpack/modpack.jsx | 42 +++++++++++++++++++--- src/service/api.tsx | 16 +++++++++ 2 files changed, 53 insertions(+), 5 deletions(-) diff --git a/src/pages/CreateServer/modpack/modpack.jsx b/src/pages/CreateServer/modpack/modpack.jsx index 57e8345..485816b 100644 --- a/src/pages/CreateServer/modpack/modpack.jsx +++ b/src/pages/CreateServer/modpack/modpack.jsx @@ -1,13 +1,39 @@ +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 (
{ backButtonText="Retour" onBackClick={() => navigate('/CreateServer')} /> -
-

Prochainement disponible

-
+
+ {error ?

{error}

: ( +
    + {modpacks.map((modpack, index) => ( +
  • {modpack.name}
  • + ))} +
+ )} +
); }; diff --git a/src/service/api.tsx b/src/service/api.tsx index 810def7..0786875 100644 --- a/src/service/api.tsx +++ b/src/service/api.tsx @@ -88,7 +88,23 @@ function toast_status(status: number, message: string) { }); } + + class serviiApi { + + public static async fetchModpacks(): Promise { + 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(endpoint: serviiRequest, body: T, token: string): Promise { const unreachable: string = "Couldn't find an available API"; try {