ajout btn start et stop

This commit is contained in:
AntoninoP 2024-06-29 20:08:08 +02:00
parent 54a12900bb
commit 7b842488cd
2 changed files with 91 additions and 9 deletions

View File

@ -1,7 +1,8 @@
import React from 'react'; import React from 'react';
import styles from './serverCard.module.scss'; import styles from './serverCard.module.scss';
const ServerCard = ({ color, status, version, link, name }) => { const ServerCard = ({ color, status, version, link, name, onRunClick, onStopClick }) => {
const getStatusColor = () => { const getStatusColor = () => {
switch (status) { switch (status) {
case 'En cours': 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 ( return (
<a href={link} className={styles.serverCard}> <a href={link} className={styles.serverCard}>
<div className={styles.leftCard}> <div className={styles.leftCard}>
@ -25,6 +42,14 @@ const ServerCard = ({ color, status, version, link, name }) => {
<div className={styles.name}>{name}</div> <div className={styles.name}>{name}</div>
</div> </div>
<div className={styles.statusText}>{status}</div> <div className={styles.statusText}>{status}</div>
<div className={styles.buttonContainer}>
{status === 'Hors ligne' && (
<button className={styles.runButton} onClick={handleRun}>Run</button>
)}
{status === 'En cours' && (
<button className={styles.stopButton} onClick={handleStop}>Stop</button>
)}
</div>
</div> </div>
<div className={styles.version}>Version: {version}</div> <div className={styles.version}>Version: {version}</div>
</a> </a>

View File

@ -19,13 +19,13 @@ const DashboardPage = ({ user }) => {
const userId = currentUser.uid; const userId = currentUser.uid;
console.log('User ID:', userId); 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', method: 'POST',
headers: { headers: {
'Content-Type': 'application/json', 'Content-Type': 'application/json',
'Authorization': `Bearer ${userId}` Authorization: `Bearer ${userId}`,
}, },
body: JSON.stringify({ token: userId }) body: JSON.stringify({ token: userId }),
}); });
if (!response.ok) { if (!response.ok) {
const errorText = await response.text(); const errorText = await response.text();
@ -43,6 +43,62 @@ const DashboardPage = ({ user }) => {
fetchServers(); fetchServers();
}, [auth]); }, [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 ( return (
<div className={styles.dashboardContainer}> <div className={styles.dashboardContainer}>
<Navbar user={user} /> <Navbar user={user} />
@ -53,8 +109,9 @@ const DashboardPage = ({ user }) => {
color="#f0f0f0" color="#f0f0f0"
status={server.running ? 'En cours' : 'Hors ligne'} status={server.running ? 'En cours' : 'Hors ligne'}
version={server.version} version={server.version}
link={`http://example.com/${server.name}`}
name={server.name} name={server.name}
onRunClick={handleRunServer}
onStopClick={handleStopServer}
/> />
))} ))}
</div> </div>