Add "Play Next" action (finally)

This commit is contained in:
Deluan
2020-09-21 20:10:52 -04:00
parent aa133e6b00
commit 7305e3aa17
14 changed files with 130 additions and 23 deletions
+35 -1
View File
@@ -1,7 +1,10 @@
import 'react-jinke-music-player/assets/index.css'
import get from 'lodash.get'
import { v4 as uuidv4 } from 'uuid'
import subsonic from '../subsonic'
const PLAYER_ADD_TRACKS = 'PLAYER_ADD_TRACKS'
const PLAYER_PLAY_NEXT = 'PLAYER_PLAY_NEXT'
const PLAYER_SET_TRACK = 'PLAYER_SET_TRACK'
const PLAYER_SYNC_QUEUE = 'PLAYER_SYNC_QUEUE'
const PLAYER_CLEAR_QUEUE = 'PLAYER_CLEAR_QUEUE'
@@ -23,6 +26,7 @@ const mapToAudioLists = (item) => {
cover: subsonic.url('getCoverArt', id, { size: 300 }),
musicSrc: subsonic.url('stream', id, { ts: true }),
scrobbled: false,
uuid: uuidv4(),
}
}
@@ -46,6 +50,14 @@ const addTracks = (data, ids) => {
}
}
const playNext = (data, ids) => {
const songs = filterSongs(data, ids)
return {
type: PLAYER_PLAY_NEXT,
data: songs,
}
}
const shuffle = (data) => {
const ids = Object.keys(data)
for (let i = ids.length - 1; i > 0; i--) {
@@ -109,6 +121,7 @@ const initialState = { queue: [], clear: true, current: {}, volume: 1 }
const playQueueReducer = (previousState = initialState, payload) => {
let queue, current
let newQueue
const { type, data } = payload
switch (type) {
case PLAYER_CLEAR_QUEUE:
@@ -124,6 +137,7 @@ const playQueueReducer = (previousState = initialState, payload) => {
? {}
: {
trackId: data.trackId,
uuid: data.uuid,
paused: data.paused,
}
return {
@@ -137,6 +151,25 @@ const playQueueReducer = (previousState = initialState, payload) => {
queue.push(mapToAudioLists(data[id]))
})
return { ...previousState, queue, clear: false }
case PLAYER_PLAY_NEXT:
current = get(previousState.current, 'uuid', '')
newQueue = []
let foundPos = false
previousState.queue.forEach((item) => {
newQueue.push(item)
if (item.uuid === current) {
foundPos = true
Object.keys(data).forEach((id) => {
newQueue.push(mapToAudioLists(data[id]))
})
}
})
if (!foundPos) {
Object.keys(data).forEach((id) => {
newQueue.push(mapToAudioLists(data[id]))
})
}
return { ...previousState, queue: newQueue, clear: true }
case PLAYER_SET_TRACK:
return {
...previousState,
@@ -152,7 +185,7 @@ const playQueueReducer = (previousState = initialState, payload) => {
current,
}
case PLAYER_SCROBBLE:
const newQueue = previousState.queue.map((item) => {
newQueue = previousState.queue.map((item) => {
return {
...item,
scrobbled:
@@ -189,6 +222,7 @@ export {
addTracks,
setTrack,
playTracks,
playNext,
syncQueue,
clearQueue,
scrobble,