"use client"; import { Link } from "@/components/navigation"; import { Card, CardContent } from "@/components/ui/card"; 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); useEffect(() => { initFetch(); }, []); useEffect(() => { if (allVideoData?.length > 0) { shuffleAndSetVideos(); const interval = setInterval(shuffleAndSetVideos, 5000); return () => clearInterval(interval); // Cleanup interval on unmount } }, [allVideoData]); const initFetch = async () => { const response = await getListContent({ page: page - 1, size: 12, sortBy: "createdAt", contentTypeId: "2", }); setAllVideoData(response?.data?.data?.content || []); }; 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;