+ gestion loader amélioré

This commit is contained in:
AntoninoP 2024-07-03 21:00:19 +02:00
parent 3886029191
commit df742285ba
7 changed files with 101 additions and 68 deletions

View File

@ -7,13 +7,7 @@ import LoginPage from './pages/LoginPage/LoginPage';
import DashboardPage from './pages/DashboardPage/DashboardPage'; import DashboardPage from './pages/DashboardPage/DashboardPage';
import { auth } from './service/firebase'; import { auth } from './service/firebase';
import styles from './App.module.scss'; import styles from './App.module.scss';
import Loading from './pages/Loading/loading';
const Loading = () => (
<div className={styles.loading}>
<div className={styles.spinner}></div>
<p style={{ color: 'white' }}>Chargement...</p>
</div>
);
const App = () => { const App = () => {
const [user, setUser] = useState(null); const [user, setUser] = useState(null);

View File

@ -1,28 +0,0 @@
.loading {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 1.5rem;
}
.spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 6rem;
height: 6rem;
border-radius: 50%;
border-left-color: #09f;
animation: spin 1s ease infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}

View File

@ -4,8 +4,10 @@ import { getUserSubdomain } from "../../service/firebase";
import serviiApi from "../../service/api.tsx"; import serviiApi from "../../service/api.tsx";
const CreateServer = ({ user, onCreateServer }) => { const CreateServer = ({ user, onCreateServer }) => {
const [subdomain, setSubdomain] = useState('loading'); // Utiliser 'loading' au lieu de null const [subdomain, setSubdomain] = useState('loading');
const [subdomainInput, setSubdomainInput] = useState(''); const [subdomainInput, setSubdomainInput] = useState('');
const [serverName, setServerName] = useState('');
const [serverVersion, setServerVersion] = useState('');
const loadSubdomain = async () => { const loadSubdomain = async () => {
try { try {
@ -33,6 +35,14 @@ const CreateServer = ({ user, onCreateServer }) => {
} }
}; };
const handleCreateServer = async () => {
try {
await onCreateServer(serverName, serverVersion);
} catch (error) {
console.error('Error creating server:', error);
}
};
if (subdomain === 'loading') { if (subdomain === 'loading') {
return ( return (
<div className={styles.container}> <div className={styles.container}>
@ -63,8 +73,23 @@ const CreateServer = ({ user, onCreateServer }) => {
<div className={styles.container}> <div className={styles.container}>
<div className={styles.mainCard}> <div className={styles.mainCard}>
<div className={styles.SubTitle}>Création du serveur</div> <div className={styles.SubTitle}>Création du serveur</div>
<input className={styles.Input} type="text" placeholder="Nom du serveur" /> <input
<button className={styles.btnServCreate} onClick={onCreateServer}> {/* Utilisation de onCreateServer ici */} className={styles.Input}
type="text"
placeholder="Nom du serveur"
value={serverName}
onChange={(e) => setServerName(e.target.value)}
/>
<select
name="version"
id="version-select"
value={serverVersion}
onChange={(e) => setServerVersion(e.target.value)}
>
<option value="">Version</option>
<option value="1.20.6">1.20.6</option>
</select>
<button className={styles.btnServCreate} onClick={handleCreateServer}>
Créer Créer
</button> </button>
</div> </div>

View File

@ -4,24 +4,25 @@ import Navbar from '../../components/navbar/Navbar';
import styles from './DashboardPage.module.scss'; import styles from './DashboardPage.module.scss';
import NoServer from '../NoServer/NoServer'; import NoServer from '../NoServer/NoServer';
import { getUserSubdomain } from "../../service/firebase"; import { getUserSubdomain } from "../../service/firebase";
import '../../service/api.tsx';
import serviiApi from "../../service/api.tsx"; import serviiApi from "../../service/api.tsx";
import Api from "../../service/api.tsx"; import Loading from '../Loading/loading';
const DashboardPage = ({ user }) => { const DashboardPage = ({ user }) => {
const [servers, setServers] = useState([]); const [servers, setServers] = useState([]);
const [subdomain, setSubdomain] = useState(""); const [subdomain, setSubdomain] = useState("");
const [loading, setLoading] = useState(true);
const loadServers = async () => { const loadServers = async () => {
try { try {
const ApiResponse = await serviiApi.fetchServers(); const ApiResponse = await serviiApi.fetchServers();
const data = ApiResponse.message const data = ApiResponse.message;
setServers(data); setServers(data);
const userSubdomain = await getUserSubdomain(user.uid); const userSubdomain = await getUserSubdomain(user.uid);
setSubdomain(userSubdomain); setSubdomain(userSubdomain);
} catch (error) { } catch (error) {
console.error('Error fetching data:', error); console.error('Error fetching data:', error);
} finally {
setLoading(false);
} }
}; };
@ -29,11 +30,10 @@ const DashboardPage = ({ user }) => {
loadServers(); loadServers();
}, []); }, []);
const handleCreateServer = async () => { const handleCreateServer = async (serverName, serverVersion) => {
try { try {
const serverName = prompt('Enter the name for the new server:'); if (!serverName || !serverVersion) return;
if (!serverName) return; await serviiApi.serverCreate(serverName, serverVersion, "paper");
await serviiApi.serverCreate(serverName, "1.20.6", "paper");
await loadServers(); await loadServers();
} catch (error) { } catch (error) {
console.error('Error creating server:', error); console.error('Error creating server:', error);
@ -68,25 +68,29 @@ const DashboardPage = ({ user }) => {
return ( return (
<div className={styles.dashboardContainer}> <div className={styles.dashboardContainer}>
<Navbar user={user} /> <Navbar user={user} />
<div className={styles.cardsContainer}> {loading ? (
{servers.length === 0 ? ( <Loading />
<NoServer user={user} onCreateServer={handleCreateServer} /> ) : (
) : ( <div className={styles.cardsContainer}>
servers.map((server, index) => ( {servers.length === 0 ? (
<ServerCard <NoServer user={user} onCreateServer={handleCreateServer} />
key={index} ) : (
color="#f0f0f0" servers.map((server, index) => (
status={server.running ? 'En cours' : 'Hors ligne'} <ServerCard
version={server.version} key={index}
name={server.name} color="#f0f0f0"
link="#" status={server.running ? 'En cours' : 'Hors ligne'}
onRunClick={() => handleRunServer(server.name)} version={server.version}
onStopClick={() => handleStopServer(server.name)} name={server.name}
onDeleteClick={() => handleDeleteServer(server.name)} link="#"
/> onRunClick={() => handleRunServer(server.name)}
)) onStopClick={() => handleStopServer(server.name)}
)} onDeleteClick={() => handleDeleteServer(server.name)}
</div> />
))
)}
</div>
)}
</div> </div>
); );
}; };

View File

@ -0,0 +1,10 @@
import styles from './loading.module.scss';
const Loading = () => (
<div className={styles.loading}>
<div className={styles.spinner}></div>
<p style={{ color: 'white' }}>Chargement...</p>
</div>
);
export default Loading;

View File

@ -0,0 +1,28 @@
.loading {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
height: 100vh;
font-size: 1.5rem;
}
.spinner {
border: 4px solid rgba(0, 0, 0, 0.1);
width: 6rem;
height: 6rem;
border-radius: 50%;
border-left-color: #09f;
animation: spin 1s ease infinite;
}
@keyframes spin {
0% {
transform: rotate(0deg);
}
100% {
transform: rotate(360deg);
}
}

View File

@ -8,15 +8,15 @@ const NoServer = ({ user, onCreateServer }) => {
return ( return (
<div className={styles.container}> <div className={styles.container}>
<div className={styles.mainCard}> <div className={styles.mainCard}>
{ isCreating === false ? ( {isCreating === false ? (
<> <>
<div className={styles.nsSubTitle}>Erreur </div> <div className={styles.nsSubTitle}>Erreur</div>
<div className={styles.nsTitle}>Aucun serveur possédé</div> <div className={styles.nsTitle}>Aucun serveur possédé</div>
<button className={styles.btnServCreate} onClick={() => setIsCreating(true)}>Créer un nouveau serveur</button> <button className={styles.btnServCreate} onClick={() => setIsCreating(true)}>Créer un nouveau serveur</button>
</> </>
) : ( ) : (
<> <>
<CreateServer user={user} onCreateServer={onCreateServer} /> {} <CreateServer user={user} onCreateServer={onCreateServer} />
</> </>
)} )}
</div> </div>