1- import React from 'react'
2-
3- import { useSetUpdateApi } from '~/contexts/updateApi'
4- import { useConfig } from '~/contexts/config'
5- import { useTheme } from '~/contexts/theme'
6- import { getApi , refreshApi } from '~/utils/api'
7- import IconButton from '~/components/button/IconButton'
8- import logger from '~/utils/logger'
9-
10- const FavoritedButton = ( { id, isFavorited = false , style = { } , size = 23 } ) => {
11- const [ favorited , setFavorited ] = React . useState ( isFavorited )
12- const theme = useTheme ( )
13- const config = useConfig ( )
14- const setUpdateApi = useSetUpdateApi ( )
15-
16- React . useEffect ( ( ) => {
17- setFavorited ( isFavorited )
18- } , [ id , isFavorited ] )
19-
20- const onPressFavorited = ( ) => {
21- getApi ( config , favorited ? 'unstar' : 'star' , { id, albumId : id , artistId : id } )
22- . then ( ( ) => {
23- setFavorited ( ! favorited )
24- refreshApi ( config , 'getStarred2' , null )
25- . then ( ( ) => {
26- setUpdateApi ( { path : 'getStarred2' , query : null , uid : 1 } )
27- } )
28- } )
29- . catch ( ( e ) => logger . error ( 'FavoritedButton' , e . message ) )
30- }
31-
32- return (
33- < IconButton
34- style = { [ { padding : 20 } , style ] }
35- onPress = { onPressFavorited }
36- size = { size }
37- icon = { favorited ? "heart" : "heart-o" }
38- color = { theme . primaryTouch }
39- />
40- )
41- }
42-
1+ import React from 'react'
2+
3+ import { useSetUpdateApi } from '~/contexts/updateApi'
4+ import { useConfig } from '~/contexts/config'
5+ import { useSongDispatch } from '~/contexts/song'
6+ import { useTheme } from '~/contexts/theme'
7+ import { getApi , refreshApi } from '~/utils/api'
8+ import IconButton from '~/components/button/IconButton'
9+ import RatingPopup from '~/components/popup/RatingPopup'
10+ import logger from '~/utils/logger'
11+
12+ const FavoritedButton = ( { id, isFavorited = false , rating : initialRating = undefined , style = { } , size = 23 } ) => {
13+ const [ favorited , setFavorited ] = React . useState ( isFavorited )
14+ const [ rating , setRating ] = React . useState ( initialRating ?? 0 )
15+ const [ isRatingOpen , setIsRatingOpen ] = React . useState ( false )
16+ const theme = useTheme ( )
17+ const config = useConfig ( )
18+ const songDispatch = useSongDispatch ( )
19+ const setUpdateApi = useSetUpdateApi ( )
20+
21+ React . useEffect ( ( ) => {
22+ setFavorited ( isFavorited )
23+ setRating ( initialRating ?? 0 )
24+ } , [ id , isFavorited , initialRating ] )
25+
26+ const onPressFavorited = ( ) => {
27+ getApi ( config , favorited ? 'unstar' : 'star' , { id, albumId : id , artistId : id } )
28+ . then ( ( ) => {
29+ setFavorited ( ! favorited )
30+ refreshApi ( config , 'getStarred2' , null )
31+ . then ( ( ) => {
32+ setUpdateApi ( { path : 'getStarred2' , query : null , uid : 1 } )
33+ } )
34+ } )
35+ . catch ( ( e ) => logger . error ( 'FavoritedButton' , e . message ) )
36+ }
37+
38+ const onSaveRating = ( ) => {
39+ getApi ( config , 'setRating' , { id, rating } )
40+ . then ( ( ) => {
41+ songDispatch ( { type : 'setRating' , id, rating } )
42+ setIsRatingOpen ( false )
43+ } )
44+ . catch ( ( e ) => logger . error ( 'FavoritedButton' , e . message ) )
45+ }
46+
47+ return (
48+ < >
49+ < IconButton
50+ style = { [ { padding : 20 } , style ] }
51+ onPress = { onPressFavorited }
52+ onLongPress = { ( ) => initialRating !== undefined ? setIsRatingOpen ( true ) : null }
53+ size = { size }
54+ icon = { favorited ? "heart" : "heart-o" }
55+ color = { theme . primaryTouch }
56+ />
57+ < RatingPopup
58+ visible = { isRatingOpen }
59+ rating = { rating }
60+ onSelectRating = { setRating }
61+ onSave = { onSaveRating }
62+ onClose = { ( ) => setIsRatingOpen ( false ) }
63+ />
64+ </ >
65+ )
66+ }
67+
4368export default FavoritedButton
0 commit comments