-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
93 lines (84 loc) · 3.04 KB
/
Copy pathmain.js
File metadata and controls
93 lines (84 loc) · 3.04 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
import {
fetchTopRatedMovies,
fetchPopularMovies,
searchMovies,
searchTVShows,
searchPeople,
fetchRandomMovie,
fetchRandomBackdropImage,
} from "./api.js";
import {
renderMovieList,
renderSearchResults,
renderRandomMovie,
renderRandomBackdropImage,
} from "./ui.js";
document
.getElementById("top-rated-movies-button")
.addEventListener("click", async () => {
try {
const topRatedMovies = await fetchTopRatedMovies();
renderMovieList(topRatedMovies, "search-results", "Top Rated Movies");
} catch (error) {
document.getElementById("search-results").innerHTML =
"<p>Failed to load top rated movies. Please try again later.</p>";
}
});
document
.getElementById("popular-movies-button")
.addEventListener("click", async () => {
try {
const popularMovies = await fetchPopularMovies();
renderMovieList(popularMovies, "search-results", "Popular Movies");
} catch (error) {
document.getElementById("search-results").innerHTML =
"<p>Failed to load popular movies. Please try again later.</p>";
}
});
document
.getElementById("random-movie-button")
.addEventListener("click", async () => {
try {
const randomMovie = await fetchRandomMovie();
renderRandomMovie(randomMovie, "#search-results", "Random Movie");
} catch (error) {
document.getElementById("search-results").innerHTML =
"<p>Failed to get random movie. Please try again later.</p>";
}
});
document
.getElementById("search-form")
.addEventListener("submit", async (event) => {
event.preventDefault();
const searchInput = document.getElementById("search-input").value.trim();
const searchType = document.getElementById("search-type").value;
if (searchInput !== "") {
try {
let results;
let heading = "";
// Search for movies, TV shows, or people based on the selected type
if (searchType === "movie") {
results = await searchMovies(searchInput);
heading = "Movie Information";
} else if (searchType === "tv") {
results = await searchTVShows(searchInput);
heading = "TV Show Information";
} else if (searchType === "person") {
results = await searchPeople(searchInput);
heading = "Person Information";
}
renderSearchResults(results, "search-results", heading);
} catch (error) {
document.getElementById("search-results").innerHTML =
"<p>Failed to perform search. Please try again later.</p>";
}
}
});
(async () => {
try {
const randomBackdropImageUrl = await fetchRandomBackdropImage();
renderRandomBackdropImage(randomBackdropImageUrl);
} catch (error) {
console.error("Failed to fetch random backdrop image:", error);
}
})();