Skip to content

🎧 Your Personal Streaming Service

Add pagination to playlists (#969)

Ritik Pandey69ee17402f4f
diff --git a/ui/src/playlist/PlaylistActions.js b/ui/src/playlist/PlaylistActions.js
index fe8958952..24cfd0b25 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 cf52ebf55..7cb705ef7 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 }}
>
<PlaylistSongs
@@ -50,8 +50,7 @@ const PlaylistShowLayout = (props) => {
}
resource={'playlistTrack'}
exporter={false}
- perPage={0}
- pagination={null}
+ pagination={<RaPagination rowsPerPageOptions={[100, 250, 500]} />}
/>
</ReferenceManyField>
)}
diff --git a/ui/src/playlist/PlaylistSongs.js b/ui/src/playlist/PlaylistSongs.js
index a9f4a7eeb..c9ca0ca71 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 <ReactDragListView {...rest}>{children}</ReactDragListView>
}
-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 }) => {
<ListToolbar
classes={{ toolbar: classes.toolbar }}
filters={props.filters}
- actions={props.actions}
- {...props}
+ actions={actions}
+ {...listContext}
/>
<div className={classes.main}>
<Card
className={clsx(classes.content, {
- [classes.bulkActionsDisplayed]: props.selectedIds.length > 0,
+ [classes.bulkActionsDisplayed]: listContext.selectedIds.length > 0,
})}
key={version}
>
- <BulkActionsToolbar {...props}>
+ <BulkActionsToolbar {...listContext}>
<PlaylistSongBulkActions
playlistId={playlistId}
onUnselectItems={onUnselectItems}
@@ -152,7 +154,7 @@ const PlaylistSongs = ({ playlistId, readOnly, ...props }) => {
<SongDatagrid
expand={!isXsmall && <SongDetails />}
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 }) => {
</Card>
</div>
<AddToPlaylistDialog />
+ {React.cloneElement(props.pagination, listContext)}
</>
)
}
const SanitizedPlaylistSongs = (props) => {
- const { loaded, loading, total, ...rest } = useListContext(props)
+ const { loaded, ...rest } = props
return (
<>
{loaded && (
- <PlaylistSongs
- {...rest}
- playlistId={props.id}
- actions={props.actions}
- />
+ <>
+ <ListBase {...props}>
+ <PlaylistSongs
+ playlistId={props.id}
+ actions={props.actions}
+ pagination={props.pagination}
+ {...rest}
+ />
+ </ListBase>
+ </>
)}
</>
)