2022-03-13 17:26:46 +01:00
|
|
|
import { IconPatch } from "components/buttons/IconPatch";
|
2022-03-13 16:55:59 +01:00
|
|
|
import { Dropdown, OptionItem } from "components/Dropdown";
|
2022-03-13 17:26:46 +01:00
|
|
|
import { Icons } from "components/Icon";
|
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,
|
2022-03-13 16:55:59 +01:00
|
|
|
MWMediaSeason,
|
2022-03-06 18:31:22 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2022-03-13 17:26:46 +01:00
|
|
|
export function LoadingSeasons(props: { error?: boolean }) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<div>
|
|
|
|
<div className="bg-denim-400 mb-3 mt-5 h-10 w-56 rounded opacity-50" />
|
|
|
|
</div>
|
|
|
|
{!props.error ? (
|
|
|
|
<>
|
|
|
|
<div className="bg-denim-400 mr-3 mb-3 inline-block h-10 w-10 rounded opacity-50" />
|
|
|
|
<div className="bg-denim-400 mr-3 mb-3 inline-block h-10 w-10 rounded opacity-50" />
|
|
|
|
<div className="bg-denim-400 mr-3 mb-3 inline-block h-10 w-10 rounded opacity-50" />
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
<div className="flex items-center space-x-3">
|
|
|
|
<IconPatch icon={Icons.WARNING} className="text-red-400" />
|
|
|
|
<p>Failed to load seasons and episodes</p>
|
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-06 18:31:22 +01:00
|
|
|
export function Seasons(props: SeasonsProps) {
|
|
|
|
const [searchSeasons, loading, error, success] = useLoading(
|
|
|
|
(portableMedia: MWPortableMedia) => getSeasonDataFromMedia(portableMedia)
|
|
|
|
);
|
|
|
|
const history = useHistory();
|
|
|
|
const [seasons, setSeasons] = useState<MWMediaSeasons>({ seasons: [] });
|
2022-03-13 16:55:59 +01:00
|
|
|
const seasonSelected = props.media.seasonId as string;
|
|
|
|
const episodeSelected = props.media.episodeId as string;
|
2022-03-06 18:31:22 +01:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
(async () => {
|
|
|
|
const seasonData = await searchSeasons(props.media);
|
|
|
|
setSeasons(seasonData);
|
|
|
|
})();
|
|
|
|
}, [searchSeasons, props.media]);
|
|
|
|
|
2022-03-13 16:55:59 +01:00
|
|
|
function navigateToSeasonAndEpisode(seasonId: string, episodeId: string) {
|
2022-03-06 18:31:22 +01:00
|
|
|
const newMedia: MWMedia = { ...props.media };
|
2022-03-13 16:55:59 +01:00
|
|
|
newMedia.episodeId = episodeId;
|
|
|
|
newMedia.seasonId = seasonId;
|
2022-03-06 18:31:22 +01:00
|
|
|
history.replace(
|
|
|
|
`/media/${newMedia.mediaType}/${serializePortableMedia(
|
|
|
|
convertMediaToPortable(newMedia)
|
|
|
|
)}`
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-03-13 16:55:59 +01:00
|
|
|
const mapSeason = (season: MWMediaSeason) => ({
|
|
|
|
id: season.id,
|
|
|
|
name: season.title || `Season ${season.sort}`,
|
|
|
|
});
|
2022-03-13 14:34:32 +01:00
|
|
|
|
2022-03-13 16:55:59 +01:00
|
|
|
const options = seasons.seasons.map(mapSeason);
|
|
|
|
|
|
|
|
const foundSeason = seasons.seasons.find(
|
|
|
|
(season) => season.id === seasonSelected
|
|
|
|
);
|
|
|
|
const selectedItem = foundSeason ? mapSeason(foundSeason) : null;
|
2022-03-13 14:34:32 +01:00
|
|
|
|
2022-03-06 18:31:22 +01:00
|
|
|
return (
|
|
|
|
<>
|
2022-03-13 17:26:46 +01:00
|
|
|
{loading ? <LoadingSeasons /> : null}
|
|
|
|
{error ? <LoadingSeasons error /> : null}
|
2022-03-06 18:31:22 +01:00
|
|
|
{success && seasons.seasons.length ? (
|
|
|
|
<>
|
|
|
|
<Dropdown
|
2022-03-13 16:55:59 +01:00
|
|
|
selectedItem={selectedItem as OptionItem}
|
2022-03-13 14:34:32 +01:00
|
|
|
options={options}
|
|
|
|
setSelectedItem={(seasonItem) =>
|
2022-03-06 18:31:22 +01:00
|
|
|
navigateToSeasonAndEpisode(
|
2022-03-13 14:34:32 +01:00
|
|
|
seasonItem.id,
|
2022-03-13 16:55:59 +01:00
|
|
|
seasons.seasons.find((s) => s.id === seasonItem.id)?.episodes[0]
|
|
|
|
.id as string
|
2022-03-06 18:31:22 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
/>
|
2022-03-13 16:55:59 +01:00
|
|
|
{seasons.seasons
|
|
|
|
.find((s) => s.id === seasonSelected)
|
|
|
|
?.episodes.map((v) => (
|
|
|
|
<WatchedEpisode
|
|
|
|
key={v.id}
|
|
|
|
media={{
|
|
|
|
...props.media,
|
|
|
|
seriesData: seasons,
|
|
|
|
episodeId: v.id,
|
|
|
|
seasonId: seasonSelected,
|
|
|
|
}}
|
|
|
|
active={v.id === episodeSelected}
|
|
|
|
onClick={() => navigateToSeasonAndEpisode(seasonSelected, v.id)}
|
|
|
|
/>
|
|
|
|
))}
|
2022-03-06 18:31:22 +01:00
|
|
|
</>
|
|
|
|
) : null}
|
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|