web-humas-fe/components/page/related-news.tsx

86 lines
2.9 KiB
TypeScript

"use client";
import { Card, CardBody, CardFooter, Image } from "@nextui-org/react";
import React, { useEffect, useState } from "react";
import { EyeIcon, UnderLine } from "../icons";
import { getListArticle } from "@/service/article";
import { Autoplay, Pagination, Navigation } from "swiper/modules";
import { Swiper, SwiperSlide } from "swiper/react";
import "swiper/css";
import "swiper/css/pagination";
import "swiper/css/effect-fade";
import "swiper/css/navigation";
import Link from "next/link";
import { convertDateFormat, textEllipsis } from "@/utils/global";
export default function RelatedNews() {
const [article, setArticle] = useState<any>([]);
useEffect(() => {
async function getArticle() {
const req = { page: 1, search: "", limit: "10" };
const response = await getListArticle(req);
setArticle(response?.data?.data);
}
getArticle();
}, []);
return (
<div className="flex flex-col items-center">
<div className="text-2xl font-bold flex flex-col items-center mb-3">
<div>Berita Terkait</div>
<div>
<UnderLine />
</div>
</div>
<div className="w-[90vw] lg:w-full bg-[#f0f0f0]">
<div>
<Swiper
centeredSlides={false}
spaceBetween={10}
autoplay={{
delay: 5000,
disableOnInteraction: false,
}}
navigation={true}
modules={[Autoplay, Navigation]}
className="mySwiper"
slidesPerView={1}
breakpoints={{
// When the window width is less than 640px
720: {
slidesPerView: 4, // Set slidesPerView to 1 on mobile
},
}}
>
{article?.map((newsItem: any) => (
<SwiperSlide key={newsItem.id}>
<Card isFooterBlurred radius="lg" className="border-none">
<img
alt="headernews"
src="/headernews.png"
className="h-[25vh] object-cover rounded-none"
/>
<CardFooter className="before:bg-white/10 border-white/20 border-1 overflow-hidden py-1 md:absolute bottom-1 shadow-small z-10">
<div className="text-black dark:text-white lg:text-white">
<Link
href={`/news/detail/${newsItem.id}-${newsItem.slug}`}
>
<p className="text-left font-semibold text-sm">
{textEllipsis(newsItem.title, 40)}
</p>
</Link>
<p className="py-[2px] text-left text-sm">
{convertDateFormat(newsItem.createdAt)} WIB
</p>
</div>
</CardFooter>
</Card>
</SwiperSlide>
))}
</Swiper>
</div>
</div>
</div>
);
}