quintodrome/ui/src/song/AddToQueueButton.js

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

50 lines
1.2 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-02-06 08:54:27 -09:00
import AddToQueueIcon from '@material-ui/icons/AddToQueue'
const AddToQueueButton = ({ selectedIds }) => {
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('song', { 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(addTracks(selectedIds.map((id) => tracks[id])))
})
.catch(() => {
notify('ra.page.error', 'warning')
})
unselectAll('song')
}
2020-02-06 08:54:27 -09:00
return (
<Button
color="secondary"
onClick={addToQueue}
2020-05-01 06:28:31 -08:00
label={translate('resources.song.actions.addToQueue')}
>
<AddToQueueIcon />
</Button>
2020-02-06 08:54:27 -09:00
)
}
export default AddToQueueButton