quintodrome/ui/src/album/AlbumContextMenu.js

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

111 lines
2.9 KiB
JavaScript
Raw Normal View History

2020-04-29 17:25:43 -08:00
import React, { useState } from 'react'
import { useDispatch } from 'react-redux'
2020-04-29 16:03:40 -08:00
import IconButton from '@material-ui/core/IconButton'
import Menu from '@material-ui/core/Menu'
import MenuItem from '@material-ui/core/MenuItem'
import MoreVertIcon from '@material-ui/icons/MoreVert'
import { makeStyles } from '@material-ui/core/styles'
import { useDataProvider, useNotify, useTranslate } from 'react-admin'
2020-05-15 14:53:33 -08:00
import { addTracks, playTracks, shuffleTracks } from '../audioplayer'
import { openAddToPlaylist } from '../dialogs/dialogState'
2020-04-29 16:03:40 -08:00
const useStyles = makeStyles({
icon: {
color: (props) => props.color,
},
})
const AlbumContextMenu = ({ record, color }) => {
const classes = useStyles({ color })
2020-04-29 17:25:43 -08:00
const dataProvider = useDataProvider()
2020-04-29 16:03:40 -08:00
const dispatch = useDispatch()
2020-04-29 17:25:43 -08:00
const translate = useTranslate()
const notify = useNotify()
2020-04-29 16:03:40 -08:00
const [anchorEl, setAnchorEl] = useState(null)
2020-04-29 16:03:40 -08:00
const options = {
2020-04-29 17:25:43 -08:00
play: {
label: translate('resources.album.actions.playAll'),
action: playTracks,
2020-04-29 17:25:43 -08:00
},
addToQueue: {
label: translate('resources.album.actions.addToQueue'),
action: addTracks,
},
2020-04-29 17:25:43 -08:00
shuffle: {
label: translate('resources.album.actions.shuffle'),
action: shuffleTracks,
},
addToPlaylist: {
label: translate('resources.song.actions.addToPlaylist'),
action: () => openAddToPlaylist({ albumId: record.id }),
2020-04-29 17:25:43 -08:00
},
2020-04-29 16:03:40 -08:00
}
const handleClick = (e) => {
e.preventDefault()
setAnchorEl(e.currentTarget)
2020-05-01 07:27:09 -08:00
e.stopPropagation()
2020-04-29 16:03:40 -08:00
}
const handleOnClose = (e) => {
e.preventDefault()
setAnchorEl(null)
2020-05-01 07:27:09 -08:00
e.stopPropagation()
2020-04-29 16:03:40 -08:00
}
const handleItemClick = (e) => {
setAnchorEl(null)
2020-04-29 17:25:43 -08:00
const key = e.target.getAttribute('value')
dataProvider
.getList('albumSong', {
pagination: { page: 1, perPage: -1 },
2020-04-29 17:25:43 -08:00
sort: { field: 'trackNumber', order: 'ASC' },
2020-05-01 07:27:09 -08:00
filter: { album_id: record.id },
2020-04-29 17:25:43 -08:00
})
.then((response) => {
const adata = response.data.reduce(
(acc, cur) => ({ ...acc, [cur.id]: cur }),
{}
)
2020-05-07 07:24:28 -08:00
dispatch(options[key].action(adata))
2020-04-29 17:25:43 -08:00
})
.catch(() => {
notify('ra.page.error', 'warning')
})
2020-05-01 07:27:09 -08:00
e.stopPropagation()
2020-04-29 16:03:40 -08:00
}
const open = Boolean(anchorEl)
2020-04-29 16:03:40 -08:00
return (
<div>
<IconButton
aria-label="more"
aria-controls="context-menu"
aria-haspopup="true"
className={classes.icon}
2020-04-29 16:03:40 -08:00
onClick={handleClick}
2020-05-02 10:50:15 -08:00
size={'small'}
2020-04-29 16:03:40 -08:00
>
<MoreVertIcon />
</IconButton>
<Menu
id="context-menu"
anchorEl={anchorEl}
keepMounted
open={open}
2020-04-29 17:25:43 -08:00
onClose={handleOnClose}
2020-04-29 16:03:40 -08:00
>
{Object.keys(options).map((key) => (
<MenuItem value={key} key={key} onClick={handleItemClick}>
{options[key].label}
</MenuItem>
))}
</Menu>
</div>
)
}
export default AlbumContextMenu