Refactor album actions, simplify usage

This commit is contained in:
Deluan
2020-05-07 11:24:28 -04:00
parent 16f2b056ef
commit 06ab88415a
6 changed files with 35 additions and 24 deletions
+2 -5
View File
@@ -21,14 +21,11 @@ export const AlbumActions = ({
const dispatch = useDispatch() const dispatch = useDispatch()
const translate = useTranslate() const translate = useTranslate()
// Filter out tracks from other albums (cached by ReactAdmin)
const filteredData = ids.reduce((acc, id) => ({ ...acc, [id]: data[id] }), {})
return ( return (
<TopToolbar className={className} {...sanitizeListRestProps(rest)}> <TopToolbar className={className} {...sanitizeListRestProps(rest)}>
<Button <Button
onClick={() => { onClick={() => {
dispatch(playAlbum(ids[0], filteredData)) dispatch(playAlbum(data, ids))
}} }}
label={translate('resources.album.actions.playAll')} label={translate('resources.album.actions.playAll')}
> >
@@ -36,7 +33,7 @@ export const AlbumActions = ({
</Button> </Button>
<Button <Button
onClick={() => { onClick={() => {
dispatch(shuffleAlbum(filteredData)) dispatch(shuffleAlbum(data, ids))
}} }}
label={translate('resources.album.actions.shuffle')} label={translate('resources.album.actions.shuffle')}
> >
+3 -3
View File
@@ -25,11 +25,11 @@ const AlbumContextMenu = ({ record, color }) => {
const options = { const options = {
play: { play: {
label: translate('resources.album.actions.playAll'), label: translate('resources.album.actions.playAll'),
action: (data, id) => playAlbum(id, data), action: (data) => playAlbum(data),
}, },
addToQueue: { addToQueue: {
label: translate('resources.album.actions.addToQueue'), label: translate('resources.album.actions.addToQueue'),
action: (data) => addTracks(Object.values(data)), action: (data) => addTracks(data),
}, },
shuffle: { shuffle: {
label: translate('resources.album.actions.shuffle'), label: translate('resources.album.actions.shuffle'),
@@ -63,7 +63,7 @@ const AlbumContextMenu = ({ record, color }) => {
(acc, cur) => ({ ...acc, [cur.id]: cur }), (acc, cur) => ({ ...acc, [cur.id]: cur }),
{} {}
) )
dispatch(options[key].action(adata, response.data[0].id)) dispatch(options[key].action(adata))
}) })
.catch(() => { .catch(() => {
notify('ra.page.error', 'warning') notify('ra.page.error', 'warning')
+1 -1
View File
@@ -108,7 +108,7 @@ const AlbumSongs = (props) => {
) : ( ) : (
<Datagrid <Datagrid
expand={!isXsmall && <SongDetails />} expand={!isXsmall && <SongDetails />}
rowClick={(id) => dispatch(playAlbum(id, data))} rowClick={(id) => dispatch(playAlbum(data, ids, id))}
{...controllerProps} {...controllerProps}
hasBulkActions={hasBulkActions} hasBulkActions={hasBulkActions}
> >
+25 -11
View File
@@ -22,10 +22,20 @@ const setTrack = (data) => ({
data, data,
}) })
const addTracks = (data) => ({ let filterAlbumSongs = function (data, ids) {
if (!ids) {
return data
}
return ids.reduce((acc, id) => ({ ...acc, [id]: data[id] }), {})
}
const addTracks = (data, ids) => {
const songs = filterAlbumSongs(data, ids)
return {
type: PLAYER_ADD_TRACK, type: PLAYER_ADD_TRACK,
data, data: songs,
}) }
}
const shuffle = (data) => { const shuffle = (data) => {
const ids = Object.keys(data) const ids = Object.keys(data)
@@ -38,8 +48,9 @@ const shuffle = (data) => {
return shuffled return shuffled
} }
const shuffleAlbum = (data) => { const shuffleAlbum = (data, ids) => {
const shuffled = shuffle(data) const songs = filterAlbumSongs(data, ids)
const shuffled = shuffle(songs)
const firstId = Object.keys(shuffled)[0] const firstId = Object.keys(shuffled)[0]
return { return {
type: PLAYER_PLAY_ALBUM, type: PLAYER_PLAY_ALBUM,
@@ -48,11 +59,14 @@ const shuffleAlbum = (data) => {
} }
} }
const playAlbum = (id, data) => ({ const playAlbum = (data, ids, selectedId) => {
const songs = filterAlbumSongs(data, ids)
return {
type: PLAYER_PLAY_ALBUM, type: PLAYER_PLAY_ALBUM,
id, id: selectedId || Object.keys(songs)[0],
data, data: songs,
}) }
}
const syncQueue = (id, data) => ({ const syncQueue = (id, data) => ({
type: PLAYER_SYNC_QUEUE, type: PLAYER_SYNC_QUEUE,
@@ -75,8 +89,8 @@ const playQueueReducer = (
switch (type) { switch (type) {
case PLAYER_ADD_TRACK: case PLAYER_ADD_TRACK:
queue = previousState.queue queue = previousState.queue
data.forEach((item) => { Object.keys(data).forEach((id) => {
queue.push(mapToAudioLists(item)) queue.push(mapToAudioLists(data[id]))
}) })
return { ...previousState, queue, clear: false } return { ...previousState, queue, clear: false }
case PLAYER_SET_TRACK: case PLAYER_SET_TRACK:
+1 -1
View File
@@ -27,7 +27,7 @@ const AddToQueueButton = ({ resource, selectedIds }) => {
{} {}
) )
// Add the tracks to the queue in the selection order // Add the tracks to the queue in the selection order
dispatch(addTracks(selectedIds.map((id) => tracks[id]))) dispatch(addTracks(tracks, selectedIds))
}) })
.catch(() => { .catch(() => {
notify('ra.page.error', 'warning') notify('ra.page.error', 'warning')
+1 -1
View File
@@ -16,7 +16,7 @@ export const SongContextMenu = ({ record }) => {
}, },
addToQueue: { addToQueue: {
label: translate('resources.song.actions.addToQueue'), label: translate('resources.song.actions.addToQueue'),
action: (record) => addTracks([record]), action: (record) => addTracks({ [record.id]: record }),
}, },
} }