Use active filters when shuffling songs

This commit is contained in:
Deluan 2020-08-14 14:10:39 -04:00
parent c2e03c8162
commit ca5da5b0ea
3 changed files with 55 additions and 47 deletions

View File

@ -0,0 +1,50 @@
import React from 'react'
import { Button, useDataProvider, useNotify, useTranslate } from 'react-admin'
import { useDispatch } from 'react-redux'
import ShuffleIcon from '@material-ui/icons/Shuffle'
import { playTracks } from '../audioplayer'
import PropTypes from 'prop-types'
const ShuffleAllButton = ({ filters }) => {
const translate = useTranslate()
const dataProvider = useDataProvider()
const dispatch = useDispatch()
const notify = useNotify()
const handleOnClick = () => {
dataProvider
.getList('song', {
pagination: { page: 1, perPage: 200 },
sort: { field: 'random', order: 'ASC' },
filter: filters,
})
.then((res) => {
const data = {}
res.data.forEach((song) => {
data[song.id] = song
})
dispatch(playTracks(data))
})
.catch(() => {
notify('ra.page.error', 'warning')
})
}
return (
<Button
onClick={handleOnClick}
label={translate('resources.song.actions.shuffleAll')}
>
<ShuffleIcon />
</Button>
)
}
ShuffleAllButton.propTypes = {
filters: PropTypes.object,
}
ShuffleAllButton.defaultProps = {
filters: {},
}
export default ShuffleAllButton

View File

@ -15,6 +15,7 @@ import SongContextMenu from './SongContextMenu'
import SongTitleField from './SongTitleField'
import QuickFilter from './QuickFilter'
import useAlbumsPerPage from './useAlbumsPerPage'
import ShuffleAllButton from './ShuffleAllButton'
import { AlbumContextMenu, ArtistContextMenu } from './ContextMenus'
export {
@ -40,4 +41,5 @@ export {
SongContextMenu,
QuickFilter,
useAlbumsPerPage,
ShuffleAllButton,
}

View File

@ -1,50 +1,6 @@
import React, { cloneElement } from 'react'
import { useDispatch } from 'react-redux'
import {
Button,
sanitizeListRestProps,
TopToolbar,
useDataProvider,
useTranslate,
useNotify,
} from 'react-admin'
import ShuffleIcon from '@material-ui/icons/Shuffle'
import { playTracks } from '../audioplayer'
const ShuffleAllButton = () => {
const translate = useTranslate()
const dataProvider = useDataProvider()
const dispatch = useDispatch()
const notify = useNotify()
const handleOnClick = () => {
dataProvider
.getList('song', {
pagination: { page: 1, perPage: 200 },
sort: { field: 'random', order: 'ASC' },
filter: {},
})
.then((res) => {
const data = {}
res.data.forEach((song) => {
data[song.id] = song
})
dispatch(playTracks(data))
})
.catch(() => {
notify('ra.page.error', 'warning')
})
}
return (
<Button
onClick={handleOnClick}
label={translate('resources.song.actions.shuffleAll')}
>
<ShuffleIcon />
</Button>
)
}
import { sanitizeListRestProps, TopToolbar } from 'react-admin'
import { ShuffleAllButton } from '../common'
export const SongListActions = ({
currentSort,
@ -74,7 +30,7 @@ export const SongListActions = ({
filterValues,
context: 'button',
})}
<ShuffleAllButton />
<ShuffleAllButton filters={filterValues} />
</TopToolbar>
)
}