Fix playlists
This commit is contained in:
@@ -72,17 +72,16 @@ const useStyles = makeStyles(
|
|||||||
|
|
||||||
const AlbumSongs = (props) => {
|
const AlbumSongs = (props) => {
|
||||||
const classes = useStyles(props)
|
const classes = useStyles(props)
|
||||||
|
const { data, ids } = props
|
||||||
const dispatch = useDispatch()
|
const dispatch = useDispatch()
|
||||||
const isXsmall = useMediaQuery((theme) => theme.breakpoints.down('xs'))
|
const isXsmall = useMediaQuery((theme) => theme.breakpoints.down('xs'))
|
||||||
const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('md'))
|
const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('md'))
|
||||||
const { id: album_id, data, ids } = props
|
|
||||||
const version = useVersion()
|
const version = useVersion()
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ListToolbar
|
<ListToolbar
|
||||||
classes={{ toolbar: classes.toolbar }}
|
classes={{ toolbar: classes.toolbar }}
|
||||||
actions={props.actions}
|
actions={props.actions}
|
||||||
permanentFilter={{ album_id }}
|
|
||||||
{...props}
|
{...props}
|
||||||
/>
|
/>
|
||||||
<div className={classes.main}>
|
<div className={classes.main}>
|
||||||
|
|||||||
@@ -14,7 +14,6 @@ const mapResource = (resource, params) => {
|
|||||||
let plsId = '0'
|
let plsId = '0'
|
||||||
if (params.filter) {
|
if (params.filter) {
|
||||||
plsId = params.filter.playlist_id
|
plsId = params.filter.playlist_id
|
||||||
delete params.filter.playlist_id
|
|
||||||
}
|
}
|
||||||
return [`playlist/${plsId}/tracks`, params]
|
return [`playlist/${plsId}/tracks`, params]
|
||||||
|
|
||||||
|
|||||||
@@ -1,43 +1,54 @@
|
|||||||
import React from 'react'
|
import React from 'react'
|
||||||
import { useSelector } from 'react-redux'
|
import {
|
||||||
import { useGetOne } from 'react-admin'
|
ReferenceManyField,
|
||||||
|
ShowContextProvider,
|
||||||
|
useShowContext,
|
||||||
|
useShowController,
|
||||||
|
} from 'react-admin'
|
||||||
import PlaylistDetails from './PlaylistDetails'
|
import PlaylistDetails from './PlaylistDetails'
|
||||||
import { Title } from '../common'
|
|
||||||
import PlaylistSongs from './PlaylistSongs'
|
import PlaylistSongs from './PlaylistSongs'
|
||||||
import PlaylistActions from './PlaylistActions'
|
import PlaylistActions from './PlaylistActions'
|
||||||
import PlaylistSongBulkActions from './PlaylistSongBulkActions'
|
import { Title, isReadOnly } from '../common'
|
||||||
import { isReadOnly } from '../common/Writable'
|
|
||||||
|
|
||||||
const PlaylistShow = (props) => {
|
|
||||||
const viewVersion = useSelector((s) => s.admin.ui && s.admin.ui.viewVersion)
|
|
||||||
const { data: record, error } = useGetOne('playlist', props.id, {
|
|
||||||
v: viewVersion,
|
|
||||||
})
|
|
||||||
|
|
||||||
if (error) {
|
|
||||||
return <p>ERROR: {error}</p>
|
|
||||||
}
|
|
||||||
|
|
||||||
|
const PlaylistShowLayout = (props) => {
|
||||||
|
const { loading, ...context } = useShowContext(props)
|
||||||
|
const { record } = context
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<PlaylistDetails {...props} record={record} />
|
{record && <PlaylistDetails {...context} />}
|
||||||
<PlaylistSongs
|
{record && (
|
||||||
{...props}
|
<ReferenceManyField
|
||||||
playlistId={props.id}
|
{...context}
|
||||||
readOnly={isReadOnly(record && record.owner)}
|
addLabel={false}
|
||||||
title={<Title subTitle={record && record.name} />}
|
reference="playlistTrack"
|
||||||
actions={<PlaylistActions record={record} />}
|
target="playlist_id"
|
||||||
filter={{ playlist_id: props.id }}
|
sort={{ field: 'id', order: 'ASC' }}
|
||||||
resource={'playlistTrack'}
|
perPage={0}
|
||||||
exporter={false}
|
filter={{ playlist_id: props.id }}
|
||||||
perPage={0}
|
>
|
||||||
pagination={null}
|
<PlaylistSongs
|
||||||
bulkActionButtons={
|
{...props}
|
||||||
<PlaylistSongBulkActions playlistId={props.id} record={record} />
|
readOnly={isReadOnly(record.owner)}
|
||||||
}
|
title={<Title subTitle={record.name} />}
|
||||||
/>
|
actions={<PlaylistActions record={record} />}
|
||||||
|
resource={'playlistTrack'}
|
||||||
|
exporter={false}
|
||||||
|
perPage={0}
|
||||||
|
pagination={null}
|
||||||
|
/>
|
||||||
|
</ReferenceManyField>
|
||||||
|
)}
|
||||||
</>
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const PlaylistShow = (props) => {
|
||||||
|
const controllerProps = useShowController(props)
|
||||||
|
return (
|
||||||
|
<ShowContextProvider value={controllerProps}>
|
||||||
|
<PlaylistShowLayout {...props} {...controllerProps} />
|
||||||
|
</ShowContextProvider>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
export default PlaylistShow
|
export default PlaylistShow
|
||||||
|
|||||||
@@ -1,17 +1,26 @@
|
|||||||
import React, { Fragment, useEffect } from 'react'
|
import React, { Fragment, useEffect } from 'react'
|
||||||
import { BulkDeleteButton, useUnselectAll } from 'react-admin'
|
import {
|
||||||
|
BulkDeleteButton,
|
||||||
|
useUnselectAll,
|
||||||
|
ResourceContextProvider,
|
||||||
|
} from 'react-admin'
|
||||||
import PropTypes from 'prop-types'
|
import PropTypes from 'prop-types'
|
||||||
|
|
||||||
const PlaylistSongBulkActions = ({ playlistId, ...rest }) => {
|
// Replace original resource with "fake" one for removing tracks from playlist
|
||||||
|
const PlaylistSongBulkActions = ({ playlistId, resource, ...rest }) => {
|
||||||
const unselectAll = useUnselectAll()
|
const unselectAll = useUnselectAll()
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
unselectAll('playlistTrack')
|
unselectAll('playlistTrack')
|
||||||
// eslint-disable-next-line
|
// eslint-disable-next-line
|
||||||
}, [])
|
}, [])
|
||||||
|
|
||||||
|
const mappedResource = `playlist/${playlistId}/tracks`
|
||||||
return (
|
return (
|
||||||
<Fragment>
|
<ResourceContextProvider value={mappedResource}>
|
||||||
<BulkDeleteButton {...rest} resource={`playlist/${playlistId}/tracks`} />
|
<Fragment>
|
||||||
</Fragment>
|
<BulkDeleteButton {...rest} resource={mappedResource} />
|
||||||
|
</Fragment>
|
||||||
|
</ResourceContextProvider>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -1,13 +1,13 @@
|
|||||||
import React from 'react'
|
import React, { useCallback } from 'react'
|
||||||
import {
|
import {
|
||||||
BulkActionsToolbar,
|
BulkActionsToolbar,
|
||||||
DatagridLoading,
|
|
||||||
ListToolbar,
|
ListToolbar,
|
||||||
TextField,
|
TextField,
|
||||||
useListController,
|
|
||||||
useRefresh,
|
useRefresh,
|
||||||
useDataProvider,
|
useDataProvider,
|
||||||
useNotify,
|
useNotify,
|
||||||
|
useVersion,
|
||||||
|
useListContext,
|
||||||
} from 'react-admin'
|
} from 'react-admin'
|
||||||
import classnames from 'classnames'
|
import classnames from 'classnames'
|
||||||
import { useDispatch } from 'react-redux'
|
import { useDispatch } from 'react-redux'
|
||||||
@@ -24,6 +24,7 @@ import {
|
|||||||
import AddToPlaylistDialog from '../dialogs/AddToPlaylistDialog'
|
import AddToPlaylistDialog from '../dialogs/AddToPlaylistDialog'
|
||||||
import { AlbumLinkField } from '../song/AlbumLinkField'
|
import { AlbumLinkField } from '../song/AlbumLinkField'
|
||||||
import { playTracks } from '../actions'
|
import { playTracks } from '../actions'
|
||||||
|
import PlaylistSongBulkActions from './PlaylistSongBulkActions'
|
||||||
|
|
||||||
const useStyles = makeStyles(
|
const useStyles = makeStyles(
|
||||||
(theme) => ({
|
(theme) => ({
|
||||||
@@ -51,16 +52,23 @@ const useStyles = makeStyles(
|
|||||||
flexWrap: 'wrap',
|
flexWrap: 'wrap',
|
||||||
},
|
},
|
||||||
noResults: { padding: 20 },
|
noResults: { padding: 20 },
|
||||||
|
toolbar: {
|
||||||
|
justifyContent: 'flex-start',
|
||||||
|
},
|
||||||
|
row: {
|
||||||
|
'&:hover': {
|
||||||
|
'& $contextMenu': {
|
||||||
|
visibility: 'visible',
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
contextMenu: {
|
||||||
|
visibility: 'hidden',
|
||||||
|
},
|
||||||
}),
|
}),
|
||||||
{ name: 'RaList' }
|
{ name: 'RaList' }
|
||||||
)
|
)
|
||||||
|
|
||||||
const useStylesListToolbar = makeStyles({
|
|
||||||
toolbar: {
|
|
||||||
justifyContent: 'flex-start',
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
const ReorderableList = ({ readOnly, children, ...rest }) => {
|
const ReorderableList = ({ readOnly, children, ...rest }) => {
|
||||||
if (readOnly) {
|
if (readOnly) {
|
||||||
return children
|
return children
|
||||||
@@ -68,113 +76,96 @@ const ReorderableList = ({ readOnly, children, ...rest }) => {
|
|||||||
return <ReactDragListView {...rest}>{children}</ReactDragListView>
|
return <ReactDragListView {...rest}>{children}</ReactDragListView>
|
||||||
}
|
}
|
||||||
|
|
||||||
const PlaylistSongs = (props) => {
|
const PlaylistSongs = ({ playlistId, readOnly, ...props }) => {
|
||||||
|
const { data, ids } = props
|
||||||
const classes = useStyles(props)
|
const classes = useStyles(props)
|
||||||
const classesToolbar = useStylesListToolbar(props)
|
|
||||||
const dispatch = useDispatch()
|
const dispatch = useDispatch()
|
||||||
const isXsmall = useMediaQuery((theme) => theme.breakpoints.down('xs'))
|
const isXsmall = useMediaQuery((theme) => theme.breakpoints.down('xs'))
|
||||||
const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('md'))
|
const isDesktop = useMediaQuery((theme) => theme.breakpoints.up('md'))
|
||||||
const controllerProps = useListController(props)
|
|
||||||
const dataProvider = useDataProvider()
|
const dataProvider = useDataProvider()
|
||||||
const refresh = useRefresh()
|
const refresh = useRefresh()
|
||||||
const notify = useNotify()
|
const notify = useNotify()
|
||||||
const { bulkActionButtons, expand, className, playlistId, readOnly } = props
|
const version = useVersion()
|
||||||
const { data, ids, version, total } = controllerProps
|
|
||||||
|
|
||||||
if (total === 0) {
|
const onAddToPlaylist = useCallback(
|
||||||
return null
|
(pls) => {
|
||||||
}
|
if (pls.id === playlistId) {
|
||||||
|
|
||||||
const anySong = data[ids[0]]
|
|
||||||
const showPlaceholder = !anySong || anySong.playlistId !== playlistId
|
|
||||||
const hasBulkActions = props.bulkActionButtons !== false
|
|
||||||
|
|
||||||
const reorder = (playlistId, id, newPos) => {
|
|
||||||
dataProvider
|
|
||||||
.update('playlistTrack', {
|
|
||||||
id,
|
|
||||||
data: { insert_before: newPos },
|
|
||||||
filter: { playlist_id: playlistId },
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
refresh()
|
refresh()
|
||||||
})
|
}
|
||||||
.catch(() => {
|
},
|
||||||
notify('ra.page.error', 'warning')
|
[playlistId, refresh]
|
||||||
})
|
)
|
||||||
}
|
|
||||||
|
|
||||||
const onAddToPlaylist = (pls) => {
|
const reorder = useCallback(
|
||||||
if (pls.id === props.id) {
|
(playlistId, id, newPos) => {
|
||||||
refresh()
|
dataProvider
|
||||||
}
|
.update('playlistTrack', {
|
||||||
}
|
id,
|
||||||
|
data: { insert_before: newPos },
|
||||||
|
filter: { playlist_id: playlistId },
|
||||||
|
})
|
||||||
|
.then(() => {
|
||||||
|
refresh()
|
||||||
|
})
|
||||||
|
.catch(() => {
|
||||||
|
notify('ra.page.error', 'warning')
|
||||||
|
})
|
||||||
|
},
|
||||||
|
[dataProvider, notify, refresh]
|
||||||
|
)
|
||||||
|
|
||||||
const handleDragEnd = (from, to) => {
|
const handleDragEnd = useCallback(
|
||||||
const toId = ids[to]
|
(from, to) => {
|
||||||
const fromId = ids[from]
|
const toId = ids[to]
|
||||||
reorder(playlistId, fromId, toId)
|
const fromId = ids[from]
|
||||||
}
|
reorder(playlistId, fromId, toId)
|
||||||
|
},
|
||||||
|
[playlistId, reorder, ids]
|
||||||
|
)
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
<ListToolbar
|
<ListToolbar
|
||||||
classes={classesToolbar}
|
classes={{ toolbar: classes.toolbar }}
|
||||||
filters={props.filters}
|
filters={props.filters}
|
||||||
{...controllerProps}
|
|
||||||
actions={props.actions}
|
actions={props.actions}
|
||||||
permanentFilter={props.filter}
|
{...props}
|
||||||
/>
|
/>
|
||||||
<div className={classes.main}>
|
<div className={classes.main}>
|
||||||
<Card
|
<Card
|
||||||
className={classnames(classes.content, {
|
className={classnames(classes.content, {
|
||||||
[classes.bulkActionsDisplayed]:
|
[classes.bulkActionsDisplayed]: props.selectedIds.length > 0,
|
||||||
controllerProps.selectedIds.length > 0,
|
|
||||||
})}
|
})}
|
||||||
key={version}
|
key={version}
|
||||||
>
|
>
|
||||||
{bulkActionButtons !== false && bulkActionButtons && (
|
<BulkActionsToolbar {...props}>
|
||||||
<BulkActionsToolbar {...controllerProps}>
|
<PlaylistSongBulkActions playlistId={playlistId} />
|
||||||
{bulkActionButtons}
|
</BulkActionsToolbar>
|
||||||
</BulkActionsToolbar>
|
<ReorderableList
|
||||||
)}
|
readOnly={readOnly}
|
||||||
{showPlaceholder ? (
|
onDragEnd={handleDragEnd}
|
||||||
<DatagridLoading
|
nodeSelector={'tr'}
|
||||||
classes={classes}
|
>
|
||||||
className={className}
|
<SongDatagrid
|
||||||
expand={expand}
|
expand={!isXsmall && <SongDetails />}
|
||||||
hasBulkActions={hasBulkActions}
|
rowClick={(id) => dispatch(playTracks(data, ids, id))}
|
||||||
nbChildren={3}
|
{...props}
|
||||||
size={'small'}
|
hasBulkActions={true}
|
||||||
/>
|
contextAlwaysVisible={!isDesktop}
|
||||||
) : (
|
classes={{ row: classes.row }}
|
||||||
<ReorderableList
|
|
||||||
readOnly={readOnly}
|
|
||||||
onDragEnd={handleDragEnd}
|
|
||||||
nodeSelector={'tr'}
|
|
||||||
>
|
>
|
||||||
<SongDatagrid
|
{isDesktop && <TextField source="id" label={'#'} />}
|
||||||
expand={!isXsmall && <SongDetails />}
|
<SongTitleField source="title" showTrackNumbers={false} />
|
||||||
rowClick={(id) => dispatch(playTracks(data, ids, id))}
|
{isDesktop && <AlbumLinkField source="album" />}
|
||||||
{...controllerProps}
|
{isDesktop && <TextField source="artist" />}
|
||||||
hasBulkActions={hasBulkActions}
|
<DurationField source="duration" className={classes.draggable} />
|
||||||
contextAlwaysVisible={!isDesktop}
|
<SongContextMenu
|
||||||
>
|
onAddToPlaylist={onAddToPlaylist}
|
||||||
{isDesktop && <TextField source="id" label={'#'} />}
|
showStar={false}
|
||||||
<SongTitleField source="title" showTrackNumbers={false} />
|
className={classes.contextMenu}
|
||||||
{isDesktop && <AlbumLinkField source="album" />}
|
/>
|
||||||
{isDesktop && <TextField source="artist" />}
|
</SongDatagrid>
|
||||||
<DurationField
|
</ReorderableList>
|
||||||
source="duration"
|
|
||||||
className={classes.draggable}
|
|
||||||
/>
|
|
||||||
<SongContextMenu
|
|
||||||
onAddToPlaylist={onAddToPlaylist}
|
|
||||||
showStar={false}
|
|
||||||
/>
|
|
||||||
</SongDatagrid>
|
|
||||||
</ReorderableList>
|
|
||||||
)}
|
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
<AddToPlaylistDialog />
|
<AddToPlaylistDialog />
|
||||||
@@ -182,4 +173,19 @@ const PlaylistSongs = (props) => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
export default PlaylistSongs
|
const SanitizedPlaylistSongs = (props) => {
|
||||||
|
const { loaded, loading, total, ...rest } = useListContext(props)
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
{loaded && (
|
||||||
|
<PlaylistSongs
|
||||||
|
{...rest}
|
||||||
|
playlistId={props.id}
|
||||||
|
actions={props.actions}
|
||||||
|
/>
|
||||||
|
)}
|
||||||
|
</>
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
export default SanitizedPlaylistSongs
|
||||||
|
|||||||
Reference in New Issue
Block a user