import { useRouter } from "next/router"; import { useEffect, useLayoutEffect, useRef, useState } from "react"; import { useQuery } from "react-query"; import { challengeParcours, getParcoursChallenge, } from "../../requests/requests.room.js"; import { countOccurences, isEmpty, parseTimer } from "../../utils/utils.js"; import InputExo from "./InputExo.jsx"; import styles from "../../styles/room/parcours.module.scss"; import ParcoursExo from "./ParcoursExo.jsx"; import Layout from "../Layout.js"; import { notificationService } from "../../services/notification.service.js"; import { MdRefresh } from "react-icons/md"; export default function Parcours({ parcours_id, user, roomCode }) { const router = useRouter(); const [exos, setExos] = useState([]); const [isCorr, setCorr] = useState({ isCorr: false, DOM: false }); //DOM pour useEffect 2 fois sinon les data-x apparaissent pas bien :( const [timer, setTimer] = useState(data && data.timer * 60); const [correctId, setCorrectId] = useState(); const [data, setData] = useState(); useEffect(() => { if (!data || isEmpty(data)) { getParcoursChallenge(parcours_id, user.code).then((res) => { setData(res); setTimer(res.timer * 60); setCorrectId(res.correctId); }); } return () => {}; }, [data]); const ref = useRef(); useEffect(() => { if (data && timer > 0 && !isCorr.isCorr) { const time = setTimeout(() => { setTimer(timer - 1); }, 1000); return () => clearTimeout(time); } else if (data && timer <= 0) { ref.current.click(); } }, [timer]); const formRef = useRef(); const first = useRef(true); const [note, setNote] = useState({ value: null, total: null, isTrust: true }); useLayoutEffect(() => { if (first.current) { first.current = false; return; } if (isCorr.isCorr == true && isCorr.DOM == false) { setCorr({ isCorr: true, DOM: true }); } if (isCorr.isCorr == true && isCorr.DOM == true) { var target = formRef.current; const resultList = []; for (var i = 0, element; (element = target[i++]); ) { if (element.type == "text") { resultList.push({ idInput: element.getAttribute("data-idinput"), idExo: element.getAttribute("data-idexo"), idCalcul: element.getAttribute("data-idcalcul"), value: element.value, }); } } var exosSend = data.exos.map((ex) => { var inputsExos = resultList.filter((e) => e.idExo == ex.order); return { ...ex, exos: ex.exos.map((e) => { return { ...e, inputs: e.inputs.map((i) => { return { ...i, value: inputsExos.filter((inp) => { return inp.idCalcul == e.order && inp.idInput == i.order; })[0].value, }; }), }; }), }; }); challengeParcours({ user_code: user.code, exos: { result: exosSend, timer: data.timer * 60 - timer, }, id_code: parcours_id, correct_code: correctId, }) .then((res) => { setData({ ...data, exos: res.exos }); setNote(res.note); }) .catch((err) => { notificationService.error("Erreur", "Réessayez plus tart"); }); } return () => {}; }, [isCorr]); return (
{data && ( <>

{data.name} { setData(); setCorr({ isCorr: false, DOM: false }); setNote({ value: null, total: null, isTrust: true }); }} />

{" "} {note.value !== null && note.total !== null && (

= data.success_condition ? styles["success"] : styles["fail"] : styles["notTrust"] } > {note.value}/{note.total} {note.total != 20 && ` = ${ Math.round(((note.value * 20) / note.total) * 100) / 100 } / 20`} {/* Round 2 decimals */} {!note.isTrust && " (Note provisoire)"}

)}

{parseTimer(timer)}

{" "}
{ e.preventDefault(); setCorr({ isCorr: true, DOM: false }); }} > {data.exos && data.exos .sort((a, b) => { return a["order "] - b["order"]; }) .map((exo, i) => { return (

Exercice {exo.order + 1}

{exo.exos .sort((a, b) => { return a.order - b.order; }) .map((ex) => { return ( ); })}
); })}
{!isCorr.isCorr && ( )} {isCorr.isCorr && ( )}
)}
{" "}
); }