-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
121 lines (106 loc) · 2.8 KB
/
index.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
119
120
121
// Query Selectors
const fetchBtn = document.getElementById("fetch-btn");
const searchInput = document.querySelector("input[name='q']");
const contentContainer = document.querySelector(".content");
// Event Listeners
document.addEventListener(
"keydown",
(e) => e.key === "Enter" && searchTVShows()
);
fetchBtn.addEventListener("click", searchTVShows);
function searchTVShows() {
const { value } = searchInput;
if (value !== "") {
showData(value);
} else {
alert("You didn't enter any value");
}
searchInput.value = "";
}
async function showData(tvShow) {
const url = `https://api.tvmaze.com/search/shows?q=${tvShow}`;
const tvShows = await fetchTVShows(url);
if (tvShows.length > 0) {
// Clear Content Container
contentContainer.innerHTML = "";
for (let $ of tvShows) {
const { show } = $;
createCard(show);
}
} else {
contentContainer.innerHTML = errorMessage(
`Sorry, but there is no data found for '${tvShow}'`
);
}
}
async function fetchTVShows(url) {
try {
const res = await fetch(url);
const data = await res.json();
return data;
} catch (error) {
contentContainer.innerHTML = errorMessage(
`Sorry, but there is no data available at the moment,try again later`
);
}
}
function createCard(show) {
// Card Container
const card = document.createElement("div");
card.className = "card";
// Create img
const img = document.createElement("img");
img.src =
show?.image?.medium ||
show?.image?.original ||
"./images/not-found-image.png";
// Create Info
const cardInfo = createCardInfo(show);
// Append
card.appendChild(img);
card.appendChild(cardInfo);
contentContainer.appendChild(card);
}
function createCardInfo(show) {
const info = cleanData(show);
const cardInfo = document.createElement("div");
cardInfo.className = "info";
for (let key in info) {
const div = document.createElement("div");
div.className = key;
const [infoType, infoValue] = [
document.createElement("span"),
document.createElement("span"),
];
infoType.className = "capitalize";
infoType.innerHTML = `${key}: `;
infoValue.innerHTML = info[key] || "No Data";
// append
div.appendChild(infoType);
div.appendChild(infoValue);
cardInfo.appendChild(div);
}
return cardInfo;
}
function cleanData(show) {
const info = {
name: show.name,
rating: show.rating.average,
premiered: show.premiered,
status: show.status,
};
if (show.network) {
info.network = show.network.name;
} else {
info["web channel"] = show.webChannel.name;
}
info.summary = show.summary;
return info;
}
function errorMessage(message) {
return `
<div class="no-data">
<img src="./images/empty-box.png" alt="empty-box.png">
<span>${message}</span>
</div>`;
}