Front: Use Mutations to update 'liked' state
This commit is contained in:
committed by
Clément Le Bihan
parent
004a541302
commit
60988dd599
@@ -0,0 +1,19 @@
|
||||
import { useMutation, useQueryClient } from "react-query"
|
||||
import API from "../API";
|
||||
|
||||
/**
|
||||
* Mutation to like/unlike a song
|
||||
*/
|
||||
export const useLikeSongMutation = () => {
|
||||
const queryClient = useQueryClient();
|
||||
|
||||
return useMutation(({ songId, like }: {songId: number, like: boolean}) => {
|
||||
const apiCall = like ? API.addLikedSong : API.removeLikedSong
|
||||
|
||||
return apiCall(songId).then(() => {
|
||||
queryClient.invalidateQueries('liked songs')
|
||||
queryClient.invalidateQueries('songs')
|
||||
queryClient.invalidateQueries([songId])
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user