ajout call api pour afficher les serveurs

This commit is contained in:
AntoninoP 2024-06-26 22:50:35 +02:00
parent f5b8f808c6
commit 3e427ce704
4 changed files with 64 additions and 15 deletions

View File

@ -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 = () => (
<div className={styles.loading}>
<div className={styles.spinner}></div>
<p style={{ color: 'white' }}>Chargement...</p>
<p style={{ color: 'white' }}>Chargement...</p>
</div>
);
@ -21,7 +22,7 @@ const App = () => {
useEffect(() => {
const unsubscribe = auth.onAuthStateChanged((user) => {
setUser(user);
setLoading(false);
setLoading(false);
});
return () => unsubscribe();

View File

@ -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 (
<div className={styles.dashboardContainer}>
<Navbar user={user} />
<div className={styles.cardsContainer}>
<ServerCard color="#f0f0f0" status="En cours" version="1.16.5" link="http://example.com" name="test1" />
<ServerCard color="#f0f0f0" status="Démarrage" version="1.17.1" link="http://example.com" name="test2" />
<ServerCard color="#f0f0f0" status="Hors ligne" version="1.15.2" link="http://example.com" name="test3" />
{servers.map((server, index) => (
<ServerCard
key={index}
color="#f0f0f0"
status={server.running ? 'En cours' : 'Hors ligne'}
version={server.version}
link={`http://example.com/${server.name}`}
name={server.name}
/>
))}
</div>
</div>
);
};
export default DashboardPage;
export default DashboardPage;

View File

@ -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 (
<div className={styles.container}>
<div className={styles.mainCard}>
<h1>Page de connexion</h1>
<button onClick={handleLogin} className={styles.logbtn}>
Se connecter avec Google
</button>
</div>
<div className={styles.mainCard}>
<h1>Page de connexion</h1>
<button onClick={handleLogin} className={styles.logbtn}>
Se connecter avec Google
</button>
</div>
</div>
);
};

View File

@ -1,3 +1,4 @@
// src/firebase.js
import { initializeApp } from 'firebase/app';
import { getAuth, GoogleAuthProvider, signInWithPopup } from 'firebase/auth';