Front: Use Mutations to update 'liked' state

This commit is contained in:
Arthur Jamet
2023-12-18 19:53:41 +01:00
committed by Clément Le Bihan
parent 004a541302
commit 60988dd599
5 changed files with 39 additions and 12 deletions
+19
View File
@@ -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])
});
});
}