'use client';
import { useState, useEffect, useRef } from 'react';
import './HeroSection.css';
import apiClient from '@/api/apiClient';
import SkeletonLoader from '@/skeletons/HeroSection';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faPlay } from '@fortawesome/free-solid-svg-icons';
import Link from 'next/link';
const HeroSection = () => {
const [currentIndex, setCurrentIndex] = useState(0);
const intervalRef = useRef(null);
const [fadeOut, setFadeOut] = useState(false);
const [items, setItems] = useState([]);
const [loading, setLoading] = useState(true);
useEffect(() => {
const fetchRecentItems = async () => {
try {
const response = await apiClient.getRecent();
console.log(response);
const movies = response.movies.map(film => ({
title: film[0],
description: film[2],
imageUrl: film[3],
type: 'Movie'
}));
const series = response.series.map(serie => ({
title: serie[0],
description: serie[2],
imageUrl: serie[3],
type: 'Series'
}));
setItems([...movies, ...series]);
} catch (error) {
console.error('Error fetching recent items:', error);
} finally {
setLoading(false);
}
};
fetchRecentItems();
}, []);
const startAutoSwitch = () => {
if (intervalRef.current) clearInterval(intervalRef.current);
intervalRef.current = setInterval(() => {
setFadeOut(true);
setTimeout(() => {
setCurrentIndex(prevIndex => (prevIndex + 1) % items.length);
setFadeOut(false);
}, 200);
}, 5000);
};
useEffect(() => {
if (items.length > 0) {
startAutoSwitch();
}
return () => {
clearInterval(intervalRef.current);
};
}, [items]);
const handleIndicatorClick = (index) => {
setFadeOut(true);
setTimeout(() => {
setCurrentIndex(index);
setFadeOut(false);
}, 100);
startAutoSwitch();
};
if (loading) {
return
{description}