71 lines
2.4 KiB
JavaScript
71 lines
2.4 KiB
JavaScript
import { OrderedMap, Map, fromJS } from "immutable"
|
|
|
|
const defaultState = Map({
|
|
loading: false,
|
|
show: Map({
|
|
seasons: OrderedMap(),
|
|
}),
|
|
});
|
|
|
|
const handlers = {
|
|
"SHOW_FETCH_DETAILS_PENDING": state => state.set("loading", true),
|
|
"SHOW_FETCH_DETAILS_FULFILLED": (state, action) => sortEpisodes(state, action.payload.response.data),
|
|
"SHOW_UPDATE_STORE_WISHLIST": (state, action) => {
|
|
return state.mergeDeep(fromJS({
|
|
"show": {
|
|
"tracked_season": action.payload.season,
|
|
"tracked_episode": action.payload.episode,
|
|
}
|
|
}))},
|
|
"EPISODE_GET_DETAILS_PENDING": (state, action) => state.setIn(["show", "seasons", action.payload.main.season, action.payload.main.episode, "fetching"], true),
|
|
"EPISODE_GET_DETAILS_FULFILLED": (state, action) => {
|
|
let data = action.payload.response.data;
|
|
if (!data) { return state }
|
|
data.fetching = false;
|
|
return state.setIn(["show", "seasons", data.season, data.episode], fromJS(data));
|
|
},
|
|
"EPISODE_SUBTITLES_UPDATE_PENDING" : (state, action) =>
|
|
state.setIn(["show", "seasons", action.payload.main.season, action.payload.main.episode, "fetchingSubtitles"], true),
|
|
"EPISODE_SUBTITLES_UPDATE_FULFILLED": (state, action) =>
|
|
state.setIn(["show", "seasons", action.payload.main.season, action.payload.main.episode, "subtitles"], fromJS(action.payload.response.data))
|
|
.setIn(["show", "seasons", action.payload.main.season, action.payload.main.episode, "fetchingSubtitles"], false),
|
|
}
|
|
|
|
const sortEpisodes = (state, show) => {
|
|
let episodes = show.episodes;
|
|
delete show["episodes"];
|
|
|
|
let ret = state.set("loading", false);
|
|
if (episodes.length == 0) {
|
|
return ret;
|
|
}
|
|
|
|
// Set the show data
|
|
ret = ret.set("show", fromJS(show));
|
|
|
|
// Set the show episodes
|
|
for (let ep of episodes) {
|
|
ep.fetching = false;
|
|
ret = ret.setIn(["show", "seasons", ep.season, ep.episode], fromJS(ep));
|
|
}
|
|
|
|
// Sort the episodes
|
|
ret = ret.updateIn(["show", "seasons"], function(seasons) {
|
|
return seasons.map(function(episodes) {
|
|
return episodes.sort((a,b) => a.get("episode") - b.get("episode"));
|
|
});
|
|
});
|
|
|
|
// Sort the seasons
|
|
ret = ret.updateIn(["show", "seasons"], function(seasons) {
|
|
return seasons.sort(function(a,b) {
|
|
return a.first().get("season") - b.first().get("season");
|
|
});
|
|
});
|
|
|
|
return ret
|
|
}
|
|
|
|
export default (state = defaultState, action) =>
|
|
handlers[action.type] ? handlers[action.type](state, action) : state;
|