feat: add React + TypeScript frontend with Vite, routing, tests and Docker setup

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
This commit is contained in:
Daniel Samson committed 2026-10-30 14:10:29 +00:00
1 parent cbeac5e16b
commit ffe19272b3
45 files changed
+9843 -4

No files matched your search

+203
View File
@@ -0,0 +1,203 @@
import { ArrowRight, Info, Search } from 'lucide-react';
import { useEffect, useMemo, useState } from 'react';
import { Link } from 'react-router-dom';
import { ApiError, getArticles } from '../api/client';
import { ArticleCard, getReadTime } from '../components/articles/ArticleCard';
import { AuthorAvatar } from '../components/articles/AuthorAvatar';
import { CategoryBadge } from '../components/articles/CategoryBadge';
import { sampleArticles } from '../data/sample';
import type { Article } from '../types';
type FeedSource = 'api' | 'sample';
/**
* Homepage: featured (hero) article, search bar, article grid built from
* ArticleCard components, and a pagination placeholder.
*
* Articles are loaded from `GET /api/v1/articles`; until that backend
* endpoint exists the page falls back to sample data (clearly labelled).
*/
export function Home() {
const [articles, setArticles] = useState<Article[]>([]);
const [source, setSource] = useState<FeedSource>('sample');
const [loading, setLoading] = useState(true);
const [query, setQuery] = useState('');
useEffect(() => {
let active = true;
getArticles()
.then((data) => {
if (!active) return;
if (data.length > 0) {
setArticles(data);
setSource('api');
} else {
setArticles(sampleArticles);
setSource('sample');
}
})
.catch((error: unknown) => {
if (!active) return;
// 404 (endpoint not implemented yet) or network failure: fall back to
// sample data so the UI stays useful without a running backend.
if (!(error instanceof ApiError)) {
console.error('Unexpected error while loading articles:', error);
}
setArticles(sampleArticles);
setSource('sample');
})
.finally(() => {
if (active) setLoading(false);
});
return () => {
active = false;
};
}, []);
const filtered = useMemo(() => {
const q = query.trim().toLowerCase();
if (!q) return articles;
return articles.filter(
(article) =>
article.title.toLowerCase().includes(q) ||
article.content.toLowerCase().includes(q),
);
}, [articles, query]);
const featured = filtered[0];
const publishedAt = (article: Article): string => {
const date = article.createdAt instanceof Date ? article.createdAt : new Date(article.createdAt);
return Number.isNaN(date.getTime())
? ''
: date.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' });
};
return (
<div className="container-page">
{/* Search / filter bar (client-side filter for now) */}
<form
role="search"
className="mb-8 flex items-center gap-3"
onSubmit={(event) => event.preventDefault()}
>
<label htmlFor="article-search" className="sr-only">
Search articles
</label>
<div className="relative w-full max-w-md">
<Search
className="pointer-events-none absolute left-3 top-1/2 h-4 w-4 -translate-y-1/2 text-slate-400"
aria-hidden="true"
/>
<input
id="article-search"
type="search"
placeholder="Search articles"
value={query}
onChange={(event) => setQuery(event.target.value)}
className="w-full rounded-md border border-slate-300 bg-white py-2 pl-9 pr-3 text-sm text-slate-900 placeholder:text-slate-400 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500"
/>
</div>
<select
aria-label="Filter by category"
className="rounded-md border border-slate-300 bg-white py-2 pl-3 pr-8 text-sm text-slate-600"
defaultValue=""
disabled
title="Category filtering arrives with a later milestone"
>
<option value="">All categories</option>
</select>
</form>
{source === 'sample' && !loading && (
<p
data-testid="sample-data-note"
className="mb-6 flex items-center gap-2 rounded-md border border-amber-200 bg-amber-50 px-4 py-3 text-sm text-amber-800"
>
<Info className="h-4 w-4 shrink-0" aria-hidden="true" />
Showing sample data — live articles arrive once the backend article
endpoints are available.
</p>
)}
{/* Hero: featured article */}
{featured && (
<section
aria-label="Featured article"
className="mb-10 overflow-hidden rounded-xl border border-slate-200 bg-white shadow-sm"
>
<div className="grid gap-6 p-6 sm:p-8 md:grid-cols-[2fr,1fr] md:items-center">
<div>
<div className="mb-3 flex items-center gap-3">
<span className="rounded-full bg-blue-600 px-2.5 py-0.5 text-xs font-semibold uppercase tracking-wide text-white">
Featured
</span>
{featured.category && <CategoryBadge category={featured.category} />}
</div>
<h1 className="mb-3 text-2xl font-bold leading-tight text-slate-900 sm:text-3xl">
{featured.title}
</h1>
<p className="mb-4 text-sm leading-relaxed text-slate-600 sm:text-base">
{featured.content}
</p>
<div className="flex flex-wrap items-center gap-x-4 gap-y-2 text-sm text-slate-500">
<span className="flex items-center gap-2">
<AuthorAvatar
author={featured.author ?? { name: 'Unknown author' }}
size="sm"
/>
{featured.author?.name ?? 'Unknown author'}
</span>
<span>{publishedAt(featured)}</span>
<span>{getReadTime(featured.content)} min read</span>
</div>
</div>
<div className="flex md:justify-end">
<Link to={`/articles/${featured.id}`} className="btn">
Read article
<ArrowRight className="h-4 w-4" aria-hidden="true" />
</Link>
</div>
</div>
</section>
)}
{/* Article grid */}
<section aria-label="Latest articles" className="mb-10">
<h2 className="mb-4 text-lg font-semibold text-slate-900">Latest articles</h2>
{loading ? (
<div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3" aria-busy="true">
{[0, 1, 2].map((index) => (
<div
key={index}
className="h-80 animate-pulse rounded-lg border border-slate-200 bg-white p-4"
/>
))}
</div>
) : filtered.length > 0 ? (
<div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3">
{filtered.map((article) => (
<ArticleCard key={article.id} article={article} />
))}
</div>
) : (
<p className="rounded-md border border-dashed border-slate-300 bg-white px-4 py-8 text-center text-sm text-slate-500">
No articles match your search.
</p>
)}
</section>
{/* Pagination placeholder */}
<nav aria-label="Pagination" className="flex items-center justify-center gap-2">
<button type="button" className="btn-secondary" disabled>
Previous
</button>
<span className="text-sm text-slate-500">Page 1 of 1</span>
<button type="button" className="btn-secondary" disabled>
Next
</button>
</nav>
</div>
);
}
export default Home;