-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtvShows.js
More file actions
47 lines (43 loc) · 1.44 KB
/
tvShows.js
File metadata and controls
47 lines (43 loc) · 1.44 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
const form = document.querySelector('form');
const content = document.querySelector('.content');
const getDetails = async (showName) => {
try {
let res = await axios.get(`https://api.tvmaze.com/search/shows?q=${showName}`);
return res.data;
} catch (e) {
return [];
}
}
form.addEventListener('submit', async (e) => {
e.preventDefault();
content.innerHTML = "";
let query = form.elements.query.value.trim();
form.elements.query.value = "";
let shows = await getDetails(query);
if (!shows) {
content.innerHTML = "<p>⚠️ Error fetching data. Please try again.</p>";
return;
}
if (shows.length === 0) {
content.innerHTML = "<p>❌ No results found.</p>";
return;
}
for (const show of shows) {
if (show.show.image) {
const div = document.createElement('div');
const a = document.createElement('a');
a.href = show.show.url;
a.target = "_blank";
const img = document.createElement('img');
img.src = show.show.image.medium;
img.alt = show.show.name;
a.append(img);
const name = document.createElement('h2');
name.textContent = show.show.name;
div.append(a);
div.append(name);
content.append(div);
div.classList.add("show-card");
}
}
});