Add "PlayNow" button to bulk actions

This commit is contained in:
Deluan
2020-10-31 10:12:38 -04:00
parent 6c0778a867
commit aabef62b11
5 changed files with 20 additions and 11 deletions
+51
View File
@@ -0,0 +1,51 @@
import React from 'react'
import {
Button,
useDataProvider,
useTranslate,
useUnselectAll,
useNotify,
} from 'react-admin'
import { useDispatch } from 'react-redux'
import { addTracks } from '../audioplayer'
import { RiPlayListAddFill } from 'react-icons/ri'
const BatchPlayButton = ({ resource, selectedIds, action, label, icon }) => {
const dispatch = useDispatch()
const translate = useTranslate()
const dataProvider = useDataProvider()
const unselectAll = useUnselectAll()
const notify = useNotify()
const addToQueue = () => {
dataProvider
.getMany(resource, { ids: selectedIds })
.then((response) => {
// Add tracks to a map for easy lookup by ID, needed for the next step
const tracks = response.data.reduce(
(acc, cur) => ({ ...acc, [cur.id]: cur }),
{}
)
// Add the tracks to the queue in the selection order
dispatch(action(tracks, selectedIds))
})
.catch(() => {
notify('ra.page.error', 'warning')
})
unselectAll(resource)
}
return (
<Button color="secondary" onClick={addToQueue} label={translate(label)}>
{icon}
</Button>
)
}
BatchPlayButton.defaultProps = {
action: addTracks,
label: 'resources.song.actions.addToQueue',
icon: <RiPlayListAddFill />,
}
export default BatchPlayButton
+2
View File
@@ -3,6 +3,7 @@ import DurationField from './DurationField'
import BitrateField from './BitrateField'
import Pagination from './Pagination'
import PlayButton from './PlayButton'
import BatchPlayButton from './BatchPlayButton'
import SimpleList from './SimpleList'
import RangeField, { formatRange } from './RangeField'
import ArtistLinkField, { useGetHandleArtistClick } from './ArtistLinkField'
@@ -27,6 +28,7 @@ export {
Pagination,
List,
PlayButton,
BatchPlayButton,
SimpleList,
RangeField,
SongDetails,