'use client'; import { useEffect, useState } from 'react' import { Dialog, DialogPanel } from '@headlessui/react' import { Bars3Icon, XMarkIcon } from '@heroicons/react/24/outline' import Logo from '../../components/logo'; import { CheckIcon } from '@heroicons/react/20/solid'; import { SessionState } from '@/app/lib/session'; import { AnswerType, Question, getQuestions } from '@/app/lib/question'; import QuestionView from '@/app/components/question_component'; import ResultScreen from '@/app/results/page'; const Page = ({ params }: { params: { category: string }}) => { const [mobileMenuOpen, setMobileMenuOpen] = useState(false) const [state, setState]: [SessionState, any] = useState( { answered: 0, wrong: 0, right: 0, category: '', opened: [], answer: [], index: 0 } ); // const category = params.category; // const [all_questions, set_all_questions] = useState([]); const [questions, setQuestions] = useState([]); useEffect( () => { // console.log( params.category ); const fetch_all_questions = async () => { let result = await getQuestions(); console.log( `all_questions: ${ JSON.stringify( result ) }` ); // set_all_questions( result ); result = result.filter( q => q.category === params.category) .map(it=>({...it, answered: AnswerType.Unset})); //result = result.filter setQuestions( result ); console.log( `q12 uestions: ${ JSON.stringify( questions ) }`, result ); } fetch_all_questions(); var ans: AnswerType[]; questions?.forEach(q => { ans[ q.id ] = AnswerType.Unset; }); /* setState( (prevState: SessionState) => ({ ...prevState, answer: ans })); */ }, [ params.category ]); console.log( "questions16", questions ); console.log( "state15", state ); // alternate version (show all of them at the same time): // {/* {questions.map( (q) => (q.answered == AnswerType.Unset) ? : <>)} */} const index = state.answered; console.log( "index18", index ) return (
) } export default Page;