Albums and playlists now loading using processes
This commit is contained in:
@ -180,10 +180,10 @@ function refreshToken( dispatch, getState ){
|
||||
})
|
||||
}
|
||||
|
||||
export function setConfig( config ){
|
||||
export function set(data){
|
||||
return {
|
||||
type: 'SPOTIFY_SET_CONFIG',
|
||||
config: config
|
||||
type: 'SPOTIFY_SET',
|
||||
data: data
|
||||
}
|
||||
}
|
||||
|
||||
@ -1081,21 +1081,6 @@ export function getAlbum( uri ){
|
||||
}
|
||||
}
|
||||
|
||||
export function getLibraryAlbums(){
|
||||
return (dispatch, getState) => {
|
||||
|
||||
dispatch({ type: 'LIBRARY_ALBUMS_LOADED', uris: false });
|
||||
|
||||
sendRequest( dispatch, getState, 'me/albums?limit=40' )
|
||||
.then( response => {
|
||||
dispatch({
|
||||
type: 'SPOTIFY_LIBRARY_ALBUMS_LOADED',
|
||||
data: response
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function toggleAlbumInLibrary( uri, method ){
|
||||
if( method == 'PUT' ) var new_state = 1
|
||||
if( method == 'DELETE' ) var new_state = 0
|
||||
@ -1252,43 +1237,6 @@ export function getAllPlaylistTracks(uri){
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Get all of our playlists in one foul swoop
|
||||
*
|
||||
* Recursively gets playlists until no more .next value. Fails when user has
|
||||
* a lot of playlists, and we hit the API limits
|
||||
**/
|
||||
function loadNextPlaylistsBatch(dispatch, getState, playlists, lastResponse){
|
||||
if( lastResponse.next ){
|
||||
sendRequest( dispatch, getState, lastResponse.next )
|
||||
.then( response => {
|
||||
playlists = [...playlists, ...response.items]
|
||||
loadNextPlaylistsBatch( dispatch, getState, playlists, response )
|
||||
});
|
||||
}else{
|
||||
|
||||
dispatch(uiActions.processFinished('SPOTIFY_GET_ALL_LIBRARY_PLAYLISTS'))
|
||||
|
||||
dispatch({
|
||||
type: 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED',
|
||||
playlists: playlists
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function getLibraryPlaylists(){
|
||||
return (dispatch, getState) => {
|
||||
|
||||
dispatch(uiActions.startProcess('SPOTIFY_GET_ALL_LIBRARY_PLAYLISTS','Loading library playlists'))
|
||||
|
||||
sendRequest( dispatch, getState, 'me/playlists?limit=50' )
|
||||
.then( response => {
|
||||
loadNextPlaylistsBatch( dispatch, getState, response.items, response )
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
export function toggleFollowingPlaylist(uri, method){
|
||||
if( method == 'PUT' ) var new_state = 1
|
||||
if( method == 'DELETE' ) var new_state = 0
|
||||
@ -1347,4 +1295,93 @@ export function reorderPlaylistTracks( uri, range_start, range_length, insert_be
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* =============================================================== LIBRARY ==============
|
||||
* ======================================================================================
|
||||
**/
|
||||
|
||||
|
||||
/**
|
||||
* ALbums
|
||||
**/
|
||||
|
||||
export function getLibraryAlbums(){
|
||||
return (dispatch, getState) => {
|
||||
dispatch(uiActions.startProcess('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR','Loading Spotify library albums', {next: 'me/albums?limit=50'}))
|
||||
}
|
||||
}
|
||||
|
||||
export function getLibraryAlbumsProcessor(data){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, data.next)
|
||||
.then( response => {
|
||||
|
||||
dispatch({
|
||||
type: 'SPOTIFY_LIBRARY_ALBUMS_LOADED',
|
||||
albums: response.items
|
||||
})
|
||||
|
||||
// Check to see if we've been cancelled
|
||||
if (getState().ui.processes['SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR'] !== undefined){
|
||||
var processor = getState().ui.processes['SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR']
|
||||
|
||||
if (processor.cancelling){
|
||||
dispatch(uiActions.processFinished('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR'))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// We got a next link, so we've got more work to be done
|
||||
if (response.next){
|
||||
dispatch(uiActions.runProcess('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR', {next: response.next}))
|
||||
} else {
|
||||
dispatch(uiActions.processFinished('SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR'))
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Playlists
|
||||
**/
|
||||
|
||||
export function getLibraryPlaylists(){
|
||||
return (dispatch, getState) => {
|
||||
dispatch(uiActions.startProcess('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR','Loading Spotify library playlists', {next: 'me/playlists?limit=50'}))
|
||||
}
|
||||
}
|
||||
|
||||
export function getLibraryPlaylistsProcessor(data){
|
||||
return (dispatch, getState) => {
|
||||
sendRequest(dispatch, getState, data.next)
|
||||
.then( response => {
|
||||
|
||||
dispatch({
|
||||
type: 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED',
|
||||
playlists: response.items
|
||||
})
|
||||
|
||||
// Check to see if we've been cancelled
|
||||
if (getState().ui.processes['SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR'] !== undefined){
|
||||
var processor = getState().ui.processes['SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR']
|
||||
|
||||
if (processor.cancelling){
|
||||
dispatch(uiActions.processFinished('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR'))
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
// We got a next link, so we've got more work to be done
|
||||
if (response.next){
|
||||
dispatch(uiActions.runProcess('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR', {next: response.next}))
|
||||
} else {
|
||||
dispatch(uiActions.processFinished('SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR'))
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
@ -180,6 +180,10 @@ const SpotifyMiddleware = (function(){
|
||||
});
|
||||
break
|
||||
|
||||
case 'SPOTIFY_GET_LIBRARY_PLAYLISTS_PROCESSOR':
|
||||
store.dispatch(spotifyActions.getLibraryPlaylistsProcessor(action.data))
|
||||
break
|
||||
|
||||
case 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED':
|
||||
var playlists = []
|
||||
for( var i = 0; i < action.playlists.length; i++ ){
|
||||
@ -236,20 +240,24 @@ const SpotifyMiddleware = (function(){
|
||||
});
|
||||
break
|
||||
|
||||
case 'SPOTIFY_GET_LIBRARY_ALBUMS_PROCESSOR':
|
||||
store.dispatch(spotifyActions.getLibraryAlbumsProcessor(action.data))
|
||||
break
|
||||
|
||||
case 'SPOTIFY_LIBRARY_ALBUMS_LOADED':
|
||||
var albums = []
|
||||
for (var i = 0; i < action.data.items.length; i++){
|
||||
for (var i = 0; i < action.albums.length; i++){
|
||||
albums.push(
|
||||
Object.assign(
|
||||
{},
|
||||
action.data.items[i].album,
|
||||
action.albums[i].album,
|
||||
{
|
||||
in_library: true, // assumed because we asked for library items
|
||||
source: 'spotify',
|
||||
added_at: action.data.items[i].added_at,
|
||||
tracks: action.data.items[i].album.tracks.items,
|
||||
tracks_more: action.data.items[i].album.tracks.next,
|
||||
tracks_total: action.data.items[i].album.tracks.total
|
||||
added_at: action.albums[i].added_at,
|
||||
tracks: action.albums[i].album.tracks.items,
|
||||
tracks_more: action.albums[i].album.tracks.next,
|
||||
tracks_total: action.albums[i].album.tracks.total
|
||||
}
|
||||
)
|
||||
)
|
||||
@ -262,9 +270,7 @@ const SpotifyMiddleware = (function(){
|
||||
|
||||
store.dispatch({
|
||||
type: 'LIBRARY_ALBUMS_LOADED',
|
||||
uris: helpers.arrayOf('uri',albums),
|
||||
more: action.data.next,
|
||||
total: action.data.total
|
||||
uris: helpers.arrayOf('uri',albums)
|
||||
});
|
||||
break
|
||||
|
||||
|
||||
@ -14,8 +14,8 @@ export default function reducer(spotify = {}, action){
|
||||
case 'SPOTIFY_DISCONNECTED':
|
||||
return Object.assign({}, spotify, { connected: false, connecting: false })
|
||||
|
||||
case 'SPOTIFY_SET_CONFIG':
|
||||
return Object.assign({},spotify,action.config)
|
||||
case 'SPOTIFY_SET':
|
||||
return Object.assign({},spotify,action.data)
|
||||
|
||||
case 'PUSHER_SPOTIFY_TOKEN':
|
||||
if (spotify.authorization) return spotify;
|
||||
|
||||
Reference in New Issue
Block a user