Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Backend/tests/GameResolvers.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// @vitest-environment node
import { beforeEach, describe, expect, it, vi } from 'vitest'

const { Query } = vi.hoisted(() => ({ Query: vi.fn() }))

vi.mock('../DatabaseConnection.ts', () => ({ pool: { query: Query } }))

import { gameQueryResolvers } from '../resolvers/GameResolvers.ts'

beforeEach(() => {
Query.mockReset()
})

describe('games resolver search', () => {
it('filters by name and returns the requested page', async () => {
const games = [{ appid: 570, name: 'Dota 2' }]
Query.mockResolvedValue({ rows: games })

const result = await gameQueryResolvers.games(null, {
search: 'dota',
limit: 20,
offset: 20
})

expect(result).toEqual(games)
expect(Query.mock.calls[0][0]).toContain('g.game_name ILIKE $1')
expect(Query.mock.calls[0][0]).toContain('LIMIT $4 OFFSET $5')
expect(Query.mock.calls[0][1].slice(-2)).toEqual([20, 20])
})

it('caps page size at the existing API maximum', async () => {
Query.mockResolvedValue({ rows: [] })

await gameQueryResolvers.games(null, { search: 'game', limit: 1000, offset: 0 })

expect(Query.mock.calls[0][1].slice(-2)).toEqual([500, 0])
})

it('passes database errors through', async () => {
Query.mockRejectedValue(new Error('connection refused'))

await expect(
gameQueryResolvers.games(null, { search: 'game', limit: 20, offset: 0 })
).rejects.toThrow('connection refused')
})
})
2 changes: 1 addition & 1 deletion Frontend/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image/svg+xml" href="/src/assets/maskot_head_gamestart.png" />
<link rel="icon" type="image/svg+xml" href="/src/assets/maskot_head_gamestart.webp" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>t10-project-2</title>
</head>
Expand Down
33 changes: 28 additions & 5 deletions Frontend/src/FrontendLogic/ProfileLogicFrontend.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,35 @@ export async function FetchProfileFrontend(userId: number): Promise<Profile | nu
return data.profile
}

export async function FetchGameOptions(): Promise<GameOption[]> {
const data = await Gql<{ gameOptions: GameOption[] }>(
`query { gameOptions { appid name } }`,
{}
export async function FetchGameOptions(
options: {
search?: string
appid?: number
limit?: number
offset?: number
} = {}
): Promise<GameOption[]> {
if (options.appid !== undefined) {
const data = await Gql<{ game: GameOption | null }>(
`query($appid: Int!) {
game(appid: $appid) { appid name }
}`,
{ appid: options.appid }
)
return data.game ? [data.game] : []
}

const data = await Gql<{ games: GameOption[] }>(
`query($search: String, $limit: Int, $offset: Int) {
games(search: $search, limit: $limit, offset: $offset) { appid name }
}`,
{
search: options.search ?? null,
limit: options.limit ?? 20,
offset: options.offset ?? 0
}
)
return data.gameOptions
return data.games
}

export async function UpdateProfileFrontend(
Expand Down
Binary file removed Frontend/src/assets/LogoGameStartTransparent.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Frontend/src/assets/maskot_gamestart.png
Binary file not shown.
Binary file added Frontend/src/assets/maskot_gamestart.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed Frontend/src/assets/maskot_head_gamestart.png
Binary file not shown.
Binary file added Frontend/src/assets/maskot_head_gamestart.webp
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions Frontend/src/components/ActiveGameCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ export default function ActiveGameCard({
component="img"
image={game.image}
alt={`${game.name} game poster`}
loading="lazy"
/>
{isLoggedIn && (
<Button
Expand Down
10 changes: 8 additions & 2 deletions Frontend/src/components/Footer.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import '../css/Footer.css'
import gameStartLogo from '../assets/LogoGameStartTransparent.png'
import gameStartLogo from '../assets/LogoGameStartTransparent.webp'
import { Divider } from '@mui/material'

export default function Footer() {
Expand Down Expand Up @@ -27,7 +27,13 @@ export default function Footer() {
/>
<footer className="footerSectionFooter">
<h1 id="footerh1">
<img className="footerBrandLogo" src={gameStartLogo} alt="GameStart" />© 2026
<img
className="footerBrandLogo"
src={gameStartLogo}
alt="GameStart"
loading="lazy"
/>
© 2026
</h1>
</footer>
</div>
Expand Down
9 changes: 7 additions & 2 deletions Frontend/src/components/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
Menu,
X
} from 'lucide-react'
import gameStartLogo from '../assets/LogoGameStartTransparent.png'
import gameStartLogo from '../assets/LogoGameStartTransparent.webp'
import Divider from '@mui/material/Divider'
import { UseAuth } from '../context/AuthContext'

Expand Down Expand Up @@ -56,7 +56,12 @@ export default function Header() {

<div id="logo">
<Link to="/" aria-label="GameStart home" onClick={CloseMenu}>
<img className="headerBrandLogo" src={gameStartLogo} alt="GameStart" />
<img
className="headerBrandLogo"
src={gameStartLogo}
alt="GameStart"
loading="lazy"
/>
</Link>
</div>

Expand Down
1 change: 0 additions & 1 deletion Frontend/src/components/PassiveGameCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ export default function PassiveGameCard({ game, rank }: Props) {
resizeObserver.disconnect()
}
}, [game.name])

return (
<div className="passiveGameCardHoverArea">
<Card
Expand Down
133 changes: 128 additions & 5 deletions Frontend/src/components/ProfileEditor.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useState } from 'react'
import { useEffect, useRef, useState } from 'react'
import {
Alert,
Autocomplete,
Expand All @@ -10,11 +10,14 @@ import {
MenuItem,
TextField
} from '@mui/material'
import { type GameOption } from '../FrontendLogic/ProfileLogicFrontend'
import { FetchGameOptions, type GameOption } from '../FrontendLogic/ProfileLogicFrontend'
import { useDebouncedValue } from '../hooks/useDebouncedValue'
import type { Profile } from '../../../Backend/logic/ProfileLogicBackend'
import { SETUP_OPTIONS } from '../types/SetupOptions'
import '../css/EditProfile.css'

const GAME_OPTIONS_PAGE_SIZE = 20

interface ProfileCardProps {
profile: Profile
gameOptions: GameOption[]
Expand All @@ -33,9 +36,87 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar
const [game, setGame] = useState<GameOption | null>(
gameOptions.find((option) => option.appid === profile.favouriteGame) ?? null
)
const [searchTerm, setSearchTerm] = useState('')
const debouncedSearchTerm = useDebouncedValue(searchTerm)
const [searchResults, setSearchResults] = useState<GameOption[]>([])
const [loadingGames, setLoadingGames] = useState(false)
const [loadingMoreGames, setLoadingMoreGames] = useState(false)
const [hasMoreGames, setHasMoreGames] = useState(false)
const [gameSearchError, setGameSearchError] = useState<string | null>(null)
const requestId = useRef(0)
const loadingMore = useRef(false)
const [saving, setSaving] = useState(false)
const [message, setMessage] = useState<{ type: 'success' | 'error'; text: string } | null>(null)

useEffect(() => {
const query = debouncedSearchTerm.trim()
if (query.length < 2) return
const currentRequestId = requestId.current

FetchGameOptions({ search: query, limit: GAME_OPTIONS_PAGE_SIZE, offset: 0 })
.then((options) => {
if (currentRequestId !== requestId.current) return
setSearchResults(options)
setHasMoreGames(options.length === GAME_OPTIONS_PAGE_SIZE)
})
.catch((err: unknown) => {
if (currentRequestId === requestId.current) {
setGameSearchError(
err instanceof Error ? err.message : 'Could not search games.'
)
}
})
.finally(() => {
if (currentRequestId === requestId.current) setLoadingGames(false)
})
}, [debouncedSearchTerm])

function ResetGameSearch() {
requestId.current += 1
loadingMore.current = false
setSearchTerm('')
setSearchResults([])
setLoadingGames(false)
setLoadingMoreGames(false)
setHasMoreGames(false)
setGameSearchError(null)
}

async function LoadMoreGames() {
const query = searchTerm.trim()
if (query.length < 2 || !hasMoreGames || loadingMore.current) return

loadingMore.current = true
setLoadingMoreGames(true)
const currentRequestId = requestId.current
try {
const options = await FetchGameOptions({
search: query,
limit: GAME_OPTIONS_PAGE_SIZE,
offset: searchResults.length
})
if (currentRequestId !== requestId.current) return
setSearchResults((current) => [
...current,
...options.filter(
(option) => !current.some((existing) => existing.appid === option.appid)
)
])
setHasMoreGames(options.length === GAME_OPTIONS_PAGE_SIZE)
} catch (err) {
if (currentRequestId === requestId.current) {
setGameSearchError(
err instanceof Error ? err.message : 'Could not load more games.'
)
}
} finally {
if (currentRequestId === requestId.current) {
loadingMore.current = false
setLoadingMoreGames(false)
}
}
}

async function HandleSave() {
setSaving(true)
setMessage(null)
Expand Down Expand Up @@ -87,9 +168,44 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar
<CardContent className="user-details-container">
<div className="user-details">
<Autocomplete
options={gameOptions}
options={searchTerm.trim().length >= 2 ? searchResults : game ? [game] : []}
value={game}
onChange={(_, value) => setGame(value)}
onChange={(_, value) => {
setGame(value)
ResetGameSearch()
}}
onInputChange={(_, value, reason) => {
if (reason === 'input' || reason === 'clear') {
requestId.current += 1
loadingMore.current = false
setSearchTerm(value)
setSearchResults([])
setLoadingGames(value.trim().length >= 2)
setLoadingMoreGames(false)
setHasMoreGames(false)
setGameSearchError(null)
}
}}
loading={loadingGames || loadingMoreGames}
noOptionsText={
searchTerm.trim().length < 2
? 'Type at least 2 characters to search'
: 'No games found'
}
filterOptions={(options) => options}
slotProps={{
listbox: {
onScroll: (event) => {
const listbox = event.currentTarget
if (
listbox.scrollTop + listbox.clientHeight >=
listbox.scrollHeight - 24
) {
void LoadMoreGames()
}
}
}
}}
getOptionLabel={(option) => option.name}
isOptionEqualToValue={(a, b) => a.appid === b.appid}
renderOption={(props, option) => {
Expand All @@ -100,7 +216,14 @@ export default function ProfileCard({ profile, gameOptions, onSave }: ProfileCar
</li>
)
}}
renderInput={(params) => <TextField {...params} label="Favorite game" />}
renderInput={(params) => (
<TextField
{...params}
label="Favorite game"
error={!!gameSearchError}
helperText={gameSearchError}
/>
)}
/>
</div>
<div className="user-details">
Expand Down
Loading