144 lines
4.1 KiB
JavaScript
144 lines
4.1 KiB
JavaScript
import { produce } from "immer";
|
|
|
|
import { formatTorrents } from "../utils";
|
|
import { formatSubtitle, formatSubtitles } from "./utils";
|
|
|
|
const defaultState = {
|
|
loading: false,
|
|
show: {},
|
|
};
|
|
|
|
const formatEpisode = (episode) => {
|
|
// Format the episode's torrents
|
|
episode.torrents = formatTorrents(episode);
|
|
episode.subtitles = formatSubtitles(episode.subtitles);
|
|
|
|
// Set the default fetching data
|
|
episode.fetching = false;
|
|
// Holds the languages of the subtitles currently fetching
|
|
episode.fetchingSubtitles = [];
|
|
};
|
|
|
|
export default (state = defaultState, action) =>
|
|
produce(state, (draft) => {
|
|
switch (action.type) {
|
|
case "SHOW_FETCH_DETAILS_PENDING":
|
|
draft.loading = true;
|
|
break;
|
|
|
|
case "SHOW_FETCH_DETAILS_FULFILLED": {
|
|
let show = action.payload.response.data;
|
|
let episodes = show.episodes;
|
|
delete show.episodes;
|
|
draft.show = show;
|
|
|
|
let seasons = {};
|
|
let seasonNumbers = [];
|
|
if (!episodes) {
|
|
episodes = [];
|
|
}
|
|
episodes.forEach((episode) => {
|
|
// Skip special episodes
|
|
if (episode.season === 0) {
|
|
return;
|
|
}
|
|
|
|
// Format the episode from polochon
|
|
formatEpisode(episode);
|
|
|
|
if (!seasons[episode.season]) {
|
|
seasons[episode.season] = [];
|
|
seasonNumbers.push(episode.season);
|
|
}
|
|
seasons[episode.season].push(episode);
|
|
});
|
|
|
|
let seasonMap = new Map();
|
|
seasonNumbers
|
|
.sort((a, b) => a - b)
|
|
.forEach((n) => {
|
|
let episodes = [];
|
|
seasons[n]
|
|
.sort((a, b) => a.episode - b.episode)
|
|
.forEach((episode) => {
|
|
episodes.push([episode.episode, episode]);
|
|
});
|
|
seasonMap.set(n, new Map(episodes));
|
|
});
|
|
draft.show.seasons = seasonMap;
|
|
|
|
draft.loading = false;
|
|
break;
|
|
}
|
|
|
|
case "SHOW_GET_DETAILS_PENDING":
|
|
draft.show.fetchingDetails = true;
|
|
break;
|
|
|
|
case "SHOW_GET_DETAILS_FULFILLED":
|
|
draft.show.fetchingDetails = false;
|
|
break;
|
|
|
|
case "SHOW_UPDATE_STORE_WISHLIST":
|
|
// TODO: check with we give the imdb in the payload
|
|
draft.show.tracked_season = action.payload.season; // eslint-disable-line camelcase
|
|
draft.show.tracked_episode = action.payload.episode; // eslint-disable-line camelcase
|
|
break;
|
|
|
|
case "EPISODE_GET_DETAILS_PENDING":
|
|
draft.show.seasons
|
|
.get(action.payload.main.season)
|
|
.get(action.payload.main.episode).fetching = true;
|
|
break;
|
|
|
|
case "EPISODE_GET_DETAILS_FULFILLED": {
|
|
let episode = action.payload.response.data;
|
|
if (!episode) {
|
|
return draft;
|
|
}
|
|
formatEpisode(episode);
|
|
draft.show.seasons.get(episode.season).set(episode.episode, episode);
|
|
break;
|
|
}
|
|
|
|
case "EPISODE_SUBTITLES_UPDATE_PENDING": {
|
|
let season = action.payload.main.season;
|
|
let episode = action.payload.main.episode;
|
|
let lang = action.payload.main.lang;
|
|
draft.show.seasons
|
|
.get(season)
|
|
.get(episode)
|
|
.fetchingSubtitles.push(lang);
|
|
if (draft.show.seasons.get(season).get(episode).subtitles.get(lang)) {
|
|
draft.show.seasons
|
|
.get(season)
|
|
.get(episode)
|
|
.subtitles.get(lang).searching = true;
|
|
}
|
|
break;
|
|
}
|
|
|
|
case "EPISODE_SUBTITLES_UPDATE_FULFILLED": {
|
|
let season = action.payload.main.season;
|
|
let episode = action.payload.main.episode;
|
|
let lang = action.payload.main.lang;
|
|
let data = action.payload.response.data;
|
|
draft.show.seasons.get(season).get(episode).fetchingSubtitles =
|
|
draft.show.seasons
|
|
.get(season)
|
|
.get(episode)
|
|
.fetchingSubtitles.filter((l) => l != lang);
|
|
if (data) {
|
|
draft.show.seasons
|
|
.get(season)
|
|
.get(episode)
|
|
.subtitles.set(lang, formatSubtitle(data));
|
|
}
|
|
break;
|
|
}
|
|
|
|
default:
|
|
return draft;
|
|
}
|
|
});
|