From 13d836946edf2879c996986609af24166638010a Mon Sep 17 00:00:00 2001 From: Captain Jack Sparrow <163903675+sussy-code@users.noreply.github.com> Date: Sun, 16 Jun 2024 16:54:36 +0000 Subject: [PATCH] polishing episode selector --- src/components/media/ModalEpisodeSelector.tsx | 86 +++++++++++++------ src/components/media/PopupModal.tsx | 37 ++++---- 2 files changed, 74 insertions(+), 49 deletions(-) diff --git a/src/components/media/ModalEpisodeSelector.tsx b/src/components/media/ModalEpisodeSelector.tsx index d056ac35..298a74b9 100644 --- a/src/components/media/ModalEpisodeSelector.tsx +++ b/src/components/media/ModalEpisodeSelector.tsx @@ -1,4 +1,5 @@ -import React, { useEffect, useState } from 'react'; +import React, { useEffect, useState } from "react"; + import { get } from "@/backend/metadata/tmdb"; import { conf } from "@/setup/config"; @@ -6,49 +7,78 @@ interface ModalEpisodeSelectorProps { tmdbId: string; } -interface Season { - season_number: number; -} - -export const EpisodeSelector: React.FC = ({ tmdbId }) => { +export function EpisodeSelector({ tmdbId }: ModalEpisodeSelectorProps) { const [seasonsData, setSeasonsData] = useState([]); + const [selectedSeason, setSelectedSeason] = useState(null); useEffect(() => { const fetchSeasons = async () => { try { - // Fetch TV show details to get seasons list const showDetails = await get(`/tv/${tmdbId}`, { api_key: conf().TMDB_READ_API_KEY, language: "en-US", }); - - const seasons = showDetails.seasons as Season[]; - const seasonsDetailsPromises = seasons.map(season => - get(`/tv/${tmdbId}/season/${season.season_number}`, { - api_key: conf().TMDB_READ_API_KEY, - language: "en-US", - }) - ); - - // Fetch details for all seasons concurrently - const seasonsDetails = await Promise.all(seasonsDetailsPromises); - setSeasonsData(seasonsDetails); + setSeasonsData(showDetails.seasons); } catch (err) { console.error(err); } }; - fetchSeasons(); }, [tmdbId]); - console.log(seasonsData) + const handleSeasonSelect = async (season: any) => { + try { + const seasonDetails = await get( + `/tv/${tmdbId}/season/${season.season_number}`, + { + api_key: conf().TMDB_READ_API_KEY, + language: "en-US", + }, + ); + setSelectedSeason(seasonDetails); + } catch (err) { + console.error(err); + } + }; + return ( -
- {seasonsData.map((season, index) => ( -
- Season {season.season_number}: {season.name} -
- ))} +
+
+ {seasonsData.map((season) => ( +
handleSeasonSelect(season)} + className="cursor-pointer hover:bg-search-background p-1 text-center rounded" + > + S{season.season_number} +
+ ))} +
+
+ {selectedSeason ? ( +
+

+ {selectedSeason.name} - {selectedSeason.episodes.length} episodes +

+
    + {selectedSeason.episodes.map( + ( + episode: { episode_number: number; name: string }, + index: number, + ) => ( +
  • + {episode.episode_number}. {episode.name} +
  • + ), + )} +
+
+ ) : ( +
+

Select a season to see details

+
+ )} +
); -}; \ No newline at end of file +} diff --git a/src/components/media/PopupModal.tsx b/src/components/media/PopupModal.tsx index d139ad29..5ec8f9cc 100644 --- a/src/components/media/PopupModal.tsx +++ b/src/components/media/PopupModal.tsx @@ -5,8 +5,8 @@ import { useNavigate } from "react-router-dom"; import { get } from "@/backend/metadata/tmdb"; import { conf } from "@/setup/config"; import { MediaItem } from "@/utils/mediaTypes"; -import { EpisodeSelector } from "./ModalEpisodeSelector"; +import { EpisodeSelector } from "./ModalEpisodeSelector"; import { Button } from "../buttons/Button"; import { Icon, Icons } from "../Icon"; @@ -154,7 +154,7 @@ export function PopupModal({ >
@@ -194,11 +194,9 @@ export function PopupModal({ const releaseInfo = mediaInfo?.results?.find( (result: any) => result.iso_3166_1 === "US", ); - return releaseInfo && releaseInfo.rating ? ( - releaseInfo.rating - ) : ( - - ); + return releaseInfo && releaseInfo.rating + ? releaseInfo.rating + : "NR"; })()}
@@ -245,25 +243,22 @@ export function PopupModal({
{data?.genres?.length > 0 ? data.genres.map((genre: { name: string }) => ( -
- {genre.name} +
+
+ {genre.name} +
)) - : Array.from({ length: 3 }).map((_) => )} + : Array.from({ length: 3 }).map((_) => ( +
+ +
+ ))}
-
+
{data?.overview}
-
- {isTVShow ? ( - - ): null} -
+
{isTVShow ? : null}