From 69ee17402f4f921de2b77b4815eaa07470909511 Mon Sep 17 00:00:00 2001 From: Ritik Pandey <57366926+RitikPandey1@users.noreply.github.com> Date: Tue, 6 Apr 2021 03:51:47 +0530 Subject: [PATCH] Add pagination to playlists (#969) * add pagination * prettier applied * perPage_bug_fixed * pagination_component_changed * getAllSongs function added * pagination component updated * catch_error from data provider * getAllSongsAndDispatch added * remove ids from action function --- ui/src/playlist/PlaylistActions.js | 46 ++++++++++++++++++++++++------ ui/src/playlist/PlaylistShow.js | 7 ++--- ui/src/playlist/PlaylistSongs.js | 34 +++++++++++++--------- 3 files changed, 62 insertions(+), 25 deletions(-) diff --git a/ui/src/playlist/PlaylistActions.js b/ui/src/playlist/PlaylistActions.js index fe895895..24cfd0b2 100644 --- a/ui/src/playlist/PlaylistActions.js +++ b/ui/src/playlist/PlaylistActions.js @@ -5,6 +5,8 @@ import { sanitizeListRestProps, TopToolbar, useTranslate, + useDataProvider, + useNotify, } from 'react-admin' import PlayArrowIcon from '@material-ui/icons/PlayArrow' import ShuffleIcon from '@material-ui/icons/Shuffle' @@ -23,23 +25,51 @@ import config from '../config' const PlaylistActions = ({ className, ids, data, record, ...rest }) => { const dispatch = useDispatch() const translate = useTranslate() + const dataProvider = useDataProvider() + const notify = useNotify() const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('md')) + const getAllSongsAndDispatch = React.useCallback( + (action) => { + if (ids.length === record.songCount) { + return dispatch(action(data, ids)) + } + + dataProvider + .getList('playlistTrack', { + pagination: { page: 1, perPage: 0 }, + sort: { field: 'id', order: 'ASC' }, + filter: { playlist_id: record.id }, + }) + .then((res) => { + const data = res.data.reduce( + (acc, curr) => ({ ...acc, [curr.id]: curr }), + {} + ) + dispatch(action(data)) + }) + .catch(() => { + notify('ra.page.error', 'warning') + }) + }, + [dataProvider, dispatch, record, data, ids, notify] + ) + const handlePlay = React.useCallback(() => { - dispatch(playTracks(data, ids)) - }, [dispatch, data, ids]) + getAllSongsAndDispatch(playTracks) + }, [getAllSongsAndDispatch]) const handlePlayNext = React.useCallback(() => { - dispatch(playNext(data, ids)) - }, [dispatch, data, ids]) + getAllSongsAndDispatch(playNext) + }, [getAllSongsAndDispatch]) const handlePlayLater = React.useCallback(() => { - dispatch(addTracks(data, ids)) - }, [dispatch, data, ids]) + getAllSongsAndDispatch(addTracks) + }, [getAllSongsAndDispatch]) const handleShuffle = React.useCallback(() => { - dispatch(shuffleTracks(data, ids)) - }, [dispatch, data, ids]) + getAllSongsAndDispatch(shuffleTracks) + }, [getAllSongsAndDispatch]) const handleDownload = React.useCallback(() => { subsonic.download(record.id) diff --git a/ui/src/playlist/PlaylistShow.js b/ui/src/playlist/PlaylistShow.js index cf52ebf5..7cb705ef 100644 --- a/ui/src/playlist/PlaylistShow.js +++ b/ui/src/playlist/PlaylistShow.js @@ -4,13 +4,13 @@ import { ShowContextProvider, useShowContext, useShowController, + Pagination as RaPagination, } from 'react-admin' import { makeStyles } from '@material-ui/core/styles' import PlaylistDetails from './PlaylistDetails' import PlaylistSongs from './PlaylistSongs' import PlaylistActions from './PlaylistActions' import { Title, isReadOnly } from '../common' - const useStyles = makeStyles( (theme) => ({ playlistActions: {}, @@ -35,7 +35,7 @@ const PlaylistShowLayout = (props) => { reference="playlistTrack" target="playlist_id" sort={{ field: 'id', order: 'ASC' }} - perPage={0} + perPage={100} filter={{ playlist_id: props.id }} > { } resource={'playlistTrack'} exporter={false} - perPage={0} - pagination={null} + pagination={} /> )} diff --git a/ui/src/playlist/PlaylistSongs.js b/ui/src/playlist/PlaylistSongs.js index a9f4a7ee..c9ca0ca7 100644 --- a/ui/src/playlist/PlaylistSongs.js +++ b/ui/src/playlist/PlaylistSongs.js @@ -8,6 +8,7 @@ import { useNotify, useVersion, useListContext, + ListBase, } from 'react-admin' import clsx from 'clsx' import { useDispatch } from 'react-redux' @@ -76,8 +77,9 @@ const ReorderableList = ({ readOnly, children, ...rest }) => { return {children} } -const PlaylistSongs = ({ playlistId, readOnly, ...props }) => { - const { data, ids, onUnselectItems } = props +const PlaylistSongs = ({ playlistId, readOnly, actions, ...props }) => { + const listContext = useListContext() + const { data, ids, onUnselectItems } = listContext const isXsmall = useMediaQuery((theme) => theme.breakpoints.down('xs')) const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('md')) const classes = useStyles({ isDesktop }) @@ -128,17 +130,17 @@ const PlaylistSongs = ({ playlistId, readOnly, ...props }) => {
0, + [classes.bulkActionsDisplayed]: listContext.selectedIds.length > 0, })} key={version} > - + { } rowClick={(id) => dispatch(playTracks(data, ids, id))} - {...props} + {...listContext} hasBulkActions={true} contextAlwaysVisible={!isDesktop} classes={{ row: classes.row }} @@ -172,20 +174,26 @@ const PlaylistSongs = ({ playlistId, readOnly, ...props }) => {
+ {React.cloneElement(props.pagination, listContext)} ) } const SanitizedPlaylistSongs = (props) => { - const { loaded, loading, total, ...rest } = useListContext(props) + const { loaded, ...rest } = props return ( <> {loaded && ( - + <> + + + + )} )