2022-03-06 18:31:22 +01:00
|
|
|
import { Dropdown } from "components/Dropdown";
|
2022-03-13 15:27:08 +01:00
|
|
|
import { WatchedEpisode } from "components/media/WatchedEpisodeButton";
|
2022-03-06 18:31:22 +01:00
|
|
|
import { useLoading } from "hooks/useLoading";
|
|
|
|
import { serializePortableMedia } from "hooks/usePortableMedia";
|
|
|
|
import {
|
|
|
|
convertMediaToPortable,
|
|
|
|
MWMedia,
|
|
|
|
MWMediaSeasons,
|
|
|
|
MWPortableMedia,
|
|
|
|
} from "providers";
|
|
|
|
import { getSeasonDataFromMedia } from "providers/methods/seasons";
|
|
|
|
import { useEffect, useState } from "react";
|
|
|
|
import { useHistory } from "react-router-dom";
|
|
|
|
|
|
|
|
export interface SeasonsProps {
|
|
|
|
media: MWMedia;
|
|
|
|
}
|
|
|
|
|
|
|
|
export function Seasons(props: SeasonsProps) {
|
|
|
|
const [searchSeasons, loading, error, success] = useLoading(
|
|
|
|
(portableMedia: MWPortableMedia) => getSeasonDataFromMedia(portableMedia)
|
|
|
|
);
|
|
|
|
const history = useHistory();
|
|
|
|
const [seasons, setSeasons] = useState<MWMediaSeasons>({ seasons: [] });
|
|
|
|
const seasonSelected = props.media.season as number;
|
|
|
|
const episodeSelected = props.media.episode as number;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
const seasonData = await searchSeasons(props.media);
|
|
|
|
setSeasons(seasonData);
|
|
|
|
})();
|
|
|
|
}, [searchSeasons, props.media]);
|
|
|
|
|
|
|
|
function navigateToSeasonAndEpisode(season: number, episode: number) {
|
|
|
|
const newMedia: MWMedia = { ...props.media };
|
|
|
|
newMedia.episode = episode;
|
|
|
|
newMedia.season = season;
|
|
|
|
history.replace(
|
|
|
|
`/media/${newMedia.mediaType}/${serializePortableMedia(
|
|
|
|
convertMediaToPortable(newMedia)
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-13 14:34:32 +01:00
|
|
|
const options = seasons.seasons.map((season) => ({
|
|
|
|
id: season.seasonNumber,
|
|
|
|
name: `Season ${season.seasonNumber}`,
|
|
|
|
}));
|
|
|
|
|
|
|
|
const selectedItem = {
|
|
|
|
id: seasonSelected,
|
|
|
|
name: `Season ${seasonSelected}`,
|
|
|
|
};
|
|
|
|
|
2022-03-06 18:31:22 +01:00
|
|
|
return (
|
|
|
|
<>
|
|
|
|
{loading ? <p>Loading...</p> : null}
|
|
|
|
{error ? <p>error!</p> : null}
|
|
|
|
{success && seasons.seasons.length ? (
|
|
|
|
<>
|
|
|
|
<Dropdown
|
2022-03-13 14:34:32 +01:00
|
|
|
selectedItem={selectedItem}
|
|
|
|
options={options}
|
|
|
|
setSelectedItem={(seasonItem) =>
|
2022-03-06 18:31:22 +01:00
|
|
|
navigateToSeasonAndEpisode(
|
2022-03-13 14:34:32 +01:00
|
|
|
seasonItem.id,
|
|
|
|
seasons.seasons[seasonItem.id]?.episodes[0].episodeNumber
|
2022-03-06 18:31:22 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
{seasons.seasons[seasonSelected]?.episodes.map((v) => (
|
2022-03-13 15:27:08 +01:00
|
|
|
<WatchedEpisode
|
2022-03-06 18:31:22 +01:00
|
|
|
key={v.episodeNumber}
|
2022-03-13 15:27:08 +01:00
|
|
|
media={{
|
|
|
|
...props.media,
|
|
|
|
episode: v.episodeNumber,
|
|
|
|
season: seasonSelected,
|
|
|
|
}}
|
2022-03-06 18:31:22 +01:00
|
|
|
active={v.episodeNumber === episodeSelected}
|
|
|
|
onClick={() =>
|
|
|
|
navigateToSeasonAndEpisode(seasonSelected, v.episodeNumber)
|
|
|
|
}
|
|
|
|
/>
|
|
|
|
))}
|
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|