From 5f28f7e90c94f09ef6b5b750addb86fcc854d4df Mon Sep 17 00:00:00 2001
From: Eliot Berriot <contact@eliotberriot.com>
Date: Tue, 27 Feb 2018 19:39:26 +0100
Subject: [PATCH] Fixed #87:Now always load next radio track on last queue
 track ended

---
 front/src/store/player.js                  |  9 ++++++++-
 front/test/unit/specs/store/player.spec.js | 13 +++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/front/src/store/player.js b/front/src/store/player.js
index df8d159f..d1de34d3 100644
--- a/front/src/store/player.js
+++ b/front/src/store/player.js
@@ -78,8 +78,15 @@ export default {
         logger.default.error('Could not record track in history')
       })
     },
-    trackEnded ({dispatch}, track) {
+    trackEnded ({dispatch, rootState}, track) {
       dispatch('trackListened', track)
+      let queueState = rootState.queue
+      if (queueState.currentIndex === queueState.tracks.length - 1) {
+        // we've reached last track of queue, trigger a reload
+        // from radio if any
+        dispatch('radios/populateQueue', null, {root: true})
+      }
+      dispatch('queue/next', null, {root: true})
       dispatch('queue/next', null, {root: true})
     },
     trackErrored ({commit, dispatch}) {
diff --git a/front/test/unit/specs/store/player.spec.js b/front/test/unit/specs/store/player.spec.js
index af0b6b43..f87b83cd 100644
--- a/front/test/unit/specs/store/player.spec.js
+++ b/front/test/unit/specs/store/player.spec.js
@@ -122,12 +122,25 @@ describe('store/player', () => {
       testAction({
         action: store.actions.trackEnded,
         payload: {test: 'track'},
+        params: {rootState: {queue: {currentIndex:0, tracks: [1, 2]}}},
         expectedActions: [
           { type: 'trackListened', payload: {test: 'track'} },
           { type: 'queue/next', payload: null, options: {root: true} }
         ]
       }, done)
     })
+    it('trackEnded calls populateQueue if last', (done) => {
+      testAction({
+        action: store.actions.trackEnded,
+        payload: {test: 'track'},
+        params: {rootState: {queue: {currentIndex:1, tracks: [1, 2]}}},
+        expectedActions: [
+          { type: 'trackListened', payload: {test: 'track'} },
+          { type: 'radios/populateQueue', payload: null, options: {root: true} },
+          { type: 'queue/next', payload: null, options: {root: true} }
+        ]
+      }, done)
+    })
     it('trackErrored', (done) => {
       testAction({
         action: store.actions.trackErrored,
-- 
GitLab