jeffvli
3 years ago
4 changed files with 155 additions and 77 deletions
@ -1,49 +1,89 @@ |
|||
import { createSlice, PayloadAction } from '@reduxjs/toolkit'; |
|||
|
|||
type PlayQueueTypes = [ |
|||
{ |
|||
id: string; |
|||
album: string; |
|||
albumId: string; |
|||
artist: string; |
|||
bitRate: number; |
|||
contentType: string; |
|||
coverArt: string; |
|||
created: string; |
|||
duration: string; |
|||
genre: string; |
|||
index: number; |
|||
isDir: boolean; |
|||
isVideo: boolean; |
|||
parent: string; |
|||
path: string; |
|||
playCount: number; |
|||
size: number; |
|||
starred?: string; |
|||
streamUrl: string; |
|||
suffix: string; |
|||
title: string; |
|||
track: number; |
|||
type: string; |
|||
year: number; |
|||
} |
|||
]; |
|||
interface Entry { |
|||
id: string; |
|||
album: string; |
|||
albumId: string; |
|||
artist: string; |
|||
bitRate: number; |
|||
contentType: string; |
|||
coverArt: string; |
|||
created: string; |
|||
duration: string; |
|||
genre: string; |
|||
index: number; |
|||
isDir: boolean; |
|||
isVideo: boolean; |
|||
parent: string; |
|||
path: string; |
|||
playCount: number; |
|||
size: number; |
|||
starred?: string; |
|||
streamUrl: string; |
|||
suffix: string; |
|||
title: string; |
|||
track: number; |
|||
type: string; |
|||
year: number; |
|||
} |
|||
|
|||
const initialState: PlayQueueTypes[] = []; |
|||
interface PlayQueue { |
|||
currentIndex: number; |
|||
volume: number; |
|||
isLoading: boolean; |
|||
entry: Entry[]; |
|||
} |
|||
|
|||
const initialState: PlayQueue = { |
|||
currentIndex: 0, |
|||
volume: 0.5, |
|||
isLoading: false, |
|||
entry: [], |
|||
}; |
|||
|
|||
const playQueueSlice = createSlice({ |
|||
name: 'nowPlaying', |
|||
initialState, |
|||
reducers: { |
|||
setPlayQueue: (state, action: PayloadAction<PlayQueueTypes>) => { |
|||
action.payload.map((entry: any) => state.push(entry)); |
|||
incrementCurrentIndex: (state) => { |
|||
if (state.currentIndex <= state.entry.length) { |
|||
state.currentIndex += 1; |
|||
} else { |
|||
state.currentIndex = 0; |
|||
} |
|||
}, |
|||
decrementCurrentIndex: (state) => { |
|||
if (state.currentIndex >= 0) { |
|||
state.currentIndex -= 1; |
|||
} |
|||
}, |
|||
setCurrentIndex: (state, action: PayloadAction<PlayQueue>) => { |
|||
state.currentIndex = action.payload.currentIndex; |
|||
}, |
|||
appendPlayQueue: (state, action: PayloadAction<PlayQueueTypes>) => { |
|||
action.payload.map((entry: any) => state.push(entry)); |
|||
setPlayQueue: (state, action: PayloadAction<Entry[]>) => { |
|||
state.currentIndex = 0; |
|||
action.payload.map((entry: any) => state.entry.push(entry)); |
|||
}, |
|||
appendPlayQueue: (state, action: PayloadAction<Entry[]>) => { |
|||
action.payload.map((entry: any) => state.entry.push(entry)); |
|||
}, |
|||
clearPlayQueue: () => initialState, |
|||
setIsLoading: (state) => { |
|||
state.isLoading = true; |
|||
}, |
|||
setIsLoaded: (state) => { |
|||
state.isLoading = false; |
|||
}, |
|||
}, |
|||
}); |
|||
|
|||
export const { setPlayQueue, clearPlayQueue } = playQueueSlice.actions; |
|||
export const { |
|||
incrementCurrentIndex, |
|||
decrementCurrentIndex, |
|||
setCurrentIndex, |
|||
setPlayQueue, |
|||
clearPlayQueue, |
|||
setIsLoading, |
|||
setIsLoaded, |
|||
} = playQueueSlice.actions; |
|||
export default playQueueSlice.reducer; |
|||
|
Loading…
Reference in new issue