2016-10-16 12:37:37 +13:00
|
|
|
|
2016-11-09 13:18:03 +13:00
|
|
|
|
2016-10-24 21:26:01 +13:00
|
|
|
var actions = require('./actions.js')
|
2016-10-16 12:37:37 +13:00
|
|
|
|
|
|
|
|
const SpotifyMiddleware = (function(){
|
|
|
|
|
|
2016-10-17 10:39:11 +13:00
|
|
|
/**
|
|
|
|
|
* The actual middleware inteceptor
|
|
|
|
|
**/
|
|
|
|
|
return store => next => action => {
|
2016-10-18 21:57:29 +13:00
|
|
|
var state = store.getState();
|
|
|
|
|
|
|
|
|
|
switch(action.type){
|
2016-10-17 10:39:11 +13:00
|
|
|
|
2016-11-06 15:47:37 +13:00
|
|
|
case 'SPOTIFY_CONNECT':
|
|
|
|
|
store.dispatch( actions.getMe() )
|
|
|
|
|
|
2016-11-13 12:55:00 +13:00
|
|
|
|
|
|
|
|
case 'SPOTIFY_REMOVE_PLAYLIST_TRACKS':
|
|
|
|
|
var playlist = state.ui.playlist
|
|
|
|
|
|
|
|
|
|
if( !store.getState().spotify.authorized ){
|
|
|
|
|
alert('Must be logged in to Spotify to do this')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if( !store.getState().spotify.me || store.getState().spotify.me.id != playlist.owner.id ){
|
2016-11-13 14:41:08 +13:00
|
|
|
alert('You can only modify playlists you own')
|
2016-11-13 12:55:00 +13:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
store.dispatch( actions.deleteTracksFromPlaylist( playlist.uri, playlist.snapshot_id, action.tracks_indexes ))
|
|
|
|
|
break
|
|
|
|
|
|
2016-11-13 14:41:08 +13:00
|
|
|
|
|
|
|
|
case 'SPOTIFY_ADD_PLAYLIST_TRACKS':
|
|
|
|
|
|
|
|
|
|
if( !store.getState().spotify.authorized ){
|
|
|
|
|
alert('Must be logged in to Spotify to do this')
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
store.dispatch( actions.addTracksToPlaylist( action.playlist_uri, action.tracks_uris ))
|
|
|
|
|
break
|
|
|
|
|
|
2016-10-24 21:26:01 +13:00
|
|
|
// when our mopidy server current track changes
|
2016-11-03 18:22:26 +13:00
|
|
|
case 'MOPIDY_CURRENTTLTRACK':
|
2016-10-21 09:56:07 +13:00
|
|
|
|
2016-10-24 21:26:01 +13:00
|
|
|
// proceed as usual so we don't inhibit default functionality
|
2016-10-21 09:56:07 +13:00
|
|
|
next(action)
|
|
|
|
|
|
2016-10-24 21:26:01 +13:00
|
|
|
// if the current track is a spotify track
|
2016-11-03 18:22:26 +13:00
|
|
|
if( action.data && action.data.track.uri.substring(0,14) == 'spotify:track:' ){
|
2016-10-24 21:26:01 +13:00
|
|
|
store.dispatch( actions.getTrack( action.data.track.uri ) )
|
|
|
|
|
}
|
2016-10-19 14:09:34 +13:00
|
|
|
|
2016-10-16 12:37:37 +13:00
|
|
|
// This action is irrelevant to us, pass it on to the next middleware
|
|
|
|
|
default:
|
|
|
|
|
return next(action);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
})();
|
|
|
|
|
|
|
|
|
|
export default SpotifyMiddleware
|