Created
November 30, 2021 22:36
-
-
Save Tsugami/2e9d9a14689852a87d72f89f987781b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const watchedEpisodesNow = ( | |
media: IMedia, | |
season: ISeason, | |
episode: IEpisode, | |
watched: WatchedDocument | null, | |
sawPrevius: boolean, | |
): [IEpisode[], SeasonWatch[]] => { | |
if (!watched) { | |
// CREATE NEW WATCHED | |
if (sawPrevius) { | |
const [episodes, seasons] = media.seasons.reduce<[IEpisode[], SeasonWatch[]]>( | |
(acc, s) => { | |
if (s.seasonNumber > season.seasonNumber) return acc; | |
const watchedEpisodeNumbers = s.episodes.filter( | |
(e) => | |
e.seasonNumber < season.seasonNumber || | |
(e.seasonNumber === season.seasonNumber && e.episodeNumber <= episode.episodeNumber), | |
); | |
acc[0].push(...watchedEpisodeNumbers); | |
acc[1].push({ | |
seasonID: String(s._id.toString()), | |
watchedEpisodeNumbers: watchedEpisodeNumbers.map((e) => e.episodeNumber), | |
}); | |
return acc; | |
}, | |
[[], []], | |
); | |
return [episodes, seasons]; | |
} | |
return [ | |
[episode], | |
[{ seasonID: String(season._id.toString()), watchedEpisodeNumbers: [episode.episodeNumber] }], | |
]; | |
} | |
const watchedSeason = watched?.seasons.find((s) => compareID(s.seasonID, season._id)); | |
const episdodeAlreadySeen = | |
watchedSeason?.watchedEpisodeNumbers?.includes(episode.episodeNumber) ?? false; | |
if (episdodeAlreadySeen) { | |
// REMOVE WATCHED EPISODE | |
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion | |
watchedSeason!.watchedEpisodeNumbers = watchedSeason!.watchedEpisodeNumbers.filter( | |
(e) => e !== episode.episodeNumber, | |
); | |
return [[episode], watched.seasons]; | |
} | |
if (!sawPrevius) { | |
// ADD WATCHED EPISODE | |
if (watchedSeason) { | |
watchedSeason.watchedEpisodeNumbers.push(episode.episodeNumber); | |
} else { | |
watched.seasons.push({ | |
seasonID: season._id, | |
watchedEpisodeNumbers: [episode.episodeNumber], | |
}); | |
} | |
return [[episode], watched.seasons]; | |
} | |
// ADD WATCHED EPISODE AND MODIFY PREVIOUS | |
const watchedEpisodes: IEpisode[] = []; | |
const watchedSeasons: SeasonWatch[] = []; | |
for (const s of media.seasons) { | |
if (s.seasonNumber > season.seasonNumber) { | |
break; | |
} | |
const watchedEpisodeNumbers = s.episodes.filter( | |
(e) => | |
e.seasonNumber < season.seasonNumber || | |
(e.seasonNumber === season.seasonNumber && e.episodeNumber <= episode.episodeNumber), | |
); | |
const watchedSeason = watched.seasons.find((ws) => compareID(ws.seasonID, s._id)); | |
watchedEpisodes.push( | |
...watchedEpisodeNumbers.filter( | |
(v) => !watchedSeason?.watchedEpisodeNumbers?.includes(v.episodeNumber), | |
), | |
); | |
watchedSeasons.push({ | |
seasonID: String(s?._id?.toString()), | |
watchedEpisodeNumbers: watchedEpisodeNumbers.map((e) => e.episodeNumber), | |
}); | |
} | |
return [watchedEpisodes, watchedSeasons]; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment