-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
298 lines (249 loc) · 8.89 KB
/
Copy pathscript.js
File metadata and controls
298 lines (249 loc) · 8.89 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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
const labelDisplay = document.getElementById("displayed-films-label");
const searchBox = document.getElementById("search");
const selectedFilm = document.getElementById("film-selector");
const selectedShow = document.getElementById("show-selector");
const container = document.getElementById("container")
let state = {
showsList: [],
films: [],
searchText: "",
selectedEpisode: "default",
inputContainer: "default",
selectedShowInput: "82",
mode: "shows",
};
const fetchShows = async () => {
const response = await fetch('https://api.tvmaze.com/shows');
if(!response.ok) {
throw new Error("Error Fetching Shows");
}
return await response.json();
}
fetchShows()
.then((showArray) => {
state.showsList = showArray;
render();
addShowSelection();
})
.catch((error) => {
console.log(error);
document.querySelector("main").innerHTML = "Failed to load data. Please try again later";
});
// make show selector
function addShowSelection() {
selectedShow.innerHTML = '<option value="default">Choose a Show</option>';
sortShowsAlphabetically(state.showsList).forEach((show) => {
const option = document.createElement("option");
option.value = String(show.id);
option.textContent = show.name;
selectedShow.appendChild(option);
});
}
const fetchFilms = async () => {
const url = `https://api.tvmaze.com/shows/${state.selectedShowInput}/episodes`;
const response = await fetch(url);
if (!response.ok) {
throw new Error(`Response status: ${response.status}`);
}
return await response.json();
};
async function displayFilms() {
await fetchFilms()
.then((films) => {
state.films = films;
render();
addFilmSelection();
})
.catch((error) => {
console.log(error);
document.querySelector("main").innerHTML =
"Failed to load data. Please try again later.";
});
}
displayFilms()
// make film selector
function addFilmSelection() {
const seeAllShowOption = document.createElement("option")
seeAllShowOption.value = "shows"
seeAllShowOption.textContent = 'See all shows'
selectedFilm.append(seeAllShowOption);
state.films.forEach((film) => {
const option = document.createElement("option");
option.value = film.name;
option.textContent = `S${film.season.toString().padStart(2, "0")}E${film.number.toString().padStart(2, "0")} - ${film.name}`;
selectedFilm.appendChild(option);
});
}
function makeFilmCard({ name, season, number, image, summary }) {
const filmCardDiv = document.createElement("div");
filmCardDiv.className = "film-card-div";
let filmTitleDiv = document.createElement("div");
filmTitleDiv.className = "film-title-div";
let filmDescriptionDiv = document.createElement("div");
filmDescriptionDiv.className = "film-description-div";
let filmTitle = document.createElement("h3");
filmTitle.textContent = `${name} - S${season.toString().padStart(2, "0")}E${number.toString().padStart(2, "0")}`;
let filmImgDiv = document.createElement("div");
filmImgDiv.className = "film-img-div";
let filmImg = document.createElement("img");
filmImg.src = image.medium;
filmImg.alt = filmTitle;
filmImgDiv.append(filmImg);
let filmDescription = document.createElement("div");
filmDescription.innerHTML = summary;
filmTitleDiv.append(filmTitle);
filmDescriptionDiv.append(filmImgDiv, filmDescription);
filmCardDiv.append(filmTitleDiv, filmDescriptionDiv);
return filmCardDiv;
}
function makeShowCard({ id, name, image, summary, genres, status, rating, runtime }) {
const showCardDiv = document.createElement("div");
showCardDiv.className = "show-card-div";
showCardDiv.role = 'button';
showCardDiv.id = `${id}`;
let showTitleDiv = document.createElement("div");
showTitleDiv.className = "show-title-div";
let showDescriptionDiv = document.createElement("div");
showDescriptionDiv.className = "show-description-div";
let showTitle = document.createElement("h1");
showTitle.textContent = `${name}`;
let showImg = document.createElement("img");
showImg.src = image.medium;
showImg.alt = name;
showImg.style.marginBottom = "2rem";
showImg.className = 'show-img'
let showDescription = document.createElement("div");
showDescription.className="show-description"
showDescription.innerHTML = summary;
let showRatingDiv = document.createElement("div");
showRatingDiv.className="show-rating-div"
let showRate = document.createElement("p")
showRate.innerHTML = `<b>Rated:</b> ${rating.average}`;
let showGenre = document.createElement("p")
showGenre.innerHTML = `<b>Genres:</b> ${genres.join(", ")}`;
let showStatus = document.createElement("p")
showStatus.innerHTML = `<b>Status:</b> ${status}`;
let showRuntime = document.createElement("p")
showRuntime.innerHTML = `<b>Runtime: </b>${runtime}`;
showTitleDiv.append(showTitle);
showRatingDiv.append(showRate, showGenre, showStatus, showRuntime);
showDescriptionDiv.append(showImg, showDescription, showRatingDiv);
showCardDiv.append(showTitleDiv, showDescriptionDiv);
return showCardDiv;
}
function renderShow() {
selectedFilm.style.display = "none"
selectedShow.style.display = "block"
labelDisplay.textContent = `Displaying ${state.showsList.length}/${state.showsList.length} show(s)`;
const filterText = sortShowsAlphabetically(state.showsList).filter(
(show) =>
show.name.toLowerCase().includes(state.searchText.toLowerCase()) ||
show.summary.toLowerCase().includes(state.searchText.toLowerCase()) || show.genres.includes(state.searchText.toLowerCase()),
);
const filterShow = filterText.map(makeShowCard);
container.append(...filterShow)
labelDisplay.textContent = `Displaying ${filterText.length}/${state.showsList.length} episode(s)`;
}
const render = () => {
container.innerHTML = "";
if (state.mode === "shows") {
renderShow()
} else {
selectedShow.style.display = "none"
selectedFilm.style.display = "block"
if (state.selectedShowInput === "none") {
labelDisplay.textContent = "Displaying 0/0";
if (!state.isInitialLoad) {
container.innerHTML = "";
}
return;
}
if (state.inputContainer === "default") {
const filterText = state.films.filter(
(film) =>
film.name.toLowerCase().includes(state.searchText.toLowerCase()) ||
(film.summary).toLowerCase().includes(state.searchText.toLowerCase()),
);
const filterFilms = filterText.map(makeFilmCard);
container.append(...filterFilms);
labelDisplay.textContent = `Displaying ${filterText.length}/${state.films.length} episode(s)`;
} else if (state.inputContainer === "select") {
const filterSelected = state.films.filter(
(film) => film.name === state.selectedEpisode,
);
const filterSelectedEpisode = filterSelected.map(makeFilmCard);
container.append(...filterSelectedEpisode);
labelDisplay.textContent = `Displaying ${filterSelectedEpisode.length}/${state.films.length}`;
}
};
}
render();
const handleSearch = (event) => {
state.searchText = event.target.value;
state.inputContainer = "default";
render();
};
searchBox.addEventListener("input", handleSearch);
// handleSelection
const handleSelection = (event) => {
state.selectedEpisode = event.target.value;
if (state.selectedEpisode === "default") {
state.inputContainer = "default";
} else if (state.selectedEpisode === "shows") {
state.mode = "shows"
}
else state.inputContainer = "select";
render();
};
selectedFilm.addEventListener("change", handleSelection);
// sorting shows
function sortShowsAlphabetically(shows) {
return [...shows].sort((a, b) =>
a.name.localeCompare(b.name, undefined, { sensitivity: "base" }),
);
}
// reseting the selector
function resetEpisodeSelector() {
selectedFilm.innerHTML = '<option value="default">Choose an episode</option>';
}
const handleShowSelection = async (event) => {
const showId = event.target.value;
state.selectedShowInput = showId;
state.selectedEpisode = "default";
state.searchText = "";
state.inputContainer = "default";
state.mode = "film"
document.getElementById("search").value = "";
if (showId === "default") {
state.selectedShowInput = "82"; //showing the same episodes from the very first time the page loads.
try {
state.films = await fetchFilms;
resetEpisodeSelector();
addFilmSelection();
render();
} catch (error) {
console.log(error);
}
return;
}
try {
state.films = await fetchFilms();
resetEpisodeSelector();
addFilmSelection();
render();
} catch (error) {
console.log(error);
document.querySelector("main").innerHTML =
"Failed to load data. Please try again later.";
}
};
selectedShow.addEventListener("change", handleShowSelection);
addShowSelection();
container.addEventListener("click", (e) => {
const link = e.target.closest('.show-card-div')
if (!link) return
e.preventDefault()
state.selectedShowInput = link.id
state.mode = 'film'
displayFilms()
})