import { useRouter } from "next/router"; import { useContext, useEffect, useState } from "react"; import { AiFillEyeInvisible, AiFillUnlock, AiOutlineCheck, AiOutlineClose, } from "react-icons/ai"; import ReconnectingWebSocket from "reconnecting-websocket"; import { AlertType, useAlert } from "../../context/alert.context.js"; import { useWebsocketContext } from "../../context/websocket.context.js"; import { changeRoomName, deleteParcours, deleteRoom, getRoom, togglePublicResult, toggleRoomLock, } from "../../requests/requests.room.js"; import styles from "../../styles/room/roomView.module.scss"; import { isBrowser, isEmpty, parseClassName } from "../../utils/utils.js"; import Layout from "../Layout.js"; import { BsCheckLg } from "react-icons/bs"; import { BiTrash } from "react-icons/bi"; import { MdRefresh, MdExitToApp, MdEdit } from "react-icons/md"; import { IoMdExit, IoMdReturnLeft } from "react-icons/io"; import { BiEdit, BiUndo } from "react-icons/bi"; import { notificationService } from "../../services/notification.service.js"; import { AiFillLock, AiFillEye } from "react-icons/ai"; export default function RoomView({ user, id_code }) { const [dataRoom, setDataRoom] = useState(); useEffect(() => { if (user != undefined && (dataRoom == undefined || isEmpty(dataRoom))) { getRoom(id_code, user.clientId) .then((res) => { setDataRoom(res.room); }) .catch((err) => { if (err.response.data.code == 111) { setJoined(false); router.push({ pathname: "/room/join" }); } }); } }, [user, dataRoom]); const router = useRouter(); const { ws, send, connect, addMessageHandler, removeMessageHandler, isConnected, disconnect, } = useWebsocketContext(); useEffect(() => { let handler = (e) => { let data = JSON.parse(e.data); let type = data.type; switch (type) { case "joined": { if (dataRoom) { let nick = data.nick; let owner = data.owner; let online = data.online; let code = data.code; let oldParticipants = dataRoom.participants; let oldWaiters = dataRoom.waiters; setDataRoom({ ...dataRoom, participants: [ ...oldParticipants, { nick: nick, owner: owner, online: online, code: code }, ], waiters: [...oldWaiters.filter((w) => w.code != code)], }); } break; } case "disconnect_participant": { if (dataRoom) { let oldParticipants = dataRoom.participants; let nick = data.nick; setDataRoom({ ...dataRoom, participants: [ ...oldParticipants.map((p) => { if (p.nick == nick) { return { ...p, online: false }; } return p; }), ], }); } break; } case "reconnect_participant": { if (dataRoom) { let oldParticipants = dataRoom.participants; let nick = data.nick; let participant = oldParticipants.filter((p) => p.nick == nick)[0]; participant.online = true; setDataRoom({ ...dataRoom, participants: [ ...oldParticipants.filter((p) => p.nick != nick), participant, ], }); } break; } case "add_waiter": { if (dataRoom) { let nick = data.nick; let code = data.code; let status = data.status; let oldWaiters = dataRoom.waiters; setDataRoom({ ...dataRoom, waiters: [ ...oldWaiters, { nick: nick, code: code, status: status }, ], }); } break; } case "del_waiter": { if (dataRoom) { let code = data.code; let oldWaiters = dataRoom.waiters; setDataRoom({ ...dataRoom, waiters: [...oldWaiters.filter((w) => w.code != code)], }); } break; } case "banned": { if (dataRoom) { let nick = data.nick; let code = data.code; if (user.user.code != code) { let oldParticipants = dataRoom.participants; let oldParcours = dataRoom.parcours; setDataRoom({ ...dataRoom, participants: [ ...oldParticipants.filter((p) => p.nick != nick), ], }); } else if (user.user.code == code) { router.push({ pathname: "/room/join" }); } } break; } case "add_parcours": { if (dataRoom) { let new_parcours = data.parcours; let id_code = dataRoom.id_code; let oldParcours = dataRoom.parcours; setDataRoom({ ...dataRoom, parcours: [...oldParcours, new_parcours], }); } break; } case "delete_parcours": { if (dataRoom) { setDataRoom({ ...dataRoom, parcours: [ ...dataRoom.parcours.filter((p) => p.id_code != data.id_code), ], }); } break; } case "edit_parcours": { if (dataRoom) { let new_parcours = data.parcours; let id_code = dataRoom.id_code; let oldParcours = dataRoom.parcours; setDataRoom({ ...dataRoom, parcours: [ ...oldParcours.map((p) => { if (p.id_code == new_parcours.id_code) { return new_parcours; } return p; }), ], }); } break; } default: break; } }; addMessageHandler(handler); return () => { removeMessageHandler(handler); }; }, [dataRoom]); const alert = useAlert(); const [edit, setEdit] = useState(null); useEffect(() => { window.addEventListener("click", () => { if (edit) { setEdit(null); } }); }); useEffect(() => { return () => { disconnect(); }; }, []); return ( {isEmpty(dataRoom) && <>} {dataRoom && !isEmpty(dataRoom) && (
{(edit === null || edit == false) && edit != "" && (

{ if (navigator.clipboard) { navigator.clipboard.write(router.asPath).then((res) => { notificationService.success("Lien", "Lien copié !"); }); } }} >

{ if (user.user.owner) { setEdit(dataRoom.name); } }} title={"Double click pour modifier"} > {dataRoom.name}

#{dataRoom.id_code}

)} {((edit !== null && edit != false) || edit == "") && (
{ e.stopPropagation(); }} > { if (e.target.value == "") { setEdit(""); } else { setEdit(e.target.value); } }} onKeyDown={(e) => { if (e.code == "Enter") { changeRoomName({ id_code: dataRoom.id_code, clientId: sessionStorage.getItem("clientId"), name: edit, }) .then((res) => { setDataRoom({ ...dataRoom, name: res.name }); setEdit(null); }) .catch((err) => { notificationService.error( "Erreur", "Erreur lors de la modification" ); }); } else if (e.code == "Escape") { setEdit(null); } }} autoFocus={true} />
)}
{ setDataRoom(); if (!isConnected) { connect("/ws/room/" + id_code); } }} className={styles.refresh} /> { disconnect(); router.push("/room", undefined, {shallow:true}); }} className={styles.exit} title={"Retour à l'acceuil"} /> {!user.user.owner && ( { alert.alert({ title: "Êtes-vous sûr ?", active: true, message: `Voulez vous quitter le salle ? Cela effacera toute vos données sur les parcours`, type: AlertType.Warning, validate: () => { send({ data: { type: "leave", code: user.user.code, nick: user.user.nick, clientId: sessionStorage.getItem("clientId"), }, }); }, }); }} /> )} {user.user.owner && ( { alert.alert({ title: "Êtes-vous sûr ?", active: true, message: `Voulez vous supprimer le salle ?`, type: AlertType.Warning, validate: () => { deleteRoom({ clientId: isBrowser && sessionStorage.getItem("clientId"), id_code, }) .then((res) => { router.push("/room/", undefined, { shallow: true }); notificationService.success( "Suppression", `Salle "${dataRoom.name}" supprimée`, { keepAfterRouteChange: true } ); }) .catch((err) => { notificationService.error( "Suppression", `Erreur lors de la suppression de la salle "${dataRoom.name}"`, { keepAfterRouteChange: true } ); }); }, }); }} /> )}

{" "} {user.user.owner && (dataRoom.public_result ? ( { togglePublicResult({ id_code: dataRoom.id_code, clientId: sessionStorage.getItem("clientId"), }) .then((res) => { setDataRoom({ ...dataRoom, public_result: res.private, }); }) .catch((err) => { notificationService.error( "Erreur", "Erreur lors de la modification" ); }); }} title="Appuyer pour rendre les résultats privé (les membres ne pourront voir que leurs propres résultats)" /> ) : ( { togglePublicResult({ id_code: dataRoom.id_code, clientId: sessionStorage.getItem("clientId"), }) .then((res) => { setDataRoom({ ...dataRoom, public_result: res.private, }); }) .catch((err) => { notificationService.error( "Erreur", "Erreur lors de la modification" ); }); }} title="Appuyer pour rendre les résultats public (les membres pourront voir les résultats des autres)" /> ))} Parcours{" "}

{user.user.owner && ( )}
{dataRoom.parcours.map((p) => { return (
{ router.push( { pathname: `/room/${dataRoom.id_code}/${p.id_code}`, }, undefined, { shallow: true } ); }} >
{p.is_validate && }

{p.name}

{" "}
{user.user.owner == true && ( { e.stopPropagation(); console.log("CLICK SVG"); alert.alert({ title: "Êtes-vous sûr ?", active: true, message: `Voulez vous supprimer le parcours "${p.name}"`, type: AlertType.Warning, validate: () => { deleteParcours({ id_code: p.id_code, code: user.user.code, }).then((r) => { setDataRoom({ ...dataRoom, parcours: [ ...dataRoom.parcours.filter( (pp) => pp.id_code != r.id_code ), ], }); }); }, }); }} /> )}
); })} {dataRoom.parcours.length == 0 && (

Aucun parcours

)}
Participants{" "} {user.user.owner && (dataRoom.private ? ( { toggleRoomLock({ id_code: dataRoom.id_code, clientId: sessionStorage.getItem("clientId"), }) .then((res) => { setDataRoom({ ...dataRoom, private: res.private }); }) .catch((err) => { notificationService.error( "Erreur", "Erreur lors de la modification" ); }); }} /> ) : ( { toggleRoomLock({ id_code: dataRoom.id_code, clientId: sessionStorage.getItem("clientId"), }) .then((res) => { setDataRoom({ ...dataRoom, private: res.private }); }) .catch((err) => { notificationService.error( "Erreur", "Erreur lors de la modification" ); }); }} /> ))}

En ligne :

{dataRoom.participants .filter((p) => p.online) .map((p) => { return (

{ if (user.user.owner && p.nick != user.user.nick) { alert.alert({ title: "Êtes-vous sûr ?", active: true, message: `Vous aller bannir ${p.nick}`, type: AlertType.Warning, validate: () => { send({ data: { type: "ban", code: p.code, nick: p.nick, status: p.status, }, }); }, }); } }} title="Bannir" className={parseClassName([ user.user.owner ? styles["nick"] : undefined, p.owner || user.user.nick == p.nick ? styles["isOwner"] : undefined, ])} > {p.nick} {p.nick == user.user.nick && isBrowser && localStorage.getItem("token") == null && ( #{user.user.code} )} {p.owner == true && ( Administrateur )} {user.user.owner && p.nick != user.user.nick && p.code.length == 6 && ( #{p.code} )}

); })}

Hors-ligne :

{dataRoom.participants .filter((p) => !p.online) .map((p) => { return (

{ if (user.user.owner && p.nick != user.user.nick) { alert.alert({ title: "Êtes-vous sûr ?", active: true, message: `Vous aller bannir ${p.nick}`, type: AlertType.Warning, validate: () => { send({ data: { type: "ban", code: p.code, nick: p.nick, status: p.status, }, }); }, }); } }} title="Bannir" className={parseClassName([ user.user.owner ? styles["nick"] : undefined, p.owner || user.user.nick == p.nick ? styles["isOwner"] : undefined, ])} > {p.nick} {p.nick == user.user.nick && isBrowser && localStorage.getItem("token") == null && ( #{user.user.code} )} {p.owner == true && ( Administrateur )} {user.user.owner && p.nick != user.user.nick && p.code.length == 6 && ( #{p.code} )}

); })} {dataRoom.waiters.length != 0 && ( <>

En attente :

{dataRoom.waiters.map((p) => { return (

{p.nick} { send({ data: { type: "acceptParticipant", code: p.code, nick: p.nick, status: p.status, }, }); setDataRoom({ ...dataRoom, waiters: dataRoom.waiters.filter( (w) => w.code != p.code ), }); }} /> { send({ data: { type: "refusedParticipant", code: p.code }, }); setDataRoom({ ...dataRoom, waiters: dataRoom.waiters.filter( (w) => w.code != p.code ), }); }} />

); })}{" "} )}
)}
); }