-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
63 lines (52 loc) · 1.32 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
import React, {useEffect} from 'react';
import './App.css';
import Login from './Components/Login';
import Player from './Components/Player';
import { getTokenFromResponse } from './spotify';
import SpotifyWebApi from 'spotify-web-api-js';
import {useDataLayerValue} from './dataLayer'
const spotify = new SpotifyWebApi();
const App = () => {
const [{user, token}, dispatch] = useDataLayerValue();
useEffect( () => {
const hash = getTokenFromResponse();
window.location.hash="";
const temp_token = hash.access_token;
if (temp_token) {
dispatch({
type: "SET_TOKEN",
token: temp_token
})
spotify.setAccessToken(temp_token);
spotify.getMe().then( user => {
dispatch({
type: 'SET_USER',
user: user
})
})
spotify.getUserPlaylists().then( ( playlist ) => {
dispatch({
type: 'SET_PLAYLIST',
playlist : playlist
})
})
spotify.getPlaylist('37i9dQZF1DXd8cOUiye1o2').then( response => {
dispatch({
type:'SET_DISCOVER_WEEKLY',
discover_weekly: response
})
})
}
}, [])
return (
<div className="App">
{
token ?
<Player spotify={spotify}/>
:
<Login/>
}
</div>
);
}
export default App;