From 6dca2dad0c7b14d36782bcd5fe1f3fd89b223fea Mon Sep 17 00:00:00 2001 From: jeffvli Date: Mon, 16 Aug 2021 05:47:53 -0700 Subject: [PATCH] add check for songs when incrementing index --- src/components/player/Player.tsx | 28 ++++++++------- src/redux/playQueueSlice.ts | 60 ++++++++++++++++++-------------- 2 files changed, 49 insertions(+), 39 deletions(-) diff --git a/src/components/player/Player.tsx b/src/components/player/Player.tsx index b1a8c72..fb49480 100644 --- a/src/components/player/Player.tsx +++ b/src/components/player/Player.tsx @@ -20,21 +20,23 @@ const Player = ({ children }: any) => { const playQueue = useAppSelector((state) => state.playQueue); useEffect(() => { - if (playQueue.status === 'PAUSED') { - player1Ref.current.audioEl.current.pause(); - player2Ref.current.audioEl.current.pause(); - } else if (playQueue.currentPlayer === 1) { - try { - player1Ref.current.audioEl.current.play(); - } catch (err) { - console.log(err); + if (playQueue.status === 'PLAYING') { + if (playQueue.currentPlayer === 1) { + try { + player1Ref.current.audioEl.current.play(); + } catch (err) { + console.log(err); + } + } else { + try { + player2Ref.current.audioEl.current.play(); + } catch (err) { + console.log(err); + } } } else { - try { - player2Ref.current.audioEl.current.play(); - } catch (err) { - console.log(err); - } + player1Ref.current.audioEl.current.pause(); + player2Ref.current.audioEl.current.pause(); } }, [playQueue.currentPlayer, playQueue.status]); diff --git a/src/redux/playQueueSlice.ts b/src/redux/playQueueSlice.ts index c369c12..e6b233b 100644 --- a/src/redux/playQueueSlice.ts +++ b/src/redux/playQueueSlice.ts @@ -84,26 +84,28 @@ const playQueueSlice = createSlice({ }, incrementCurrentIndex: (state, action: PayloadAction) => { - if (state.currentIndex < state.entry.length - 1) { - state.currentIndex += 1; - if (action.payload === 'usingHotkey') { - state.currentPlayer = 1; - state.player1.volume = state.volume; - state.player1.index = state.currentIndex; - state.player2.index = state.currentIndex + 1; + if (state.entry.length >= 1) { + if (state.currentIndex < state.entry.length - 1) { + state.currentIndex += 1; + if (action.payload === 'usingHotkey') { + state.currentPlayer = 1; + state.player1.volume = state.volume; + state.player1.index = state.currentIndex; + state.player2.index = state.currentIndex + 1; + } } - } - if (state.repeatAll) { - state.currentIndex = 0; - if (action.payload === 'usingHotkey') { - state.currentPlayer = 1; - state.player1.index = 0; - state.player2.index = 1; + if (state.repeatAll) { + state.currentIndex = 0; + if (action.payload === 'usingHotkey') { + state.currentPlayer = 1; + state.player1.index = 0; + state.player2.index = 1; + } } - } - state.currentSongId = state.entry[state.currentIndex].id; + state.currentSongId = state.entry[state.currentIndex].id; + } }, incrementPlayerIndex: (state, action: PayloadAction) => { @@ -121,9 +123,11 @@ const playQueueSlice = createSlice({ (track) => track.id === action.payload.id ); - state.currentPlayer = 1; state.player1.index = findIndex; + state.player1.volume = state.volume; state.player2.index = findIndex + 1; + state.player2.volume = 0; + state.currentPlayer = 1; state.currentIndex = findIndex; state.currentSongId = action.payload.id; }, @@ -140,16 +144,18 @@ const playQueueSlice = createSlice({ }, decrementCurrentIndex: (state, action: PayloadAction) => { - if (state.currentIndex > 0) { - state.currentIndex -= 1; - if (action.payload === 'usingHotkey') { - state.currentPlayer = 1; - state.player1.index = state.currentIndex; - state.player2.index = state.currentIndex + 1; + if (state.entry.length >= 1) { + if (state.currentIndex > 0) { + state.currentIndex -= 1; + if (action.payload === 'usingHotkey') { + state.currentPlayer = 1; + state.player1.index = state.currentIndex; + state.player2.index = state.currentIndex + 1; + } } - } - state.currentSongId = state.entry[state.currentIndex].id; + state.currentSongId = state.entry[state.currentIndex].id; + } }, setCurrentIndex: (state, action: PayloadAction) => { @@ -162,7 +168,9 @@ const playQueueSlice = createSlice({ }, setPlayQueue: (state, action: PayloadAction) => { - state.status = 'PLAYING'; + if (state.status !== 'PLAYING') { + state.status = 'PLAYING'; + } state.currentIndex = 0; state.currentSongId = action.payload[0].id; action.payload.map((entry: any) => state.entry.push(entry));