"use client"; import { Link } from "@/components/navigation"; import { Card, CardContent } from "@/components/ui/card"; import { listCuratedContent } from "@/service/curated-content/curated-content"; import { getListContent } from "@/service/landing/landing"; import { formatDateToIndonesian } from "@/utils/globals"; import { Icon } from "@iconify/react/dist/iconify.js"; import image from "next/image"; import React, { useEffect, useState } from "react"; const VideoSliderPage = () => { const [allVideoData, setAllVideoData] = useState([]); const [displayVideos, setDisplayVideos] = useState([]); const [page, setPage] = useState(1); const [limit, setLimit] = React.useState(10); const [search, setSearch] = React.useState(""); useEffect(() => { fetchData(); }, [page, limit, search]); useEffect(() => { if (allVideoData?.length > 0) { shuffleAndSetVideos(); const interval = setInterval(shuffleAndSetVideos, 5000); return () => clearInterval(interval); // Cleanup interval on unmount } }, [allVideoData]); const fetchData = async () => { const response = await listCuratedContent(search, limit, page - 1, 1, "2"); console.log(response); const data = response?.data?.data; const contentData = data?.content; setAllVideoData(contentData); }; const shuffleAndSetVideos = () => { const shuffled = shuffleArray([...allVideoData]); setDisplayVideos(shuffled.slice(0, 3)); }; const shuffleArray = (array: any[]) => { for (let i = array.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [array[i], array[j]] = [array[j], array[i]]; } return array; }; return (
{displayVideos.map((video: any) => (
{formatDateToIndonesian(new Date(video?.createdAt))}{" "} {video?.timezone || "WIB"} | {video?.clickCount}
{video?.title}
))}
); }; export default VideoSliderPage;