forked from mujz/recoded-oop-exercise-movie
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
91 lines (81 loc) · 2.58 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
82
83
84
85
86
87
88
89
90
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'
class App {
static run() {
APIService.fetchMovie(534)
.then(movie => Page.renderMovie(movie))
APIService.fetchAct(534)
.then(actor => Page.renderActor(actor))
}
}
class APIService {
static fetchMovie(movieId) {
const url = APIService._constructUrl(`movie/${movieId}`)
return fetch(url)
.then(res => res.json())
.then(json => new Movie(json))
}
static fetchAct(movieId) {//movie
const urlAct = APIService._constructUrl(`movie/${movieId}/credits`)//movie.id
return fetch(urlAct)
.then(res => res.json())
.then(json => {
let actors=[];
for (let i = 0; i <4; i++) {
actors.push(new Actor(json.cast[i]))
}
//
return actors;
})//should be an array
}
static _constructUrl(path) {
return `${TMDB_BASE_URL}/${path}?api_key=${atob('NTQyMDAzOTE4NzY5ZGY1MDA4M2ExM2M0MTViYmM2MDI=')}`
}
}
class Page {
static backdrop = document.getElementById('movie-backdrop')
static title = document.getElementById('movie-title')
static releaseDate = document.getElementById('movie-release-date')
static runtime = document.getElementById('movie-runtime')
static overview = document.getElementById('movie-overview')
static actors=document.getElementById('actors');
static renderMovie(movie) {
Page.backdrop.src = BACKDROP_BASE_URL + movie.backdropPath
Page.title.innerText = movie.title
Page.releaseDate.innerText = movie.releaseDate
Page.runtime.innerText = movie.runtime + " minutes"
Page.overview.innerText = movie.overview
}
static renderActor(act) {
//mmmmmm
act.map(e=>{//return an array and i dont need any return value so we will use the foreach act.foreach(e=>{
let img=document.createElement('img');
let p=document.createElement('p');
img.src=PROFILE_BASE_URL+"/"+e.pic1;
p.innerHTML=e.name;
let newLi=document.createElement('li');
newLi.appendChild(img);
newLi.appendChild(p);
newLi.className="col-sm-3";
Page.actors.appendChild(newLi);
})
}}
class Movie {
constructor(json) {
this.id = json.id
this.title = json.title
this.releaseDate = json.release_date
this.runtime = json.runtime
this.overview = json.overview
this.backdropPath = json.backdrop_path
}
}
class Actor {
constructor(json) {
//mmmmmmmmm
this.name=json.name;
this.pic1=json.profile_path;
}
}
document.addEventListener("DOMContentLoaded", App.run);