-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelper.js
103 lines (83 loc) · 3.25 KB
/
helper.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
// // Populate dropdown menu with all the available genres
const populateGenreDropdown = (genres) => {
const select = document.getElementById('genres')
for (const genre of genres) {
let option = document.createElement("option");
option.value = genre.id;
option.text = genre.name;
select.appendChild(option);
}
};
// Returns the current genre selection from the dropdown menu
const getSelectedGenre = () => {
const selectedGenre = document.getElementById('genres').value;
return selectedGenre;
};
// Displays the like and dislike buttons on the page
const showBtns = () => {
const btnDiv = document.getElementById('likeOrDislikeBtns');
btnDiv.removeAttribute('hidden');
};
// Clear the current movie from the screen
const clearCurrentMovie = () => {
const moviePosterDiv = document.getElementById('moviePosterDiv');
const movieTextDiv = document.getElementById('movieText');
moviePosterDiv.innerHTML = '';
movieTextDiv.innerHTML = '';
}
// After liking a movie, clears the current movie from the screen and gets another random movie
const likeMovie = () => {
clearCurrentMovie();
showRandomMovie();
};
// After disliking a movie, clears the current movie from the screen and gets another random movie
const dislikeMovie = () => {
clearCurrentMovie();
showRandomMovie();
};
// Create HTML for movie poster
const createMoviePoster = (posterPath) => {
const moviePosterUrl = `https://image.tmdb.org/t/p/original/${posterPath}`;
const posterImg = document.createElement('img');
posterImg.setAttribute('src', moviePosterUrl);
posterImg.setAttribute('id', 'moviePoster');
return posterImg;
};
// Create HTML for movie title
const createMovieTitle = (title) => {
const titleHeader = document.createElement('h1');
titleHeader.setAttribute('id', 'movieTitle');
titleHeader.innerHTML = title;
return titleHeader;
};
// Create HTML for movie overview
const createMovieOverview = (overview) => {
const overviewParagraph = document.createElement('p');
overviewParagraph.setAttribute('id', 'movieOverview');
overviewParagraph.innerHTML = overview;
return overviewParagraph;
};
// Returns a random movie from the first page of movies
const getRandomMovie = (movies) => {
const randomIndex = Math.floor(Math.random() * movies.length);
const randomMovie = movies[randomIndex];
return randomMovie;
};
// Uses the DOM to create HTML to display the movie
const displayMovie = (movieInfo) => {
const moviePosterDiv = document.getElementById('moviePosterDiv');
const movieTextDiv = document.getElementById('movieText');
const likeBtn = document.getElementById('likeBtn');
const dislikeBtn = document.getElementById('dislikeBtn');
// Create HTML content containing movie info
const moviePoster = createMoviePoster(movieInfo.poster_path);
const titleHeader = createMovieTitle(movieInfo.title);
const overviewText = createMovieOverview(movieInfo.overview);
// Append title, poster, and overview to page
moviePosterDiv.appendChild(moviePoster);
movieTextDiv.appendChild(titleHeader);
movieTextDiv.appendChild(overviewText);
showBtns();
likeBtn.onclick = likeMovie;
dislikeBtn.onclick = dislikeMovie;
};