-
Notifications
You must be signed in to change notification settings - Fork 0
/
search.js
37 lines (31 loc) · 1.08 KB
/
search.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
const resultArtist = document.getElementById("result-artist");
const playlistContainer = document.getElementById("result-playlists");
const searchInput = document.getElementById("search-input");
function requestApi(searchTerm) {
const url = `http://localhost:3000/artists?name_like=${searchTerm}`
fetch(url)
.then((response) => response.json())
.then((results) => displayResults(results));
}
function displayResults(results) {
hidePlaylists();
const artistImage = document.getElementById("artist-img");
const artistName = document.getElementById("artist-name");
results.forEach((element) => {
artistImage.src = element.urlImg;
artistName.innerText = element.name;
});
resultArtist.classList.remove("hidden");
}
function hidePlaylists() {
playlistContainer.classList.add("hidden");
}
searchInput.addEventListener("input", function () {
const searchTerm = searchInput.value.toLowerCase();
if (searchTerm === "") {
resultArtist.classList.add("hidden");
playlistContainer.classList.remove("hidden");
return;
}
requestApi(searchTerm);
});