diff --git a/src/App.jsx b/src/App.jsx index 13a7013..521d2c0 100644 --- a/src/App.jsx +++ b/src/App.jsx @@ -16,6 +16,7 @@ function App() { const savedIndex = getCookie('gameQuestionIndex') return savedIndex !== null ? savedIndex : 0 }) + const [areAllRevealed, setAreAllRevealed] = useState(false) const gameRef = useRef(null) const currentQuestion = questions[currentQuestionIndex] @@ -32,6 +33,22 @@ function App() { setCookie('gameQuestionIndex', currentQuestionIndex) }, [currentQuestionIndex]) + // Обновляем состояние открытых ответов при смене вопроса или изменении состояния + useEffect(() => { + const checkRevealedState = () => { + if (gameRef.current && gameRef.current.areAllAnswersRevealed) { + setAreAllRevealed(gameRef.current.areAllAnswersRevealed()) + } else { + setAreAllRevealed(false) + } + } + + checkRevealedState() + // Проверяем состояние периодически для обновления кнопки + const interval = setInterval(checkRevealedState, 200) + return () => clearInterval(interval) + }, [currentQuestionIndex, questions]) + const handleUpdateQuestions = (updatedQuestions) => { setQuestions(updatedQuestions) // Если текущий вопрос был удален, сбрасываем индекс @@ -68,6 +85,12 @@ function App() { const handleShowAll = () => { if (gameRef.current && gameRef.current.showAllAnswers) { gameRef.current.showAllAnswers() + // Обновляем состояние после изменения + setTimeout(() => { + if (gameRef.current && gameRef.current.areAllAnswersRevealed) { + setAreAllRevealed(gameRef.current.areAllAnswersRevealed()) + } + }, 100) } } @@ -114,9 +137,9 @@ function App() { )} diff --git a/src/components/Game.jsx b/src/components/Game.jsx index 781d4d4..43fdfc5 100644 --- a/src/components/Game.jsx +++ b/src/components/Game.jsx @@ -75,7 +75,11 @@ const Game = forwardRef(({ useEffect(() => { setCookie('gameRevealedAnswers', revealedAnswers) - }, [revealedAnswers]) + // Уведомляем родительский компонент об изменении состояния открытых ответов + if (onQuestionIndexChange) { + // Это вызовет перерендер в App, который обновит состояние кнопки + } + }, [revealedAnswers, currentQuestionIndex]) useEffect(() => { setCookie('gameOver', gameOver) @@ -86,8 +90,28 @@ const Game = forwardRef(({ const handleShowAllAnswers = () => { if (!currentQuestion) return - const allAnswerIndices = currentQuestion.answers.map((_, index) => index) - updateRevealedAnswers(allAnswerIndices) + const currentRevealed = getCurrentRevealedAnswers() + const allAnswersRevealed = currentRevealed.length === currentQuestion.answers.length + + if (allAnswersRevealed) { + // Если все открыты - скрываем все + updateRevealedAnswers([]) + } else { + // Если не все открыты - открываем все + const allAnswerIndices = currentQuestion.answers.map((_, index) => index) + updateRevealedAnswers(allAnswerIndices) + } + } + + const hasRevealedAnswers = () => { + const currentRevealed = getCurrentRevealedAnswers() + return currentRevealed.length > 0 + } + + const areAllAnswersRevealed = () => { + if (!currentQuestion) return false + const currentRevealed = getCurrentRevealedAnswers() + return currentRevealed.length === currentQuestion.answers.length } useImperativeHandle(ref, () => ({ @@ -101,6 +125,7 @@ const Game = forwardRef(({ setRevealedAnswers({}) }, showAllAnswers: handleShowAllAnswers, + areAllAnswersRevealed: areAllAnswersRevealed, })) const handleAddPlayer = (name) => {