Users now using index

This commit is contained in:
James Barnsley
2017-01-19 08:56:12 +13:00
parent 99d27c2365
commit b611970907
5 changed files with 121 additions and 38 deletions

View File

@ -713,29 +713,43 @@ export function getLibraryArtists(){
export function getUser( uri ){
return (dispatch, getState) => {
dispatch({ type: 'SPOTIFY_USER_LOADED', data: false });
// get the user
sendRequest( dispatch, getState, 'users/'+ helpers.getFromUri('userid',uri) )
.then( response => {
dispatch({
type: 'USER_LOADED',
uri: response.uri,
user: response
});
})
var user = {};
// get the first page of playlists
sendRequest( dispatch, getState, 'users/'+ helpers.getFromUri('userid', uri) +'/playlists?limit=40' )
.then( response => {
// get both the artist and the top tracks
$.when(
var playlists = []
for (var i = 0; i < response.items.length; i++){
playlists.push(Object.assign(
{},
response.items[i],
{
can_edit: (getState().spotify.me && response.items[i].owner.id == getState().spotify.me.id),
tracks_total: response.items[i].tracks.total
}
))
}
sendRequest( dispatch, getState, 'users/'+ helpers.getFromUri('userid',uri) )
.then( response => {
Object.assign(user, response);
}),
dispatch({
type: 'PLAYLISTS_LOADED',
playlists: playlists
});
sendRequest( dispatch, getState, 'users/'+ helpers.getFromUri('userid', uri) +'/playlists?limit=50' )
.then( response => {
Object.assign(user, { playlists: response.items, playlists_more: response.next, playlists_total: response.total });
})
).then( () => {
dispatch({
type: 'SPOTIFY_USER_LOADED',
data: user
});
});
dispatch({
type: 'SPOTIFY_USER_PLAYLISTS_LOADED',
uri: uri,
data: response
});
})
}
}

View File

@ -123,6 +123,16 @@ const SpotifyMiddleware = (function(){
});
break
case 'SPOTIFY_USER_FOLLOWING_LOADED':
store.dispatch({
type: 'USER_LOADED',
uri: action.uri,
user: {
is_following: action.is_following
}
});
break
case 'SPOTIFY_NEW_RELEASES_LOADED':
store.dispatch({
type: 'ALBUMS_LOADED',
@ -150,6 +160,38 @@ const SpotifyMiddleware = (function(){
});
break
case 'SPOTIFY_USER_PLAYLISTS_LOADED':
var playlists = []
for( var i = 0; i < action.data.items.length; i++ ){
var playlist = Object.assign(
{},
action.data.items[i],
{
can_edit: (store.getState().spotify.authorized && store.getState().spotify.me && action.data.items[i].owner.id == store.getState().spotify.me.id),
tracks_total: action.data.items[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: 'USER_PLAYLISTS_LOADED',
uri: action.uri,
uris: helpers.asURIs(playlists),
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++ ){