ac37bf3631
* Added setRating feature to AlbumListView * Refactored the iconography from ⭐ to ❤️ * Refactored the current component from StarButton to LoveButton * Refactored all translations from Starred to Loved, and all props from showStar to showLove * Refactored useToggleStar hook to useToggleLove * rebased repository from master and removed stray commmits * Refactored handler name from TOGGLE_STAR to TOGGLE_LOVE in PlayerToolbar.js * Change "starred" translation to "Favorite" Co-authored-by: Deluan <deluan@navidrome.org>
44 lines
1.1 KiB
JavaScript
44 lines
1.1 KiB
JavaScript
import { useCallback, useEffect, useRef, useState } from 'react'
|
|
import { useDataProvider, useNotify } from 'react-admin'
|
|
import subsonic from '../subsonic'
|
|
|
|
export const useToggleLove = (resource, record = {}) => {
|
|
const [loading, setLoading] = useState(false)
|
|
const notify = useNotify()
|
|
|
|
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 toggleLove = () => {
|
|
const toggle = record.starred ? subsonic.unstar : subsonic.star
|
|
|
|
setLoading(true)
|
|
toggle(record.id)
|
|
.then(refreshRecord)
|
|
.catch((e) => {
|
|
console.log('Error toggling love: ', e)
|
|
notify('ra.page.error', 'warning')
|
|
if (mountedRef.current) {
|
|
setLoading(false)
|
|
}
|
|
})
|
|
}
|
|
|
|
return [toggleLove, loading]
|
|
}
|