fix bug with end screen and btnCorrectAnswer

This commit is contained in:
Stachu 2023-03-14 21:02:22 +01:00
parent 89ee81f11f
commit 1092ac659b
4 changed files with 21 additions and 26 deletions

View File

@ -136,7 +136,7 @@ export const QuizDataImageFromDb = [
}, },
} }
]; ];
let MAX_POINT = 9;
@ -379,10 +379,10 @@ useEffect(() => {
const buttonsElementArray = Array.from(buttonsElement) const buttonsElementArray = Array.from(buttonsElement)
setIsClick(!isClick); setIsClick(!isClick);
if(score === 9) { if(score === MAX_POINT) {
navigate('/result'); navigate('/result');
} }
if(isClick === true) { if(isClick) {
buttonsElementArray.forEach(button => { buttonsElementArray.forEach(button => {

View File

@ -1,47 +1,45 @@
import { TruckId } from "./Truck"; import { TruckId } from "./Truck";
import { AppContext } from './AppContext'; import { AppContext } from './AppContext';
import React, {useContext, useEffect, useState} from 'react'; import React, {useContext, useEffect, useState, useReducer} from 'react';
import { ButtonShowCorrectAnswer } from "./GetImageAndQuestion"; import { ButtonShowCorrectAnswer } from "./GetImageAndQuestion";
export let EndCacheId = ""; export let EndCacheId = "";
export let EndCorrectAnswer = ""; export let EndCorrectAnswer = "";
let clickcounter = 0;
const FetchData = ({isClick,score,setIsClick,setScore,navigate}) => { const FetchData = ({isClick,score,setIsClick,setScore,navigate}) => {
if(clickcounter < 3)
clickcounter++;
useEffect(() => { useEffect(() => {
fetch(`/random-question?fire-truck=${TruckId}`) fetch(`/random-question?fire-truck=${TruckId}`)
.then(res => res.json()) .then(res => res.json())
.then(dataQuestion => { .then(dataQuestion => {
if(isClick === true) {
const propertValuesObjectFromDb = Object.values(dataQuestion); const propertValuesObjectFromDb = Object.values(dataQuestion);
EndCorrectAnswer = propertValuesObjectFromDb[1];
if(clickcounter === 1)
EndCorrectAnswer = propertValuesObjectFromDb[1];
EndCacheId = propertValuesObjectFromDb[0]; EndCacheId = propertValuesObjectFromDb[0];
if(!isClick){
EndCorrectAnswer = propertValuesObjectFromDb[1];
EndCacheId = propertValuesObjectFromDb[0];
} }
}) })
}) },[isClick])
return ( return (
<> <>
<div className="questionDiv"> <div className="questionDiv">
<h1 className='questionText1'>{EndCorrectAnswer}</h1> <h1 className='questionText1'>{EndCorrectAnswer}</h1>
<button onClick={() => ButtonShowCorrectAnswer(isClick, setIsClick , score , setScore, navigate)} className='NextQuestionButton'>{isClick ? "Pokaż odpowiedź" : "Następne pytanie"}</button> <button onClick={() => ButtonShowCorrectAnswer(isClick, setIsClick , score , setScore, navigate)} className='NextQuestionButton'>{isClick ? "Pokaż odpowiedź" : "Następne pytanie"}</button>
</div> </div>
</> </>
) )
} }
export const QuizGetQuestion = ({score,isClick,setScore,setIsClick,navigate}) => { export const QuizGetQuestion = ({score,isClick,setScore,setIsClick,navigate}) => {
return ( return (
<FetchData score = {score} isClick = {isClick} setScore = {setScore} setIsClick = {setIsClick} navigate = {navigate}/> <FetchData score = {score} isClick = {isClick} setScore = {setScore} setIsClick = {setIsClick} navigate = {navigate}/>

View File

@ -11,12 +11,9 @@ import 'react-circular-progressbar/dist/styles.css';
const Result = () => { const Result = () => {
const navigate = useNavigate(); const navigate = useNavigate();
console.log(endScore); useEffect(() => {
useEffect(() => { if(!SecondSeconds) navigate('/');
if(!endScore) navigate('/'); },[])
},[])
if(endScore) { if(endScore) {
useEffect(() => { useEffect(() => {

View File

@ -1,11 +1,11 @@
export let SecondSeconds = 1; export let SecondSeconds = 0;
export let SecondMinutes = 0; export let SecondMinutes = 0;
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
const Timer = () => { const Timer = () => {
const [time, setTime] = useState(0); const [time, setTime] = useState(0);
let seconds = Math.floor(time % 60); let seconds = Math.floor(time % 60) + 1;
let minutes = Math.floor(time / 60); let minutes = Math.floor(time / 60);
useEffect(() => { useEffect(() => {