-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Commit para a Main da aula 4 da Imersão Front-end da Alura!!
- Loading branch information
1 parent
d3d3fbf
commit 29851d0
Showing
2 changed files
with
40 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
const searchInput = document.getElementById('search-input'); | ||
const resultArtist = document.getElementById('result-artist'); | ||
const resultPlaylist = document.getElementById('result-playlists'); | ||
|
||
function requestApi(searchTerm) { | ||
const url = `http://localhost:3000/artists?name_like=${searchTerm}` | ||
fetch(url) | ||
.then((response) => response.json()) | ||
.then((result) => displayResults(result)) | ||
} | ||
|
||
function displayResults(result) { | ||
resultPlaylist.classList.add('hidden'); | ||
const artistName = document.getElementById('artist-name'); | ||
const artistImage = document.getElementById('artist-img'); | ||
|
||
result.forEach(element => { | ||
artistName.innerText = element.name; | ||
artistImage.src = element.urlImg; | ||
}); | ||
|
||
resultArtist.classList.remove('hidden'); | ||
} | ||
|
||
document.addEventListener('input', function() { | ||
const searchTerm = searchInput.value.toLowerCase(); | ||
if (searchTerm === '') { | ||
resultPlaylist.classList.add('hidden'); | ||
resultArtist.classList.remove('hidden'); | ||
return; | ||
} | ||
|
||
requestApi(searchTerm); | ||
}) |