Generateurv2/frontend/components/room/RoomPage.jsx

113 lines
3.5 KiB
React
Raw Normal View History

2022-05-18 10:15:54 +02:00
import { useRouter } from "next/router";
import { useContext, useEffect, useState } from "react";
import { useQuery } from "react-query";
import { delRoom, getRoom } from "../../requests/requests.room.js";
import CreateParcours from "./CreateParcours.jsx";
import Parcours from "./Parcours.jsx";
import ParcoursView from "./ParcoursView.jsx";
import RoomView from "./RoomView.jsx";
2022-06-24 13:42:16 +02:00
import { isBrowser, isEmpty } from "../../utils/utils.js";
2022-05-18 10:15:54 +02:00
import useReconnecting from "../../hooks/useReconnecting.jsx";
import useWebsocketConsumer from "../../hooks/useWebsocketConsumer.jsx";
import ParcoursEdit from "./ParcoursEdit.jsx";
import { useWebsocketContext } from "../../context/websocket.context.js";
import Correction from "./Correction.jsx";
2022-06-24 13:42:16 +02:00
import RoomEdit from "./roomEdit.jsx";
import Layout from "../Layout.js";
2022-05-18 10:15:54 +02:00
export default function RoomPage({ id_code, user }) {
const [joined, setJoined] = useState(false);
const router = useRouter();
const {
ws,
send,
connect,
addMessageHandler,
removeMessageHandler,
isConnected,
} = useWebsocketContext();
useEffect(() => {
2022-06-24 13:42:16 +02:00
if (isConnected == false && isBrowser) {
2022-07-02 17:48:50 +02:00
console.log('CONNECTED')
2022-06-25 00:26:07 +02:00
connect("/ws/room/" + id_code);
2022-05-18 10:15:54 +02:00
send({
data: {
type: "reconnect",
clientId: sessionStorage.getItem("clientId"),
},
});
}
2022-06-24 13:42:16 +02:00
}, [isConnected]);
2022-05-18 10:15:54 +02:00
2022-06-24 13:42:16 +02:00
2022-05-18 10:15:54 +02:00
return (
2022-06-11 23:39:03 +02:00
<>
2022-05-18 10:15:54 +02:00
{ws == null && user == undefined && (
2022-06-24 13:42:16 +02:00
<Layout page={'Chargement'}>
2022-05-18 10:15:54 +02:00
<div className="loader"></div>
2022-06-24 13:42:16 +02:00
</Layout>
2022-05-18 10:15:54 +02:00
)}
2022-06-24 13:42:16 +02:00
{isConnected == true&& user != undefined && (
2022-05-18 10:15:54 +02:00
<>
{router.query.mode.length == 1 && (
<RoomView
user={user}
id_code={id_code}
/>
)}
{router.query.mode[1] == "new_parcours" && (
<CreateParcours roomCode={id_code} ws={ws} user={user.user} />
)}
{router.query.mode.length == 3 &&
router.query.mode[2] == "challenge" && (
<Parcours
parcours_id={router.query.mode[1]}
roomCode={id_code}
user={user.user}
/>
)}
{router.query.mode.length == 3 &&
router.query.mode[2] == "correct" &&
router.query.c && (
<Correction
owner={user.user.owner}
2022-06-24 13:42:16 +02:00
room_code={router.query.mode[0]}
2022-05-18 10:15:54 +02:00
user_code={router.query.c.split("_")[0]}
correct_code={router.query.c.split("_")[1]}
parcours_id={router.query.mode[1]}
/>
)}
{router.query.mode[2] == "edit" && router.query.mode.length == 3 && (
<ParcoursEdit
parcours_id={router.query.mode[1]}
room_code={id_code}
user={user.user}
2022-06-24 13:42:16 +02:00
/>
)}
{router.query.mode[1] == "edit" && router.query.mode.length == 2 && (
<RoomEdit
id_code={id_code}
user={user.user}
2022-05-18 10:15:54 +02:00
/>
)}
{router.query.mode.length == 2 &&
router.query.mode[1] != "new_parcours" &&
router.query.mode[1] != "edit" && (
<ParcoursView
parcours_id={router.query.mode[1]}
room_code={id_code}
user={user.user}
addMessageHandler={addMessageHandler}
removeMessageHandler={removeMessageHandler}
/>
)}
</>
)}
2022-06-11 23:39:03 +02:00
</>
2022-05-18 10:15:54 +02:00
);
}