-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
85 lines (73 loc) · 2.17 KB
/
index.js
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
import formatDateLocally from "../utils/formatDateLocally";
const BASE_URL = "https://api.themoviedb.org/3/movie";
const TMDB_API_KEY = "b69d9e5ccb7d627bc02f3d704e5ca529";
const LANGUAGE = "es-MX";
const normalizeResults = (results) => {
return results.map((result) => {
const {
id,
title,
release_date: releaseDate,
backdrop_path: backdropPath,
overview,
} = result;
return {
id,
title,
releaseDate: formatDateLocally(releaseDate),
backdropPath,
overview,
};
});
};
export const getPopularMovies = async () => {
const query = `${BASE_URL}/popular?api_key=${TMDB_API_KEY}&language=${LANGUAGE}&page=1`;
const res = await fetch(query);
const { results } = await res.json();
return normalizeResults(results);
};
export const getTopRatedMovies = async () => {
const query = `${BASE_URL}/top_rated?api_key=${TMDB_API_KEY}&language=${LANGUAGE}&page=1`;
const res = await fetch(query);
const { results } = await res.json();
return normalizeResults(results);
};
export const getUpcomingMovies = async () => {
const query = `${BASE_URL}/upcoming?api_key=${TMDB_API_KEY}&language=${LANGUAGE}&page=1`;
const res = await fetch(query);
const { results } = await res.json();
return normalizeResults(results);
};
export const getLatestMovies = async () => {
const query = `${BASE_URL}/now_playing?api_key=${TMDB_API_KEY}&language=${LANGUAGE}&page=1`;
const res = await fetch(query);
const { results } = await res.json();
return normalizeResults(results);
};
export const getMovieDetailsById = async (movieId) => {
const query = `${BASE_URL}/${movieId}?api_key=${TMDB_API_KEY}&language=${LANGUAGE}`;
const res = await fetch(query);
const data = await res.json();
const {
id,
title,
release_date: releaseDate,
backdrop_path: backdropPath,
overview,
} = data;
return {
id,
title,
releaseDate,
backdropPath,
overview,
};
};
export const getFavoritesMoviesWithDetails = async (moviesArray) => {
const favoritesMovies = await Promise.all(
moviesArray.map(async ({ movieId }) => {
return getMovieDetailsById(movieId);
})
);
return favoritesMovies;
};