inLibrary() instead of is_following for all library items - need to handle initial loads
This commit is contained in:
@ -487,10 +487,11 @@ export function following(uri, method = 'GET'){
|
||||
.then( response => {
|
||||
if( response ) is_following = response
|
||||
if( typeof(is_following) === 'object' ) is_following = is_following[0]
|
||||
|
||||
dispatch({
|
||||
type: 'SPOTIFY_'+asset_name.toUpperCase()+'_FOLLOWING_LOADED',
|
||||
type: asset_name.toUpperCase()+'_LIBRARY_CHECK',
|
||||
key: uri,
|
||||
is_following: is_following
|
||||
in_library: is_following
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@ -98,46 +98,6 @@ const SpotifyMiddleware = (function(){
|
||||
}
|
||||
break
|
||||
|
||||
case 'SPOTIFY_PLAYLIST_FOLLOWING_LOADED':
|
||||
store.dispatch({
|
||||
type: 'PLAYLIST_LOADED',
|
||||
key: action.key,
|
||||
playlist: {
|
||||
is_following: action.is_following
|
||||
}
|
||||
});
|
||||
break
|
||||
|
||||
case 'SPOTIFY_ALBUM_FOLLOWING_LOADED':
|
||||
store.dispatch({
|
||||
type: 'ALBUM_LOADED',
|
||||
key: action.key,
|
||||
album: {
|
||||
is_following: action.is_following
|
||||
}
|
||||
});
|
||||
break
|
||||
|
||||
case 'SPOTIFY_ARTIST_FOLLOWING_LOADED':
|
||||
store.dispatch({
|
||||
type: 'ARTIST_LOADED',
|
||||
key: action.key,
|
||||
artist: {
|
||||
is_following: action.is_following
|
||||
}
|
||||
});
|
||||
break
|
||||
|
||||
case 'SPOTIFY_USER_FOLLOWING_LOADED':
|
||||
store.dispatch({
|
||||
type: 'USER_LOADED',
|
||||
key: action.key,
|
||||
user: {
|
||||
is_following: action.is_following
|
||||
}
|
||||
});
|
||||
break
|
||||
|
||||
case 'SPOTIFY_NEW_RELEASES_LOADED':
|
||||
store.dispatch({
|
||||
type: 'ALBUMS_LOADED',
|
||||
@ -237,6 +197,7 @@ const SpotifyMiddleware = (function(){
|
||||
action.playlists[i],
|
||||
{
|
||||
source: 'spotify',
|
||||
in_library: true, // assumed because we asked for library items
|
||||
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
|
||||
}
|
||||
@ -260,13 +221,25 @@ const SpotifyMiddleware = (function(){
|
||||
break
|
||||
|
||||
case 'SPOTIFY_LIBRARY_ARTISTS_LOADED':
|
||||
var artists = []
|
||||
for (var i = 0; i < action.data.artists.items.length; i++){
|
||||
artists.push(
|
||||
Object.assign(
|
||||
{},
|
||||
action.data.artists.items[i],
|
||||
{
|
||||
in_library: true // assumed because we asked for library items
|
||||
}
|
||||
)
|
||||
)
|
||||
}
|
||||
store.dispatch({
|
||||
type: 'ARTISTS_LOADED',
|
||||
artists: action.data.artists.items
|
||||
artists: artists
|
||||
});
|
||||
store.dispatch({
|
||||
type: 'LIBRARY_ARTISTS_LOADED',
|
||||
uris: helpers.asURIs(action.data.artists.items),
|
||||
uris: helpers.asURIs(artists),
|
||||
more: action.data.artists.next,
|
||||
total: action.data.artists.total
|
||||
});
|
||||
@ -280,6 +253,7 @@ const SpotifyMiddleware = (function(){
|
||||
{},
|
||||
action.data.items[i].album,
|
||||
{
|
||||
in_library: true, // assumed because we asked for library items
|
||||
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,
|
||||
|
||||
@ -258,6 +258,21 @@ export default function reducer(ui = {}, action){
|
||||
library_albums_total: action.total
|
||||
});
|
||||
|
||||
case 'ALBUM_LIBRARY_CHECK':
|
||||
var items = Object.assign([], ui.library_albums)
|
||||
|
||||
// add/remove library reference
|
||||
var index = items.indexOf(action.key)
|
||||
|
||||
// removing existing
|
||||
if (index > -1 && !action.in_library){
|
||||
items.splice(index, 1)
|
||||
} else if (index < 0 && action.in_library){
|
||||
items.push(action.key)
|
||||
}
|
||||
|
||||
return Object.assign({}, ui, { library_albums: items });
|
||||
|
||||
case 'LOCAL_ALBUMS_LOADED':
|
||||
if (!action.uris) return Object.assign({}, ui, { local_albums: null });
|
||||
return Object.assign({}, ui, { local_albums: action.uris });
|
||||
@ -349,6 +364,21 @@ export default function reducer(ui = {}, action){
|
||||
library_artists_total: action.total
|
||||
});
|
||||
|
||||
case 'ARTIST_LIBRARY_CHECK':
|
||||
var items = Object.assign([], ui.library_artists)
|
||||
|
||||
// add/remove library reference
|
||||
var index = items.indexOf(action.key)
|
||||
|
||||
// removing existing
|
||||
if (index > -1 && !action.in_library){
|
||||
items.splice(index, 1)
|
||||
} else if (index < 0 && action.in_library){
|
||||
items.push(action.key)
|
||||
}
|
||||
|
||||
return Object.assign({}, ui, { library_artists: items });
|
||||
|
||||
case 'LOCAL_ARTISTS_LOADED':
|
||||
if (!action.uris) return Object.assign({}, ui, { local_artists: null });
|
||||
return Object.assign({}, ui, { local_artists: action.uris });
|
||||
@ -511,13 +541,6 @@ export default function reducer(ui = {}, action){
|
||||
playlists[action.key] = playlist
|
||||
return Object.assign({}, ui, { playlists: playlists });
|
||||
|
||||
case 'PLAYLIST_FOLLOWING_LOADED':
|
||||
var playlists = Object.assign([], ui.playlists)
|
||||
var playlist = Object.assign({}, playlists[action.key], { following: action.is_following })
|
||||
|
||||
playlists[action.key] = playlist
|
||||
return Object.assign({}, ui, { playlists: playlists });
|
||||
|
||||
case 'LIBRARY_PLAYLISTS_LOADED':
|
||||
if (ui.library_playlists){
|
||||
var library_playlists = [...ui.library_playlists, ...action.uris]
|
||||
@ -531,6 +554,21 @@ export default function reducer(ui = {}, action){
|
||||
library_playlists: library_playlists
|
||||
});
|
||||
|
||||
case 'PLAYLIST_LIBRARY_CHECK':
|
||||
var items = Object.assign([], ui.library_playlists)
|
||||
|
||||
// add/remove library reference
|
||||
var index = items.indexOf(action.key)
|
||||
|
||||
// removing existing
|
||||
if (index > -1 && !action.in_library){
|
||||
items.splice(index, 1)
|
||||
} else if (index < 0 && action.in_library){
|
||||
items.push(action.key)
|
||||
}
|
||||
|
||||
return Object.assign({}, ui, { library_playlists: items });
|
||||
|
||||
|
||||
/**
|
||||
* Search results
|
||||
|
||||
@ -91,6 +91,10 @@ class Album extends React.Component{
|
||||
this.props.mopidyActions.playURIs([this.props.params.uri], this.props.params.uri)
|
||||
}
|
||||
|
||||
inLibrary(){
|
||||
return (this.props.library_albums && this.props.library_albums.indexOf(this.props.params.uri) > -1)
|
||||
}
|
||||
|
||||
render(){
|
||||
if (this.props.album){
|
||||
var artists = []
|
||||
@ -128,7 +132,7 @@ class Album extends React.Component{
|
||||
|
||||
<div className="actions">
|
||||
<button className="primary" onClick={e => this.play()}>Play</button>
|
||||
{ helpers.uriSource(this.props.params.uri) == 'spotify' ? <FollowButton className="secondary" uri={this.props.params.uri} addText="Add to library" removeText="Remove from library" is_following={this.props.album.is_following} /> : null }
|
||||
{ helpers.uriSource(this.props.params.uri) == 'spotify' ? <FollowButton className="secondary" uri={this.props.params.uri} addText="Add to library" removeText="Remove from library" is_following={this.inLibrary()} /> : null }
|
||||
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e)} />
|
||||
</div>
|
||||
|
||||
@ -186,6 +190,7 @@ const mapStateToProps = (state, ownProps) => {
|
||||
artists: state.ui.artists,
|
||||
album: (state.ui.albums && typeof(state.ui.albums[ownProps.params.uri]) !== 'undefined' ? state.ui.albums[ownProps.params.uri] : false ),
|
||||
albums: state.ui.albums,
|
||||
library_albums: state.ui.library_albums,
|
||||
spotify_authorized: state.spotify.authorized,
|
||||
mopidy_connected: state.mopidy.connected
|
||||
};
|
||||
|
||||
@ -41,7 +41,12 @@ class App extends React.Component{
|
||||
this.props.pusherActions.connect();
|
||||
this.props.mopidyActions.connect();
|
||||
this.props.spotifyActions.connect();
|
||||
if( this.props.spotify_authorized ) this.props.spotifyActions.getAllLibraryPlaylists();
|
||||
|
||||
if (this.props.spotify_authorized){
|
||||
this.props.spotifyActions.getAllLibraryPlaylists();
|
||||
this.props.spotifyActions.getLibraryAlbums();
|
||||
this.props.spotifyActions.getLibraryArtists();
|
||||
}
|
||||
|
||||
// when we navigate to a new route
|
||||
hashHistory.listen( location => {
|
||||
|
||||
@ -95,6 +95,10 @@ class Artist extends React.Component{
|
||||
this.props.spotifyActions.getURL( this.props.artist.albums_more, 'SPOTIFY_ARTIST_ALBUMS_LOADED', this.props.params.uri );
|
||||
}
|
||||
|
||||
inLibrary(){
|
||||
return (this.props.library_artists && this.props.library_artists.indexOf(this.props.params.uri) > -1)
|
||||
}
|
||||
|
||||
renderSubViewMenu(){
|
||||
return (
|
||||
<div className="sub-views">
|
||||
@ -214,7 +218,7 @@ class Artist extends React.Component{
|
||||
<h1>{this.props.artist ? this.props.artist.name : null}</h1>
|
||||
<div className="actions">
|
||||
{ can_play_radio ? <button className="primary" onClick={e => this.props.pusherActions.startRadio([this.props.artist.uri])}>Start radio</button> : null}
|
||||
{ can_follow ? <FollowButton className="white" uri={this.props.params.uri} removeText="Unfollow" addText="Follow" is_following={this.props.artist.is_following} /> : null}
|
||||
{ can_follow ? <FollowButton className="white" uri={this.props.params.uri} removeText="Unfollow" addText="Follow" is_following={this.inLibrary()} /> : null}
|
||||
<ContextMenuTrigger className="white" onTrigger={e => this.handleContextMenu(e)} />
|
||||
</div>
|
||||
{ this.renderSubViewMenu() }
|
||||
@ -300,6 +304,7 @@ const mapStateToProps = (state, ownProps) => {
|
||||
return {
|
||||
artist: (state.ui.artists && typeof(state.ui.artists[ownProps.params.uri]) !== 'undefined' ? state.ui.artists[ownProps.params.uri] : false ),
|
||||
artists: state.ui.artists,
|
||||
library_artists: state.ui.library_artists,
|
||||
albums: state.ui.albums,
|
||||
spotify_authorized: state.spotify.authorized,
|
||||
mopidy_connected: state.mopidy.connected
|
||||
|
||||
@ -100,6 +100,10 @@ class Playlist extends React.Component{
|
||||
this.props.uiActions.removeTracksFromPlaylist( this.props.playlist.uri, tracks_indexes )
|
||||
}
|
||||
|
||||
inLibrary(){
|
||||
return (this.props.library_playlists && this.props.library_playlists.indexOf(this.props.params.uri) > -1)
|
||||
}
|
||||
|
||||
renderActions(){
|
||||
switch( helpers.uriSource( this.props.playlist.uri ) ){
|
||||
|
||||
@ -125,7 +129,7 @@ class Playlist extends React.Component{
|
||||
return (
|
||||
<div className="actions">
|
||||
<button className="primary" onClick={ e => this.play() }>Play</button>
|
||||
<FollowButton className="secondary" uri={this.props.playlist.uri} addText="Add to library" removeText="Remove from library" is_following={this.props.playlist.is_following} />
|
||||
<FollowButton className="secondary" uri={this.props.playlist.uri} addText="Add to library" removeText="Remove from library" is_following={this.inLibrary()} />
|
||||
<ContextMenuTrigger onTrigger={e => this.handleContextMenu(e)} />
|
||||
</div>
|
||||
)
|
||||
@ -226,6 +230,7 @@ const mapStateToProps = (state, ownProps) => {
|
||||
uri = uri.replace(' ','%20')
|
||||
return {
|
||||
playlist: (state.ui.playlists && typeof(state.ui.playlists[uri]) !== 'undefined' ? state.ui.playlists[uri] : false ),
|
||||
library_playlists: state.ui.library_playlists,
|
||||
mopidy_connected: state.mopidy.connected,
|
||||
spotify_authorized: state.spotify.authorized,
|
||||
spotify_userid: state.spotify.me.id
|
||||
|
||||
Reference in New Issue
Block a user