contextProvider
This commit is contained in:
@@ -4,7 +4,7 @@ import HandleNavigate from "../../utils/HandleNavigate/HandleNavigate"
|
||||
import Navbar from "../../components/Navbar/Navbar.jsx"
|
||||
import Background from "../../assets/backgrounds/Background.jsx"
|
||||
import useRequireAuth from "../../hooks/useRequireAuth.jsx"
|
||||
import { useGameWebSocket } from "../../hooks/useGameWebSocket.js"
|
||||
import { useGameWebSocketContext } from "../../contexts/GameWebSocketContext"
|
||||
import { startGame } from "../../api/gameApi.js"
|
||||
|
||||
const Lobby = () => {
|
||||
@@ -20,7 +20,9 @@ const Lobby = () => {
|
||||
const gameCodeFromState = location.state?.gameCode
|
||||
const gameToken = localStorage.getItem('gameToken')
|
||||
|
||||
// Use the shared WebSocket context
|
||||
const {
|
||||
connect,
|
||||
isConnected,
|
||||
gameState,
|
||||
players,
|
||||
@@ -30,7 +32,14 @@ const Lobby = () => {
|
||||
approvalStatus,
|
||||
approvePlayer,
|
||||
rejectPlayer,
|
||||
} = useGameWebSocket(gameToken)
|
||||
} = useGameWebSocketContext()
|
||||
|
||||
// Connect to WebSocket when component mounts
|
||||
useEffect(() => {
|
||||
if (gameToken) {
|
||||
connect(gameToken)
|
||||
}
|
||||
}, [gameToken, connect])
|
||||
|
||||
const gameCode = gameCodeFromState || gameState?.gameCode || 'Loading...'
|
||||
|
||||
@@ -67,18 +76,18 @@ const Lobby = () => {
|
||||
console.log('🎮 Game started, navigating to /game')
|
||||
goGame()
|
||||
}
|
||||
}, [gameStarted, navigate])
|
||||
}, [gameStarted, goGame])
|
||||
|
||||
// Handle approval status changes
|
||||
useEffect(() => {
|
||||
if (approvalStatus === 'denied') {
|
||||
alert('A gamemaster elutasította a csatlakozási kérelmedet.')
|
||||
localStorage.removeItem('gameToken')
|
||||
navigate("/home")
|
||||
goHome()
|
||||
} else if (approvalStatus === 'approved') {
|
||||
console.log('✅ Join approved, you can now see the lobby')
|
||||
}
|
||||
}, [approvalStatus, navigate])
|
||||
}, [approvalStatus, goHome])
|
||||
|
||||
const handleExit = () => {
|
||||
if (window.confirm("Biztosan ki szeretnél lépni a lobbyból?")) {
|
||||
@@ -116,7 +125,7 @@ const Lobby = () => {
|
||||
|
||||
// Navigate immediately after successful start (don't wait for WebSocket)
|
||||
console.log('🎮 Navigating to /game...')
|
||||
navigate('/game')
|
||||
goGame()
|
||||
|
||||
} catch (error) {
|
||||
console.error('Failed to start game:', error)
|
||||
@@ -125,7 +134,7 @@ const Lobby = () => {
|
||||
if (error.response?.status === 409) {
|
||||
console.log('Game already started, navigating to /game...')
|
||||
// Navigate anyway - game is already running
|
||||
navigate('/game')
|
||||
goGame()
|
||||
} else {
|
||||
alert(`Nem sikerült elindítani a játékot: ${error.response?.data?.error || error.message}`)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user