-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
143 lines (114 loc) · 3.69 KB
/
script.js
File metadata and controls
143 lines (114 loc) · 3.69 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
const searchInput = document.getElementById("search-input");
const resultsDiv = document.getElementById("results");
const statusTxt = document.getElementById("status");
const heroSection = document.getElementById("hero");
// API - paprasta nuoroda i serverius kurie grazina informacija
const allShowsApi = "https://api.tvmaze.com/shows";
const searchShows = "https://api.tvmaze.com/search/shows?q=";
// .then suveiks tik tuo atveju jeigu pavyks
function getAllShows() {
// kai pradedama funkcija
statusTxt.textContent = "Loading movies....";
fetch(allShowsApi)
// Apsirasome resolve
.then((response) => {
// .json irgi yra Promise (pazadu kad paversiu atsakyma json pavidalu)
return response.json();
})
.then((data) => {
console.log(data);
statusTxt.textContent = `Loaded ${data.length} movies`;
// data- jau is response paverstas i normalu json pavidala informacija, ateis INFO is JSON!!!!!
// einu per masyva duomenu ir sukuriu korteles
renderMovies(data);
allShows = data;
getGenres();
}) // end of resolve
// reject
.catch((error) => {
console.log(error);
statusTxt.textContent = "An error occurred";
});
// end of reject
}
// ant puslapio uzkrovimo paleis sita funkcija
getAllShows();
searchInput.addEventListener("input", () => {
const value = searchInput.value;
if (value.length >= 2) {
heroSection.classList.add("hidden");
searchShowsByName(value);
} else if (value.length === 0) {
heroSection.classList.remove("hidden");
getAllShows();
resultsDiv.innerHTML = "";
}
});
function searchShowsByName(value) {
statusTxt.textContent = "Loading...";
resultsDiv.innerHTML = "";
fetch(`${searchShows}${value}`)
.then((response) => {
return response.json();
})
.then((data) => {
const movies = data.map((item) => item.show);
statusTxt.textContent = `Found ${movies.length} movies`;
renderMovies(movies);
});
}
function renderMovies(movies) {
resultsDiv.innerHTML = "";
movies.forEach((movie) => {
const div = document.createElement("div");
div.className = "card";
const rating = movie.rating?.average ?? "No rating available";
const image = movie.image?.medium ?? "";
div.innerHTML = `
${image ? `<img src="${image}" alt="photo">` : ""}
<h3>${movie.name}</h3>
<h3><i class="bi bi-star-fill"></i> (${rating})</h3>
`;
div.addEventListener("click", () => {
// leidzia smogu nukreipti i kita arba vidini puslapi
// indikacija, ant kurio filmo paspaudziam
window.location.href = `details.html?id=${movie.id}`;
});
resultsDiv.appendChild(div);
});
}
// GENRES LOGIC
const genresBox = document.getElementById("genres-select");
let allShows = [];
function getGenres() {
let genres = [];
allShows.forEach((show) => {
show.genres.forEach((genre) => {
genres.push(genre);
});
});
const uniqueGenres = [...new Set(genres)];
console.log(uniqueGenres);
genresBox.innerHTML = "";
const select = document.createElement("select");
select.className = "genres-select";
let options = `<option value="">All genres</option>`;
uniqueGenres.forEach((genre) => {
options += `<option value="${genre}">${genre}</option>`;
});
select.innerHTML = options;
genresBox.appendChild(select);
// Rendering cards by selected genre
select.addEventListener("change", (event) => {
const selectedGenre = event.target.value;
let filtered = [];
if (selectedGenre === "") {
filtered = allShows;
} else {
filtered = allShows.filter((show) => {
return show.genres.includes(selectedGenre);
});
}
renderMovies(filtered);
});
}