From 0730c667a232275c0871ab7a5b755af5c5dc5223 Mon Sep 17 00:00:00 2001 From: Deluan Date: Fri, 7 Aug 2020 10:47:55 -0400 Subject: [PATCH] Add "Shuffle All" option to Song List. Closes #256 --- resources/i18n/pt.json | 3 +- ui/src/i18n/en.json | 3 +- ui/src/song/SongList.js | 2 + ui/src/song/SongListActions.js | 85 ++++++++++++++++++++++++++++++++++ 4 files changed, 91 insertions(+), 2 deletions(-) create mode 100644 ui/src/song/SongListActions.js diff --git a/resources/i18n/pt.json b/resources/i18n/pt.json index 15f25e692..c3b725027 100644 --- a/resources/i18n/pt.json +++ b/resources/i18n/pt.json @@ -23,7 +23,8 @@ "actions": { "addToQueue": "Adicionar à fila", "playNow": "Tocar agora", - "addToPlaylist": "Adicionar à playlist" + "addToPlaylist": "Adicionar à playlist", + "shuffleAll": "Aleatório" } }, "album": { diff --git a/ui/src/i18n/en.json b/ui/src/i18n/en.json index 781a7ca16..a189a4de7 100644 --- a/ui/src/i18n/en.json +++ b/ui/src/i18n/en.json @@ -24,7 +24,8 @@ "actions": { "addToQueue": "Play Later", "addToPlaylist": "Add to Playlist", - "playNow": "Play Now" + "playNow": "Play Now", + "shuffleAll": "Shuffle All" } }, "album": { diff --git a/ui/src/song/SongList.js b/ui/src/song/SongList.js index a9d64642d..dfcbfbb07 100644 --- a/ui/src/song/SongList.js +++ b/ui/src/song/SongList.js @@ -23,6 +23,7 @@ import { import { useDispatch } from 'react-redux' import { setTrack } from '../audioplayer' import { SongBulkActions } from './SongBulkActions' +import { SongListActions } from './SongListActions' import { AlbumLinkField } from './AlbumLinkField' import AddToPlaylistDialog from '../dialogs/AddToPlaylistDialog' @@ -62,6 +63,7 @@ const SongList = (props) => { sort={{ field: 'title', order: 'ASC' }} exporter={false} bulkActionButtons={} + actions={} filters={} perPage={isXsmall ? 50 : 15} > diff --git a/ui/src/song/SongListActions.js b/ui/src/song/SongListActions.js new file mode 100644 index 000000000..4a25ed2d8 --- /dev/null +++ b/ui/src/song/SongListActions.js @@ -0,0 +1,85 @@ +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 ( + + ) +} + +export const SongListActions = ({ + currentSort, + className, + resource, + filters, + displayedFilters, + filterValues, + permanentFilter, + exporter, + basePath, + selectedIds, + onUnselectItems, + showFilter, + maxResults, + total, + ids, + ...rest +}) => { + return ( + + {filters && + cloneElement(filters, { + resource, + showFilter, + displayedFilters, + filterValues, + context: 'button', + })} + + + ) +} + +SongListActions.defaultProps = { + selectedIds: [], + onUnselectItems: () => null, +}