ajout verif subdomaine

This commit is contained in:
AntoninoP 2024-07-03 01:51:52 +02:00
parent 9c550dc916
commit 9accc4f279
4 changed files with 96 additions and 20 deletions

View File

@ -1,16 +1,70 @@
import React, { useState } from 'react'; import React, { useState, useEffect } from 'react';
import styles from './CreateServer.module.scss'; import styles from './CreateServer.module.scss';
import { getUserSubdomain } from "../../service/firebase";
import { saveSubdomain } from "../../service/serverService";
const CreateServer = ({ user, onCreateServer }) => {
const [subdomain, setSubdomain] = useState(null);
const [subdomainInput, setSubdomainInput] = useState('');
const loadSubdomain = async () => {
try {
if (user && user.uid) {
const userSubdomain = await getUserSubdomain(user.uid);
setSubdomain(userSubdomain);
} else {
console.error('User or user.uid is undefined');
}
} catch (error) {
console.error('Error fetching subdomain:', error);
}
};
useEffect(() => {
loadSubdomain();
}, [user]);
const handleSaveSubdomain = async () => {
try {
await saveSubdomain(subdomainInput);
setSubdomain(subdomainInput);
} catch (error) {
console.error('Error setting subdomain:', error);
}
};
const CreateServer = ({ onCreateServer }) => {
return ( return (
<>
{subdomain === null ? (
<div className={styles.container}>
<div className={styles.mainCardSubdomain}>
<div className={styles.Title}>Ecrivez votre sous domaine</div>
<div className={styles.SubTitlesdomain}>
Le sous-domaine est le nom sous lequel vos amis et vous rejoignez le serveur, un peu comme une adresse. Choisissez-le bien, car il n'est pas facilement modifiable !
</div>
<input
type="text"
value={subdomainInput}
onChange={(e) => setSubdomainInput(e.target.value)}
/>
<button className={styles.btnSubCreate} onClick={handleSaveSubdomain}>
Envoyer
</button>
</div>
</div>
) : (
<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 className={styles.Input} type="text" placeholder="Nom du serveur" />
<button className={styles.btnServCreate} onClick={() => onCreateServer()}>Créer</button> <button className={styles.btnServCreate} onClick={onCreateServer}> {/* Utilisation de onCreateServer ici */}
Créer
</button>
</div> </div>
</div> </div>
)}
</>
); );
}; };
export default CreateServer ; export default CreateServer;

View File

@ -3,7 +3,7 @@ import ServerCard from '../../components/serverCard/serverCard';
import Navbar from '../../components/navbar/Navbar'; 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 { fetchServers, createServer, runServer, stopServer, deleteServer } from '../../service/serverService'; import { fetchServers, createServer, runServer, stopServer, deleteServer } from '../../service/serverService';
const DashboardPage = ({ user }) => { const DashboardPage = ({ user }) => {
@ -14,9 +14,8 @@ const DashboardPage = ({ user }) => {
try { try {
const data = await fetchServers(); const data = await fetchServers();
setServers(data); setServers(data);
const subdomain = await getUserSubdomain(user.uid); const userSubdomain = await getUserSubdomain(user.uid);
setSubdomain(subdomain); setSubdomain(userSubdomain);
console.log(subdomain);
} catch (error) { } catch (error) {
console.error('Error fetching data:', error); console.error('Error fetching data:', error);
} }
@ -67,7 +66,7 @@ const DashboardPage = ({ user }) => {
<Navbar user={user} /> <Navbar user={user} />
<div className={styles.cardsContainer}> <div className={styles.cardsContainer}>
{servers.length === 0 ? ( {servers.length === 0 ? (
<NoServer onCreateServer={handleCreateServer} /> <NoServer user={user} onCreateServer={handleCreateServer} />
) : ( ) : (
servers.map((server, index) => ( servers.map((server, index) => (
<ServerCard <ServerCard

View File

@ -2,7 +2,7 @@ import React, { useState } from 'react';
import styles from './NoServer.module.scss'; import styles from './NoServer.module.scss';
import CreateServer from '../../components/CreateServer/CreateServer'; import CreateServer from '../../components/CreateServer/CreateServer';
const NoServer = ({ onCreateServer }) => { const NoServer = ({ user, onCreateServer }) => {
const [isCreating, setIsCreating] = useState(false); const [isCreating, setIsCreating] = useState(false);
return ( return (
@ -16,7 +16,7 @@ const NoServer = ({ onCreateServer }) => {
</> </>
) : ( ) : (
<> <>
<CreateServer></CreateServer> <CreateServer user={user} onCreateServer={onCreateServer} /> {}
</> </>
)} )}
</div> </div>

View File

@ -120,4 +120,27 @@ const deleteServer = async (serverName) => {
} }
}; };
export { fetchServers, createServer, runServer, stopServer, deleteServer }; const saveSubdomain = async (subdomain) => {
const auth = getAuth();
const currentUser = auth.currentUser;
if (!currentUser) {
throw new Error('No current user found');
}
const userId = currentUser.uid;
const response = await fetch(`${apiUrl}/SetSubdomain`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${userId}`,
},
body: JSON.stringify({ subdomain, token: userId }),
});
if (!response.ok) {
const errorText = await response.text();
throw new Error(`Failed to save subdomain: ${errorText}`);
}
};
export { fetchServers, createServer, runServer, stopServer, deleteServer, saveSubdomain };