stuff
This commit is contained in:
parent
9eb487035b
commit
7d9cedfe7f
2 changed files with 14 additions and 4 deletions
|
|
@ -2,7 +2,7 @@ import { useState, useEffect, useCallback } from 'react';
|
||||||
import { roomsApi } from '../services/api';
|
import { roomsApi } from '../services/api';
|
||||||
import socketService from '../services/socket';
|
import socketService from '../services/socket';
|
||||||
|
|
||||||
export const useRoom = (roomCode) => {
|
export const useRoom = (roomCode, onGameStarted = null) => {
|
||||||
const [room, setRoom] = useState(null);
|
const [room, setRoom] = useState(null);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [error, setError] = useState(null);
|
const [error, setError] = useState(null);
|
||||||
|
|
@ -45,6 +45,10 @@ export const useRoom = (roomCode) => {
|
||||||
|
|
||||||
const handleGameStarted = (updatedRoom) => {
|
const handleGameStarted = (updatedRoom) => {
|
||||||
setRoom(updatedRoom);
|
setRoom(updatedRoom);
|
||||||
|
// Вызываем callback для навигации на страницу игры
|
||||||
|
if (onGameStarted) {
|
||||||
|
onGameStarted(updatedRoom);
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
const handleAnswerRevealed = (data) => {
|
const handleAnswerRevealed = (data) => {
|
||||||
|
|
@ -86,7 +90,7 @@ export const useRoom = (roomCode) => {
|
||||||
socketService.off('questionChanged', handleQuestionChanged);
|
socketService.off('questionChanged', handleQuestionChanged);
|
||||||
socketService.off('gameEnded', handleGameEnded);
|
socketService.off('gameEnded', handleGameEnded);
|
||||||
};
|
};
|
||||||
}, [roomCode]);
|
}, [roomCode, onGameStarted]);
|
||||||
|
|
||||||
const createRoom = useCallback(async (hostId, questionPackId, settings = {}) => {
|
const createRoom = useCallback(async (hostId, questionPackId, settings = {}) => {
|
||||||
try {
|
try {
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState, useCallback } from 'react';
|
||||||
import { useParams, useNavigate } from 'react-router-dom';
|
import { useParams, useNavigate } from 'react-router-dom';
|
||||||
import { useAuth } from '../context/AuthContext';
|
import { useAuth } from '../context/AuthContext';
|
||||||
import { useRoom } from '../hooks/useRoom';
|
import { useRoom } from '../hooks/useRoom';
|
||||||
|
|
@ -11,6 +11,12 @@ const RoomPage = () => {
|
||||||
const { roomCode } = useParams();
|
const { roomCode } = useParams();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
const { user, loginAnonymous, loading: authLoading } = useAuth();
|
const { user, loginAnonymous, loading: authLoading } = useAuth();
|
||||||
|
|
||||||
|
// Callback для автоматической навигации при старте игры
|
||||||
|
const handleGameStartedEvent = useCallback(() => {
|
||||||
|
navigate(`/game/${roomCode}`);
|
||||||
|
}, [navigate, roomCode]);
|
||||||
|
|
||||||
const {
|
const {
|
||||||
room,
|
room,
|
||||||
participants,
|
participants,
|
||||||
|
|
@ -19,7 +25,7 @@ const RoomPage = () => {
|
||||||
joinRoom,
|
joinRoom,
|
||||||
startGame,
|
startGame,
|
||||||
updateQuestionPack,
|
updateQuestionPack,
|
||||||
} = useRoom(roomCode);
|
} = useRoom(roomCode, handleGameStartedEvent);
|
||||||
const [qrCode, setQrCode] = useState('');
|
const [qrCode, setQrCode] = useState('');
|
||||||
const [joined, setJoined] = useState(false);
|
const [joined, setJoined] = useState(false);
|
||||||
const [isQRModalOpen, setIsQRModalOpen] = useState(false);
|
const [isQRModalOpen, setIsQRModalOpen] = useState(false);
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue