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
15 changes: 11 additions & 4 deletions frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ function App() {
const [favorites, setFavorites] = useState<Set<string>>(new Set());
const [view, setView] = useState<GameView>('list');
const [showFavorites, setShowFavorites] = useState(false);
const navigate = useNavigate();
const navigate = useNavigate();
const { pathname } = useLocation();
// Søk, filtre og sortering vises på listesiden /spill. Forsiden / har rader
const onListPage = useLocation().pathname === '/spill';

const onListPage = pathname === '/spill';
// Forsiden har sitt eget store søkefelt, så toppfeltet viser ikke søk der
const onHomePage = pathname === '/';
// Søket i hovedlista. Settes først når brukeren trykker Enter, ikke for hver bokstav
// (forslagene i søkefeltet tar seg av det), så lista og pagineringen ikke hopper
const [query, setQuery] = useState('');
Expand Down Expand Up @@ -202,17 +204,22 @@ function App() {
favoritesCount={favorites.size}
showFavorites={showFavorites && onListPage}
onToggleFavorites={handleToggleFavorites}
showSearch={!onHomePage}
initialSearch={query}
/>

<Routes>
{/* Forsiden med rader */}
{/* Forsiden med søk øverst og rader */}
<Route
path="/"
element={
<Home
favorites={favorites}
onToggleFavorite={toggleFavorite}
onShowAll={handleShowAll}
onSearch={handleSearch}
onSelectGame={handleSelectGame}
searchResetKey={searchResetKey}
/>
}
/>
Expand Down
6 changes: 4 additions & 2 deletions frontend/src/components/GameSearch/GameSearch.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ interface GameSearchProps {
onSearch: (term: string) => void;
/** Brukeren valgte ett spill i forslagslista */
onSelectGame: (game: Suggestion) => void;
/** Teksten feltet starter med, f.eks. søket som gjelder når feltet vises på nytt */
initialValue?: string;
}

/**
Expand All @@ -24,8 +26,8 @@ interface GameSearchProps {
* Følger WAI-ARIA-mønsteret "combobox med listbox".
*/

export function GameSearch({ onSearch, onSelectGame }: GameSearchProps) {
const [input, setInput] = useState('');
export function GameSearch({ onSearch, onSelectGame, initialValue = '' }: GameSearchProps) {
const [input, setInput] = useState(initialValue);
const [open, setOpen] = useState(false);
const [active, setActive] = useState(-1); // markert forslag, -1 = ingen
const id = useId();
Expand Down
17 changes: 14 additions & 3 deletions frontend/src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ interface HeaderProps {
onToggleFavorites: () => void;
onHome: () => void;
searchResetKey: number;
showSearch: boolean;
initialSearch: string;
}

/**
Expand All @@ -28,15 +30,24 @@ export function Header({
favoritesCount,
showFavorites,
onToggleFavorites,
showSearch,
initialSearch,
}: HeaderProps) {
const { user, logOut } = useAuth();
const location = useLocation();
return (
<header className="header">
<Logo onHome={onHome} />
<div className="header__search">
<GameSearch key={searchResetKey} onSearch={onSearch} onSelectGame={onSelectGame} />
</div>
{showSearch && (
<div className="header__search">
<GameSearch
key={searchResetKey}
initialValue={initialSearch}
onSearch={onSearch}
onSelectGame={onSelectGame}
/>
</div>
)}
<nav className="header__nav" aria-label="Meny">
{user ? (
<button type="button" className="text-button" onClick={logOut}>
Expand Down
46 changes: 46 additions & 0 deletions frontend/src/components/HomeHero/HomeHero.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/* Toppen av forsiden: overskrift og søk i midten, tynn linje under som radene */
.home-hero {
margin-bottom: 40px;
padding: 56px 0 48px;
text-align: center;
border-bottom: 1px solid var(--border);
}

/* Størrelsen kommer fra h1 i index.css (56px, 36px på smal skjerm). Her bare luften */
.home-hero__title {
margin: 0 0 32px;
line-height: 1.1;
}

.home-hero__search {
max-width: 680px;
margin: 0 auto;
}

/* Samme søkefelt som i toppfeltet, bare større og med mørkere linje */
.home-hero__search .game-search__input {
font-size: 24px;
border-bottom-color: var(--text-h);
}

.home-hero__examples {
margin-top: 16px;
font-family: var(--mono);
font-size: 14px;
}

/* Eksempelsøkene er knapper, men skal se ut som lenker */
.home-hero__examples .text-button {
color: var(--text-h);
text-decoration: underline;
}

@media (max-width: 600px) {
.home-hero {
padding: 32px 0;
}

.home-hero__search .game-search__input {
font-size: 20px;
}
}
45 changes: 45 additions & 0 deletions frontend/src/components/HomeHero/HomeHero.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { GameSearch } from '../GameSearch/GameSearch';
import type { Suggestion } from '../../graphql/searchSuggestions';
import './HomeHero.css';

// Eksempelsøk under feltet, så nye besøkende ser hva de kan skrive
const EXAMPLES = ['zelda', 'stardew valley', 'portal'];

interface HomeHeroProps {
/** Enter i feltet eller et eksempelsøk: søk i hovedlista (App går til /spill) */
onSearch: (term: string) => void;
/** Valgt forslag i feltet: åpner detaljsiden */
onSelectGame: (game: Suggestion) => void;
}

/**
* Toppen av forsiden: overskrift og et stort søkefelt. På forsiden ligger søket her
* i stedet for i toppfeltet (se Header), så siden ikke har to søkefelt.
* Samme GameSearch som i toppfeltet, så forslag og tastaturstyring virker likt.
*/
export function HomeHero({ onSearch, onSelectGame }: HomeHeroProps) {
return (
<section className="home-hero" aria-labelledby="home-hero-title">
<h1 id="home-hero-title" className="home-hero__title">
Søk i over 100 000 videospill
</h1>

<div className="home-hero__search">
<GameSearch onSearch={onSearch} onSelectGame={onSelectGame} />
</div>

{/* Knapper og ikke lenker: de starter et søk, akkurat som Enter i feltet */}
<p className="home-hero__examples">
prøv:{' '}
{EXAMPLES.map((term, i) => (
<span key={term}>
{i > 0 && ' · '}
<button type="button" className="text-button" onClick={() => onSearch(term)}>
{term}
</button>
</span>
))}
</p>
</section>
);
}
17 changes: 15 additions & 2 deletions frontend/src/pages/Home/Home.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useQuery } from '@apollo/client/react';
import { GameRow } from '../../components/GameRow/GameRow';
import { HomeHero } from '../../components/HomeHero/HomeHero';
import type { Suggestion } from '../../graphql/searchSuggestions';
import { HOME_ROWS, type HomeRowsData } from '../../graphql/homeRows';

// Plattform-ID-er fra databasen. Sjekk dem med { platforms { id name } } i Apollo Sandbox
Expand All @@ -26,10 +28,20 @@ interface HomeProps {
onToggleFavorite: (id: string) => void;
/** "Se alle" på en rad: App setter plattformfilteret før listesiden vises */
onShowAll: (platformIds: string[]) => void;
onSearch: (term: string) => void;
onSelectGame: (game: Suggestion) => void;
searchResetKey: number;
}

/** Forsiden: rader med populære spill, totalt og per plattform. */
export function Home({ favorites, onToggleFavorite, onShowAll }: HomeProps) {
/** Forsiden: søk øverst, så rader med populære spill, totalt og per plattform. */
export function Home({
favorites,
onToggleFavorite,
onShowAll,
onSearch,
onSelectGame,
searchResetKey,
}: HomeProps) {
const { data, loading, error } = useQuery(HOME_ROWS, { variables: PLATFORMS });

// Hver rad viser bare spill som ikke allerede står i en rad over.
Expand All @@ -49,6 +61,7 @@ export function Home({ favorites, onToggleFavorite, onShowAll }: HomeProps) {

return (
<main className="main">
<HomeHero key={searchResetKey} onSearch={onSearch} onSelectGame={onSelectGame} />
{loading && !data && <p role="status">Laster …</p>}
{error && <p role="alert">Kunne ikke hente spill: {error.message}</p>}

Expand Down
Loading