quintodrome/ui/src/common/BatchPlayButton.js

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

62 lines
1.4 KiB
JavaScript
Raw Normal View History

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