web-humas-fe/components/landing/NewsTicker.tsx

80 lines
2.5 KiB
TypeScript

"use client";
import { useEffect, useState } from "react";
import { ChevronLeftIcon, ChevronRightIcon } from "../icons";
import { getListArticle } from "@/service/article";
import { convertDateFormat } from "@/utils/global";
export default function NewsTicker() {
const [article, setArticle] = useState<any>([]);
const [currentNewsIndex, setCurrentNewsIndex] = useState(0);
const [animate, setAnimate] = useState(false);
useEffect(() => {
async function getArticle() {
const req = { page: 1, search: "", limit: "10" };
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.length]);
return (
<div className="fixed bottom-0 z-50 flex flex-row h-[60px] gap-3 w-full justify-between bg-stone-800">
<div className="relative px-4 py-2 font-semibold text-sm flex items-center bg-amber-500 text-white w-[10%]">
<span className="mr-2 "></span> BREAKING NEWS
<div className="absolute right-0 top-0 h-full w-4 bg-amber-500 transform translate-x-full clip-path-triangle"></div>
</div>
<div
className={`w-full text-white px-5 py-1 flex flex-col gap-1 transition-transform duration-300 ${
animate ? "opacity-0 translate-y-5" : "opacity-100 translate-y-0"
}`}
>
<p>{article[currentNewsIndex]?.title}</p>
<p className="text-xs">
{convertDateFormat(article[currentNewsIndex]?.createdAt)}
</p>
</div>
<div className="flex flex-row text-white h-full gap-[1px]">
<a
className="bg-amber-500 h-full flex items-center"
onClick={() => handlePrev()}
>
<ChevronLeftIcon />
</a>
<a
className="bg-amber-500 h-full flex items-center"
onClick={() => handleNext()}
>
<ChevronRightIcon />
</a>
</div>
</div>
);
}