"use client"; import apiClient from "@/api/apiClient"; import { useEffect, useState, useRef } from "react"; import Link from "next/link"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { faCaretLeft, faCaretRight, faFilter, faTimes, faXmark, // Import the close icon } from "@fortawesome/free-solid-svg-icons"; import Image from "next/image"; import "./genres.css"; import GenreFilterModal from "@/modals/GenreFilterModal"; // Import the Modal import { Spinner } from "@/components/Spinner"; export default function GenrePage({ params }) { const [genreItems, setGenreItems] = useState(null); const [loading, setLoading] = useState(true); const [error, setError] = useState(null); const [itemLimit, setItemLimit] = useState(10); const [selectedGenres, setSelectedGenres] = useState( new Set(params.genre ? decodeURIComponent(params.genre).split(",") : []) ); const [isModalOpen, setIsModalOpen] = useState(false); // State for modal visibility const moviesRef = useRef(null); const seriesRef = useRef(null); useEffect(() => { async function fetchData() { try { if (selectedGenres.size === 0) { setGenreItems({ movies: [], series: [] }); setLoading(false); return; } const genreArray = Array.from(selectedGenres); const data = await apiClient.getGenreItems(genreArray, null, itemLimit); if (data) { setGenreItems(data); console.log(data); } else { setError("Genre data not found."); } } catch (err) { setError(err.message || "An error occurred while fetching genre data."); console.log(err); } finally { setLoading(false); } } fetchData(); }, [selectedGenres, itemLimit]); const loadMore = () => { setItemLimit((prevLimit) => prevLimit + 5); }; const scroll = (ref, direction) => { if (ref.current) { const scrollAmount = direction === "left" ? -360 : 360; ref.current.scrollBy({ left: scrollAmount, behavior: "smooth" }); } }; const openModal = () => setIsModalOpen(true); const closeModal = () => setIsModalOpen(false); const removeGenre = (genreToRemove) => { setSelectedGenres((prevGenres) => { const updatedGenres = new Set(prevGenres); updatedGenres.delete(genreToRemove); return updatedGenres; }); }; if (loading) { return (
{item[2]}
{item[2]}
{selectedGenres.size === 0 ? "Please select a genre to see results." : "No items available for the selected genres."}
)}