mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-03 20:47:35 +03:00
Use Subsonic API to star/unstar
This removes the need to update the annotations on Put(model), removing complexity and making it less buggy
This commit is contained in:
parent
47976e13b1
commit
abd51b2156
4 changed files with 37 additions and 32 deletions
|
@ -96,12 +96,3 @@ func (r sqlRepository) cleanAnnotations() error {
|
|||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (r sqlRepository) updateAnnotations(id string, m interface{}) error {
|
||||
ans := m.(model.AnnotatedModel).GetAnnotations()
|
||||
err := r.SetStar(ans.Starred, id)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return r.SetRating(ans.Rating, id)
|
||||
}
|
||||
|
|
|
@ -171,9 +171,6 @@ func (r sqlRepository) put(id string, m interface{}) (newId string, err error) {
|
|||
return "", err
|
||||
}
|
||||
if count > 0 {
|
||||
if _, ok := m.(model.AnnotatedModel); ok {
|
||||
err = r.updateAnnotations(id, m)
|
||||
}
|
||||
return id, err
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,10 +1,11 @@
|
|||
import React from 'react'
|
||||
import React, { useCallback, useEffect, useRef, useState } from 'react'
|
||||
import PropTypes from 'prop-types'
|
||||
import { useNotify, useRefresh, useUpdate } from 'react-admin'
|
||||
import { useNotify, useDataProvider } from 'react-admin'
|
||||
import StarIcon from '@material-ui/icons/Star'
|
||||
import StarBorderIcon from '@material-ui/icons/StarBorder'
|
||||
import IconButton from '@material-ui/core/IconButton'
|
||||
import { makeStyles } from '@material-ui/core/styles'
|
||||
import subsonic from '../subsonic'
|
||||
|
||||
const useStyles = makeStyles({
|
||||
star: {
|
||||
|
@ -15,30 +16,42 @@ const useStyles = makeStyles({
|
|||
})
|
||||
|
||||
const StarButton = ({ resource, record, color, visible, size }) => {
|
||||
const [loading, setLoading] = useState(false)
|
||||
const classes = useStyles({ color, visible, starred: record.starred })
|
||||
const notify = useNotify()
|
||||
const refresh = useRefresh()
|
||||
|
||||
const [toggleStarred, { loading }] = useUpdate(
|
||||
resource,
|
||||
record.id,
|
||||
{
|
||||
...record,
|
||||
starred: !record.starred,
|
||||
},
|
||||
{
|
||||
undoable: false,
|
||||
onFailure: (error) => {
|
||||
console.log(error)
|
||||
notify('ra.page.error', 'warning')
|
||||
refresh()
|
||||
},
|
||||
const mountedRef = useRef(false)
|
||||
useEffect(() => {
|
||||
mountedRef.current = true
|
||||
return () => {
|
||||
mountedRef.current = false
|
||||
}
|
||||
)
|
||||
}, [])
|
||||
|
||||
const dataProvider = useDataProvider()
|
||||
|
||||
const refreshRecord = useCallback(() => {
|
||||
dataProvider.getOne(resource, { id: record.id }).then(() => {
|
||||
if (mountedRef.current) {
|
||||
setLoading(false)
|
||||
}
|
||||
})
|
||||
}, [dataProvider, record.id, resource])
|
||||
|
||||
const handleToggleStar = (e) => {
|
||||
e.preventDefault()
|
||||
toggleStarred()
|
||||
const toggleStar = record.starred ? subsonic.unstar : subsonic.star
|
||||
|
||||
setLoading(true)
|
||||
toggleStar(record.id)
|
||||
.then(refreshRecord)
|
||||
.catch((e) => {
|
||||
console.log('Error toggling star: ', e)
|
||||
notify('ra.page.error', 'warning')
|
||||
if (mountedRef.current) {
|
||||
setLoading(false)
|
||||
}
|
||||
})
|
||||
e.stopPropagation()
|
||||
}
|
||||
|
||||
|
|
|
@ -26,6 +26,10 @@ const url = (command, id, options) => {
|
|||
const scrobble = (id, submit) =>
|
||||
fetchUtils.fetchJson(url('scrobble', id, { submission: submit }))
|
||||
|
||||
const star = (id) => fetchUtils.fetchJson(url('star', id))
|
||||
|
||||
const unstar = (id) => fetchUtils.fetchJson(url('unstar', id))
|
||||
|
||||
const download = (id) => (window.location.href = url('download', id))
|
||||
|
||||
export default { url, scrobble, download }
|
||||
export default { url, scrobble, download, star, unstar }
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue