From 0150078df50cc4c9fa39aaf9c08d48edf07c1c9e Mon Sep 17 00:00:00 2001 From: Cooper Ransom Date: Mon, 8 Apr 2024 21:30:52 -0400 Subject: [PATCH] Add most popular shows --- src/pages/Discover.tsx | 49 ++++++++++++++++++++++++++++++++++++++---- src/utils/discover.ts | 11 ++++++++++ 2 files changed, 56 insertions(+), 4 deletions(-) diff --git a/src/pages/Discover.tsx b/src/pages/Discover.tsx index 03108aed..6e85e6f7 100644 --- a/src/pages/Discover.tsx +++ b/src/pages/Discover.tsx @@ -15,6 +15,7 @@ import { Movie, TVShow, categories, + tvCategories, } from "@/utils/discover"; import { PageTitle } from "./parts/util/PageTitle"; @@ -26,7 +27,7 @@ export function Discover() { const { t } = useTranslation(); const [showBg] = useState(false); const [genres, setGenres] = useState([]); - const [randomMovie, setRandomMovie] = useState(null); // Add this line + const [randomMovie, setRandomMovie] = useState(null); const [genreMovies, setGenreMovies] = useState<{ [genreId: number]: Movie[]; }>({}); @@ -36,6 +37,9 @@ export function Discover() { const bgColor = currentTheme?.extend?.colors?.background?.accentA ?? "#7831BF"; const navigate = useNavigate(); + const [categoryShows, setCategoryShows] = useState<{ + [categoryName: string]: Movie[]; + }>({}); const [categoryMovies, setCategoryMovies] = useState<{ [categoryName: string]: Movie[]; }>({}); @@ -71,6 +75,28 @@ export function Discover() { categories.forEach(fetchMoviesForCategory); }, []); + useEffect(() => { + const fetchShowsForCategory = async (category: Category) => { + try { + const data = await get(category.endpoint, { + api_key: conf().TMDB_READ_API_KEY, + language: "en-US", + }); + + setCategoryShows((prevCategoryShows) => ({ + ...prevCategoryShows, + [category.name]: data.results, + })); + } catch (error) { + console.error( + `Error fetching movies for category ${category.name}:`, + error, + ); + } + }; + tvCategories.forEach(fetchShowsForCategory); + }, []); + // Fetch TV show genres useEffect(() => { const fetchTVGenres = async () => { @@ -215,7 +241,7 @@ export function Discover() { }; function renderMovies(medias: Media[], category: string, isTVShow = false) { - const categorySlug = category.toLowerCase().replace(/ /g, "-"); // Convert the category to a slug + const categorySlug = `${category.toLowerCase().replace(/ /g, "-")}${Math.random()}`; // Convert the category to a slug const displayCategory = category === "Now Playing" ? "In Cinemas" @@ -342,8 +368,8 @@ export function Discover() { [data.genres[i], data.genres[j]] = [data.genres[j], data.genres[i]]; } - // Fetch only the first 6 genres - setGenres(data.genres.slice(0, 6)); + // Fetch only the first 5 genres + setGenres(data.genres.slice(0, 5)); } catch (error) { console.error("Error fetching genres:", error); } @@ -479,6 +505,21 @@ export function Discover() {

Shows

+ {tvCategories.map((category) => ( +
+ {renderMovies( + categoryShows[category.name] || [], + category.name, + true, + )} +
+ ))} {tvGenres.map((genre) => (