import { useState, useEffect, useRef } from "react"; import getSchedInfo from "../../utils/getScheduleInfo.utils"; import { Pagination, Navigation } from "swiper/modules"; import { Swiper, SwiperSlide } from "swiper/react"; import { FaChevronLeft, FaChevronRight } from "react-icons/fa"; import BouncingLoader from "../ui/bouncingloader/Bouncingloader"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faPlay } from "@fortawesome/free-solid-svg-icons"; import "./schedule.css"; import { Link } from "react-router-dom"; const Schedule = () => { const [dates, setDates] = useState([]); const [loading, setLoading] = useState(false); const [error, setError] = useState(null); const [showAll, setShowAll] = useState(false); const [currentActiveIndex, setCurrentActiveIndex] = useState(null); const [scheduleData, setscheduleData] = useState([]); const [currentTime, setCurrentTime] = useState(new Date()); const cardRefs = useRef([]); const swiperRef = useRef(null); const currentDate = new Date(); const year = currentDate.getFullYear(); const month = currentDate.getMonth(); const monthName = currentDate.toLocaleString("default", { month: "short" }); const daysInMonth = new Date(year, month + 1, 0).getDate(); const GMTOffset = `GMT ${ new Date().getTimezoneOffset() > 0 ? "-" : "+" }${String(Math.floor(Math.abs(new Date().getTimezoneOffset()) / 60)).padStart( 2, "0" )}:${String(Math.abs(new Date().getTimezoneOffset()) % 60).padStart(2, "0")}`; const months = []; useEffect(() => { for (let day = 1; day <= daysInMonth; day++) { const date = new Date(year, month, day); const dayname = date.toLocaleString("default", { weekday: "short" }); const yearr = date.getFullYear(); const monthh = String(date.getMonth() + 1).padStart(2, "0"); const dayy = String(date.getDate()).padStart(2, "0"); const fulldate = `${yearr}-${monthh}-${dayy}`; months.push({ day, monthName, dayname, fulldate }); } setDates(months); const timer = setInterval(() => { setCurrentTime(new Date()); }, 1000); return () => clearInterval(timer); }, []); useEffect(() => { const todayIndex = dates.findIndex( (date) => date.fulldate === `${currentDate.getFullYear()}-${String( currentDate.getMonth() + 1 ).padStart(2, "0")}-${String(currentDate.getDate()).padStart(2, "0")}` ); if (todayIndex !== -1) { setCurrentActiveIndex(todayIndex); toggleActive(todayIndex); } }, [dates]); const fetchSched = async (date) => { try { setLoading(true); // Check if cached data exists const cachedData = localStorage.getItem(`schedule-${date}`); if (cachedData) { const parsedData = JSON.parse(cachedData); setscheduleData(Array.isArray(parsedData) ? parsedData : []); } else { const data = await getSchedInfo(date); setscheduleData(Array.isArray(data) ? data : []); localStorage.setItem(`schedule-${date}`, JSON.stringify(data || [])); } } catch (err) { console.error("Error fetching schedule info:", err); setError(err); } finally { setLoading(false); } }; const toggleActive = (index) => { cardRefs.current.forEach((card) => { if (card) { card.classList.remove("active"); } }); if (cardRefs.current[index]) { cardRefs.current[index].classList.add("active"); if (dates[index] && dates[index].fulldate) { fetchSched(dates[index].fulldate); } setCurrentActiveIndex(index); } }; const toggleShowAll = () => { setShowAll(!showAll); }; useEffect(() => { setShowAll(false); if (currentActiveIndex !== null && swiperRef.current) { swiperRef.current.slideTo(currentActiveIndex); } }, [currentActiveIndex]); return ( <>
Estimated Schedule

({GMTOffset}) {currentTime.toLocaleDateString()}{" "} {currentTime.toLocaleTimeString()}

(swiperRef.current = swiper)} > {dates && dates.map((date, index) => (
(cardRefs.current[index] = el)} onClick={() => toggleActive(index)} className={`h-[60px] flex flex-col justify-center items-center w-full text-center rounded-lg cursor-pointer transition-all duration-200 ${ currentActiveIndex === index ? "bg-white text-black" : "bg-zinc-800 text-white hover:bg-zinc-700" }`} >
{date.dayname}
{date.monthName} {date.day}
))}
{loading ? (
) : !scheduleData || scheduleData.length === 0 ? (
No data to display
) : error ? (
Something went wrong
) : (
{(showAll ? scheduleData : Array.isArray(scheduleData) ? scheduleData.slice(0, 7) : [] ).map((item, idx) => (
{item.time || "N/A"}

{item.title || "N/A"}

EP {item.episode_no || "N/A"}

))} {scheduleData.length > 7 && ( )}
)} ); }; export default Schedule;