-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.html
More file actions
327 lines (245 loc) · 15.4 KB
/
Copy pathcode.html
File metadata and controls
327 lines (245 loc) · 15.4 KB
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title> MovieFinder: By Tom Farrell</title>
<!-- Bootstrap CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" crossorigin="anonymous">
<!-- Bootstrap Icons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.11.1/font/bootstrap-icons.css">
<!-- Devicnons -->
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/devicons/devicon@v2.15.1/devicon.min.css">
<!-- Prism CSS -->
<link rel="stylesheet" href="/css/prism.css" />
<!-- Custom CSS -->
<link rel="stylesheet" href="/css/site.css" />
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">
<img src="/img/Logo_Only.svg" height="50" />
  Film Finder
</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse"
data-bs-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false"
aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarSupportedContent">
<ul class="navbar-nav me-auto mb-2 mb-lg-0">
<li class="nav-item">
<a class="nav-link" href="index.html">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/app.html">The App</a>
</li>
<li class="nav-item">
<a class="nav-link active" aria-current="page" href="/code.html">The Code</a>
</li>
<li class="nav-item">
<!-- CHANGE ME TO THE REPO LINK -->
<a class="nav-link" href="https://github.com/TJF71/GuardianMovieList">
The Repo
</a>
</li>
<li class="nav-item">
<!-- CHANGE ME TO THE AOBOUT LINK -->
<a class="nav-link" href="https://tomfarrell-portfolio.netlify.app/">
Portfolio
</a>
</li>
<li class="nav-item">
<!-- CHANGE ME TO THE DEPLOYED BLOG LINK -->
<!-- NOTE: it would be a great idea to write a blog post
about each challenge and link it here -->
<a class="nav-link" href="https://bits-and-bytes.up.railway.app/">
Blog
</a>
</li>
</ul>
</div>
</div>
</nav>
<main class="container py-5">
<h1 class="border-bottom border-2 border-dark">The Code</h1>
<div class="row">
<div class="col col-12 col-lg-8">
<pre class="line-numbers">
<code class="language-js">
const API_KEY = "eyJhbGciOiJIUzI1NiJ9.eyJhdWQiOiI4OTgxYTU4OGIxMGE3ZTllNzRjMGFhYjUwNWQ5MWExZiIsInN1YiI6IjY1MmVlMWExMDI0ZWM4MDExZTM1OWJhOCIsInNjb3BlcyI6WyJhcGlfcmVhZCJdLCJ2ZXJzaW9uIjoxfQ.Hmug88MC6q75pV1WJhGLU0seYmOOnKzQYuzX_KY5c4U"
async function getMovies() { // calls API and gets movies
try {
let response = await fetch('https://api.themoviedb.org/3/movie/popular', { // get the movie db from the net
headers: {
'Authorization': `Bearer ${API_KEY}`
}
});
let data = await response.json(); // assingn the db to variable 'data'
/*
data = {
page: 1,
results: [ {}, {}, {}...]
total_pages: 29045,
total_results: 817902
}
*/
return data;
} catch (error) {
Swal.fire({
backdrop: false,
title: 'Ooops!',
text: 'Something went wrong reaching the TMDV API.',
icon: 'error'
})
}
}
async function getMovie(movie_id) { // calls API and gets movies
try {
let response = await fetch(`https://api.themoviedb.org/3/movie/${movie_id}`, { // get the movie db from the net
headers: {
'Authorization': `Bearer ${API_KEY}`
}
});
let data = await response.json(); // assingn the db to variable 'data'
return data;
} catch (error) {
Swal.fire({
backdrop: false,
title: 'Ooops!',
text: 'Something went wrong reaching the TMDV API.',
icon: 'error'
})
}
}
async function displayMovies() {
let data = await getMovies(); // call the movies from the getMovies function
const movieListDiv = document.getElementById('movie-list');
movieListDiv.innerHTML = ''; // prevents repeats of adds of movie list
const moviePosterTemplate = document.getElementById('movie-card-template');
let movies = data.results; // create a variable from the results
for (let i = 0; i < movies.length; i++) {
let movie = movies[i]; // cycle through each movie one at a time
let movieCard = moviePosterTemplate.content.cloneNode(true); // get a copy of template and assign it
let movieImageElement = movieCard.querySelector('.card-img-top'); // lock onto .card-img-top template
movieImageElement.src = `https://image.tmdb.org/t/p/w500${movie.poster_path}`; //load the image to movieImageElement
let movieTitleElement = movieCard.querySelector('.card-body > h5'); // get the move title from app.html
movieTitleElement.textContent = movie.title; // target movie title only and apply it
let movieParagraphElement = movieCard.querySelector('.card-text') // target the element of .card-text
movieParagraphElement.textContent = movie.overview; // target movie overview and apply it
let movieButton = movieCard.querySelector('.btn-primary');
movieButton.setAttribute('data-movieId', movie.id);
movieListDiv.appendChild(movieCard); // adding movie details to the card movieListDiv
}
}
// get the movie genres
async function getGenres() {
// attempting to get genre dae from the movie database
try {
// wait for async response from URL
let response = await fetch(`https://api.themoviedb.org/3/genre/movie/list`, {
headers: {
'Authorization': `Bearer ${API_KEY}`
}
});
// take the string returned back adn convert it to JSON object
let data = await response.json();
return data;
// if something goes wrong, run this backup code
} catch (error) {
console.error(error);
}
}
async function showMovieDetails(clickedBtn) {
// get the ID of the movie that was clicked
let movieId = clickedBtn.getAttribute('data-movieId');
let moviedata = await getMovie(movieId);
//TESTING: put the movie ID in the modal
let poster = document.querySelector('#movieModal .poster');
poster.src = `https://image.tmdb.org/t/p/w500${moviedata.poster_path}`;
let movieTagline = document.querySelector('#movieTagline');
movieTagline.innerHTML = `<strong>Tagline: </strong> "<em>${moviedata.tagline}</em>"`;
let movieOverview = document.querySelector('#movieOverview');
movieOverview.innerHTML = `<strong>Overview: </strong> ${moviedata.overview}`;
let format = {
style: 'currency',
currency: 'USD',
minimumFractionDigits: 0,
maximumFractionDigits: 0
};
let movieBudget = document.querySelector('#movieBudget');
movieBudget.innerHTML = `<strong>Budget: </strong> ${moviedata.budget.toLocaleString('en-US', format)}`;
let movieRuntime = document.querySelector('#movieRuntime');
movieRuntime.innerHTML = `<strong>Runtime: </strong> ${moviedata.runtime} minutes`;
let dateFormat = {
year: 'numeric',
month: 'numeric',
day: 'numeric'
};
let movieReleaseDate = document.querySelector('#movieReleaseDate');
movieReleaseDate.innerHTML = `<strong>Release Date: </strong> ${new Date(moviedata.release_date).toLocaleDateString('en-US', dateFormat)}`;
// ADD GENRES
let movieGenres = document.querySelector('#movieGenres');
let genres = [];
for (let i = 0; i < moviedata.genres.length; i++) {
genres.push(moviedata.genres[i].name);
}
let genreString = genres.join(', ');
movieGenres.innerHTML = `<strong>Genres: </strong>${genreString}`;
let movieHomepage = document.getElementById('movieHomepageBtn');
movieHomepage.href = moviedata.homepage;
}
</code>
</pre>
</div>
<div class="col col-12 col-lg-4">
<!-- <p>The code is structured in one function</p>
<p>
<code>display message</code> displays a sweetalert
when the button is pressed.
</p> -->
</div>
</div>
</main>
<footer class="py-3 container-fluid">
<div class="row align-items-center row-cols-1 row-cols-lg-3 gy-2">
<div classs="container">
<div class="col text-center text-lg-start">
© 2023 Tom Farrell
</div>
</div>
<div class="col text-center">
<img src="/img/Logo_Only.svg" height="50" class="myLogo"/>
</div>
<div class="col text-center text-lg-end">
<a href="mailto:tjf7101@gmail.com" target="_blank" class="text-decoration-none">
<i class="bi bi-envelope p-2"></i>
</a>
<a href="https://www.linkedin.com/in/thomas-j-farrell-jr-75195a94"> <i class="bi bi-linkedin"></i></a>
<a href="https://github.com/TJF71/GuardianMovieList"> <i class="bi bi-github"></i></a>
<!-- <a href="#"> <i class="bi bi-youtube"></i></a>
<a href="#"> <i class="bi bi-twitter"></i></a> -->
</div>
</div>
</footer>
<!--Bootstrap JS -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"
integrity="sha384-C6RzsynM9kWDrMNeT87bh95OGNyZPhcTNXj1NW7RuBCsyN/o0jlpcV8Qyq46cDfL"
crossorigin="anonymous"></script>
<script>
const tooltipTriggerList = document.querySelectorAll('[data-bs-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new bootstrap.Tooltip(tooltipTriggerEl))
</script>
<script src="/js/prism.js"></script>
<script>
Prism.plugins.NormalizeWhitespace.setDefaults({
'remove-trailing': true,
'remove-indent': true,
'left-trim': true,
'right-trim': true
})
</script>
</body>
</html>