-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
135 lines (105 loc) · 3.76 KB
/
App.js
File metadata and controls
135 lines (105 loc) · 3.76 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import React, { useState, useEffect } from 'react';
import Dropdown from './Dropdown';
import Listbox from './Listbox';
import Detail from './Detail';
import { Credentials } from './Credentials';
import axios from 'axios';
const App = () => {
const spotify = Credentials();
console.log('RENDERING APP.JS');
const data = [
{value: 1, name: 'A'},
{value: 2, name: 'B'},
{value: 3, name: 'C'},
];
const [token, setToken] = useState('');
const [genres, setGenres] = useState({selectedGenre: '', listOfGenresFromAPI: []});
const [playlist, setPlaylist] = useState({selectedPlaylist: '', listOfPlaylistFromAPI: []});
const [tracks, setTracks] = useState({selectedTrack: '', listOfTracksFromAPI: []});
const [trackDetail, setTrackDetail] = useState(null);
useEffect(() => {
axios('https://accounts.spotify.com/api/token', {
headers: {
'Content-Type' : 'application/x-www-form-urlencoded',
'Authorization' : 'Basic ' + btoa(spotify.ClientId + ':' + spotify.ClientSecret)
},
data: 'grant_type=client_credentials',
method: 'POST'
})
.then(tokenResponse => {
setToken(tokenResponse.data.access_token);
axios('https://api.spotify.com/v1/browse/categories?locale=sv_US', {
method: 'GET',
headers: { 'Authorization' : 'Bearer ' + tokenResponse.data.access_token}
})
.then (genreResponse => {
setGenres({
selectedGenre: genres.selectedGenre,
listOfGenresFromAPI: genreResponse.data.categories.items
})
});
});
}, [genres.selectedGenre, spotify.ClientId, spotify.ClientSecret]);
const genreChanged = val => {
setGenres({
selectedGenre: val,
listOfGenresFromAPI: genres.listOfGenresFromAPI
});
axios(`https://api.spotify.com/v1/browse/categories/${val}/playlists?limit=10`, {
method: 'GET',
headers: { 'Authorization' : 'Bearer ' + token}
})
.then(playlistResponse => {
setPlaylist({
selectedPlaylist: playlist.selectedPlaylist,
listOfPlaylistFromAPI: playlistResponse.data.playlists.items
})
});
console.log(val);
}
const playlistChanged = val => {
console.log(val);
setPlaylist({
selectedPlaylist: val,
listOfPlaylistFromAPI: playlist.listOfPlaylistFromAPI
});
}
const buttonClicked = e => {
e.preventDefault();
axios(`https://api.spotify.com/v1/playlists/${playlist.selectedPlaylist}/tracks?limit=10`, {
method: 'GET',
headers: {
'Authorization' : 'Bearer ' + token
}
})
.then(tracksResponse => {
setTracks({
selectedTrack: tracks.selectedTrack,
listOfTracksFromAPI: tracksResponse.data.items
})
});
}
const listboxClicked = val => {
const currentTracks = [...tracks.listOfTracksFromAPI];
const trackInfo = currentTracks.filter(t => t.track.id === val);
setTrackDetail(trackInfo[0].track);
}
return (
<div className="container">
<form onSubmit={buttonClicked}>
<Dropdown label="Genre :" options={genres.listOfGenresFromAPI} selectedValue={genres.selectedGenre} changed={genreChanged} />
<Dropdown label="Playlist :" options={playlist.listOfPlaylistFromAPI} selectedValue={playlist.selectedPlaylist} changed={playlistChanged} />
<div className="col-sm-6 row form-group px-0">
<button type='submit' className="btn btn-success col-sm-12">
Search
</button>
</div>
<div className="row">
<Listbox items={tracks.listOfTracksFromAPI} clicked={listboxClicked} />
{trackDetail && <Detail {...trackDetail} /> }
</div>
</form>
</div>
);
}
export default App;