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 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 (
<a href={link} className={styles.serverCard}>
<div className={styles.leftCard}>
@ -25,6 +42,14 @@ const ServerCard = ({ color, status, version, link, name }) => {
<div className={styles.name}>{name}</div>
</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 className={styles.version}>Version: {version}</div>
</a>

View File

@ -19,13 +19,13 @@ const DashboardPage = ({ user }) => {
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();
@ -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 (
<div className={styles.dashboardContainer}>
<Navbar user={user} />
@ -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}
/>
))}
</div>