'use client'; import { Queries } from '@/api/queries'; import { v4 } from 'uuid'; import { useState, useEffect, useContext, Dispatch, SetStateAction } from 'react'; import { Answer, IQuizQuestionsWinners } from '@/models/quizQuestionsWinners.model'; import QuizContext from '@/context/QuizContext'; import { IQuizQuestions } from '@/models/quizQuestions.model'; interface Message { answer: string; score: number; date: string; serial_number: number; serial_number_for_correct: number; starred_src: string; quiz_id: number; question_id: number; } interface Winner { total_score_of_client: string; correct_answers_time: string; client_id: number; client: { id: number; phone: string; answers: { id: number; question_id: number; score: number; serial_number_for_correct: number; client_id: number; }[]; }; } interface IProps { quizId: number; quizFinished: boolean; smsNumber: string; } const QuizWinnerTable = ({ quizId, quizFinished, smsNumber }: IProps) => { // const [questionsData, setQuestionsData] = useState(); const [winnersData, setWinnersData] = useState(); const { questionsData } = useContext(QuizContext).quizQuestionsContext; const [socket, setSocket] = useState(null); const [isConnected, setIsConnected] = useState(false); useEffect(() => { Queries.getQuizWinners(quizId).then((res) => { setWinnersData(res); }); }, [quizId]); // useEffect(() => { // let socket: WebSocket | null = null; // let reconnectTimeout: NodeJS.Timeout | null = null; // let pingInterval: NodeJS.Timeout | null = null; // const connectWebSocket = () => { // try { // socket = new WebSocket(`wss://sms.turkmentv.gov.tm/ws/quiz?dst=${smsNumber}`); // setSocket(socket); // socket.onopen = () => { // console.log('WebSocket is connected'); // setIsConnected(true); // pingInterval = setInterval(() => { // if (socket?.readyState === WebSocket.OPEN) { // try { // socket.send(JSON.stringify({ type: 'ping' })); // } catch (error) { // console.error('Error sending ping:', error); // } // } // }, 25000); // Ping every 25 seconds // }; // socket.onmessage = (event) => { // try { // console.log('Message received from WebSocket:', event.data); // const message = JSON.parse(event.data); // handleOnMessage(message); // } catch (error) { // console.error('Error processing message:', error); // } // }; // socket.onerror = (error) => { // console.error('WebSocket error:', error); // }; // socket.onclose = () => { // console.log('WebSocket is closed'); // setIsConnected(false); // if (pingInterval) { // clearInterval(pingInterval); // } // if (!reconnectTimeout) { // reconnectTimeout = setTimeout(() => { // console.log('Attempting to reconnect WebSocket...'); // connectWebSocket(); // }, 5000); // Reconnect after 5 seconds // } // }; // } catch (error) { // console.error('WebSocket connection error:', error); // } // }; // if (smsNumber && winnersData) { // connectWebSocket(); // } // return () => { // if (socket) { // socket.close(); // } // if (reconnectTimeout) { // clearTimeout(reconnectTimeout); // } // if (pingInterval) { // clearInterval(pingInterval); // } // }; // }, [smsNumber]); // Function to handle incoming WebSocket message and update winnersData const handleOnMessage = (message: Message) => { if (!winnersData) { console.error('winnersData is undefined'); return; } console.log('updating winnersData'); // Update the winnersData by matching phone with starred_src from the message setWinnersData((prevWinnersData) => { if (!prevWinnersData) { return prevWinnersData; } return { ...prevWinnersData, data: prevWinnersData.data.map((winner) => { if (winner.client.phone === message.starred_src) { const updatedAnswers = [ ...winner.client.answers, { id: message.question_id, question_id: message.question_id, score: message.score, serial_number_for_correct: message.serial_number_for_correct, client_id: winner.client.id, }, ]; // Calculate the new correct_answers_time by summing serial_number_for_correct const updatedCorrectAnswersTime = updatedAnswers .reduce((sum, answer) => sum + answer.serial_number_for_correct, 0) .toString(); return { ...winner, client: { ...winner.client, answers: updatedAnswers, }, total_score_of_client: ( parseInt(winner.total_score_of_client) + message.score ).toString(), correct_answers_time: updatedCorrectAnswersTime, // Update correct_answers_time }; } return winner; }), }; }); console.log('winnersData is updated'); }; return winnersData?.data.length !== 0 ? (

Bäsleşigiň jemi

{/* Desktop table */}
{/* Table Head */}
{winnersData?.data[0].client_id ? (
) : null} {winnersData?.data[0].client.phone ? (
Gatnaşyjynyň tel. Beligisi
) : null} {winnersData?.data[0].client.answers.length !== 0 ? (
Soraglara jogap berilişiň nobaty
) : null} {winnersData?.data[0].total_score_of_client ? (
Nobatlaryň jemi
) : null} {winnersData?.data[0].total_score_of_client ? (
Utuklaryň jemi
) : null}
{/* Table Body */}
{winnersData?.data.map((winner, id) => (
{id + 1}
{winnersData.data[0].client.phone ? (
+{winner.client.phone}
) : null} {winnersData.data[0].client.answers.length !== 0 ? (
{questionsData ? questionsData.map((question) => { // const matchingAnswer = winner.client.answers.find( // (answer) => answer.question_id === question.id, // ); const matchingAnswer = winner.client.answers.find( (answer) => answer.question_id === question.id && answer.score > 0, ) || winner.client.answers.find( (answer) => answer.question_id === question.id, ); // const matchingAnswer = () => { // const duplicateAnswers: Answer[] = []; // winner.client.answers.map((answer) => // answer.question_id === question.id // ? duplicateAnswers.push(answer) // : null, // ); // console.log(duplicateAnswers); // }; return ( {matchingAnswer && matchingAnswer.serial_number_for_correct !== 0 ? matchingAnswer.serial_number_for_correct : matchingAnswer && matchingAnswer?.serial_number_for_correct === 0 ? 'X' : '0'} ); }) : null}
) : null} {winnersData.data[0].total_score_of_client ? (
{winner.correct_answers_time}
) : null} {winnersData.data[0].total_score_of_client ? (
{winner.total_score_of_client}
) : null}
))}
{/* Mobile table */}
{/* Table Head */}
{winnersData?.data[0].client_id ? (
) : null} {winnersData?.data[0].client.phone ? (
Gatnaşyjynyň tel. Beligisi
) : null} {winnersData?.data[0].total_score_of_client ? (
Soraglara jogap berilişiň nobaty
) : null} {winnersData?.data[0].total_score_of_client ? (
Nobatlaryň jemi
) : null}
{/* Table Body */}
{winnersData?.data.map((winner, id) => (
{id + 1}
{winnersData.data[0].client.phone ? (
+{winner.client.phone}
) : null} {winnersData.data[0].total_score_of_client ? (
{winner.correct_answers_time}
) : null} {winnersData.data[0].total_score_of_client ? (
{winner.total_score_of_client}
) : null}
{winnersData?.data[0].client.answers.length !== 0 ? (
Soraglara näçinji jogap berdi :
) : null} {winnersData.data[0].client.answers.length !== 0 ? (
{questionsData ? questionsData.map((question) => { const matchingAnswer = winner.client.answers.find( (answer) => answer.question_id === question.id, ); return ( {matchingAnswer && matchingAnswer.serial_number_for_correct !== 0 ? matchingAnswer.serial_number_for_correct : matchingAnswer && matchingAnswer.serial_number_for_correct === 0 ? 'X' : '0'} ); }) : null}
) : null}
))}
{/* Rules block */}

Belgileriň düşündirilişi

100
Bäsleşikde gazanylan utuklaryň jemi
100
Soraga jogaplaryň tertip belgisiniň jemi
1
Dogry jogaplara näçinji bolup jogap berdi
X
Soraga nädogry jogap berdi
0
Soraga jogap ugratmady
) : null; }; export default QuizWinnerTable;