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,15 +1,69 @@
import React, { useState } from 'react';
import React, { useState, useEffect } from 'react';
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 (
<>
{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.mainCard}>
<div className={styles.SubTitle}>Création du serveur</div>
<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>
)}
</>
);
};

View File

@ -14,9 +14,8 @@ const DashboardPage = ({ user }) => {
try {
const data = await fetchServers();
setServers(data);
const subdomain = await getUserSubdomain(user.uid);
setSubdomain(subdomain);
console.log(subdomain);
const userSubdomain = await getUserSubdomain(user.uid);
setSubdomain(userSubdomain);
} catch (error) {
console.error('Error fetching data:', error);
}
@ -67,7 +66,7 @@ const DashboardPage = ({ user }) => {
<Navbar user={user} />
<div className={styles.cardsContainer}>
{servers.length === 0 ? (
<NoServer onCreateServer={handleCreateServer} />
<NoServer user={user} onCreateServer={handleCreateServer} />
) : (
servers.map((server, index) => (
<ServerCard

View File

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