"use client"; import { getNextQuizWinnners, getQuizWinnersById, Queries, } from "@/api/queries"; import { v4 } from "uuid"; import { useState, useEffect, useContext } from "react"; import { Datum, IQuizQuestionsWinners, } from "@/models/quizQuestionsWinners.model"; import QuizContext from "@/context/QuizContext"; import { useQuizSearchActive } from "@/store/store"; interface IProps { quizId: number; quizFinished: boolean; smsNumber: string; } const QuizWinnerTable = ({ quizId, quizFinished, smsNumber }: IProps) => { const [winnersData, setWinnersData] = useState([]); const { questionsData } = useContext(QuizContext).quizQuestionsContext; const [winnersTotal, setWinnersTotal] = useState(0); const [nextPageQueries, setQueries] = useState<{ limit: number; offset: number; }>({ limit: 0, offset: 0, }); const { quizSearchData } = useContext(QuizContext).quizSearchContext; const { active } = useQuizSearchActive(); async function getData(next?: boolean) { if (next) { const res = await getNextQuizWinnners( quizId, nextPageQueries.limit, nextPageQueries.offset ); if (res) { setWinnersData([...winnersData, ...res.data]); setQueries({ limit: res?.meta.per_page, offset: nextPageQueries.offset + res?.meta.per_page, }); } } else { const res = await getQuizWinnersById(quizId); if (res) { setWinnersTotal(res.meta.total); setWinnersData(res.data); setQueries({ limit: res?.meta.per_page, offset: nextPageQueries.offset + res?.meta.per_page, }); } } } useEffect(() => { if (!active) { // Queries.getQuizWinners(quizId).then((res) => { // setWinnersData(res); // }); getData(); } else if (active) { setWinnersData([]); } }, [quizId, active]); return quizSearchData?.data || (winnersData && winnersData?.length) !== 0 ? (

Bäsleşigiň jemi

{/* Desktop table */}
{/* Table Head */}
{winnersData[0].client_id || quizSearchData?.data ? (
Ýeri
) : null} {winnersData[0].phone || quizSearchData?.data ? (
Telefon beligisi
) : null} {winnersData[0].answers.length || quizSearchData?.data ? (
Jogap beriş nobatlary
) : null} {winnersData[0].total_score_of_client || quizSearchData?.data ? (
Nobatlaryň jemi
) : null} {winnersData[0].total_score_of_client || quizSearchData?.data ? (
Utuklaryň jemi
) : null}
{/* Table Body */}
{winnersData ? winnersData.map((winner, id) => (
{id > 0 && winner.correct_answers_time === winnersData[id - 1].correct_answers_time ? id : id + 1}
{winnersData[0].phone ? (
+{winner.phone}
) : null} {winnersData[0].answers.length !== 0 ? (
{questionsData ? questionsData.map((question) => { const matchingAnswer = winner.answers.find( (answer) => answer.question_id === question.id && answer.score > 0 ) || winner.answers.find( (answer) => answer.question_id === question.id ); return ( {matchingAnswer && matchingAnswer.score !== 0 ? matchingAnswer.serial_number_for_correct : matchingAnswer && matchingAnswer?.score === 0 ? "X" : "0"} ); }) : null}
) : null} {winnersData[0].total_score_of_client ? (
{winner.correct_answers_time}
) : null} {winnersData[0].total_score_of_client ? (
{winner.total_score_of_client}
) : null}
)) : quizSearchData && (
{/* Place of the client */}
{quizSearchData.result.place}
{/* Client phone number */}
+ {Object.keys(quizSearchData.data).map( (questionId, i) => i === 0 && quizSearchData.data[questionId].answers[0].client )}
{/* Serial number answer to questions */}
{Object.keys(quizSearchData.data) .map((quistionId) => quizSearchData.data[quistionId]) .map((question) => { const matchingAnswer = question.answers[0]; return ( {matchingAnswer && matchingAnswer.score !== 0 ? matchingAnswer.serial_number_for_correct : matchingAnswer && matchingAnswer?.score === 0 ? "X" : "0"} ); })}
{quizSearchData.result.total_serial}
{quizSearchData.result.total_score}
)}
{/* Mobile table */}
{/* Table Head */}
{winnersData[0].client_id || quizSearchData?.data ? (
Ýeri
) : null} {winnersData[0].phone || quizSearchData?.data ? (
Telefon beligisi
) : null} {winnersData[0].total_score_of_client || quizSearchData?.data ? (
Nobatlaryň jemi
) : null} {winnersData[0].total_score_of_client || quizSearchData?.data ? (
Utuklaryň jemi
) : null}
{/* Table Body */}
{winnersData ? winnersData.map((winner, id) => (
{id + 1}
{winnersData[0].phone ? (
+{winner.phone}
) : null} {winnersData[0].total_score_of_client ? (
{winner.correct_answers_time}
) : null} {winnersData[0].total_score_of_client ? (
{winner.total_score_of_client}
) : null}
{winnersData[0].answers.length !== 0 ? (
Jogap beriş nobatlary:
) : null} {winnersData[0].answers.length !== 0 ? (
{questionsData ? questionsData.map((question) => { const matchingAnswer = winner.answers.find( (answer) => answer.question_id === question.id && answer.score > 0 ) || winner.answers.find( (answer) => answer.question_id === question.id ); return ( {matchingAnswer && matchingAnswer.score !== 0 ? matchingAnswer.serial_number_for_correct : matchingAnswer && matchingAnswer?.score === 0 ? "X" : "0"} ); }) : null}
) : null}
)) : quizSearchData && (
{quizSearchData.result.place}
+ {Object.keys(quizSearchData.data).map( (questionId, i) => i === 0 && quizSearchData.data[questionId].answers[0] .client )}
{quizSearchData.result.total_serial}
{quizSearchData.result.total_score}
Jogap beriş nobatlary:
{Object.keys(quizSearchData.data) .map( (quistionId) => quizSearchData.data[quistionId] ) .map((question) => { const matchingAnswer = question.answers[0]; return ( {matchingAnswer && matchingAnswer.score !== 0 ? matchingAnswer.serial_number_for_correct : matchingAnswer && matchingAnswer?.score === 0 ? "X" : "0"} ); })}
)}
{winnersData.length < winnersTotal && ( )} {/* 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;