-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscript.js
119 lines (102 loc) · 4.15 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
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
var output = document.getElementById('movies');
var searchInput = document.getElementById('searchText');
searchInput.addEventListener('keyup', function(e) {
e.preventDefault();
if(e.keyCode == 13) {
getMovies(searchInput.value);
}
});
function getMovies(searchText) {
output.innerHTML = 'Loading';
async function getData() {
//await the response of the fetch call
let response = await fetch('https://api.themoviedb.org/3/search/movie?api_key=9d58539c5ba127904dce76603c0bcbca&language=en-US&query=' + searchText + '&include_adult=true');
//proceed once the first promise is resolved.
let data = await response.json()
//proceed only when the second promise is resolved
return data;
}
//call getData function
getData()
.then(data => {
let movie = data.results;
output.innerHTML = '';
if(movie == 0) {
output.innerHTML = 'No results';
}
movie.forEach(function(m, i) {
if(m.poster_path == null) {
m.poster_path = 'https://cdn.glitch.com/833b6908-d1bb-4b8d-aef7-4cdaaa642c4a%2Fno-img.png?1527172635831';
} else {
m.poster_path = 'http://image.tmdb.org/t/p/w185/' + m.poster_path;
}
output.innerHTML += `<div class="card" style="width: 18rem;">
<img class="card-img-top" src="${m.poster_path}" alt="Card image cap">
<div class="card-body">
<h5 class="card-title">${m.title}</h5>
<a class="btn btn-primary" id="${i}" onclick="movieSelected(${m.id})" href="#">Movie Details</a>
</div>
</div>`
});
});//log the data
}
function movieSelected(id) {
sessionStorage.setItem('movieDetails', id);
window.location = 'movie.html';
return false;
}
function getMovie() {
let movieId = sessionStorage.getItem('movieDetails');
async function getData() {
//await the response of the fetch call
let response = await fetch('https://api.themoviedb.org/3/movie/' + movieId + '?api_key=9d58539c5ba127904dce76603c0bcbca&language=en-US');
//proceed once the first promise is resolved.
let data = await response.json()
//proceed only when the second promise is resolved
return data;
}
//call getData function
getData()
.then(data => {
let movie = data;
console.log(data);
if(movie.poster_path == null) {
movie.poster_path = 'https://cdn.glitch.com/833b6908-d1bb-4b8d-aef7-4cdaaa642c4a%2Fno-img.png?1527172635831';
} else {
movie.poster_path = 'http://image.tmdb.org/t/p/w185/' + movie.poster_path;
}
let output =`
<div class="row">
<div class="col-md-4">
<img src="${movie.poster_path}" class="thumbnail">
</div>
<div class="col-md-8">
<h2>${movie.original_title}</h2>
<ul class="list-group">
<li class="list-group-item"><strong>Genre:</strong>
${movie.genres.map(function(genre) {
return ' ' + genre.name;
})}
</li>
<li class="list-group-item"><strong>Released:</strong> ${movie.release_date}</li>
<li class="list-group-item"><strong>Rated:</strong> ${movie.vote_average}</li>
<li class="list-group-item"><strong>IMDB Rating:</strong> ${movie.imdbRating}</li>
<li class="list-group-item"><strong>Director:</strong> ${movie.Director}</li>
<li class="list-group-item"><strong>Writer:</strong> ${movie.Writer}</li>
<li class="list-group-item"><strong>Actors:</strong> ${movie.Actors}</li>
</ul>
</div>
</div>
<div class="row">
<div class="well">
<h3>Plot</h3>
${movie.overview}
<hr>
<a href="http://imdb.com/title/${movie.imdb_id}" target="_blank" class="btn btn-primary">View IMDB</a>
<a href="index.html" class="btn btn-default">Go Back To Search</a>
</div>
</div>
`;
document.getElementById('movie').innerHTML = output;
});
}