-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
68 lines (53 loc) · 2.05 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
66
67
68
async function getData(url) {
const response = await axios.get(url);
return response.data;
}
function clearData() {
const container = document.querySelector('.results');
container.innerHTML = '';
}
function init() {
function generateArtistData(artistName, genre) {
const infoContainer = document.querySelector('.results');
const artcontainer = document.createElement('div');
artcontainer.className = 'artcontainer';
infoContainer.appendChild(artcontainer);
const artName = document.createElement('h3');
artName.innerText = `Artist name: ${artistName}`;
artcontainer.appendChild(artName);
const artGenre = document.createElement('p');
artGenre.innerText = `Genre: ${genre}`;
artcontainer.appendChild(artGenre);
}
function handleSearch(ev) {
ev.preventDefault();
const formData = new FormData(form);
const searchResult = formData.get('form');
const url = `https://itunes.apple.com/search?entity=allArtist&attribute=allArtistTerm&term=${searchResult}`;
if (searchResult.trim() !== '') {
clearData();
showArtist(url);
form.reset();
} else {
const infoContainer = document.querySelector('.results');
const artcontainer = document.createElement('div');
artcontainer.className = 'artcontainer';
infoContainer.appendChild(artcontainer);
const artName = document.createElement('h3');
artName.innerText = `Artist does not exist`;
artcontainer.appendChild(artName);
form.reset();
}
}
async function showArtist(url) {
const data = await getData(url);
for (let i = 0; i < 20; i++) {
const nm = data.results[i].artistName;
const gnr = data.results[i].primaryGenreName;
generateArtistData(nm, gnr);
}
}
const form = document.getElementById('formSearch');
form.addEventListener('submit', (ev) => handleSearch(ev));
}
init();