generated from CodeYourFuture/tv-show-dom-project
-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
212 lines (193 loc) · 6.18 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
// get allEpisodes from the api
function getAllEpisodes(allEpisodes) {
makePageForEpisodes(allEpisodes); // make page for all episodes
addOptions(allEpisodes); //select options for all episodes
// search episodes through select
document
.getElementById("select")
.addEventListener("change", searchEpisodesThroughSelect(allEpisodes));
// search episodes by search bar input
document.getElementById("total-episodes").textContent = allEpisodes.length;
document
.getElementById("text")
.addEventListener("input", searchEpisodesBySearchInput(allEpisodes));
}
function searchEpisodesThroughSelect(allEpisodes) {
return function (e) {
e.preventDefault();
let selectedEpisode = document.getElementById("select").value;
let input = selectedEpisode.slice(9, selectedEpisode.length);
let displayEpisode = allEpisodes.filter((element) => {
if (element.name.includes(input)) {
return element;
}
});
makePageForEpisodes(displayEpisode);
};
}
function searchEpisodesBySearchInput(allEpisodes) {
return function (e) {
e.preventDefault();
let inputValue = document.getElementById("text").value;
let input = inputValue.toLowerCase();
let searchResults = allEpisodes.filter((element) => {
if (
element.name.toLowerCase().match(input) ||
element.summary.toLowerCase().match(input)
) {
return element;
}
});
makePageForEpisodes(searchResults);
};
}
function SearchTvShowBySelectOptionsOnTheEpisodesPage(
allShows,
getAllEpisodes
) {
return function (e) {
e.preventDefault();
let selectedValue = document.getElementById("select-show").value;
let selectedTvShow = allShows.filter((element) => {
if (element.name === selectedValue) {
return element;
}
});
// retrieve all episodes
fetch(`https://api.tvmaze.com/shows/${selectedTvShow[0].id}/episodes`)
.then((response) => {
if (response.status >= 200 && response.status <= 299) {
return response.json();
} else {
throw new Error(
`error message: ${response.status} ${response.statusText}`
);
}
})
.then((allEpisodes) => getAllEpisodes(allEpisodes))
.catch((error) => console.log(error));
};
}
// show on the shows page
function findShowEpisodes(i, allShows, getAllEpisodes) {
return function () {
let selectedValue =
document.getElementsByClassName("tv-show")[i].textContent;
document.getElementById("select-show").value = selectedValue;
let selectedTvShow = allShows.filter((element) => {
if (element.name === selectedValue) {
return element;
}
});
// retrieve all episodes for the clicked tv show on tv show home page
fetch(`https://api.tvmaze.com/shows/${selectedTvShow[0].id}/episodes`)
.then((response) => {
if (response.status >= 200 && response.status <= 299) {
return response.json();
} else {
throw new Error(
`error message: ${response.status} ${response.statusText}`
);
}
})
.then((allEpisodes) => getAllEpisodes(allEpisodes))
.catch((error) => console.log(error));
hideDom();
};
}
function GetTheEpisodesOfTheSearchedTvShow(i, displayTvShow, getAllEpisodes) {
return function () {
let selectedValue =
document.getElementsByClassName("tv-show")[i].textContent;
document.getElementById("select-show").value = selectedValue;
fetch(`https://api.tvmaze.com/shows/${displayTvShow[i].id}/episodes`)
.then((response) => {
if (response.status >= 200 && response.status <= 299) {
return response.json();
} else {
throw new Error(
`error message: ${response.status} ${response.statusText}`
);
}
})
.then((allEpisodes) => getAllEpisodes(allEpisodes))
.catch((error) => console.log(error));
hideDom();
};
}
function SearchTvShowBySearchInput(allShows, getAllEpisodes) {
return function (e) {
e.preventDefault();
let selectedTvShow = document.getElementById("search-shows").value;
let input = selectedTvShow.toLowerCase();
let displayTvShow = allShows.filter((element) => {
if (
element.name.toLowerCase().match(input) ||
element.summary.toLowerCase().match(input)
) {
return element;
}
});
// get the episodes of the selected tv show
makePageForTvShows(displayTvShow);
for (let i = 0; i < displayTvShow.length; i++) {
document
.getElementsByClassName("tv-show-group")
[i].addEventListener(
"click",
GetTheEpisodesOfTheSearchedTvShow(i, displayTvShow, getAllEpisodes)
);
}
};
}
function getTheEpisodesOfTheSelectedTvShowOnTvShowPage(
i,
displayTvShow,
getAllEpisodes
) {
return function () {
let selectedValue =
document.getElementsByClassName("tv-show")[i].textContent;
document.getElementById("select-show").value = selectedValue;
fetch(`https://api.tvmaze.com/shows/${displayTvShow[i].id}/episodes`)
.then((response) => {
if (response.status >= 200 && response.status <= 299) {
return response.json();
} else {
throw new Error(
`error message: ${response.status} ${response.statusText}`
);
}
})
.then((allEpisodes) => getAllEpisodes(allEpisodes))
.catch((error) => console.log(error));
hideDom();
};
}
function searchTvShowBySelectOnTvShowPage(allShows, getAllEpisodes) {
return function (e) {
e.preventDefault();
let selectedTvShow = document.getElementById(
"select-show-on-show-page"
).value;
let displayTvShow = allShows.filter((element) => {
if (element.name.includes(selectedTvShow)) {
return element;
}
});
// get the episodes of the selected tv show
makePageForTvShows(displayTvShow);
for (let i = 0; i < displayTvShow.length; i++) {
document
.getElementsByClassName("tv-show-group")
[i].addEventListener(
"click",
getTheEpisodesOfTheSelectedTvShowOnTvShowPage(
i,
displayTvShow,
getAllEpisodes
)
);
}
};
}