From 3e427ce70416aa87734ba4040b8bf4825d814c75 Mon Sep 17 00:00:00 2001 From: AntoninoP Date: Wed, 26 Jun 2024 22:50:35 +0200 Subject: [PATCH] ajout call api pour afficher les serveurs --- src/App.jsx | 5 +- src/pages/DashboardPage/DashboardPage.jsx | 56 +++++++++++++++++++++-- src/pages/LoginPage/LoginPage.jsx | 17 +++---- src/pages/LoginPage/firebase.js | 1 + 4 files changed, 64 insertions(+), 15 deletions(-) diff --git a/src/App.jsx b/src/App.jsx index 8bf0ab1..f48abd1 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -1,3 +1,4 @@ +// src/App.js import React, { useEffect, useState } from 'react'; import { BrowserRouter as Router, Route, Routes, Navigate } from 'react-router-dom'; import { ToastContainer } from 'react-toastify'; @@ -10,7 +11,7 @@ import styles from './App.module.scss'; const Loading = () => (
-

Chargement...

+

Chargement...

); @@ -21,7 +22,7 @@ const App = () => { useEffect(() => { const unsubscribe = auth.onAuthStateChanged((user) => { setUser(user); - setLoading(false); + setLoading(false); }); return () => unsubscribe(); diff --git a/src/pages/DashboardPage/DashboardPage.jsx b/src/pages/DashboardPage/DashboardPage.jsx index 953ab0a..1331ba5 100644 --- a/src/pages/DashboardPage/DashboardPage.jsx +++ b/src/pages/DashboardPage/DashboardPage.jsx @@ -1,19 +1,65 @@ -import React from 'react'; +import React, { useEffect, useState } from 'react'; +import { getAuth } from 'firebase/auth'; import ServerCard from '../../components/serverCard/serverCard'; import Navbar from '../../components/navbar/Navbar'; import styles from './DashboardPage.module.scss'; const DashboardPage = ({ user }) => { + const [servers, setServers] = useState([]); + const auth = getAuth(); + + useEffect(() => { + const fetchServers = async () => { + try { + const currentUser = auth.currentUser; + if (!currentUser) { + console.error('No current user found'); + return; + } + const userId = currentUser.uid; + console.log('User ID:', userId); + + const response = await fetch('http://api.servii.fr:3000/FetchServers', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'Authorization': `Bearer ${userId}` + }, + body: JSON.stringify({ token: userId }) + }); + if (!response.ok) { + const errorText = await response.text(); + console.error('Error response text:', errorText); + throw new Error(`Network response was not ok: ${response.statusText}`); + } + const data = await response.json(); + console.log('Data:', data); + setServers(data); + } catch (error) { + console.error('Error fetching servers:', error); + } + }; + + fetchServers(); + }, [auth]); + return (
- - - + {servers.map((server, index) => ( + + ))}
); }; -export default DashboardPage; \ No newline at end of file +export default DashboardPage; diff --git a/src/pages/LoginPage/LoginPage.jsx b/src/pages/LoginPage/LoginPage.jsx index f41e67f..31a7c3c 100644 --- a/src/pages/LoginPage/LoginPage.jsx +++ b/src/pages/LoginPage/LoginPage.jsx @@ -1,6 +1,7 @@ +// src/pages/LoginPage/LoginPage.js import React from 'react'; -import { auth, googleProvider, signInWithPopup } from './firebase'; import { useNavigate } from 'react-router-dom'; +import { auth, googleProvider, signInWithPopup } from './firebase'; import styles from './LoginPage.module.scss'; const LoginPage = () => { @@ -8,7 +9,7 @@ const LoginPage = () => { const handleLogin = async () => { try { - await signInWithPopup(auth, googleProvider); + await signInWithPopup(auth, googleProvider); navigate('/dashboard'); } catch (error) { console.error("Erreur lors de la connexion : ", error); @@ -17,12 +18,12 @@ const LoginPage = () => { return (
-
-

Page de connexion

- -
+
+

Page de connexion

+ +
); }; diff --git a/src/pages/LoginPage/firebase.js b/src/pages/LoginPage/firebase.js index c582963..3643ad0 100644 --- a/src/pages/LoginPage/firebase.js +++ b/src/pages/LoginPage/firebase.js @@ -1,3 +1,4 @@ +// src/firebase.js import { initializeApp } from 'firebase/app'; import { getAuth, GoogleAuthProvider, signInWithPopup } from 'firebase/auth';