67 lines
3.1 KiB
JavaScript
67 lines
3.1 KiB
JavaScript
import { OrderedMap, Map, fromJS } from "immutable"
|
|
|
|
const defaultState = Map({
|
|
loading: false,
|
|
movies: OrderedMap(),
|
|
filter: "",
|
|
selectedImdbId: "",
|
|
lastFetchUrl: "",
|
|
exploreOptions: Map(),
|
|
});
|
|
|
|
const handlers = {
|
|
"MOVIE_LIST_FETCH_PENDING": state => state.set("loading", true),
|
|
"MOVIE_LIST_FETCH_ERROR": state => state.set("loading", false),
|
|
"MOVIE_LIST_FETCH_FULFILLED": (state, action) => {
|
|
let allMoviesInPolochon = true
|
|
let movies = Map()
|
|
action.payload.response.data.map(function (movie) {
|
|
movie.fetchingDetails = false;
|
|
movie.fetchingSubtitles = false;
|
|
if (movie.polochon_url === "") {
|
|
allMoviesInPolochon = false
|
|
}
|
|
movies = movies.set(movie.imdb_id, fromJS(movie));
|
|
})
|
|
|
|
// Select the first movie if the list is not empty
|
|
let selectedImdbId = "";
|
|
if (movies.size > 0) {
|
|
// Sort by year
|
|
movies = movies.sort((a,b) => {
|
|
if (!allMoviesInPolochon) {
|
|
return b.get("year") - a.get("year")
|
|
}
|
|
|
|
const dateA = new Date(a.get("date_added"))
|
|
const dateB = new Date(b.get("date_added"))
|
|
return dateA > dateB ? -1 : dateA < dateB ? 1 : 0;
|
|
});
|
|
|
|
selectedImdbId = movies.first().get("imdb_id");
|
|
}
|
|
|
|
return state.delete("movies").merge(Map({
|
|
movies: movies,
|
|
filter: "",
|
|
loading: false,
|
|
selectedImdbId: selectedImdbId,
|
|
}))
|
|
},
|
|
"MOVIE_GET_DETAILS_PENDING" : (state, action) => state.setIn(["movies", action.payload.main.imdbId, "fetchingDetails"], true),
|
|
"MOVIE_GET_DETAILS_FULFILLED" : (state, action) => state.setIn(["movies", action.payload.response.data.imdb_id], fromJS(action.payload.response.data))
|
|
.setIn(["movies", action.payload.response.data.imdb_id, "fetchingDetails"], false)
|
|
.setIn(["movies", action.payload.response.data.imdb_id, "fetchingSubtitles"], false),
|
|
"MOVIE_UPDATE_STORE_WISHLIST" : (state, action) => state.setIn(["movies", action.payload.imdbId, "wishlisted"], action.payload.wishlisted),
|
|
"MOVIE_GET_EXPLORE_OPTIONS_FULFILLED" : (state, action) => state.set("exploreOptions", fromJS(action.payload.response.data)),
|
|
"UPDATE_LAST_MOVIE_FETCH_URL" : (state, action) => state.set("lastFetchUrl", action.payload.url),
|
|
"MOVIE_SUBTITLES_UPDATE_PENDING" : (state, action) => state.setIn(["movies", action.payload.main.imdbId, "fetchingSubtitles"], true),
|
|
"MOVIE_SUBTITLES_UPDATE_FULFILLED" : (state, action) => state.setIn(["movies", action.payload.main.imdbId, "fetchingSubtitles"], false)
|
|
.setIn(["movies", action.payload.main.imdbId, "subtitles"], fromJS(action.payload.response.data)),
|
|
"SELECT_MOVIE" : (state, action) => state.set("selectedImdbId", action.payload.imdbId),
|
|
"MOVIE_UPDATE_FILTER" : (state, action) => state.set("filter", action.payload.filter),
|
|
}
|
|
|
|
export default (state = defaultState, action) =>
|
|
handlers[action.type] ? handlers[action.type](state, action) : state;
|