generated from CodeYourFuture/Project-TV-Show
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathscript.js
238 lines (224 loc) · 7.05 KB
/
script.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
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
const getEpisodes = document.getElementById("selectEpisodes");
const getShow = document.getElementById("selectShow");
const Home = document.getElementById("home");
const search = document.getElementById("search");
const root = document.getElementById("root");
const allShows = [];
let specialEp = [];
let clickedEpisodes = [];
function fetchShows() {
return fetch("https://api.tvmaze.com/shows")
.then((res) => {
if (!res.ok) {
throw new Error("Failed to fetch shows");
}
return res.json();
})
.then((data) => {
console.log(data);
allShows.push(...data);
getAllShows();
displayShow(data);
render();
return data;
})
.catch((err) => {
console.error(err);
return [];
});
}
function attachH2EventListeners() {
const h2Elements = root.querySelectorAll(".forEpisode");
h2Elements.forEach((h2) => {
h2.addEventListener("click", function () {
search.value = "";
const episodeName = this.textContent.trim();
const selectedEpisode = clickedEpisodes.find(
(episode) => episode.name === episodeName
);
if (selectedEpisode) {
// If episode is already fetched, just use it
displayEpisodes(clickedEpisodes);
} else {
// Find the show associated with the clicked episode
const selectedShow = allShows.find((show) => show.name === episodeName);
if (selectedShow) {
fetchEpisodes(selectedShow.id)
.then((episodes) => {
displayEpisodes(episodes);
populateEpisodeDropdown(episodes);
clickedEpisodes.push(
...episodes.map((episode) => ({
...episode,
showId: selectedShow.id,
}))
);
})
.catch((error) => {
console.error("Error fetching episodes:", error);
});
}
}
});
});
}
function fetchShowsAndAttachListeners() {
fetchShows()
.then(() => {
attachH2EventListeners();
})
.catch((error) => {
console.error("Error fetching shows:", error);
});
}
fetchShowsAndAttachListeners();
function getAllShows() {
getEpisodes.innerHTML = "";
allShows.forEach((show) => {
const option = document.createElement("option");
option.textContent = show.name;
getEpisodes.appendChild(option);
});
}
getEpisodes.addEventListener("change", function () {
const showValue = this.value;
console.log("Selected Show:", showValue);
root.innerHTML = "";
getShow.innerHTML = "";
search.value = "";
const selectedShow = allShows.find((show) => show.name === showValue);
if (selectedShow) {
const clickedShowEpisodes = clickedEpisodes.filter(
(episode) => episode.showId === selectedShow.id
);
if (clickedShowEpisodes.length > 0) {
displayEpisodes(clickedShowEpisodes);
} else {
fetchEpisodes(selectedShow.id);
}
}
});
getShow.addEventListener("change", function () {
search.value = "";
const selectedSpecificEpisode = this.value;
console.log("Selected Specific Episode:", selectedSpecificEpisode);
root.innerHTML = "";
const filteredEpisodes = specialEp.filter((episode) =>
episode.name.includes(selectedSpecificEpisode)
);
if (filteredEpisodes.length > 0) {
displayEpisodes(filteredEpisodes);
}
});
function createCard(episode) {
const episodeDiv = document.createElement("div");
episodeDiv.setAttribute("id", "container");
episodeDiv.innerHTML = `
<h2 class="forEpisode">${episode.name} - S${String(
episode.season
).padStart(2, "0")}E${String(episode.number).padStart(2, "0")}</h2>
<img src="${episode.image.medium}" alt="${episode.name}">
<p class="forSummary">${episode.summary}</p>
`;
root.appendChild(episodeDiv);
}
function createCardShow(episode) {
const episodeDiv = document.createElement("div");
episodeDiv.setAttribute("id", "container");
const diving = document.createElement("div");
diving.innerHTML = `
<h2 class="forEpisode">${episode.name}</h2><img src="${episode.image.medium}" alt="${episode.name}">
`;
const divbott = document.createElement("div");
const divu = document.createElement("div");
divbott.setAttribute("id", "conta");
divu.innerHTML = `<p class="forSummary">${episode.summary}</p>
`;
const divgene = document.createElement("div");
divgene.innerHTML = ` <p class="forSummary"> Rating: ${episode.rating.average}</p> <p class="forSummary">Genres: ${episode.genres}
</p> <p class="forSummary">Status: ${episode.status}</p> <p class="forSummary">Runtime: ${episode.runtime}</p>`;
divbott.append(divu);
divbott.append(divgene);
episodeDiv.append(diving);
episodeDiv.append(divbott);
root.appendChild(episodeDiv);
}
function displayEpisodes(episodes) {
root.innerHTML = "";
specialEp = episodes;
episodes.forEach((episode) => {
const option = document.createElement("option");
option.textContent = episode.name;
getShow.appendChild(option);
createCard(episode);
});
}
function displayShow(episodes) {
root.innerHTML = "";
specialEp = episodes;
episodes.forEach((episode) => {
const option = document.createElement("option");
option.textContent = episode.name;
getShow.appendChild(option);
createCardShow(episode);
});
}
function fetchEpisodes(showId) {
return fetch(`https://api.tvmaze.com/shows/${showId}/episodes`)
.then((response) => {
if (!response.ok) {
throw new Error("Failed to fetch episodes");
}
return response.json();
})
.then((data) => {
displayEpisodes(data);
return data;
})
.catch((error) => {
console.error("Error fetching episodes:", error);
});
}
function populateEpisodeDropdown(episodes) {
getShow.innerHTML = "";
episodes.forEach((episode) => {
const option = document.createElement("option");
option.textContent = episode.name;
getShow.appendChild(option);
});
}
function render() {
search.addEventListener("input", function () {
const searchValue = search.value.toLowerCase();
root.innerHTML = "";
const filteredShows = allShows.filter((show) =>
show.name.toLowerCase().includes(searchValue)
);
const filteredShowSummary = allShows.filter((show) =>
show.summary.toLowerCase().includes(searchValue)
);
const filteredEpisodeSummary = specialEp.filter((show) =>
show.summary.toLowerCase().includes(searchValue)
);
const filteredEpisodes = specialEp.filter((episode) =>
episode.name.toLowerCase().includes(searchValue)
);
if (filteredShows.length > 0 && specialEp.length === 0) {
filteredShows.forEach((show) => {
createCard(show);
});
} else if (specialEp.length > 0) {
filteredEpisodes.forEach((episode) => {
createCard(episode);
});
} else {
const noMatchMessage = document.createElement("p");
noMatchMessage.textContent = "No matching shows or episodes found.";
root.appendChild(noMatchMessage);
}
});
}
Home.addEventListener("click", function () {
root.innerHTML = "";
fetchShowsAndAttachListeners();
});