-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
65 lines (48 loc) · 1.63 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
const searchInput = document.getElementById('search-input');
const resultArtists = document.getElementById('result-artist');
const resultPlaylist = document.getElementById('result-playlist');
window.onload = function() {
var greeting;
var hora = new Date().getHours();
if (hora >= 6 && hora < 12) {
greeting = "Bom dia!";
} else if (hora >= 12 && hora < 18) {
greeting = "Boa tarde!";
} else {
greeting = "Boa noite!";
}
document.getElementById("greeting").innerText = greeting;
}
function requestApi(searchTerm) {
const url = `http://localhost:3000/artists`;
fetch(url)
.then((response) => response.json())
.then(result =>
displayResults(result.filter(
(item => item.name.toLowerCase().indexOf(searchTerm.toLowerCase()) > -1 ? true : false )
))
);
}
function displayResults(results) {
resultPlaylist.classList.add('hidden')
resultArtists.classList.remove('hidden');
const artistName = document.getElementById('artist-name');
const artistImage = document.getElementById('artist-img');
if (results.length === 0) {
resultArtists.classList.add('hidden');
return;
}
results.forEach(element => {
artistName.innerText = element.name;
artistImage.src = element.urlImg;
})
}
document.addEventListener('input', function() {
const searchTerm = searchInput.value.toLowerCase();
if (searchTerm === '') {
resultPlaylist.classList.remove('hidden');
resultArtists.classList.add('hidden');
return;
}
requestApi(searchTerm);
});