File size: 4,347 Bytes
f5071ca |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
import { API_KEY, API_URL } from '../config'
// Action types for Home
export const GET_POPULAR_MOVIES = "GET_POPULAR_MOVIES";
export const SEARCH_MOVIES = "SEARCH_MOVIES";
export const LOAD_MORE_MOVIES = "LOAD_MORE_MOVIES";
export const CLEAR_MOVIES = "CLEAR_MOVIES";
// action types for Movie
export const GET_MOVIE = "GET_MOVIE";
export const CLEAR_MOVIE = "CLEAR_MOVIE";
// Action type for both Home and Movie Component
// We are showing the loading spinner when fetching the movies
export const SHOW_LOADING_SPINNER = "SHOW_LOADING_SPINNER";
// action creator for Both
export function showLoadingSpinner() {
return {
type: SHOW_LOADING_SPINNER,
payload: null
}
}
// action creators for Movie
export function clearMovie() {
return {
type: CLEAR_MOVIE,
payload: null
}
}
export function getMovie(movieId) {
let endpoint = `${API_URL}movie/${movieId}?api_key=${API_KEY}&language=en-US`;
let newState = [];
const request = fetch(endpoint)
.then(result => result.json())
.then(result => {
if (result.status.code) {
//if we don't find any movie
return newState;
} else {
newState = { movie: result };
endpoint = `${API_URL}movie/${movieId}/credits?api_key=${API_KEY}`;
return fetch(endpoint)
.then(result => result.json())
.then(result => {
const directors = result.crew.filter((member) => member.job === "Director");
newState.actors = result.cast;
newState.directors = directors;
return newState;
})
}
})
.catch(error => console.error("Error:", error));
return {
type: GET_MOVIE,
payload: request
}
}
// action creators for Home
export function getPopularMovies() {
const endpoint = `${API_URL}movie/popular?api_key=${API_KEY}&language=en-US&page=1`;
const request = fetch(endpoint)
.then(result => result.json())
.then(result => {
return result;
})
.catch(error => console.error('Error:', error));
// Since action is an object
return {
type: GET_POPULAR_MOVIES,
// request contains all the movies we fetching here
payload: request
}
}
export function searchMovies(searchTerm) {
let endpoint;
if (!searchTerm) {
endpoint = `${API_URL}movie/popular?api_key=${API_KEY}&language=en-US&page=1`;
} else {
endpoint = `${API_URL}search/movie?api_key=${API_KEY}&language=en-US&query=${searchTerm}`;
}
const request = fetch(endpoint)
.then(result => result.json())
.then(result => {
return { ...result, searchTerm };
})
.catch(error => console.error('Error:', error));
// Since action is an object
return {
type: SEARCH_MOVIES,
// request contains all the movies we fetching here
payload: request
}
}
export function loadMoreMovies(searchTerm, currentPage) {
let endpoint;
if (!searchTerm) {
endpoint = `${API_URL}movie/popular?api_key=${API_KEY}&language=en-US&page=${currentPage + 1}`;
} else {
endpoint = `${API_URL}search/movie?api_key=${API_KEY}&language=en-US&query=${searchTerm}&page=${currentPage + 1}`;
}
const request = fetch(endpoint)
.then(result => result.json())
.then(result => {
return result;
})
.catch(error => console.error('Error:', error));
// Since action is an object
return {
type: LOAD_MORE_MOVIES,
// request contains all the movies we fetching here
payload: request
}
}
export function clearMovies() {
return {
type: CLEAR_MOVIES,
payload: null
}
}
// this.setState({
// movies: [...movies, ...result.results],
// heroImage: heroImage || result.results[0],
// loading: false,
// currentPage: result.page,
// totalPages: result.total_pages
// , () => {
// // Remember state for the next mount if we´re not in a search view
// if (searchTerm === "") {
// sessionStorage.setItem('HomeState', JSON.stringify(this.state));
// }
// }) |