"use client"; import { useEffect, useState } from "react"; import { ChevronLeftIcon, ChevronRightIcon } from "../icons"; import { getListArticle } from "@/services/article"; import { convertDateFormat, textEllipsis } from "@/utils/global"; import Link from "next/link"; export default function NewsTicker() { const [article, setArticle] = useState([]); const [currentNewsIndex, setCurrentNewsIndex] = useState(0); const [animate, setAnimate] = useState(false); useEffect(() => { async function getArticle() { const req = { page: 1, search: "", limit: "10", isPublish: true }; const response = await getListArticle(req); setArticle(response?.data?.data); } getArticle(); }, []); const triggerAnimation = (newIndex: number) => { setAnimate(true); setTimeout(() => { setCurrentNewsIndex(newIndex); setAnimate(false); }, 300); }; const handlePrev = () => { const newIndex = (currentNewsIndex - 1 + article?.length) % article?.length; triggerAnimation(newIndex); }; const handleNext = () => { const newIndex = (currentNewsIndex + 1) % article?.length; triggerAnimation(newIndex); }; useEffect(() => { const interval = setInterval(() => { triggerAnimation((currentNewsIndex + 1) % article?.length); }, 7000); return () => clearInterval(interval); }, [article]); return (
BREAKING NEWS
{article?.length > 0 ? (

{article[currentNewsIndex]?.title}

{textEllipsis(article[currentNewsIndex]?.title, 28)}

{convertDateFormat(article[currentNewsIndex]?.createdAt)}

) : (

Loading...

)}
handlePrev()} > handleNext()} >
); }