Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 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
4 changes: 2 additions & 2 deletions frontend/src/components/GameListItem.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ describe("GameListItem", () => {
expect(screen.getByRole("img", { name: "Great Game" })).toHaveAttribute("src", "/game.png");
});

it("falls back to an em dash when year or tags are missing", () => {
it("falls back to N/A when year or tags are missing", () => {
renderItem({ year: null, tags: "" });
expect(screen.getAllByText("—")).toHaveLength(2);
expect(screen.getAllByText("N/A")).toHaveLength(2);
});

it("links to the game's detail page", () => {
Expand Down
8 changes: 4 additions & 4 deletions frontend/src/components/Header.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ describe("Header", () => {

it("renders the wordmark, search field and My Ratings link", () => {
renderHeader("/project2");
expect(screen.getByText("Working Title")).toBeInTheDocument();
expect(screen.getByText("NTNU GameShelf")).toBeInTheDocument();
expect(screen.getByPlaceholderText("Search games...")).toHaveValue("");
expect(screen.getByText("My Ratings")).toBeInTheDocument();
expect(screen.queryByRole("button", { name: "Clear" })).not.toBeInTheDocument();
Expand Down Expand Up @@ -58,7 +58,7 @@ describe("Header", () => {

it("links home to the current query on the grid page", () => {
renderHeader("/project2?search=zelda");
expect(screen.getByText("Working Title").closest("a")).toHaveAttribute(
expect(screen.getByText("NTNU GameShelf").closest("a")).toHaveAttribute(
"href",
"/project2/?search=zelda",
);
Expand All @@ -67,14 +67,14 @@ describe("Header", () => {
it("links home to the persisted grid query from other pages", () => {
sessionStorage.setItem(GRID_QUERY_STORAGE_KEY, "search=bar");
renderHeader("/project2/my-ratings");
expect(screen.getByText("Working Title").closest("a")).toHaveAttribute(
expect(screen.getByText("NTNU GameShelf").closest("a")).toHaveAttribute(
"href",
"/project2/?search=bar",
);
});

it("links home plainly when there is no persisted query", () => {
renderHeader("/project2/my-ratings");
expect(screen.getByText("Working Title").closest("a")).toHaveAttribute("href", "/project2");
expect(screen.getByText("NTNU GameShelf").closest("a")).toHaveAttribute("href", "/project2");
});
});
14 changes: 14 additions & 0 deletions frontend/src/components/Pagination.module.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.pagination {
display: inline-flex;
align-items: center;
justify-self: center;
gap: 0.75rem;
}

.pageButton {
display: inline-flex;
align-items: center;
padding: 0;
color: inherit;
font: inherit;
}
45 changes: 45 additions & 0 deletions frontend/src/components/Pagination.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import { Button } from "react-bootstrap";
import { ChevronLeft, ChevronRight } from "react-bootstrap-icons";
import styles from "./Pagination.module.css";

interface PaginationProps {
page: number;
totalPages: number;
onPrevPage: () => void;
onNextPage: () => void;
}

export default function Pagination({
page,
totalPages,
onPrevPage,
onNextPage,
}: PaginationProps) {
return (
<div className={styles.pagination}>
<Button
variant="link"
className={`${styles.pageButton} text-decoration-none`}
type="button"
aria-label="Previous page"
disabled={page <= 1}
onClick={onPrevPage}
>
<ChevronLeft />
</Button>
<span>
Page {page} of {totalPages}
</span>
<Button
variant="link"
className={`${styles.pageButton} text-decoration-none`}
type="button"
aria-label="Next page"
disabled={page >= totalPages}
onClick={onNextPage}
>
<ChevronRight />
</Button>
</div>
);
}
15 changes: 0 additions & 15 deletions frontend/src/components/Toolbar/Toolbar.module.css
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,6 @@
background-color: var(--color-brand);
}

.pagination {
display: inline-flex;
align-items: center;
justify-self: center;
gap: 0.75rem;
}

.pageButton {
display: inline-flex;
align-items: center;
padding: 0;
color: inherit;
font: inherit;
}

.sort {
display: inline-flex;
align-items: center;
Expand Down
41 changes: 8 additions & 33 deletions frontend/src/components/Toolbar/Toolbar.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
import { useEffect, useRef, useState } from "react";
import type { ChangeEvent } from "react";
import { Button, Form } from "react-bootstrap";
import {
ArrowDownUp,
ChevronDown,
ChevronLeft,
ChevronRight,
Funnel,
X,
} from "react-bootstrap-icons";
import { ArrowDownUp, ChevronDown, Funnel, X } from "react-bootstrap-icons";
import {
ESRB_OPTIONS,
PLATFORM_OPTIONS,
Expand All @@ -18,6 +11,7 @@ import {
} from "../../constants/filterOptions";
import type { GameFilters, SortField, SortState } from "../../types/gameQuery";
import { EMPTY_FILTERS, hasActiveFilters } from "../../types/gameQuery";
import Pagination from "../Pagination";
import styles from "./Toolbar.module.css";

interface ToolbarProps {
Expand Down Expand Up @@ -101,31 +95,12 @@ export default function Toolbar({
)}
</div>

<div className={styles.pagination}>
<Button
variant="link"
className={`${styles.pageButton} text-decoration-none`}
type="button"
aria-label="Previous page"
disabled={page <= 1}
onClick={onPrevPage}
>
<ChevronLeft />
</Button>
<span>
Page {page} of {totalPages}
</span>
<Button
variant="link"
className={`${styles.pageButton} text-decoration-none`}
type="button"
aria-label="Next page"
disabled={page >= totalPages}
onClick={onNextPage}
>
<ChevronRight />
</Button>
</div>
<Pagination
page={page}
totalPages={totalPages}
onPrevPage={onPrevPage}
onNextPage={onNextPage}
/>

<div className={styles.menu}>
<div className={styles.sort}>
Expand Down
25 changes: 23 additions & 2 deletions frontend/src/pages/MyRatings.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useMemo } from "react";
import { useMemo, useState } from "react";
import { gql } from "@apollo/client";
import { useQuery } from "@apollo/client/react";
import Header from "../components/Header";
Expand All @@ -8,6 +8,9 @@ import StarRating from "../components/StarRating";
import { useIsMobile } from "../utils/isMobile";
import { Link } from "react-router-dom";
import { resizedImage } from "../utils/resizedImage";
import Pagination from "../components/Pagination";

const RATINGS_PAGE_SIZE = 10;

const GET_MY_REVIEWS = gql`
query GetMyReviews($userId: String!) {
Expand Down Expand Up @@ -82,6 +85,14 @@ export default function MyRatings() {
})
.sort((a, b) => b.score - a.score);

const [rawPage, setPage] = useState(1);
const totalPages = Math.max(1, Math.ceil(ratings.length / RATINGS_PAGE_SIZE));
const page = Math.min(rawPage, totalPages);
const pagedRatings = ratings.slice(
(page - 1) * RATINGS_PAGE_SIZE,
page * RATINGS_PAGE_SIZE,
);

return (
<>
<Header />
Expand All @@ -108,9 +119,19 @@ export default function MyRatings() {
{!error && !loading && ratings.length === 0 && (
<p className="text-muted m-4">You haven’t rated any games yet.</p>
)}
{!error && !loading && ratings.length > 0 && (
<div className="d-flex justify-content-center mb-3">
<Pagination
page={page}
totalPages={totalPages}
onPrevPage={() => setPage((p) => Math.max(1, p - 1))}
onNextPage={() => setPage((p) => Math.min(totalPages, p + 1))}
/>
</div>
)}
{!error && (
<div className="row row-cols row-cols-sm-2 row-cols-md-3 row-cols-lg-5 g-3">
{ratings.map(({ game, score }) => (
{pagedRatings.map(({ game, score }) => (
<div className="col" key={game.id}>
<Link
className="d-block h-100 text-decoration-none text-reset"
Expand Down