From 7b842488cd6430c2e6566886e4e5095efac2c697 Mon Sep 17 00:00:00 2001 From: AntoninoP Date: Sat, 29 Jun 2024 20:08:08 +0200 Subject: [PATCH] ajout btn start et stop --- src/components/serverCard/serverCard.jsx | 27 ++++++++- src/pages/DashboardPage/DashboardPage.jsx | 73 ++++++++++++++++++++--- 2 files changed, 91 insertions(+), 9 deletions(-) diff --git a/src/components/serverCard/serverCard.jsx b/src/components/serverCard/serverCard.jsx index 5a1924d..fde86e9 100644 --- a/src/components/serverCard/serverCard.jsx +++ b/src/components/serverCard/serverCard.jsx @@ -1,7 +1,8 @@ import React from 'react'; import styles from './serverCard.module.scss'; -const ServerCard = ({ color, status, version, link, name }) => { +const ServerCard = ({ color, status, version, link, name, onRunClick, onStopClick }) => { + const getStatusColor = () => { switch (status) { case 'En cours': @@ -15,6 +16,22 @@ const ServerCard = ({ color, status, version, link, name }) => { } }; + const handleRun = async () => { + try { + await onRunClick(name); + } catch (error) { + console.error('Error starting server:', error); + } + }; + + const handleStop = async () => { + try { + await onStopClick(name); + } catch (error) { + console.error('Error stopping server:', error); + } + }; + return (
@@ -25,6 +42,14 @@ const ServerCard = ({ color, status, version, link, name }) => {
{name}
{status}
+
+ {status === 'Hors ligne' && ( + + )} + {status === 'En cours' && ( + + )} +
Version: {version}
diff --git a/src/pages/DashboardPage/DashboardPage.jsx b/src/pages/DashboardPage/DashboardPage.jsx index 1331ba5..e2e0d1f 100644 --- a/src/pages/DashboardPage/DashboardPage.jsx +++ b/src/pages/DashboardPage/DashboardPage.jsx @@ -16,24 +16,24 @@ const DashboardPage = ({ user }) => { console.error('No current user found'); return; } - const userId = currentUser.uid; - console.log('User ID:', userId); + const userId = currentUser.uid; + console.log('User ID:', userId); - const response = await fetch('http://api.servii.fr:3000/FetchServers', { + const response = await fetch('https://api.servii.fr:3000/FetchServers', { method: 'POST', headers: { 'Content-Type': 'application/json', - 'Authorization': `Bearer ${userId}` + Authorization: `Bearer ${userId}`, }, - body: JSON.stringify({ token: userId }) + body: JSON.stringify({ token: userId }), }); if (!response.ok) { const errorText = await response.text(); - console.error('Error response text:', errorText); + 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); + console.log('Data:', data); setServers(data); } catch (error) { console.error('Error fetching servers:', error); @@ -43,6 +43,62 @@ const DashboardPage = ({ user }) => { fetchServers(); }, [auth]); + const handleRunServer = async (serverName) => { + try { + const currentUser = auth.currentUser; + if (!currentUser) { + console.error('No current user found'); + return; + } + const userId = currentUser.uid; + console.log('Run server:', serverName); + + const response = await fetch('https://api.servii.fr:3000/ServerRun', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${userId}`, + }, + body: JSON.stringify({ name: serverName, token: userId }), + }); + if (!response.ok) { + const errorText = await response.text(); + console.error('Error response text:', errorText); + throw new Error(`Failed to start server: ${response.statusText}`); + } + } catch (error) { + console.error('Error starting server:', error); + } + }; + + const handleStopServer = async (serverName) => { + try { + const currentUser = auth.currentUser; + if (!currentUser) { + console.error('No current user found'); + return; + } + const userId = currentUser.uid; + console.log('Stop server:', serverName); + + const response = await fetch('https://api.servii.fr:3000/ServerStop', { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + Authorization: `Bearer ${userId}`, + }, + body: JSON.stringify({ name: serverName, token: userId }), + }); + if (!response.ok) { + const errorText = await response.text(); + console.error('Error response text:', errorText); + throw new Error(`Failed to stop server: ${response.statusText}`); + } + } catch (error) { + console.error('Error stopping server:', error); + } + }; + return (
@@ -53,8 +109,9 @@ const DashboardPage = ({ user }) => { color="#f0f0f0" status={server.running ? 'En cours' : 'Hors ligne'} version={server.version} - link={`http://example.com/${server.name}`} name={server.name} + onRunClick={handleRunServer} + onStopClick={handleStopServer} /> ))}