62 lines
1.6 KiB
JavaScript
62 lines
1.6 KiB
JavaScript
const defaultState = {
|
|
loading: false,
|
|
movies: [],
|
|
filter: "",
|
|
perPage: 30,
|
|
selectedImdbId: "",
|
|
fetchingDetails: false,
|
|
search: "",
|
|
};
|
|
|
|
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.data.length > 0) {
|
|
selectedImdbId = action.payload.data[0].imdb_id;
|
|
}
|
|
return Object.assign({}, state, {
|
|
movies: action.payload.data,
|
|
selectedImdbId: selectedImdbId,
|
|
loading: false,
|
|
})
|
|
case 'SEARCH_MOVIES_PENDING':
|
|
return Object.assign({}, state, {
|
|
loading: true,
|
|
})
|
|
case 'SEARCH_MOVIES_FULFILLED':
|
|
return Object.assign({}, state, {
|
|
movies: action.payload.data,
|
|
loading: false,
|
|
})
|
|
case 'MOVIE_GET_DETAILS_PENDING':
|
|
return Object.assign({}, state, {
|
|
fetchingDetails: true,
|
|
})
|
|
case 'MOVIE_GET_DETAILS_FULFILLED':
|
|
let movies = state.movies.slice();
|
|
let index = movies.map((el) => el.imdb_id).indexOf(action.payload.data.imdb_id);
|
|
movies[index] = action.payload.data;
|
|
return Object.assign({}, state, {
|
|
movies: movies,
|
|
fetchingDetails: false,
|
|
})
|
|
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
|
|
}
|
|
}
|