-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
74 lines (67 loc) · 2.25 KB
/
app.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
const searchSong = async () => {
const searchText = document.getElementById('search-field').value;
document.getElementById('song-lyrics').innerText = "";
toggleSpinner();
try {
const res = await fetch(`https://api.lyrics.ovh/suggest/${searchText}`);
const data = await res.json();
displaySongs(data.data);
}
catch (error) {
displayError();
}
}
const toggleSpinner = () => {
const spinner = document.getElementById("loading-spinner");
spinner.classList.toggle("d-none")
}
const displaySongs = songs => {
const songContainer = document.getElementById('song-container');
songContainer.innerText = "";
if (songs.length === 0) {
displayError();
}
else {
songs.forEach(song => {
const songDiv = document.createElement("div");
document.getElementById('error').innerText = "";
songDiv.className = "single-result row align-items-center my-3 p-3";
songDiv.innerHTML = `
<div class="col-md-9">
<h3 class="lyrics-name">${song.title}</h3>
<p class="author lead">Album by <span>${song.artist.name}</span></p>
<audio controls>
<source src="${song.preview}" type="audio/mpeg">
</audio>
</div>
<div class="col-md-3 text-md-right text-center">
<button onclick="getLyric('${song.artist.name}', '${song.title}')" class="btn btn-success">Get Lyrics</button>
</div>
`;
songContainer.appendChild(songDiv);
});
toggleSpinner();
}
}
const getLyric = (artist, title) => {
fetch(`https://api.lyrics.ovh/v1/${artist}/${title}`)
.then(res => res.json())
.then(data => displayLyrics(data.lyrics))
.catch(error => displayError())
}
const displayLyrics = lyrics => {
const lyricsDiv = document.getElementById('song-lyrics');
lyricsDiv.innerText = "";
lyricsDiv.innerText = lyrics;
}
const displayError = () => {
document.getElementById('error').innerText = "Something went wrong. Please try again";
toggleSpinner();
}
document.getElementById('search').addEventListener('click', searchSong);
document.getElementById('search-field').addEventListener("keypress", (event) => {
if (event.key === "Enter") {
event.preventDefault();
document.getElementById("search").click();
}
});