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 {
|
2024-09-07 09:12:48 +02:00
|
|
|
Pagination,
|
|
|
|
PaginationContent,
|
|
|
|
PaginationItem,
|
|
|
|
PaginationLink,
|
|
|
|
PaginationNext,
|
|
|
|
PaginationPrevious
|
2024-08-08 19:23:56 +02:00
|
|
|
} from "@/components/ui/pagination";
|
2024-08-20 16:28:54 +02:00
|
|
|
import { format } from "date-fns";
|
2024-08-08 11:21:56 +02:00
|
|
|
|
|
|
|
interface BlogPostcard {
|
2024-09-07 09:12:48 +02:00
|
|
|
title: string;
|
|
|
|
smallDescription: string;
|
|
|
|
currentSlug: string;
|
|
|
|
titleImage: string;
|
|
|
|
_createdAt: string;
|
2024-08-08 11:21:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 14:24:30 +02:00
|
|
|
interface BlogCardInterface {
|
|
|
|
page: number;
|
2024-08-08 19:23:56 +02:00
|
|
|
}
|
|
|
|
|
2024-09-07 14:24:30 +02:00
|
|
|
const BlogCards: React.FC<BlogCardInterface> = async (props) => {
|
2024-09-07 13:24:41 +02:00
|
|
|
"use server";
|
2024-09-07 14:24:30 +02:00
|
|
|
|
|
|
|
// Change in /blog/page/[id] route too!
|
2024-09-07 09:12:48 +02:00
|
|
|
const cardsPerPage = 6;
|
2024-09-07 14:24:30 +02:00
|
|
|
const currentPage = props.page;
|
2024-08-08 19:23:56 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const query = `*[_type == 'blog'] | order(_createdAt desc) {
|
2024-08-26 18:20:10 +02:00
|
|
|
title,
|
|
|
|
smallDescription,
|
|
|
|
"currentSlug": slug.current,
|
|
|
|
titleImage,
|
|
|
|
_createdAt
|
|
|
|
}[${(currentPage - 1) * cardsPerPage}...${currentPage * cardsPerPage}]`;
|
2024-08-08 11:21:56 +02:00
|
|
|
|
2024-09-07 14:31:17 +02:00
|
|
|
const posts: BlogPostcard[] = await client.fetch(
|
|
|
|
query,
|
|
|
|
{},
|
|
|
|
{ cache: "no-store" }
|
|
|
|
);
|
2024-08-08 11:21:56 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const totalPostsQuery = `count(*[_type == 'blog'])`;
|
|
|
|
const totalPosts: number = await client.fetch(totalPostsQuery);
|
2024-08-08 11:21:56 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const totalPages = Math.ceil(totalPosts / cardsPerPage);
|
2024-08-03 11:06:50 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<section className="grid max-w-6xl gap-4 mx-auto sm:grid-cols-2 lg:grid-cols-3">
|
|
|
|
{posts.map((post, idx) => {
|
|
|
|
const formattedDate = format(
|
|
|
|
new Date(post._createdAt),
|
|
|
|
"MMMM d, yyyy"
|
|
|
|
);
|
2024-08-20 16:28:54 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
const truncatedDescription =
|
|
|
|
post.smallDescription.length > 130
|
|
|
|
? post.smallDescription.substring(0, 130) + "..."
|
|
|
|
: post.smallDescription;
|
2024-08-26 18:20:10 +02:00
|
|
|
|
2024-09-07 09:12:48 +02:00
|
|
|
return (
|
|
|
|
<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
|
2024-09-07 13:30:30 +02:00
|
|
|
src={
|
|
|
|
post.titleImage
|
|
|
|
? urlFor(post.titleImage).url()
|
|
|
|
: "/blog-missing.png"
|
|
|
|
}
|
2024-09-07 09:12:48 +02:00
|
|
|
alt={post.title}
|
|
|
|
width={500}
|
|
|
|
height={300}
|
|
|
|
priority
|
|
|
|
className="w-full object-cover transition-transform duration-200 group-hover:scale-105"
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<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">
|
|
|
|
{truncatedDescription}
|
|
|
|
</p>
|
|
|
|
<p className="text-xs text-muted-foreground mt-2">
|
|
|
|
Published on: {formattedDate}
|
|
|
|
</p>
|
|
|
|
</CardContent>
|
|
|
|
</Link>
|
|
|
|
</Card>
|
|
|
|
);
|
|
|
|
})}
|
|
|
|
</section>
|
|
|
|
<div className="flex-center mt-12">
|
|
|
|
{totalPages > 1 && (
|
|
|
|
<Pagination>
|
|
|
|
<PaginationContent>
|
|
|
|
<PaginationItem>
|
|
|
|
{currentPage > 1 && (
|
2024-09-07 14:24:30 +02:00
|
|
|
<PaginationPrevious href={`/blog/page/${currentPage - 1}`} />
|
2024-09-07 09:12:48 +02:00
|
|
|
)}
|
|
|
|
</PaginationItem>
|
|
|
|
{Array.from({ length: totalPages }).map((_, i) => (
|
|
|
|
<PaginationItem key={i}>
|
|
|
|
<PaginationLink
|
2024-09-07 14:24:30 +02:00
|
|
|
href={`/blog/page/${i + 1}`}
|
2024-09-07 09:12:48 +02:00
|
|
|
isActive={currentPage === i + 1}
|
|
|
|
>
|
|
|
|
{i + 1}
|
|
|
|
</PaginationLink>
|
|
|
|
</PaginationItem>
|
|
|
|
))}
|
|
|
|
<PaginationItem>
|
|
|
|
{currentPage < totalPages && (
|
2024-09-07 14:24:30 +02:00
|
|
|
<PaginationNext href={`/blog/page/${currentPage + 1}`} />
|
2024-09-07 09:12:48 +02:00
|
|
|
)}
|
|
|
|
</PaginationItem>
|
|
|
|
</PaginationContent>
|
|
|
|
</Pagination>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
</>
|
|
|
|
);
|
2024-08-03 11:06:50 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
export default BlogCards;
|