svrjs-nextjs-website/components/cards/BlogCards.tsx

116 lines
3.1 KiB
TypeScript
Raw Normal View History

2024-08-03 11:06:50 +02:00
import React from "react";
import Link from "next/link";
import Image from "next/image";
import { ExternalLink } from "lucide-react";
2024-08-08 11:21:56 +02:00
import { client, urlFor } from "@/lib/sanity";
import { Card, CardContent } from "../ui/card";
2024-08-08 19:23:56 +02:00
import {
Pagination,
PaginationContent,
PaginationItem,
PaginationLink,
PaginationNext,
PaginationPrevious,
} from "@/components/ui/pagination";
2024-08-08 11:21:56 +02:00
interface BlogPostcard {
title: string;
smallDescription: string;
currentSlug: string;
titleImage: string;
}
2024-08-08 19:23:56 +02:00
interface BlogCardsProps {
searchParams: { page?: string };
}
const BlogCards: React.FC<BlogCardsProps> = async ({ searchParams }) => {
const cardsPerPage = 6;
const currentPage = searchParams.page ? parseInt(searchParams.page) : 1;
// Fetch the blog posts
2024-08-08 11:21:56 +02:00
const query = `*[_type == 'blog'] | order(_createdAt desc) {
2024-08-08 19:23:56 +02:00
title,
2024-08-08 11:21:56 +02:00
smallDescription,
"currentSlug": slug.current,
titleImage
2024-08-08 19:23:56 +02:00
}[${(currentPage - 1) * cardsPerPage}...${currentPage * cardsPerPage}]`;
2024-08-08 11:21:56 +02:00
2024-08-08 19:23:56 +02:00
const posts: BlogPostcard[] = await client.fetch(query);
2024-08-08 11:21:56 +02:00
2024-08-08 19:23:56 +02:00
// Fetch the total number of blog posts
const totalPostsQuery = `count(*[_type == 'blog'])`;
const totalPosts: number = await client.fetch(totalPostsQuery);
2024-08-08 11:21:56 +02:00
2024-08-08 19:23:56 +02:00
const totalPages = Math.ceil(totalPosts / cardsPerPage);
2024-08-03 11:06:50 +02:00
return (
2024-08-08 19:23:56 +02:00
<>
<section className="grid max-w-6xl gap-4 mx-auto sm:grid-cols-2 lg:grid-cols-3">
{posts.map((post, idx) => (
<Card
className="group h-full w-full rounded-lg border overflow-hidden"
key={idx}
>
<Link href={`/blog/${post.currentSlug}`} className="block">
<div className="relative overflow-hidden rounded-t-lg">
<Image
src={urlFor(post.titleImage).url()}
alt={post.title}
width={500}
height={300}
priority
className="w-full object-cover transition-transform duration-200 group-hover:scale-105"
/>
2024-08-08 11:21:56 +02:00
</div>
2024-08-08 19:23:56 +02:00
<CardContent className="p-4">
<div className="flex-between mb-2 py-2">
<h3 className="text-xl font-semibold leading-tight">
{post.title}
</h3>
<div className="text-sm text-muted-foreground opacity-0 group-hover:opacity-100 duration-300">
<ExternalLink />
</div>
</div>
<p className="text-sm text-muted-foreground">
{post.smallDescription}
</p>
</CardContent>
</Link>
</Card>
))}
</section>
<div className="flex-center mt-12">
{totalPages > 1 && (
<Pagination>
<PaginationContent>
<PaginationItem>
{currentPage > 1 && (
<PaginationPrevious href={`?page=${currentPage - 1}`} />
)}
</PaginationItem>
{Array.from({ length: totalPages }).map((_, i) => (
<PaginationItem key={i}>
<PaginationLink
href={`?page=${i + 1}`}
isActive={currentPage === i + 1}
>
{i + 1}
</PaginationLink>
</PaginationItem>
))}
<PaginationItem>
{currentPage < totalPages && (
<PaginationNext href={`?page=${currentPage + 1}`} />
)}
</PaginationItem>
</PaginationContent>
</Pagination>
)}
</div>
</>
2024-08-03 11:06:50 +02:00
);
};
export default BlogCards;