MOAR STILE

This commit is contained in:
the-all-good 2024-12-15 16:39:51 +11:00
parent 8f8c4fb267
commit 6ecc1dd7e0
5 changed files with 35 additions and 18 deletions

View File

@ -1,19 +1,19 @@
import { BrowserRouter, Routes, Route, Link, useParams } from 'react-router-dom'; import { BrowserRouter, Routes, Route } from 'react-router-dom';
import '../css/App.css'; import '../css/App.css';
import Leaderboard from './components/leaderboard';
import GameCard from './components/gameCard';
import Button from './components/button';
import GameView from './pages/gameView'; import GameView from './pages/gameView';
import Home from './pages/home'; import Home from './pages/home';
import { GameProvider } from './contexts/GameContext';
function App() { function App() {
return ( return (
<BrowserRouter> <GameProvider>
<Routes> <BrowserRouter>
<Route path="/" element={<Home />} /> <Routes>
<Route path="/game/:id" element={<GameView />} /> <Route path="/" element={<Home />} />
</Routes> <Route path="/game/:id" element={<GameView />} />
</BrowserRouter> </Routes>
</BrowserRouter>
</GameProvider>
); );
} }

View File

@ -8,8 +8,6 @@ function GameList(props){
<ListGameCell <ListGameCell
key={game.gameId} key={game.gameId}
data={game} data={game}
selectedGame={props.selected}
setGame={props.toggle}
/> />
))} ))}
</div> </div>

View File

@ -1,14 +1,16 @@
import React from 'react'; import React, { useContext } from 'react';
import { GameContext } from '../contexts/GameContext';
function ListGameCell(props){ function ListGameCell(props){
const isSelected = props.selectedGame === props.data.gameId; const { selectedGame, setSelectedGame } = useContext(GameContext);
const isSelected = selectedGame === props.data.gameId;
return ( return (
<div <div
className={`w-full p-2 my-1 border-2 rounded-sm border-black ${ className={`w-full p-2 my-1 border-2 rounded-sm border-black ${
isSelected ? 'bg-blue-400' : 'bg-slate-200 hover:bg-slate-400' isSelected ? 'bg-blue-400' : 'bg-slate-200 hover:bg-slate-400'
}`} }`}
onClick={() => isSelected ? props.setGame(null) :props.setGame(props.data.gameId)} onClick={() => isSelected ? setSelectedGame(null) : setSelectedGame(props.data.gameId)}
> >
<p>Game ID: {props.data.gameId}</p> <p>Game ID: {props.data.gameId}</p>
<p> <p>

View File

@ -0,0 +1,16 @@
import React, { createContext, useState } from 'react';
// Create a context
export const GameContext = createContext(null);
// Context Provider
export function GameProvider({ children }) {
const [selectedGame, setSelectedGame] = useState({ selectedGame: null });
return (
<GameContext.Provider value={{ selectedGame, setSelectedGame }}>
{children}
</GameContext.Provider>
);
}

View File

@ -1,11 +1,12 @@
import React, { useState} from 'react'; import React, { useContext } from 'react';
import Leaderboard from '../components/leaderboard'; import Leaderboard from '../components/leaderboard';
import GameList from '../components/gameList'; import GameList from '../components/gameList';
import Button from '../components/button'; import Button from '../components/button';
import { GameContext } from '../contexts/GameContext';
function Home(){ function Home(){
const [selectedGame, setSelectedGame] = useState(null); let {selectedGame, setSelectedGame} = useContext(GameContext);
const games = [ const games = [
{ {
@ -29,7 +30,7 @@ function Home(){
return ( return (
<div className="min-h-full h-full flex flex-row"> <div className="min-h-full h-full flex flex-row">
<Leaderboard /> <Leaderboard />
<GameList games={games} selected={selectedGame} toggle={setSelectedGame}/> <GameList games={games}/>
<div className="flex flex-col"> <div className="flex flex-col">
<Button displayValue="New Game" link="/game/1"/> <Button displayValue="New Game" link="/game/1"/>
<Button displayValue="Join Game" link={`/game/${selectedGame}`} disabled={selectedGame ? false : true}/> <Button displayValue="Join Game" link={`/game/${selectedGame}`} disabled={selectedGame ? false : true}/>