2020-02-04 05:26:54 -09:00
|
|
|
import React from 'react'
|
2020-10-31 06:23:52 -08:00
|
|
|
import PropTypes from 'prop-types'
|
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-02-06 08:54:27 -09:00
|
|
|
|
2020-11-10 15:27:28 -09:00
|
|
|
export const BatchPlayButton = ({
|
2020-10-31 14:46:14 -08:00
|
|
|
resource,
|
|
|
|
|
selectedIds,
|
|
|
|
|
action,
|
|
|
|
|
label,
|
|
|
|
|
icon,
|
|
|
|
|
className,
|
|
|
|
|
}) => {
|
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-10-31 15:40:25 -08:00
|
|
|
const caption = translate(label)
|
2020-02-06 08:54:27 -09:00
|
|
|
return (
|
2020-10-31 14:46:14 -08:00
|
|
|
<Button
|
2020-10-31 15:40:25 -08:00
|
|
|
aria-label={caption}
|
2020-10-31 14:46:14 -08:00
|
|
|
onClick={addToQueue}
|
2020-10-31 15:40:25 -08:00
|
|
|
label={caption}
|
2020-10-31 14:46:14 -08:00
|
|
|
className={className}
|
|
|
|
|
>
|
2020-09-21 16:10:52 -08:00
|
|
|
{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:23:52 -08:00
|
|
|
BatchPlayButton.propTypes = {
|
|
|
|
|
action: PropTypes.func.isRequired,
|
|
|
|
|
label: PropTypes.string.isRequired,
|
|
|
|
|
icon: PropTypes.object.isRequired,
|
2020-09-21 16:10:52 -08:00
|
|
|
}
|