-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
58 lines (43 loc) · 1.87 KB
/
main.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
import { MusicSearch } from './src/elements/music-search/music-search';
import { MusicArtist } from './src/elements/music-artist/music-artist';
import { MusicError } from './src/elements/music-error/music-error';
class MusicSearch {
constructor() {
this.artistElement = document.querySelector('music-artist');
this.artistErrorElement = document.querySelector('music-error');
document.addEventListener('onTyping', (e) => this.onSearch(e));
}
onSearch(e) {
const query = e.detail;
const url = `http://localhost:4000/search`;
this.searchMusic(url, query)
.then(
response => {
const parsedData = JSON.parse(response);
if (parsedData.error) {
this.artistErrorElement.setAttribute('error_message', parsedData.error);
this.artistElement.removeAttribute('tracks');
this.artistElement.removeAttribute('artist');
} else {
this.artistErrorElement.removeAttribute('error_message');
this.artistElement.setAttribute('artist', parsedData.genre);
this.artistElement.setAttribute('tracks', response);
}
},
error => window.console.error('Failed!', error)
);
}
searchMusic(url, query) {
return new Promise(
(resolve, reject) => {
const req = new XMLHttpRequest();
req.open('POST', url);
req.setRequestHeader("Content-Type", "application/json;charset=UTF-8");
req.onload = () => req.status == 200 ? resolve(req.response) : reject(Error(req.statusText));
req.onerror = () => reject(Error('Network Error'));
req.send(JSON.stringify({ "query": query.toLowerCase()}));
}
);
}
}
const app = new MusicSearch();