supprimer et ajouter un serveur add

This commit is contained in:
AntoninoP 2024-06-30 00:40:32 +02:00
parent c8b4095bb8
commit d3fc09370a
2 changed files with 111 additions and 35 deletions

View File

@ -1,7 +1,7 @@
import React from 'react';
import styles from './serverCard.module.scss';
const ServerCard = ({ color, status, version, link, name, onRunClick, onStopClick }) => {
const ServerCard = ({ color, status, version, link, name, onRunClick, onStopClick, onDeleteClick }) => {
const getStatusColor = () => {
switch (status) {
@ -32,6 +32,14 @@ const ServerCard = ({ color, status, version, link, name, onRunClick, onStopClic
}
};
const handleDelete = async () => {
try {
await onDeleteClick(name);
} catch (error) {
console.error('Error deleting server:', error);
}
};
return (
<a href={link} className={styles.serverCard}>
<div className={styles.leftCard}>
@ -44,14 +52,15 @@ const ServerCard = ({ color, status, version, link, name, onRunClick, onStopClic
<div className={styles.statusText}>{status}</div>
<div className={styles.buttonContainer}>
{status === 'Hors ligne' && (
<button className={styles.runButton} onClick={handleRun}>Run</button>
<button className={styles.runButton} onClick={handleRun}>Démarrer</button>
)}
{status === 'En cours' && (
<button className={styles.stopButton} onClick={handleStop}>Stop</button>
<button className={styles.stopButton} onClick={handleStop}>Arrêter</button>
)}
</div>
</div>
<div className={styles.version}>Version: {version}</div>
<button className={styles.deleteButton} onClick={handleDelete}>Supprimer</button>
</a>
);
};

View File

@ -8,38 +8,38 @@ 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('https://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);
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://176.165.62.226: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);
}
};
useEffect(() => {
fetchServers();
}, [auth]);
@ -53,7 +53,7 @@ const DashboardPage = ({ user }) => {
const userId = currentUser.uid;
console.log('Run server:', serverName);
const response = await fetch('https://api.servii.fr:3000/ServerRun', {
const response = await fetch('http://176.165.62.226:3000/ServerRun', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -81,7 +81,7 @@ const DashboardPage = ({ user }) => {
const userId = currentUser.uid;
console.log('Stop server:', serverName);
const response = await fetch('https://api.servii.fr:3000/ServerStop', {
const response = await fetch('http://176.165.62.226:3000/ServerStop', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
@ -99,10 +99,75 @@ const DashboardPage = ({ user }) => {
}
};
const handleDeleteServer = async (serverName) => {
try {
const currentUser = auth.currentUser;
if (!currentUser) {
console.error('No current user found');
return;
}
const userId = currentUser.uid;
console.log('Delete server:', serverName);
const response = await fetch('http://176.165.62.226:3000/ServerDelete', {
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 delete server: ${response.statusText}`);
} else {
// Fetch the updated list of servers
fetchServers();
}
} catch (error) {
console.error('Error deleting server:', error);
}
};
const handleCreateServer = async () => {
try {
const currentUser = auth.currentUser;
if (!currentUser) {
console.error('No current user found');
return;
}
const userId = currentUser.uid;
const serverName = prompt('Enter server name:');
const serverVersion = prompt('Enter server version:');
console.log('Create server:', serverName);
const response = await fetch('http://176.165.62.226:3000/ServerCreate', {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userId}`,
},
body: JSON.stringify({ name: serverName, version: serverVersion, token: userId , framework: "paper"}),
});
if (!response.ok) {
const errorText = await response.text();
console.error('Error response text:', errorText);
throw new Error(`Failed to create server: ${response.statusText}`);
} else {
// Fetch the updated list of servers
fetchServers();
}
} catch (error) {
console.error('Error creating server:', error);
}
};
return (
<div className={styles.dashboardContainer}>
<Navbar user={user} />
<div className={styles.cardsContainer}>
<button onClick={handleCreateServer}>Create Server</button>
{servers.map((server, index) => (
<ServerCard
key={index}
@ -110,8 +175,10 @@ const DashboardPage = ({ user }) => {
status={server.running ? 'En cours' : 'Hors ligne'}
version={server.version}
name={server.name}
link={`#`}
onRunClick={handleRunServer}
onStopClick={handleStopServer}
onDeleteClick={handleDeleteServer}
/>
))}
</div>