Repository navigation
Expand file tree
/
Copy pathindex.js
More file actions
executable file
·106 lines (99 loc) · 2.97 KB
/
Copy pathindex.js
File metadata and controls
executable file
·106 lines (99 loc) · 2.97 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
/* eslint-disable no-console */
const API = 'https://itunes.apple.com/search';
const audioTag = document.querySelector('audio');
const searchInput = document.querySelector('#search');
const searchBtn = document.querySelector('.search-container .btn');
const songsList = document.querySelector('.results');
/**
* Fetch iTunes API with HTTP GET
* return JSON
* @param {string} term
*/
const fetchItunesSongs = async (term) => {
try {
const url = `${API}?term=${term}&test=${Math.random()}`;
const response = await fetch(url);
console.log(response);
const responseJson = await response.json();
return responseJson;
} catch (err) {
console.log(err);
throw err;
}
};
/**
* Handle click on the <ul> results list
* Get the corresponding <li>, and the data attribute attached
* Play the song
* @param {object} event
*/
const handleClickSong = (event) => {
// on utilise currentTarget pour être sur d'avoir le DIV
// même si on a cliqué sur un des enfants (le h1 par ex)
const target = event.currentTarget;
console.log(target);
if (target.tagName !== 'LI' && !target.getAttribute('data-preview')) {
return;
}
audioTag.setAttribute('src', target.getAttribute('data-preview'));
audioTag.play();
};
/**
* From a song object returned by the API, create a <li> tag
* @param {object} s
*/
const createSongLI = (s) => {
const li = document.createElement('li');
const h1 = document.createElement('h1');
const span = document.createElement('span');
h1.textContent = s.artistName;
span.textContent = s.trackName;
li.id = s.trackId;
li.setAttribute('data-preview', s.previewUrl);
li.addEventListener('click', handleClickSong);
li.appendChild(h1);
li.appendChild(span);
return li;
};
/**
* Perform a search request + add the results to the DOM
*/
const search = async () => {
let searchValue = searchInput.value.trim();
if (!searchValue) return;
songsList.innerHTML = '';
searchValue = searchValue.replace(' ', '+');
let response;
try {
response = await fetchItunesSongs(searchValue);
} catch (err) { // affichage des erreurs éventuelles
document.querySelector('.error').style.display = 'block';
document.querySelector('.no-result').style.display = 'none';
return;
}
if (response.resultCount) {
document.querySelector('.error').style.display = 'none';
document.querySelector('.no-result').style.display = 'none';
const songs = response.results.filter((r) => r.kind === 'song');
const ul = document.createElement('ul');
songs.forEach((s) => {
ul.appendChild(createSongLI(s));
});
songsList.appendChild(ul);
} else {
document.querySelector('.no-result').style.display = 'block';
document.querySelector('.error').style.display = 'none';
}
};
/**
* Bind keypress event on input to catch the enter key event
*/
searchInput.addEventListener('keypress', (e) => {
if (e.key === 'Enter') {
search();
}
});
// bind the click on button
searchBtn.addEventListener('click', search);
// bind the click on result list
songsList.addEventListener('click', handleClickSong);