import { useRouter } from "next/router"; import { useContext, useEffect, useState } from "react"; import { useSessionContext } from "../../context/session.context.js"; import { useWebsocketContext, WebsocketContext, } from "../../context/websocket.context.js"; import { checkRoom, getRoom, joinRoom } from "../../requests/requests.room.js"; import styles from "../../styles/room/join.module.scss"; import { isBrowser, isEmpty, parseClassName } from "../../utils/utils.js"; import Input from "../Input.jsx"; import Layout from "../Layout.js"; import { MdKeyboardArrowDown } from "react-icons/md"; export default function Join() { const [infos, setInfos] = useState({ nick: "", pin: "", reconnectCode: "", joined: null, }); const router = useRouter(); const { connect, send, isConnected } = useContext(WebsocketContext); const { authData } = useSessionContext(); useEffect(() => { if (!isEmpty(authData)) { setInfos({ ...infos, nick: authData.username }); } }, [authData]); useEffect(() => { if (router.query.mode.length == 2) { checkRoom( router.query.mode[1], isBrowser && isEmpty(authData) && sessionStorage.getItem("clientId") ) .then((res) => { connect(`/ws/room/${res.id_code}`); if (res.is_auth) { router.push({ pathname: "/room/" + res.id_code }, undefined, { shallow: true, }); } else { setInfos({ ...infos, pin: res.id_code, joined: false }); } }) .catch((err) => { router.push({ pathname: "/room/join" }, undefined, { shallow: true }); }); } return () => { setInfos({ nick: "", pin: "", reconnectCode: "", joined: null }); }; }, []); const [errorRoom, setErrorRoom] = useState({ message: "" }); useEffect(() => { if ( !isEmpty(authData) && infos.joined == false ) { if (isConnected) { send({ data: { type: "login", nick: "", }, }); } } }, [infos.joined, isConnected, authData]); const [fade, setFade] = useState(null); useEffect(() => { if (fade != null) { setTimeout(() => { setInfos({ ...infos, joined: !fade }); }, 300); } }, [fade]); const [active, setActive] = useState(false); return (
{infos.joined == null && (

Rejoindre une salle

{ setInfos({ ...infos, pin: e.target.value }); }} />

{errorRoom.message}

)} {infos.joined == false && (

Entrez un pseudo

{ setInfos({ ...infos, nick: e.target.value }); }} />

{errorRoom.message}

{ setActive(!active); }} className={parseClassName([ styles.activeToggler, active ? styles.active : styles.unactive, ])} > Avec un code de reconnexion

{active && ( { setInfos({ ...infos, reconnectCode: e.target.value }); }} /> )}
)}
); }