/
AddToQueueButton.js
49 lines (45 loc) 路 1.25 KB
/
AddToQueueButton.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import React from 'react'
import {
Button,
useDataProvider,
useTranslate,
useUnselectAll,
useNotify,
} from 'react-admin'
import { useDispatch } from 'react-redux'
import { addTracks } from '../audioplayer'
import AddToQueueIcon from '@material-ui/icons/AddToQueue'
const AddToQueueButton = ({ selectedIds }) => {
const dispatch = useDispatch()
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
return acc
}, {})
// 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')
}
return (
<Button
color="secondary"
onClick={addToQueue}
label={translate('resources.song.actions.addToQueue')}
>
<AddToQueueIcon />
</Button>
)
}
export default AddToQueueButton