Following; PLAYLISTS_LOADED; Full index

This commit is contained in:
James Barnsley
2017-01-15 11:40:08 +13:00
parent ce4c26f18b
commit 8b15e024cd
11 changed files with 269 additions and 171 deletions

View File

@ -355,14 +355,11 @@ export function getCategory( id ){
export function getNewReleases(){
return (dispatch, getState) => {
dispatch({ type: 'SPOTIFY_NEW_RELEASES_LOADED', data: false });
sendRequest( dispatch, getState, 'browse/new-releases?country='+getState().spotify.country+'&limit=50' )
.then( response => {
dispatch({
type: 'SPOTIFY_NEW_RELEASES_LOADED',
data: response.albums
data: response
});
});
}
@ -405,8 +402,6 @@ export function getSearchResults( query, type = 'album,artist,playlist,track', l
export function following(uri, method = 'GET'){
return (dispatch, getState) => {
dispatch({ type: 'FOLLOWING_LOADING' });
if( method == 'PUT' ) var is_following = true
if( method == 'DELETE' ) var is_following = false
@ -450,7 +445,8 @@ export function following(uri, method = 'GET'){
if( response ) is_following = response
if( typeof(is_following) === 'object' ) is_following = is_following[0]
dispatch({
type: asset_name.toUpperCase()+'_FOLLOWING_LOADED',
type: 'SPOTIFY_'+asset_name.toUpperCase()+'_FOLLOWING_LOADED',
uri: uri,
is_following: is_following
});
});
@ -603,31 +599,29 @@ export function getArtist( uri ){
sendRequest( dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/related-artists' )
.then( response => {
for (var i = 0; i < response.artists.length; i++){
dispatch({
type: 'ARTIST_LOADED',
artist: response.artists[i]
});
}
Object.assign(artist, { related_artists: helpers.asURIs(response.artists) });
}),
sendRequest( dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/albums' )
.then( response => {
for (var i = 0; i < response.items.length; i++){
dispatch({
type: 'ALBUM_LOADED',
album: response.items[i]
});
}
Object.assign(artist, { albums: helpers.asURIs(response.items), albums_more: response.next });
dispatch({
type: 'ARTISTS_LOADED',
artists: response.artists
});
Object.assign(artist, { related_artists_uris: helpers.asURIs(response.artists) });
})
).then( () => {
dispatch({
type: 'ARTIST_LOADED',
uri: artist.uri,
artist: artist
});
// now go get our artist albums
sendRequest( dispatch, getState, 'artists/'+ helpers.getFromUri('artistid', uri) +'/albums' )
.then( response => {
dispatch({
type: 'SPOTIFY_ARTIST_ALBUMS_LOADED',
data: response,
uri: uri
});
})
});
}
}
@ -732,19 +726,34 @@ export function getAlbum( uri ){
sendRequest( dispatch, getState, 'albums/'+ helpers.getFromUri('albumid', uri) )
.then( response => {
// dispatch our loaded artists (simple objects)
dispatch({
type: 'ARTISTS_LOADED',
artists: response.artists
});
var album = Object.assign(
{},
response,
{
artists: helpers.asURIs(response.artists),
artists_uris: helpers.asURIs(response.artists),
tracks: response.tracks.items,
tracks_more: response.tracks.next,
tracks_total: response.tracks.total
}
)
// add our album to all the tracks
for (var i = 0; i < album.tracks.length; i++){
album.tracks[i].album = {
name: album.name,
uri: album.uri
}
}
dispatch({
type: 'ALBUM_LOADED',
uri: album.uri,
album: album
});
@ -854,6 +863,7 @@ export function getPlaylist( uri ){
dispatch({
type: 'PLAYLIST_LOADED',
uri: playlist.uri,
playlist: playlist
})
})
@ -868,38 +878,15 @@ function loadNextPlaylistsBatch( dispatch, getState, playlists, lastResponse ){
loadNextPlaylistsBatch( dispatch, getState, playlists, response )
});
}else{
for( var i = 0; i < playlists.length; i++ ){
var playlist = Object.assign(
{},
playlists[i],
{
can_edit: (getState().spotify.authorized && getState().spotify.me && playlists[i].owner.id == getState().spotify.me.id),
tracks_total: playlists[i].tracks.total
}
)
// remove our tracklist. It'll overwrite any full records otherwise
delete playlist.tracks
dispatch({
type: 'PLAYLIST_LOADED',
playlist: playlist
});
}
dispatch({
type: 'LIBRARY_PLAYLISTS_LOADED',
uris: helpers.asURIs(playlists)
type: 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED',
playlists: playlists
});
}
}
export function getAllLibraryPlaylists(){
return (dispatch, getState) => {
dispatch({ type: 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED', data: false });
sendRequest( dispatch, getState, 'me/playlists?limit=50' )
.then( response => {
loadNextPlaylistsBatch( dispatch, getState, response.items, response )

View File

@ -93,6 +93,92 @@ const SpotifyMiddleware = (function(){
}
break
case 'SPOTIFY_PLAYLIST_FOLLOWING_LOADED':
store.dispatch({
type: 'PLAYLIST_LOADED',
uri: action.uri,
playlist: {
is_following: action.is_following
}
});
break
case 'SPOTIFY_ALBUM_FOLLOWING_LOADED':
store.dispatch({
type: 'ALBUM_LOADED',
uri: action.uri,
album: {
is_following: action.is_following
}
});
break
case 'SPOTIFY_ARTIST_FOLLOWING_LOADED':
store.dispatch({
type: 'ARTIST_LOADED',
uri: action.uri,
artist: {
is_following: action.is_following
}
});
break
case 'SPOTIFY_NEW_RELEASES_LOADED':
store.dispatch({
type: 'ALBUMS_LOADED',
albums: action.data.albums.items
});
store.dispatch({
type: 'NEW_RELEASES_LOADED',
uris: helpers.asURIs(action.data.albums.items),
more: action.data.albums.next,
total: action.data.albums.total
});
break
case 'SPOTIFY_ARTIST_ALBUMS_LOADED':
store.dispatch({
type: 'ALBUMS_LOADED',
albums: action.data.items
});
store.dispatch({
type: 'ARTIST_ALBUMS_LOADED',
uri: action.uri,
uris: helpers.asURIs(action.data.items),
more: action.data.next,
total: action.data.total
});
break
case 'SPOTIFY_LIBRARY_PLAYLISTS_LOADED':
var playlists = []
for( var i = 0; i < action.playlists.length; i++ ){
var playlist = Object.assign(
{},
action.playlists[i],
{
can_edit: (store.getState().spotify.authorized && store.getState().spotify.me && action.playlists[i].owner.id == store.getState().spotify.me.id),
tracks_total: action.playlists[i].tracks.total
}
)
// remove our tracklist. It'll overwrite any full records otherwise
delete playlist.tracks
playlists.push(playlist)
}
store.dispatch({
type: 'PLAYLISTS_LOADED',
playlists: playlists
});
store.dispatch({
type: 'LIBRARY_PLAYLISTS_LOADED',
uris: helpers.asURIs(playlists)
});
break
case 'SPOTIFY_LIBRARY_ARTISTS_LOADED':
store.dispatch({
type: 'ARTISTS_LOADED',
@ -107,9 +193,7 @@ const SpotifyMiddleware = (function(){
break
case 'SPOTIFY_LIBRARY_ALBUMS_LOADED':
var albums = []
for (var i = 0; i < action.data.items.length; i++){
albums.push(
Object.assign(