97 lines
3.0 KiB
JavaScript
97 lines
3.0 KiB
JavaScript
const defaultState = {
|
|
loading: false,
|
|
movies: [],
|
|
filter: "",
|
|
perPage: 30,
|
|
selectedImdbId: "",
|
|
fetchingDetails: false,
|
|
lastFetchUrl: "",
|
|
exploreOptions: {},
|
|
};
|
|
|
|
export default function movieStore(state = defaultState, action) {
|
|
switch (action.type) {
|
|
case 'MOVIE_LIST_FETCH_PENDING':
|
|
return Object.assign({}, state, {
|
|
loading: true,
|
|
})
|
|
case 'MOVIE_LIST_FETCH_FULFILLED':
|
|
let selectedImdbId = "";
|
|
// Select the first movie
|
|
if (action.payload.response.data.length > 0) {
|
|
// Sort by year
|
|
action.payload.response.data.sort((a,b) => b.year - a.year);
|
|
selectedImdbId = action.payload.response.data[0].imdb_id;
|
|
}
|
|
return Object.assign({}, state, {
|
|
movies: action.payload.response.data,
|
|
selectedImdbId: selectedImdbId,
|
|
filter: defaultState.filter,
|
|
perPage: defaultState.perPage,
|
|
loading: false,
|
|
})
|
|
case 'MOVIE_GET_DETAILS_PENDING':
|
|
return Object.assign({}, state, {
|
|
fetchingDetails: true,
|
|
})
|
|
case 'MOVIE_GET_DETAILS_FULFILLED':
|
|
return Object.assign({}, state, {
|
|
movies: updateMovieDetails(state.movies.slice(), action.payload.response.data.imdb_id, action.payload.response.data),
|
|
fetchingDetails: false,
|
|
})
|
|
case 'MOVIE_UPDATE_STORE_WISHLIST':
|
|
return Object.assign({}, state, {
|
|
movies: updateStoreWishlist(state.movies.slice(), action.payload.imdbId, action.payload.wishlisted),
|
|
})
|
|
case 'MOVIE_GET_EXPLORE_OPTIONS_FULFILLED':
|
|
return Object.assign({}, state, {
|
|
exploreOptions: action.payload.response.data,
|
|
})
|
|
case 'UPDATE_LAST_MOVIE_FETCH_URL':
|
|
return Object.assign({}, state, {
|
|
lastFetchUrl: action.payload.url,
|
|
})
|
|
case 'MOVIE_SUBTITLES_UPDATE_PENDING':
|
|
return Object.assign({}, state, {
|
|
movies: updateMovieSubtitles(state.movies.slice(), state.selectedImdbId, true),
|
|
})
|
|
case 'MOVIE_SUBTITLES_UPDATE_FULFILLED':
|
|
console.log("payload :", action.payload);
|
|
return Object.assign({}, state, {
|
|
movies: updateMovieSubtitles(state.movies.slice(), state.selectedImdbId, false, action.payload.response.data),
|
|
})
|
|
case 'SELECT_MOVIE':
|
|
// Don't select the movie if we're fetching another movie's details
|
|
if (state.fetchingDetails) {
|
|
return state
|
|
}
|
|
|
|
return Object.assign({}, state, {
|
|
selectedImdbId: action.imdbId,
|
|
})
|
|
default:
|
|
return state
|
|
}
|
|
}
|
|
|
|
function updateMovieDetails(movies, imdbId, data) {
|
|
let index = movies.map((el) => el.imdb_id).indexOf(imdbId);
|
|
movies[index] = data;
|
|
return movies
|
|
}
|
|
|
|
function updateStoreWishlist(movies, imdbId, wishlisted) {
|
|
let index = movies.map((el) => el.imdb_id).indexOf(imdbId);
|
|
movies[index].wishlisted = wishlisted;
|
|
return movies
|
|
}
|
|
|
|
function updateMovieSubtitles(movies, imdbId, fetching, data = null) {
|
|
let index = movies.map((el) => el.imdb_id).indexOf(imdbId);
|
|
if (data) {
|
|
movies[index].subtitles = data;
|
|
}
|
|
movies[index].fetchingSubtitles = fetching;
|
|
return movies
|
|
}
|