2020-02-04 05:26:54 -09:00
|
|
|
import React from 'react'
|
2020-02-07 05:40:52 -09:00
|
|
|
import {
|
|
|
|
|
Button,
|
|
|
|
|
useDataProvider,
|
2020-02-12 16:35:35 -09:00
|
|
|
useTranslate,
|
|
|
|
|
useUnselectAll,
|
2020-05-05 07:19:41 -08:00
|
|
|
useNotify,
|
2020-02-07 05:40:52 -09:00
|
|
|
} from 'react-admin'
|
2020-02-04 05:26:54 -09:00
|
|
|
import { useDispatch } from 'react-redux'
|
2020-05-05 08:07:50 -08:00
|
|
|
import { addTracks } from '../audioplayer'
|
2020-09-21 16:10:52 -08:00
|
|
|
import { RiPlayListAddFill } from 'react-icons/ri'
|
2020-02-06 08:54:27 -09:00
|
|
|
|
2020-10-31 06:12:38 -08:00
|
|
|
const BatchPlayButton = ({ resource, selectedIds, action, label, icon }) => {
|
2020-02-04 05:26:54 -09:00
|
|
|
const dispatch = useDispatch()
|
2020-02-07 05:40:52 -09:00
|
|
|
const translate = useTranslate()
|
2020-02-04 05:26:54 -09:00
|
|
|
const dataProvider = useDataProvider()
|
|
|
|
|
const unselectAll = useUnselectAll()
|
2020-05-05 07:19:41 -08:00
|
|
|
const notify = useNotify()
|
|
|
|
|
|
2020-02-04 05:26:54 -09:00
|
|
|
const addToQueue = () => {
|
2020-05-05 07:19:41 -08:00
|
|
|
dataProvider
|
2020-05-05 11:08:30 -08:00
|
|
|
.getMany(resource, { ids: selectedIds })
|
2020-05-05 07:19:41 -08:00
|
|
|
.then((response) => {
|
2020-05-05 08:07:50 -08:00
|
|
|
// Add tracks to a map for easy lookup by ID, needed for the next step
|
2020-05-05 08:20:41 -08:00
|
|
|
const tracks = response.data.reduce(
|
|
|
|
|
(acc, cur) => ({ ...acc, [cur.id]: cur }),
|
|
|
|
|
{}
|
|
|
|
|
)
|
2020-05-05 08:07:50 -08:00
|
|
|
// Add the tracks to the queue in the selection order
|
2020-09-21 16:10:52 -08:00
|
|
|
dispatch(action(tracks, selectedIds))
|
2020-05-05 07:19:41 -08:00
|
|
|
})
|
|
|
|
|
.catch(() => {
|
|
|
|
|
notify('ra.page.error', 'warning')
|
2020-02-04 05:26:54 -09:00
|
|
|
})
|
2020-05-05 11:08:30 -08:00
|
|
|
unselectAll(resource)
|
2020-02-04 05:26:54 -09:00
|
|
|
}
|
|
|
|
|
|
2020-02-06 08:54:27 -09:00
|
|
|
return (
|
2020-09-21 16:10:52 -08:00
|
|
|
<Button color="secondary" onClick={addToQueue} label={translate(label)}>
|
|
|
|
|
{icon}
|
2020-02-07 07:45:56 -09:00
|
|
|
</Button>
|
2020-02-06 08:54:27 -09:00
|
|
|
)
|
2020-02-04 05:26:54 -09:00
|
|
|
}
|
|
|
|
|
|
2020-10-31 06:12:38 -08:00
|
|
|
BatchPlayButton.defaultProps = {
|
2020-09-21 16:10:52 -08:00
|
|
|
action: addTracks,
|
|
|
|
|
label: 'resources.song.actions.addToQueue',
|
|
|
|
|
icon: <RiPlayListAddFill />,
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-31 06:12:38 -08:00
|
|
|
export default BatchPlayButton
|