From deb1340141b5016bbeea4de66d5751867d7964af Mon Sep 17 00:00:00 2001 From: James Barnsley Date: Sun, 8 Jan 2017 13:32:46 +1300 Subject: [PATCH] Resolving radio seeds --- src/js/services/spotify/actions.js | 39 +++++++++++++++++++++++++++ src/js/services/spotify/middleware.js | 15 ++++++++++- src/js/services/ui/reducer.js | 6 ++++- src/js/views/Queue.js | 8 +++++- src/js/views/library/LibraryAlbums.js | 8 ++++-- 5 files changed, 71 insertions(+), 5 deletions(-) diff --git a/src/js/services/spotify/actions.js b/src/js/services/spotify/actions.js index 9900d3bd..06b76f6a 100755 --- a/src/js/services/spotify/actions.js +++ b/src/js/services/spotify/actions.js @@ -412,6 +412,45 @@ export function following(uri, method = 'GET'){ } } +/** + * Resolve radio seeds into full objects + * + * @param radio object + **/ +export function resolveRadioSeeds( radio ){ + return (dispatch, getState) => { + + // flush out the previous store value + dispatch({ type: 'PUSHER_RADIO_SEEDS_RESOLVING' }); + + var resolved_seeds = { + seed_artists: [], + seed_tracks: [], + seed_genres: [] + } + + var artist_ids = ''; + for (var i = 0; i < radio.seed_artists.length; i++){ + if (i > 0) artist_ids += ',' + artist_ids += helpers.getFromUri('artistid', radio.seed_artists[i]) + } + + $.when( + sendRequest( dispatch, getState, 'artists/'+ artist_ids ) + .then( response => { + if (!(response instanceof Array)) response = [response] + Object.assign(resolved_seeds.seed_artists, response); + }) + + ).then( () => { + dispatch({ + type: 'PUSHER_RADIO_SEEDS_RESOLVED', + data: resolved_seeds + }); + }); + } +} + /** * =============================================================== ARTIST(S) ============ diff --git a/src/js/services/spotify/middleware.js b/src/js/services/spotify/middleware.js index 7d3dc0e2..2c7a74b4 100755 --- a/src/js/services/spotify/middleware.js +++ b/src/js/services/spotify/middleware.js @@ -1,5 +1,5 @@ - +var helpers = require('./../../helpers.js') var spotifyActions = require('./actions.js') var uiActions = require('../ui/actions.js') @@ -81,6 +81,19 @@ const SpotifyMiddleware = (function(){ if( action.data && action.data.track.uri.substring(0,14) == 'spotify:track:' ){ store.dispatch( spotifyActions.getTrack( action.data.track.uri ) ) } + break + + // when our mopidy server current track changes + case 'PUSHER_RADIO': + + // proceed as usual so we don't inhibit default functionality + next(action) + + // only resolve if radio is enabled + if( action.data.radio.enabled ){ + store.dispatch( spotifyActions.resolveRadioSeeds( action.data.radio ) ) + } + break // This action is irrelevant to us, pass it on to the next middleware default: diff --git a/src/js/services/ui/reducer.js b/src/js/services/ui/reducer.js index a1daa1bc..e337fe91 100755 --- a/src/js/services/ui/reducer.js +++ b/src/js/services/ui/reducer.js @@ -119,7 +119,11 @@ export default function reducer(ui = {}, action){ case 'PUSHER_RADIO': case 'PUSHER_START_RADIO': - return Object.assign({}, ui, { radio: action.data.radio }) + return Object.assign({}, ui, { seeds_resolved: false }, { radio: action.data.radio }) + + case 'PUSHER_RADIO_SEEDS_RESOLVED': + var radio = Object.assign({}, ui.radio, action.data, { seeds_resolved: true }) + return Object.assign({}, ui, { radio: radio }) diff --git a/src/js/views/Queue.js b/src/js/views/Queue.js index 55515662..37bde442 100755 --- a/src/js/views/Queue.js +++ b/src/js/views/Queue.js @@ -46,7 +46,13 @@ class Queue extends React.Component{ if (!this.props.radio || !this.props.radio.enabled) return null var seed_sentence = 'nothing' - if (this.props.radio.seed_artists.length > 0) seed_sentence = this.props.radio.seed_artists.length+' artists' + if (this.props.radio.seed_artists.length > 0){ + if (this.props.radio.seeds_resolved){ + seed_sentence = + }else{ + seed_sentence = this.props.radio.seed_artists.length +' artists' + } + } if (this.props.radio.seed_genres.length > 0) seed_sentence =this.props.radio.seed_artists.length+' genres' if (this.props.radio.seed_tracks.length > 0) seed_sentence =this.props.radio.seed_artists.length+' tracks' diff --git a/src/js/views/library/LibraryAlbums.js b/src/js/views/library/LibraryAlbums.js index d5a51562..b3f731c7 100755 --- a/src/js/views/library/LibraryAlbums.js +++ b/src/js/views/library/LibraryAlbums.js @@ -76,9 +76,13 @@ class LibraryAlbums extends React.Component{ this.props.albums.map( album => { return (
- + + +
-

{ album.name }

+ +

{ album.name }

+