Skip to content

Commit

Permalink
some initial setup for client
Browse files Browse the repository at this point in the history
  • Loading branch information
Timtam committed Mar 9, 2024
1 parent 527b9aa commit 6b8c9b7
Show file tree
Hide file tree
Showing 12 changed files with 692 additions and 83 deletions.
2 changes: 1 addition & 1 deletion client/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/vite.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Vite + React + TS</title>
<title>Hitster</title>
</head>
<body>
<div id="root"></div>
Expand Down
572 changes: 570 additions & 2 deletions client/package-lock.json

Large diffs are not rendered by default.

7 changes: 5 additions & 2 deletions client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"react-dom": "^18.2.0",
"react-helmet-async": "^2.0.4",
"react-router": "^6.22.3",
"react-router-dom": "^6.22.3"
"react-router-dom": "^6.22.3",
"zod": "^3.22.4"
},
"devDependencies": {
"@types/react": "^18.2.56",
Expand All @@ -31,6 +32,8 @@
"prettier": "^3.2.5",
"prettier-plugin-organize-imports": "^3.2.4",
"typescript": "^5.2.2",
"vite": "^5.1.4"
"vite": "^5.1.4",
"vite-plugin-checker": "^0.6.4",
"vite-tsconfig-paths": "^4.3.1"
}
}
42 changes: 0 additions & 42 deletions client/src/App.css

This file was deleted.

33 changes: 0 additions & 33 deletions client/src/App.tsx

This file was deleted.

21 changes: 21 additions & 0 deletions client/src/entities.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { z } from "zod"

export const User = z.object({
username: z.string(),
id: z.number(),
})

export type User = z.infer<typeof User>

export const Game = z.object({
id: z.number(),
players: z.array(User),
})

export type Game = z.infer<typeof Game>

export const GamesResponse = z.object({
games: z.array(Game),
})

export type GamesResponse = z.infer<typeof GamesResponse>
20 changes: 20 additions & 0 deletions client/src/error-page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { useRouteError, isRouteErrorResponse } from "react-router-dom"

export default function ErrorPage() {
const error = useRouteError()
console.error(error)

if (isRouteErrorResponse(error)) {
return (
<div>
<h1>Oops!</h1>
<p>Sorry, an unexpected error has occurred.</p>
<p>
<i>{error.statusText || error.data?.message}</i>
</p>
</div>
)
} else {
return <div>Oops</div>
}
}
13 changes: 13 additions & 0 deletions client/src/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import "primereact/resources/themes/lara-light-indigo/theme.css" //theme
import "primereact/resources/primereact.min.css" //core css
import "primeicons/primeicons.css" //icons
import "primeflex/primeflex.css" // flex
import { Outlet } from "react-router-dom"

export default function Layout() {
return (
<>
<Outlet />
</>
)
}
24 changes: 24 additions & 0 deletions client/src/lobby.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Helmet } from "react-helmet-async"
import { Game } from "./entities"
import GameService from "./services/games.service"
import { useLoaderData } from "react-router-dom"

export async function loader(): Promise<Game[]> {
let gs = new GameService()
return await gs.getAll()
}

export function Lobby() {
let games = useLoaderData() as Game[]

return (
<>
<Helmet>
<title>Game Lobby - Hitster</title>
</Helmet>
{games.length
? "Voilla, we found some games!"
: "Nope, no games..."}
</>
)
}
21 changes: 19 additions & 2 deletions client/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,32 @@
import { HelmetProvider } from "react-helmet-async"
import ErrorPage from "./error-page"
import Layout from "./layout"
import React from "react"
import ReactDOM from "react-dom/client"
import App from "./App.tsx"
import { Lobby, loader as LobbyLoader } from "./lobby"
import { PrimeReactProvider } from "primereact/api"
import "./index.css"
import { createBrowserRouter, RouterProvider } from "react-router-dom"

const router = createBrowserRouter([
{
element: <Layout />,
children: [
{
element: <Lobby />,
path: "/",
loader: LobbyLoader,
},
],
errorElement: <ErrorPage />,
},
])

ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<HelmetProvider>
<PrimeReactProvider>
<App />
<RouterProvider router={router} />
</PrimeReactProvider>
</HelmetProvider>
</React.StrictMode>,
Expand Down
10 changes: 10 additions & 0 deletions client/src/services/games.service.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { GamesResponse, Game } from "../entities"

export default class GameService {
async getAll(): Promise<Game[]> {
let res = await fetch("/api/games/", {
method: "GET",
})
return GamesResponse.parse(await res.json()).games
}
}
10 changes: 9 additions & 1 deletion client/vite.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
import { defineConfig } from "vite"
import checker from "vite-plugin-checker"
import viteTsconfigPaths from "vite-tsconfig-paths"
import react from "@vitejs/plugin-react-swc"

// https://vitejs.dev/config/
export default defineConfig({
plugins: [react()],
plugins: [
react(),
viteTsconfigPaths(),
checker({
typescript: true,
}),
],
})

0 comments on commit 6b8c9b7

Please sign in to comment.