Skip to content

Conversation

@newmaldenite
Copy link
Collaborator

Book Detail Page Implementation

Overview

This PR introduces the book detail page, allowing users to view the full details of a single book. It builds upon the existing book list by providing navigation to individual book pages and includes robust error handling for various scenarios.


Features

Feature Description
Dynamic Route Creates a new dynamic route at /books/:bookId that fetches and displays a single book's title, author, and synopsis.
Enhanced List Page Enhances the /books list page by making each book title a clickable link to its corresponding detail page.

Error Handling

  • Implements graceful error handling for the detail page:
    • Shows a user-friendly "Page not found" message for 404 errors.
    • Shows a generic "service problem" message for all other API errors.

Refactor

  • Improves the HATEOAS link handling by creating a reusable helper function that transforms absolute URLs from the API into relative paths for the view, making the frontend more resilient.

Key TDD & Architectural Decisions

  • All new functionality, including error paths and link enhancements, was developed using a strict "Red-Green-Refactor" TDD cycle.
  • The test suite for the books feature (books.test.js) was expanded to cover:
    • "Happy path" (200)
    • "Not found" (404)
    • "Server error" (500) scenarios for the detail page.
  • The application now correctly consumes and transforms HATEOAS links from the backend, adhering to REST principles and improving maintainability.

Implements the book detail page, allowing users to view the title, author, and synopsis of a single book.

- Creates a new dynamic route `GET /books/:bookId` to handle requests for specific books.
- Extends the `apiClient` with a `getBookById` function to fetch a single book from the backend API.
- Adds a new Nunjucks template (`book-data.njk`) to render the book's information using GOV.UK Design System components.
- Follows a TDD approach by first creating a failing integration test for the "happy path" (when a book is successfully found) and then writing the minimal code to make it pass.
- The test suite is organized by feature, with the new test added to the existing `books.test.js` file.
Updates the book list page to use the HATEOAS links provided by the backend API for navigating to book detail pages. This makes the frontend more resilient to changes in the backend URL structure.

- The `GET /books` test is updated to assert that relative links are rendered correctly for each book.
- The route handler for `GET /books` now includes logic to transform all absolute HATEOAS URLs from the API into relative paths suitable for the view.
- This transformation logic is extracted into a reusable helper function to keep the code DRY and is now used by both the list and detail page route handlers.
Implements graceful error handling for the `GET /books/:bookId` endpoint when a requested book is not found in the backend API.

- Follows a TDD workflow by first adding a failing test case that mocks a 404 API response and asserts that the application returns a 404 status.
- The route handler is updated with a `try...catch` block to handle exceptions from the API client.
- A user-friendly, GOV.UK-styled 404 page is rendered when the API returns a 404, separating this expected "not found" case from unexpected server errors.
Adds a new test case to cover the scenario where the backend API returns a non-404 error (e.g., 500 Internal Server Error) when fetching a single book.

This test acts as a regression guard, ensuring the application's `try...catch` block correctly falls through to the generic 500 error handler and renders the appropriate user-friendly error page.
@newmaldenite newmaldenite merged commit 03e5c86 into main Nov 20, 2025
1 check passed
@newmaldenite newmaldenite deleted the feat/book-detail branch November 20, 2025 14:48
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants