forked from hudahamid/movie-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
81 lines (72 loc) · 2.56 KB
/
script.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
'use strict';
const TMDB_BASE_URL = "https://api.themoviedb.org/3";
const PROFILE_BASE_URL = "http://image.tmdb.org/t/p/w185";
const BACKDROP_BASE_URL = "http://image.tmdb.org/t/p/w780";
const CONTAINER = document.querySelector(".container");
// Don't touch this function please
const autorun = async () => {
const movies = await fetchMovies();
renderMovies(movies.results);
};
// Don't touch this function please
const constructUrl = (path) => {
return `${TMDB_BASE_URL}/${path}?api_key=${atob(
"NTQyMDAzOTE4NzY5ZGY1MDA4M2ExM2M0MTViYmM2MDI="
)}`;
};
// You may need to add to this function, definitely don't delete it.
const movieDetails = async (movie) => {
const movieRes = await fetchMovie(movie.id);
renderMovie(movieRes);
};
// This function is to fetch movies. You may need to add it or change some part in it in order to apply some of the features.
const fetchMovies = async () => {
const url = constructUrl(`movie/now_playing`);
const res = await fetch(url);
return res.json();
};
// Don't touch this function please. This function is to fetch one movie.
const fetchMovie = async (movieId) => {
const url = constructUrl(`movie/${movieId}`);
const res = await fetch(url);
return res.json();
};
// You'll need to play with this function in order to add features and enhance the style.
const renderMovies = (movies) => {
movies.map((movie) => {
const movieDiv = document.createElement("div");
movieDiv.innerHTML = `
<img src="${BACKDROP_BASE_URL + movie.backdrop_path}" alt="${
movie.title
} poster">
<h3>${movie.title}</h3>`;
movieDiv.addEventListener("click", () => {
movieDetails(movie);
});
CONTAINER.appendChild(movieDiv);
});
};
// You'll need to play with this function in order to add features and enhance the style.
const renderMovie = (movie) => {
CONTAINER.innerHTML = `
<div class="row">
<div class="col-md-4">
<img id="movie-backdrop" src=${
BACKDROP_BASE_URL + movie.backdrop_path
}>
</div>
<div class="col-md-8">
<h2 id="movie-title">${movie.title}</h2>
<p id="movie-release-date"><b>Release Date:</b> ${
movie.release_date
}</p>
<p id="movie-runtime"><b>Runtime:</b> ${movie.runtime} Minutes</p>
<h3>Overview:</h3>
<p id="movie-overview">${movie.overview}</p>
</div>
</div>
<h3>Actors:</h3>
<ul id="actors" class="list-unstyled"></ul>
</div>`;
};
document.addEventListener("DOMContentLoaded", autorun);