M3 frontend infrastructure for the newzy news application: - Vite + React 18 + TypeScript project (Node >=18) in frontend/ - React Router v6: BrowserRouter in App.tsx, routes for /, /articles, /articles/:id and /categories, Link/NavLink-based navigation - lucide-react icons (Home, FileText, Settings, Search, ...) in the responsive Header (desktop + mobile menu) and article components - Reusable components: ArticleCard, AuthorAvatar, CategoryBadge (CVA), Layout shell with backend API status indicator in the footer - Typed API client (fetch wrapper, ApiError, endpoint helpers) with types matching the backend models; sample-data fallback until the backend article endpoints exist - Tailwind CSS global/component styles, cn() helper (clsx + tailwind-merge) - Unit tests: Vitest + React Testing Library (23 tests, 100% statement coverage on components) - E2E tests: Playwright (9 tests) covering page shell, feed rendering, client-side navigation, article route and API interactions - ESLint (react + react-hooks + typescript) configured and passing - Multi-stage Dockerfile (Vite build -> Nginx) with SPA fallback and an /api/ proxy to the backend service; docker-compose frontend service on port 3000 in both frontend/ and the root docker-compose.yml - README with setup, API documentation, testing and Docker instructions
241 lines
11 KiB
Markdown
241 lines
11 KiB
Markdown
# newzy — Frontend
|
|
|
|
React + TypeScript frontend for the **newzy** news web application, built with
|
|
[Vite](https://vitejs.dev/), styled with [Tailwind CSS](https://tailwindcss.com/),
|
|
icons from [lucide-react](https://lucide.dev/), client-side routing via
|
|
[React Router v6](https://reactrouter.com/), unit-tested with
|
|
[Vitest](https://vitest.dev/) + React Testing Library, and E2E-tested with
|
|
[Playwright](https://playwright.dev/).
|
|
|
|
> Infrastructure milestone (M3): project structure, routing, types, reusable
|
|
> components, API client and testing setup. Complex UI features arrive in
|
|
> later milestones.
|
|
|
|
## Requirements
|
|
|
|
- **Node.js 18 or later** (Node 20+ recommended)
|
|
- npm 9+ (ships with Node 20)
|
|
- Optional: a running [backend](../backend/) on `http://localhost:8000`
|
|
(the UI falls back to sample data when the API is unavailable)
|
|
|
|
## Getting started
|
|
|
|
```bash
|
|
# 1. Install dependencies
|
|
npm install
|
|
|
|
# 2. Start the dev server (http://localhost:5173)
|
|
npm run dev
|
|
```
|
|
|
|
The Vite dev server proxies `/api` to `http://localhost:8000`, so no CORS
|
|
configuration is needed during development.
|
|
|
|
## Scripts
|
|
|
|
| Script | Description |
|
|
| --------------------- | -------------------------------------------------------- |
|
|
| `npm run dev` | Start the Vite dev server with HMR on port 5173 |
|
|
| `npm run build` | Type-check (`tsc`) and build the production bundle to `dist/` |
|
|
| `npm run preview` | Serve the production build locally (port 4173) |
|
|
| `npm test` | Run unit tests once (Vitest) |
|
|
| `npm run test:watch` | Run unit tests in watch mode |
|
|
| `npm run test:coverage` | Run unit tests with coverage report |
|
|
| `npm run e2e` | Build the app and run Playwright E2E tests |
|
|
| `npm run e2e:ui` | Run Playwright E2E tests in the interactive UI |
|
|
| `npm run lint` | Lint the codebase with ESLint |
|
|
|
|
## Project structure
|
|
|
|
```
|
|
frontend/
|
|
├── index.html # Vite entry HTML (must live at the project root)
|
|
├── public/ # Static assets served as-is (favicon, robots.txt)
|
|
├── src/
|
|
│ ├── assets/
|
|
│ │ ├── logo.svg # Brand logo (placeholder)
|
|
│ │ └── styles/globals.css # Tailwind + global/component styles
|
|
│ ├── components/
|
|
│ │ ├── ui/ # Generic UI (lucide icons + router links)
|
|
│ │ │ ├── Header.tsx # Navigation header (desktop + mobile menu)
|
|
│ │ │ └── Layout.tsx # Page shell: header, <main>, footer, API status
|
|
│ │ └── articles/ # Domain-specific components
|
|
│ │ ├── ArticleCard.tsx # Article preview (thumb, title, author, badge)
|
|
│ │ ├── AuthorAvatar.tsx # Initials/profile image with name tooltip
|
|
│ │ └── CategoryBadge.tsx # Color-coded category pill (CVA variants)
|
|
│ ├── pages/ # Page-level components (one per route)
|
|
│ │ ├── Home.tsx # Featured article, search bar, article grid
|
|
│ │ ├── ArticlePage.tsx # /articles and /articles/:id placeholders
|
|
│ │ └── CategoriesPage.tsx # /categories listing placeholder
|
|
│ ├── api/
|
|
│ │ └── client.ts # Typed fetch wrapper + endpoint helpers
|
|
│ ├── data/
|
|
│ │ └── sample.ts # Sample feed (fallback until backend endpoints exist)
|
|
│ ├── lib/
|
|
│ │ └── utils.ts # cn() — clsx + tailwind-merge helper
|
|
│ ├── types/
|
|
│ │ └── index.ts # Article / Author / Category / API types
|
|
│ ├── tests/ # Unit tests (Vitest + React Testing Library)
|
|
│ │ ├── Header.test.tsx
|
|
│ │ ├── ArticleCard.test.tsx
|
|
│ │ ├── Layout.test.tsx
|
|
│ │ └── components/
|
|
│ ├── App.tsx # BrowserRouter + route definitions
|
|
│ └── main.tsx # React entry point
|
|
├── playwright/
|
|
│ └── e2e/ # Playwright E2E tests
|
|
│ ├── home.spec.ts # Shell, feed rendering, client-side navigation
|
|
│ └── api.spec.ts # API interactions (mocked endpoints)
|
|
├── Dockerfile # Multi-stage build: Vite build -> Nginx
|
|
├── nginx.conf # SPA fallback + /api/ proxy to the backend
|
|
├── docker-compose.yml # Standalone frontend (+backend) service config
|
|
├── vite.config.ts # Dev server (5173), /api proxy, build output
|
|
├── vitest.config.ts # Unit test config (jsdom)
|
|
├── playwright.config.ts # E2E config (chromium, vite preview webServer)
|
|
└── tailwind.config.js # Tailwind content scan + theme
|
|
```
|
|
|
|
### pages/ vs components/
|
|
|
|
- **`src/pages/`** — one component per route; owns page-level state and is
|
|
imported only by the router in `App.tsx`.
|
|
- **`src/components/`** — reusable UI. `ui/` holds generic building blocks
|
|
(header, layout) with lucide icons; `articles/` holds domain components.
|
|
|
|
## Routing (React Router v6)
|
|
|
|
Routes are defined in [`src/App.tsx`](src/App.tsx) with `BrowserRouter` at the
|
|
root. In-app navigation always uses `<Link>`/`<NavLink>` from
|
|
`react-router-dom` — **never plain anchor tags** — so navigation stays
|
|
client-side without page reloads.
|
|
|
|
| Path | Page component | Notes |
|
|
| -------------- | ------------------ | -------------------------------------- |
|
|
| `/` | `Home` | Feed, featured article, search bar |
|
|
| `/articles` | `ArticlePage` | "All articles" placeholder |
|
|
| `/articles/:id`| `ArticlePage` | Single article placeholder (`useParams`) |
|
|
| `/categories` | `CategoriesPage` | Category listing placeholder |
|
|
| `*` | `NotFound` (404) | Fallback route |
|
|
|
|
## API client
|
|
|
|
[`src/api/client.ts`](src/api/client.ts) wraps `fetch`:
|
|
|
|
- Base URL: `/api/v1` (override with the `VITE_API_BASE_URL` env var).
|
|
- Sends `Accept: application/json`; JSON-serializes request bodies.
|
|
- Non-2xx responses throw `ApiError` with the backend's standardized error
|
|
envelope (`{ detail, code, errors? }`).
|
|
- Typed endpoint helpers: `getHealth()`, `getVersion()`, `getArticles()`
|
|
(backend snake_case payloads are mapped onto the camelCase types in
|
|
`src/types/`).
|
|
|
|
### Backend endpoints (M2)
|
|
|
|
| Method | Path | Description |
|
|
| ------ | ---------------- | ------------------------------------ |
|
|
| GET | `/health` | Root liveness probe |
|
|
| GET | `/api/v1/health` | Service health (`status`, `version`) |
|
|
| GET | `/api/v1/version`| Application name and version |
|
|
|
|
Article/author/category endpoints are planned for a later backend milestone;
|
|
until then the home page renders the sample feed from
|
|
`src/data/sample.ts` (clearly labelled in the UI).
|
|
|
|
### How `/api` resolves per environment
|
|
|
|
| Environment | Mechanism |
|
|
| ------------------ | ---------------------------------------------------------------- |
|
|
| Dev (`npm run dev`) | Vite dev server proxies `/api` → `http://localhost:8000` |
|
|
| Preview (`npm run preview`) | Same proxy (used by the Playwright webServer) |
|
|
| Docker (compose) | Nginx `location /api/` proxies to the `backend:8000` service |
|
|
|
|
## Testing
|
|
|
|
### Unit tests (Vitest + React Testing Library)
|
|
|
|
Located in `src/tests/`. Run with `npm test`.
|
|
|
|
- `Header.test.tsx` — brand, router links, lucide icons, active state, mobile menu
|
|
- `ArticleCard.test.tsx` — data rendering, links, fallbacks, read time
|
|
- `Layout.test.tsx` — shell structure, API status indicator (mocked `getHealth`)
|
|
- `components/` — `CategoryBadge` variants, `AuthorAvatar` initials/image
|
|
|
|
Components that use the router are rendered through the `renderWithRouter`
|
|
helper (`src/tests/utils.tsx`), which wraps them in a `MemoryRouter`.
|
|
|
|
### E2E tests (Playwright)
|
|
|
|
Located in `playwright/e2e/`. Run with `npm run e2e` (builds the app first and
|
|
serves it via `vite preview` on port 4173).
|
|
|
|
- `home.spec.ts` — page shell, feed rendering with data, client-side
|
|
navigation between pages (asserts no full page reloads), article detail
|
|
route, console/page-error hygiene
|
|
- `api.spec.ts` — health request on load, rendering API-provided articles,
|
|
sample-data fallback when the API is down, 404 error-envelope handling.
|
|
Endpoints are mocked with `page.route`, so the tests are deterministic
|
|
without a running backend.
|
|
|
|
Install browsers after a fresh clone:
|
|
|
|
```bash
|
|
npx playwright install chromium
|
|
```
|
|
|
|
## Docker
|
|
|
|
Multi-stage [`Dockerfile`](Dockerfile): `node:20-alpine` builds the production
|
|
bundle; `nginx:1.27-alpine` serves it with SPA fallback and an `/api/` proxy
|
|
to the `backend` service (see [`nginx.conf`](nginx.conf)).
|
|
|
|
```bash
|
|
# From this directory (standalone: frontend + backend)
|
|
docker compose up --build
|
|
|
|
# Or from the repository root (full orchestration)
|
|
docker compose up --build
|
|
```
|
|
|
|
The app is served on <http://localhost:3000>.
|
|
|
|
## Environment variables
|
|
|
|
| Variable | Default | Description |
|
|
| --------------------- | --------- | ------------------------------------ |
|
|
| `VITE_API_BASE_URL` | `/api/v1` | API base URL baked in at build time |
|
|
|
|
No secrets are required on the client side; authentication and credentials
|
|
are handled by the backend in future milestones.
|
|
|
|
## Icons (lucide-react)
|
|
|
|
Icons are imported from `lucide-react` (SVG-based, no font downloads) and are
|
|
always decorative — paired with a text label or `aria-label`, and marked
|
|
`aria-hidden="true"`:
|
|
|
|
| Icon | Where used |
|
|
| ----------------- | ------------------------------------------------- |
|
|
| `Home` | Header — "Home" nav item |
|
|
| `FileText` | Header — "Articles" nav item; article thumbnail |
|
|
| `Settings` | Header — "Categories" nav item |
|
|
| `Search` | Home — search bar |
|
|
| `Menu` / `X` | Header — mobile menu toggle |
|
|
| `Clock` | ArticleCard / hero — read time |
|
|
| `ArrowRight` | Hero — "Read article" call to action |
|
|
| `ArrowLeft` | ArticlePage — back links |
|
|
| `Info` | Home — sample-data notice |
|
|
| `FolderOpen` | CategoriesPage — category cards |
|
|
|
|
Browse all available icons at <https://lucide.dev/icons>.
|
|
|
|
## Troubleshooting
|
|
|
|
- **Port 5173 already in use** — Vite picks the next free port; use
|
|
`npm run dev -- --strictPort` to fail instead.
|
|
- **E2E fails with "browser not found"** — run `npx playwright install
|
|
chromium` (add `--with-deps` on distros where you have root/sudo access).
|
|
- **"API: offline" in the footer** — the backend is not running on
|
|
`http://localhost:8000`; expected when only the frontend is up.
|
|
- **Sample data notice on the home page** — the backend does not implement
|
|
`GET /api/v1/articles` yet (planned milestone); the fallback is intentional.
|