From 9accc4f279304b21b9e6cad53153a037d185ab75 Mon Sep 17 00:00:00 2001 From: AntoninoP Date: Wed, 3 Jul 2024 01:51:52 +0200 Subject: [PATCH] ajout verif subdomaine --- src/components/CreateServer/CreateServer.jsx | 74 +++++++++++++++++--- src/pages/DashboardPage/DashboardPage.jsx | 9 ++- src/pages/NoServer/NoServer.jsx | 8 +-- src/service/serverService.jsx | 25 ++++++- 4 files changed, 96 insertions(+), 20 deletions(-) diff --git a/src/components/CreateServer/CreateServer.jsx b/src/components/CreateServer/CreateServer.jsx index 58bf520..17ca1f1 100644 --- a/src/components/CreateServer/CreateServer.jsx +++ b/src/components/CreateServer/CreateServer.jsx @@ -1,16 +1,70 @@ -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 ( -
-
-
Création du serveur
- - -
-
+ <> + {subdomain === null ? ( +
+
+
Ecrivez votre sous domaine
+
+ 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 ! +
+ setSubdomainInput(e.target.value)} + /> + +
+
+ ) : ( +
+
+
Création du serveur
+ + +
+
+ )} + ); }; -export default CreateServer ; +export default CreateServer; diff --git a/src/pages/DashboardPage/DashboardPage.jsx b/src/pages/DashboardPage/DashboardPage.jsx index f434f0e..bff93f4 100644 --- a/src/pages/DashboardPage/DashboardPage.jsx +++ b/src/pages/DashboardPage/DashboardPage.jsx @@ -3,7 +3,7 @@ import ServerCard from '../../components/serverCard/serverCard'; import Navbar from '../../components/navbar/Navbar'; import styles from './DashboardPage.module.scss'; import NoServer from '../NoServer/NoServer'; -import {getUserSubdomain} from "../../service/firebase"; +import { getUserSubdomain } from "../../service/firebase"; import { fetchServers, createServer, runServer, stopServer, deleteServer } from '../../service/serverService'; const DashboardPage = ({ user }) => { @@ -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 }) => {
{servers.length === 0 ? ( - + ) : ( servers.map((server, index) => ( { +const NoServer = ({ user, onCreateServer }) => { const [isCreating, setIsCreating] = useState(false); return ( @@ -15,9 +15,9 @@ const NoServer = ({ onCreateServer }) => { ) : ( - <> - - + <> + {} + )}
diff --git a/src/service/serverService.jsx b/src/service/serverService.jsx index 860fee6..6612155 100644 --- a/src/service/serverService.jsx +++ b/src/service/serverService.jsx @@ -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 };