2025-01-24 17:24:45 -05:00
|
|
|
'use client';
|
|
|
|
|
|
|
|
import { useEffect, useState } from 'react';
|
|
|
|
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
|
|
|
|
import Link from "next/link"
|
|
|
|
import strings from "@/strings.json"
|
|
|
|
import { formatDistanceToNow, format } from 'date-fns'
|
|
|
|
|
2025-01-30 19:59:24 -05:00
|
|
|
type Post = {
|
|
|
|
id: string;
|
|
|
|
title: string;
|
|
|
|
description: string;
|
|
|
|
date: number;
|
|
|
|
slug: string;
|
|
|
|
};
|
|
|
|
|
2025-01-24 17:24:45 -05:00
|
|
|
export default function CategorySlug() {
|
|
|
|
const [posts, setPosts] = useState([]);
|
|
|
|
const [error, setError] = useState('');
|
|
|
|
const [category, setCategory] = useState('Category View'); // TODO: needs a better title
|
|
|
|
const [loading, setLoading] = useState(true);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2025-01-30 19:59:24 -05:00
|
|
|
console.log(strings.logsFetchPostList);
|
2025-01-24 17:24:45 -05:00
|
|
|
(async () => {
|
|
|
|
try {
|
|
|
|
const catReq = await fetch(`http://localhost:3001/api/categories/fetchList`, {
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
const postReq = await fetch(`http://localhost:3001/api/posts/fetchList`, {
|
|
|
|
method: 'GET',
|
|
|
|
});
|
|
|
|
|
|
|
|
if (!catReq.ok) {
|
2025-01-30 19:59:24 -05:00
|
|
|
throw new Error(`${strings.errorsFetchCategoryListFailCtx} ${catReq.status}`);
|
2025-01-24 17:24:45 -05:00
|
|
|
}
|
|
|
|
if (!postReq.ok) {
|
2025-01-30 19:59:24 -05:00
|
|
|
throw new Error(`${strings.errorsFetchPostListFailCtx} ${postReq.status}`);
|
2025-01-24 17:24:45 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
const catData = await catReq.json();
|
|
|
|
const postData = await postReq.json();
|
|
|
|
|
|
|
|
if (!catData) {
|
2025-01-30 19:59:24 -05:00
|
|
|
setError(strings.errorsFetchCategoryListFail);
|
2025-01-24 17:24:45 -05:00
|
|
|
} else {
|
2025-01-30 19:59:24 -05:00
|
|
|
console.log(strings.logsOnFetchedCategory);
|
2025-01-24 17:24:45 -05:00
|
|
|
const slug = window.location.pathname.split('/').slice(-1)[0];
|
|
|
|
const category = catData.categories.find((cat: { slug: string }) => cat.slug === slug);
|
|
|
|
if (category) {
|
2025-01-30 19:59:24 -05:00
|
|
|
console.log(`${strings.logsOnFoundCategory} ${category.name}`);
|
2025-01-24 17:24:45 -05:00
|
|
|
setCategory(category.name);
|
|
|
|
if (postData.success === false) {
|
|
|
|
if (postData.message) {
|
|
|
|
throw new Error(postData.message);
|
|
|
|
} else {
|
2025-01-30 19:59:24 -05:00
|
|
|
throw new Error(strings.errorsUnknownError);
|
2025-01-24 17:24:45 -05:00
|
|
|
}
|
|
|
|
} else {
|
2025-01-30 19:59:24 -05:00
|
|
|
const sortedPosts = postData.posts.sort((a: Post, b: Post) => b.date - a.date);
|
2025-01-24 17:24:45 -05:00
|
|
|
setPosts(sortedPosts);
|
|
|
|
}
|
|
|
|
} else {
|
2025-01-30 19:59:24 -05:00
|
|
|
setError(strings.errorsFetchCategoryNotFound);
|
|
|
|
throw new Error(strings.errorsFetchCategoryNotFound);
|
2025-01-24 17:24:45 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (error) {
|
2025-01-30 19:59:24 -05:00
|
|
|
console.error(strings.errorsFetchPostListErrCtxFancy, error);
|
|
|
|
setError(strings.errorsFetchPostListErr);
|
2025-01-24 17:24:45 -05:00
|
|
|
} finally {
|
|
|
|
setLoading(false);
|
|
|
|
}
|
|
|
|
})();
|
|
|
|
}, []);
|
|
|
|
|
2025-01-30 19:59:24 -05:00
|
|
|
interface FormatDate {
|
|
|
|
(timestamp: number): string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const formatDate: FormatDate = (timestamp) => {
|
2025-01-24 17:24:45 -05:00
|
|
|
const date = new Date(timestamp * 1000);
|
|
|
|
const now = new Date();
|
|
|
|
if (date.getFullYear() !== now.getFullYear()) {
|
|
|
|
return format(date, 'MM/DD/YYYY');
|
|
|
|
} else {
|
|
|
|
return formatDistanceToNow(date, { addSuffix: true });
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div className="space-y-8">
|
|
|
|
{!loading && <h1 className="text-4xl font-bold text-primary">{category}</h1>}
|
|
|
|
{loading ? (
|
|
|
|
<div className="flex items-center justify-center h-[80vh]">
|
|
|
|
<div className="animate-spin rounded-full h-16 w-16 border-4 border-t-slate-800 border-white"></div>
|
|
|
|
</div>
|
|
|
|
) : error ? (
|
|
|
|
<div className="text-red-500">{error}</div>
|
|
|
|
) : (
|
|
|
|
<div className="grid gap-6 sm:grid-cols-2 lg:grid-cols-3">
|
|
|
|
{posts.map((post) => (
|
|
|
|
<Card key={post.id} className="flex flex-col justify-between border-border/40 hover:border-border/60 transition-colors">
|
|
|
|
<CardHeader>
|
|
|
|
<div className="flex justify-between items-start">
|
|
|
|
<CardTitle className="text-xl text-primary">{post.title}</CardTitle>
|
|
|
|
</div>
|
|
|
|
<CardDescription className="mt-2">{post.description}</CardDescription>
|
|
|
|
</CardHeader>
|
|
|
|
<CardContent>
|
|
|
|
<p className="text-sm text-muted-foreground">
|
|
|
|
{strings.recentPostsPublishedOnLabel} {formatDate(post.date)}
|
|
|
|
</p>
|
|
|
|
</CardContent>
|
|
|
|
<CardFooter className="flex justify-end">
|
|
|
|
<Link
|
|
|
|
href={`/posts/${post.slug}`}
|
|
|
|
className="text-sm font-medium text-primary hover:underline"
|
|
|
|
>
|
|
|
|
{strings.recentPostsReadMoreFromLinkText}
|
|
|
|
</Link>
|
|
|
|
</CardFooter>
|
|
|
|
</Card>
|
|
|
|
))}
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|