import { useQuery } from "react-query"; import { getCorrectionInfo, getParcoursInfos, } from "../../requests/requests.room.js"; import styles from "../../styles/room/parcoursView.module.scss"; import { useRouter } from "next/router"; import Note from "./Note.jsx"; import { useContext, useEffect, useState } from "react"; import { useWebsocketContext } from "../../context/websocket.context.js"; import Correction from "./Correction.jsx"; import Layout from "../Layout.js"; import annotationPlugin from "chartjs-plugin-annotation"; import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, } from "chart.js"; import { Line, Scatter } from "react-chartjs-2"; import { parseClassName, parseDate, parseTimer } from "../../utils/utils.js"; import { FaRegEdit } from "react-icons/fa"; import { MdKeyboardArrowDown } from "react-icons/md"; import autocolors from "chartjs-plugin-autocolors"; import chroma from "chroma-js"; import Back from "./back.jsx"; ChartJS.register( CategoryScale, LinearScale, PointElement, LineElement, Title, Tooltip, Legend, annotationPlugin, autocolors ); export default function ParcoursView({ parcours_id, room_code, user }) { const [parcours, setParcours] = useState(); const { addMessageHandler, removeMessageHandler } = useWebsocketContext(); useEffect(() => { if (!parcours) { getParcoursInfos(parcours_id, user.code) .then((res) => { setParcours(res); }) .catch((err) => { router.push({ pathname: "/room/" + room_code }); }); } }, []); useEffect(() => { let handler = (e) => { let data = JSON.parse(e.data); let type = data.type; switch (type) { case "challenge_parcours": { if (parcours) { if (data.id_code == parcours.id_code) { let oldChallenger = parcours.challenger; let challenger = parcours.challenger.filter( (c) => c.code == data.participant.code ); if (challenger.length == 0) { setParcours({ ...parcours, challenger: [ ...oldChallenger, { ...data.participant, exos: [data.exos], validate: data.validate, moyenne: data.moyenne }, ], }); } else { setParcours({ ...parcours, challenger: [ ...oldChallenger.map((c) => { if (c.code == data.participant.code) { return { ...c, exos: [...challenger[0].exos, data.exos], validate: data.validate, moyenne: data.moyenne, }; } return c })] }); } } } break; } case "delete_parcours": { if (parcours) { if (data.id_code == parcours.id_code) { router.push({ pathname: "/room/" + room_code }); } } break; } case "edit_parcours": { if (parcours) { let new_parcours = data.parcours; if (new_parcours.id_code == parcours_id) { getParcoursInfos(parcours_id, user.code) .then((res) => { setParcours(res); }) .catch((err) => { router.push({ pathname: "/room/" + room_code }); }); } } break; } default: return } }; addMessageHandler(handler); return () => { removeMessageHandler(handler); }; }, [parcours]); const options = { scales: { x: { ticks: { callback: function (label, index, labels) { return parseDate(new Date(label)); }, }, }, y: { suggestedMin: 0, suggestedMax: 20, }, }, responsive: true, plugins: { autocolors: { customize(context) { const colors = context.colors; return { background: chroma(colors.background).alpha(1.5), border: chroma(colors.border).alpha(1.5), }; }, }, annotation: { annotations: { line1: { type: "line", yMin: 10, yMax: 10, borderColor: "rgba(255, 99, 132, 0.6)", borderWidth: 1, }, }, }, tooltip: { callbacks: { label: (context) => { return ( parseDate(new Date(parseInt(context.parsed.x))) + ", " + context.formattedValue ); }, }, }, legend: { position: "bottom", title: { display: true, text: "Legende, clickez sur un nom pour cacher/montrer la courbe", padding: 10, color: chroma("white").alpha(0.7), }, }, title: { display: false, }, }, }; const [chartData, setChartData] = useState({}); useEffect(() => { if (parcours) { var values = parcours.challenger.map((c) => { return { label: c.nick, hidden: c.code != user.code, //borderColor: colors, showLine: true, data: c.exos .sort((a, b) => { return new Date(a.endAt) - new Date(b.endAt); }) .map((e) => { var date = new Date(e.endAt); return { x: date.getTime(), y: (e.note.value * 20) / e.note.total, }; }), }; }); setChartData({ datasets: values, }); } }, [parcours]); const router = useRouter(); const [dataCorrection, setDataCorrection] = useState(); const [onglet, setOnglet] = useState("tab"); useEffect(() => { window.matchMedia("(max-width: 840px)").addEventListener("change", (e) => { if (onglet == "graph") { setOnglet("tab"); } }); }, []); const [active, setActive] = useState(-1); return (
{parcours && (

{parcours.name} {" "} {user.owner && ( { router.push( { pathname: `/room/${room_code}/${parcours_id}/edit`, }, undefined, { shallow: true } ); }} /> )}

)} {parcours && !dataCorrection && (
{ setOnglet("tab"); }} className={ onglet == "tab" ? styles["onglet-active"] : undefined } > Notes
{ setOnglet("graph"); }} className={ onglet == "graph" ? styles["onglet-active"] : undefined } > Graph
{onglet == "graph" && chartData && chartData.datasets && (
)} {onglet == "tab" && parcours && (
{parcours.challenger.length == 0 && (

Aucun essai effectué :(

)} {parcours.challenger.sort((a,b)=>{return b.moyenne.value - a.moyenne.value}).map((c) => { return (

{ setActive( active == parcours.challenger.indexOf(c) ? -1 : parcours.challenger.indexOf(c) ); }} > {c.nick} {c.moyenne && active != parcours.challenger.indexOf(c)&&( <> {" "}( = parcours.success_condition ? styles["success"] : styles["fail"] } > {c.moyenne.value}/{c.moyenne.total}{" "} {c.moyenne.total != 20 && `= ${ Math.round( ((c.moyenne.value * 20) / c.moyenne.total) * 100 ) / 100 }/20`} ){" "} )}

{active == parcours.challenger.indexOf(c) && ( <> {c.moyenne && (

Moyenne:{" "} = parcours.success_condition ? styles["success"] : styles["fail"] } > {c.moyenne.value}/{c.moyenne.total}{" "} {c.moyenne.total != 20 && `= ${ Math.round( ((c.moyenne.value * 20) / c.moyenne.total) * 100 ) / 100 }/20`}{" "}

)} {c.exos .sort((a, b) => { return ( new Date(b.endAt) - new Date(a.endAt) ); }) .map((e) => { return (

= parcours.success_condition ? styles["success"] : styles["fail"] : styles["noTrust"] } > {e.note.value}/{e.note.total} - {parseTimer(e.timer)} {" "} {e.canCorrige && ( { router.push( { pathname: `/room/${room_code}/${parcours_id}/correct/`, query: { c: `${c.code}_${e.code}`, }, }, undefined, { shallow: true, } ); }} > {user.owner ? "Corriger" : "Voir la correction"} )} {!e.canCorrige && ( Correction indisponible )}

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