Update the show reducer from switch to map
This commit is contained in:
parent
e286f5d2da
commit
7014b3933d
@ -1,4 +1,4 @@
|
|||||||
import { OrderedMap, Map, List, fromJS } from 'immutable'
|
import { OrderedMap, Map, fromJS } from "immutable"
|
||||||
|
|
||||||
const defaultState = Map({
|
const defaultState = Map({
|
||||||
loading: false,
|
loading: false,
|
||||||
@ -7,13 +7,10 @@ const defaultState = Map({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
export default function showStore(state = defaultState, action) {
|
const handlers = {
|
||||||
switch (action.type) {
|
"SHOW_FETCH_DETAILS_PENDING": state => state.set("loading", true),
|
||||||
case 'SHOW_FETCH_DETAILS_PENDING':
|
"SHOW_FETCH_DETAILS_FULFILLED": (state, action) => sortEpisodes(state, action.payload.response.data),
|
||||||
return state.set('loading', true)
|
"SHOW_UPDATE_STORE_WISHLIST": (state, action) => {
|
||||||
case 'SHOW_FETCH_DETAILS_FULFILLED':
|
|
||||||
return sortEpisodes(state, action.payload.response.data);
|
|
||||||
case 'SHOW_UPDATE_STORE_WISHLIST':
|
|
||||||
let season = action.payload.season;
|
let season = action.payload.season;
|
||||||
let episode = action.payload.episode;
|
let episode = action.payload.episode;
|
||||||
if (action.payload.wishlisted && season === null) {
|
if (action.payload.wishlisted && season === null) {
|
||||||
@ -21,74 +18,61 @@ export default function showStore(state = defaultState, action) {
|
|||||||
episode = 0;
|
episode = 0;
|
||||||
}
|
}
|
||||||
return state.mergeDeep(fromJS({
|
return state.mergeDeep(fromJS({
|
||||||
'show': {
|
"show": {
|
||||||
'tracked_season': season,
|
"tracked_season": season,
|
||||||
'tracked_episode': episode,
|
"tracked_episode": episode,
|
||||||
}
|
}
|
||||||
}));
|
}))},
|
||||||
case 'EPISODE_GET_DETAILS_PENDING':
|
"EPISODE_GET_DETAILS_PENDING": (state, action) => state.setIn(["show", "seasons", action.payload.main.season, action.payload.main.episode, "fetching"], true),
|
||||||
return state.setIn(['show', 'seasons', action.payload.main.season, action.payload.main.episode, 'fetching'], true);
|
"EPISODE_GET_DETAILS_FULFILLED": (state, action) => {
|
||||||
case 'EPISODE_GET_DETAILS_FULFILLED':
|
|
||||||
let data = action.payload.response.data;
|
let data = action.payload.response.data;
|
||||||
if (!data) { return state }
|
if (!data) { return state }
|
||||||
data.fetching = false;
|
data.fetching = false;
|
||||||
return state.setIn(['show', 'seasons', data.season, data.episode], fromJS(data));
|
return state.setIn(["show", "seasons", data.season, data.episode], fromJS(data));
|
||||||
case 'EPISODE_SUBTITLES_UPDATE_PENDING':
|
},
|
||||||
return state.setIn(['show', 'seasons', action.payload.main.season, action.payload.main.episode, 'fetchingSubtitles'], true);
|
"EPISODE_SUBTITLES_UPDATE_PENDING": (state, action) => state.setIn(["show", "seasons", action.payload.main.season, action.payload.main.episode, "fetchingSubtitles"], true),
|
||||||
case 'EPISODE_SUBTITLES_UPDATE_FULFILLED':
|
"EPISODE_SUBTITLES_UPDATE_FULFILLED": (state, action) => {
|
||||||
let epId = ['show', 'seasons', action.payload.main.season, action.payload.main.episode];
|
let epId = ["show", "seasons", action.payload.main.season, action.payload.main.episode];
|
||||||
let ep = state.getIn(epId);
|
let ep = state.getIn(epId); (state, action) =>
|
||||||
ep = ep.set('subtitles', fromJS(action.payload.response.data)).set('fetchingSubtitles', false);
|
ep = ep.set("subtitles", fromJS(action.payload.response.data)).set("fetchingSubtitles", false);
|
||||||
return state.setIn(epId, ep);
|
return state.setIn(epId, ep);
|
||||||
default:
|
},
|
||||||
return state
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function updateEpisode(state, fetching, data = null) {
|
const sortEpisodes = (state, show) => {
|
||||||
if (data === null) {
|
|
||||||
return state;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!state.hasIn(['show', 'season', data.season, data.episode])) {
|
|
||||||
return show;
|
|
||||||
}
|
|
||||||
|
|
||||||
data.fetching = fetching
|
|
||||||
return show.updateIn(['show', 'seasons', data.season, data.episode], fromJS(data));
|
|
||||||
}
|
|
||||||
|
|
||||||
function sortEpisodes(state, show) {
|
|
||||||
let episodes = show.episodes;
|
let episodes = show.episodes;
|
||||||
delete show["episodes"];
|
delete show["episodes"];
|
||||||
|
|
||||||
let ret = state.set('loading', false);
|
let ret = state.set("loading", false);
|
||||||
if (episodes.length == 0) {
|
if (episodes.length == 0) {
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the show data
|
// Set the show data
|
||||||
ret = ret.set('show', fromJS(show));
|
ret = ret.set("show", fromJS(show));
|
||||||
|
|
||||||
// Set the show episodes
|
// Set the show episodes
|
||||||
for (let ep of episodes) {
|
for (let ep of episodes) {
|
||||||
ep.fetching = false;
|
ep.fetching = false;
|
||||||
ret = ret.setIn(['show', 'seasons', ep.season, ep.episode], fromJS(ep));
|
ret = ret.setIn(["show", "seasons", ep.season, ep.episode], fromJS(ep));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sort the episodes
|
// Sort the episodes
|
||||||
ret = ret.updateIn(['show', 'seasons'], function(seasons) {
|
ret = ret.updateIn(["show", "seasons"], function(seasons) {
|
||||||
return seasons.map(function(episodes) {
|
return seasons.map(function(episodes) {
|
||||||
return episodes.sort((a,b) => a.get('episode') - b.get('episode'));
|
return episodes.sort((a,b) => a.get("episode") - b.get("episode"));
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// Sort the seasons
|
// Sort the seasons
|
||||||
ret = ret.updateIn(['show', 'seasons'], function(seasons) {
|
ret = ret.updateIn(["show", "seasons"], function(seasons) {
|
||||||
return seasons.sort(function(a,b) {
|
return seasons.sort(function(a,b) {
|
||||||
return a.first().get('season') - b.first().get('season');
|
return a.first().get("season") - b.first().get("season");
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
return ret
|
return ret
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export default (state = defaultState, action) =>
|
||||||
|
handlers[action.type] ? handlers[action.type](state, action) : state;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user