-
Notifications
You must be signed in to change notification settings - Fork 53
/
oop-script.js
103 lines (91 loc) · 3.08 KB
/
oop-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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
//the API documentation site https://developers.themoviedb.org/3/
class App {
static async run() {
const movies = await APIService.fetchMovies()
HomePage.renderMovies(movies);
}
}
class APIService {
static TMDB_BASE_URL = 'https://api.themoviedb.org/3';
static async fetchMovies() {
const url = APIService._constructUrl(`movie/now_playing`)
const response = await fetch(url)
const data = await response.json()
return data.results.map(movie => new Movie(movie))
}
static async fetchMovie(movieId) {
const url = APIService._constructUrl(`movie/${movieId}`)
const response = await fetch(url)
const data = await response.json()
return new Movie(data)
}
static _constructUrl(path) {
return `${this.TMDB_BASE_URL}/${path}?api_key=${atob('NTQyMDAzOTE4NzY5ZGY1MDA4M2ExM2M0MTViYmM2MDI=')}`;
}
}
class HomePage {
static container = document.getElementById('container');
static renderMovies(movies) {
movies.forEach(movie => {
const movieDiv = document.createElement("div");
const movieImage = document.createElement("img");
movieImage.src = `${movie.backdropUrl}`;
const movieTitle = document.createElement("h3");
movieTitle.textContent = `${movie.title}`;
movieImage.addEventListener("click", function() {
Movies.run(movie);
});
movieDiv.appendChild(movieTitle);
movieDiv.appendChild(movieImage);
this.container.appendChild(movieDiv);
})
}
}
class Movies {
static async run(movie) {
const movieData = await APIService.fetchMovie(movie.id)
MoviePage.renderMovieSection(movieData);
APIService.fetchActors(movieData)
}
}
class MoviePage {
static container = document.getElementById('container');
static renderMovieSection(movie) {
MovieSection.renderMovie(movie);
}
}
class MovieSection {
static renderMovie(movie) {
MoviePage.container.innerHTML = `
<div class="row">
<div class="col-md-4">
<img id="movie-backdrop" src=${movie.backdropUrl}>
</div>
<div class="col-md-8">
<h2 id="movie-title">${movie.title}</h2>
<p id="genres">${movie.genres}</p>
<p id="movie-release-date">${movie.releaseDate}</p>
<p id="movie-runtime">${movie.runtime}</p>
<h3>Overview:</h3>
<p id="movie-overview">${movie.overview}</p>
</div>
</div>
<h3>Actors:</h3>
`;
}
}
class Movie {
static BACKDROP_BASE_URL = 'http://image.tmdb.org/t/p/w780';
constructor(json) {
this.id = json.id;
this.title = json.title;
this.releaseDate = json.release_date;
this.runtime = json.runtime + " minutes";
this.overview = json.overview;
this.backdropPath = json.backdrop_path;
}
get backdropUrl() {
return this.backdropPath ? Movie.BACKDROP_BASE_URL + this.backdropPath : "";
}
}
document.addEventListener("DOMContentLoaded", App.run);