Add transition to lobby page

landing-page-layout
MiguelMLorente 2025-02-09 14:38:08 +01:00
parent 0650f0481b
commit 44cb010faf
4 changed files with 57 additions and 11 deletions

View File

@ -3,12 +3,12 @@ import { ServerEvent } from "./ServerEvent";
export type UpdateLobbyEvent = {
playerNames: Array<string>;
gameCode: string;
};
export function attachHandlerToUpdateLobbyEvent(
socket: Socket,
handler: (event: UpdateLobbyEvent) => void,
): () => void {
socket.on(ServerEvent.LOBBY_UPDATE, handler);
return () => socket.off(ServerEvent.LOBBY_UPDATE, handler);
): void {
socket.once(ServerEvent.LOBBY_UPDATE, handler);
}

View File

@ -1,21 +1,44 @@
import React from "react";
import React, { useCallback, useState } from "react";
import { Socket } from "socket.io-client";
import LandingPage from "./pages/landing/LandingPage";
import GamePage from "./pages/game/GamePage";
import "./App.scss";
import LobbyPage from "./pages/lobby/LobbyPage";
import { attachHandlerToUpdateLobbyEvent } from "interface";
export interface AppProps {
socket: Socket;
}
const App = (props: AppProps) => {
const renderedPage: string = "LOBBY";
const { socket } = props;
const [renderedPage, setRenderedPage] = useState("LANDING");
const [initialPlayerNames, setInitialPlayerNames] = useState([""]);
const [gameCode, setGameCode] = useState("");
const setupNextPageTransition = useCallback(() => {
if (renderedPage === "LANDING") {
attachHandlerToUpdateLobbyEvent(socket, (event) => {
setInitialPlayerNames(event.playerNames);
setGameCode(event.gameCode);
setRenderedPage("LOBBY");
});
}
}, [renderedPage]);
setupNextPageTransition();
return (
<div className="app">
{renderedPage === "LANDING" && <LandingPage {...props} />}
{renderedPage === "LOBBY" && <LobbyPage />}
{renderedPage === "LOBBY" && (
<LobbyPage
{...props}
initialPlayerNames={initialPlayerNames}
gameCode={gameCode}
/>
)}
{renderedPage === "GAME" && <GamePage />}
</div>
);

View File

@ -35,8 +35,16 @@
border-radius: 10px;
}
.game-code {
margin: 0 auto 10px;
padding: 10px;
width: 60%;
border: 2px solid blue;
border-radius: 10px;
}
.start-game-button {
margin-top: 30px;
margin: 30px 0;
}
}
}

View File

@ -1,7 +1,21 @@
import { useState } from "react";
import "./LobbyPage.scss";
import { Socket } from "socket.io-client";
import { attachHandlerToUpdateLobbyEvent } from "interface";
const LobbyPage = () => {
const userNames = ["Laura", "Miguel", "David"];
export interface LobbyPageProps {
initialPlayerNames: string[];
gameCode: string;
socket: Socket;
}
const LobbyPage = (props: LobbyPageProps) => {
const { initialPlayerNames, gameCode, socket } = props;
const [playerNames, setPlayerNames] = useState(initialPlayerNames);
attachHandlerToUpdateLobbyEvent(socket, (event) => {
setPlayerNames(event.playerNames);
});
return (
<div className="lobby-page">
@ -9,12 +23,13 @@ const LobbyPage = () => {
<h1>Trains And Roads</h1>
</div>
<div className="lobby-page-body">
{userNames.map((name) => (
<div className="lobby-user-name">{name}</div>
{playerNames.map((name) => (
<article className="lobby-user-name">{name}</article>
))}
<div className="start-game-button">
<button>Start Game</button>
</div>
<article className="game-code">Game code: {gameCode}</article>
</div>
</div>
);