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:
1 parent
cbeac5e16b
commit
ffe19272b3
45 files changed
+9843
-4
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import { Clock, FileText } from 'lucide-react';
|
||||
import { Link } from 'react-router-dom';
|
||||
import type { Article } from '../../types';
|
||||
import { cn } from '../../lib/utils';
|
||||
import { AuthorAvatar } from './AuthorAvatar';
|
||||
import { CategoryBadge } from './CategoryBadge';
|
||||
|
||||
const WORDS_PER_MINUTE = 200;
|
||||
|
||||
/** Estimate reading time in minutes (minimum 1). */
|
||||
export function getReadTime(content: string): number {
|
||||
const words = content.trim().split(/\s+/).filter(Boolean).length;
|
||||
return Math.max(1, Math.round(words / WORDS_PER_MINUTE));
|
||||
}
|
||||
|
||||
function formatPublished(value: Article['createdAt']): string {
|
||||
const date = value instanceof Date ? value : new Date(value);
|
||||
if (Number.isNaN(date.getTime())) return '';
|
||||
return date.toLocaleDateString('en-GB', { day: 'numeric', month: 'short', year: 'numeric' });
|
||||
}
|
||||
|
||||
export interface ArticleCardProps {
|
||||
article: Article;
|
||||
className?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reusable article preview: thumbnail placeholder, category badge, title,
|
||||
* excerpt, author (via AuthorAvatar) and estimated read time. Links to the
|
||||
* article detail route via react-router-dom.
|
||||
*/
|
||||
export function ArticleCard({ article, className }: ArticleCardProps) {
|
||||
const author =
|
||||
article.author ?? { id: article.authorId, name: 'Unknown author', email: '' };
|
||||
|
||||
return (
|
||||
<article
|
||||
data-testid="article-card"
|
||||
className={cn(
|
||||
'group flex flex-col overflow-hidden rounded-lg border border-slate-200 bg-white shadow-sm transition-shadow hover:shadow-md',
|
||||
className,
|
||||
)}
|
||||
>
|
||||
{/* Thumbnail placeholder (images arrive with a later milestone) */}
|
||||
<Link
|
||||
to={`/articles/${article.id}`}
|
||||
aria-label={`Read article: ${article.title}`}
|
||||
className="flex h-36 items-center justify-center bg-slate-100 focus:outline-none focus-visible:ring-2 focus-visible:ring-inset focus-visible:ring-blue-500"
|
||||
>
|
||||
<FileText className="h-10 w-10 text-slate-400" aria-hidden="true" />
|
||||
</Link>
|
||||
|
||||
<div className="flex flex-1 flex-col gap-2 p-4">
|
||||
{article.category && <CategoryBadge category={article.category} />}
|
||||
|
||||
<h3 className="text-base font-semibold leading-snug text-slate-900">
|
||||
<Link
|
||||
to={`/articles/${article.id}`}
|
||||
className="hover:text-blue-700 hover:underline focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500"
|
||||
>
|
||||
{article.title}
|
||||
</Link>
|
||||
</h3>
|
||||
|
||||
<p className="line-clamp-3 text-sm text-slate-600">{article.content}</p>
|
||||
</div>
|
||||
|
||||
<div className="flex items-center justify-between gap-2 border-t border-slate-100 px-4 py-3">
|
||||
<span className="flex min-w-0 items-center gap-2 text-sm text-slate-600">
|
||||
<AuthorAvatar author={author} size="sm" />
|
||||
<span className="truncate">{author.name}</span>
|
||||
</span>
|
||||
<span
|
||||
className="flex shrink-0 items-center gap-1 text-xs text-slate-500"
|
||||
title={formatPublished(article.createdAt)}
|
||||
>
|
||||
<Clock className="h-3.5 w-3.5" aria-hidden="true" />
|
||||
{getReadTime(article.content)} min read
|
||||
</span>
|
||||
</div>
|
||||
</article>
|
||||
);
|
||||
}
|
||||
|
||||
export default ArticleCard;
|
||||
Reference in new issue
Block a user