-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
157 lines (137 loc) · 3.9 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import React, {useEffect, useState} from 'react';
import PropTypes from 'prop-types';
import hash from "./helper/hash";
import {connect} from 'react-redux';
import {
createPlaylist,
getPlaylistsBySpotify,
getPlaylistTracks,
getUser,
setPlaylistTracks,
setToken,
userLogout
} from "./redux/actions";
import Start from "./components/Start";
import Player from "./components/Player/Player";
import Layout from "./components/Layout";
import Imprint from "./components/Imprint";
const App = ({
token,
setToken,
getUser,
user,
getPlaylistsBySpotify,
playlists,
getPlaylistTracks,
tracks,
setPlaylistTracks
}) => {
const [currentPlaylist, setCurrentPlaylist] = useState(null);
const [showImprint, setShowImprint] = useState(false);
useEffect(() => {
if (!token) {
let _token = hash.access_token;
if (_token) {
setToken(_token);
}
}
}, [])
useEffect(() => {
if (token) {
getUser();
}
}, [token])
useEffect(() => {
if (user && user.name) {
getPlaylistsBySpotify()
}
}, [user])
useEffect(() => {
if (playlists && playlists.length) {
setCurrentPlaylist(playlists[0]);
getPlaylistTracks(playlists[0].id)
}
}, [playlists])
const handleTracks = (id) => {
let newTracks = tracks;
newTracks.forEach((track) => {
if (track.track.id === id) {
track.active = !track.active
}
});
let x = [...newTracks];
setPlaylistTracks(x);
}
const changePlaylist = (id) => {
let newPlaylist = playlists.filter(playlist => playlist.id === id);
setCurrentPlaylist(newPlaylist[0])
getPlaylistTracks(id)
}
if (showImprint) {
return <Imprint setShowImprint={setShowImprint}/>
}
if (!token) {
return <Start showImprint={showImprint} setShowImprint={setShowImprint}/>
}
return (
<Layout setShowImprint={setShowImprint}>
{
user && tracks && tracks.length && currentPlaylist ?
<Player
username={user.name}
playlist={currentPlaylist}
handleTracks={handleTracks}
changePlaylist={changePlaylist}
playlists={playlists}
/>
: null
}
</Layout>
);
};
const mapStateToProps = state => {
return {
token: state.token,
user: state.user,
tracks: state.tracks,
message: state.message,
playlists: state.playlists
}
}
const mapDispatchToProps = dispatch => {
return {
setToken: token => {
dispatch(setToken(token))
},
getUser: () => {
dispatch(getUser())
},
createPlaylist: (playlistBody, userId, tracksUris) => {
dispatch(createPlaylist(playlistBody, userId, tracksUris))
},
userLogout: () => {
dispatch(userLogout())
},
getPlaylistsBySpotify: () => {
dispatch(getPlaylistsBySpotify(0))
},
getPlaylistTracks: (id) => {
dispatch(getPlaylistTracks(id))
},
setPlaylistTracks: (tracks) => {
dispatch(setPlaylistTracks(tracks));
}
}
}
App.propTypes = {
token: PropTypes.string,
message: PropTypes.object,
setToken: PropTypes.func,
setPlaylistTracks: PropTypes.func,
getPlaylistTracks: PropTypes.func,
getUser: PropTypes.func,
user: PropTypes.object,
playlists: PropTypes.array,
tracks: PropTypes.array
};
export default connect(mapStateToProps, mapDispatchToProps)(App);