|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import React, { useEffect, useState } from "react"; |
| 4 | +import { motion } from "framer-motion"; |
| 5 | +import Navbar from "@/components/Navbar"; |
| 6 | +import Footer from "@/components/Footer"; |
| 7 | +import { usePathname, useSearchParams } from "next/navigation"; |
| 8 | +import GlobalLoading from "@/components/GlobalLoading"; |
| 9 | +import SeasonFeed from "@/components/SeasonFeed"; |
| 10 | + |
| 11 | +type Props = {}; |
| 12 | + |
| 13 | +function SeasonPage({}: Props) { |
| 14 | + const pathname = usePathname(); |
| 15 | + const searchParams = useSearchParams()!; |
| 16 | + const [seasons, setSeasons] = useState([]); |
| 17 | + const [loading, setLoading] = useState(true); |
| 18 | + |
| 19 | + const search = searchParams.get("sessionNumber"); |
| 20 | + |
| 21 | + const fetchData = async (seasonId: string, seasonNumber: string | null) => { |
| 22 | + if (!seasonId && !seasonNumber) return; |
| 23 | + |
| 24 | + try { |
| 25 | + setLoading(true); |
| 26 | + |
| 27 | + const seasonsData = await fetch( |
| 28 | + `https://api.themoviedb.org/3/tv/${seasonId}/season/${seasonNumber}?api_key=${process.env.NEXT_PUBLIC_API_KEY}&language=en-US` |
| 29 | + ).then((res) => res.json()); |
| 30 | + |
| 31 | + setSeasons(seasonsData.episodes); |
| 32 | + |
| 33 | + setTimeout(() => { |
| 34 | + setLoading(false); |
| 35 | + }, 2000); |
| 36 | + } catch (error: any) { |
| 37 | + console.log("🚀 ~ file: page.tsx:23 ~ fetchData ~ error:", error); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + useEffect(() => { |
| 42 | + if (!pathname) return; |
| 43 | + |
| 44 | + const replaceName = pathname.replace("/season/", ""); |
| 45 | + fetchData(replaceName, search!); |
| 46 | + }, []); |
| 47 | + |
| 48 | + return ( |
| 49 | + <motion.div |
| 50 | + initial={{ opacity: 0 }} |
| 51 | + whileInView={{ opacity: 1 }} |
| 52 | + viewport={{ once: true }} |
| 53 | + > |
| 54 | + <Navbar /> |
| 55 | + <GlobalLoading isLoading={loading} /> |
| 56 | + <main> |
| 57 | + <SeasonFeed seasons={seasons} /> |
| 58 | + </main> |
| 59 | + <Footer /> |
| 60 | + </motion.div> |
| 61 | + ); |
| 62 | +} |
| 63 | + |
| 64 | +export default SeasonPage; |
0 commit comments