quintodrome/ui/src/common/BatchPlayButton.js

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

52 lines
1.3 KiB
JavaScript
Raw Normal View History

import React from 'react'
2020-02-07 05:40:52 -09:00
import {
Button,
useDataProvider,
useTranslate,
useUnselectAll,
useNotify,
2020-02-07 05:40:52 -09:00
} from 'react-admin'
import { useDispatch } from 'react-redux'
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 }) => {
const dispatch = useDispatch()
2020-02-07 05:40:52 -09:00
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
2020-09-21 16:10:52 -08:00
dispatch(action(tracks, selectedIds))
})
.catch(() => {
notify('ra.page.error', 'warning')
})
unselectAll(resource)
}
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}
</Button>
2020-02-06 08:54:27 -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