From caa3322676bab26760ccc9cd4cd1056588041e4a Mon Sep 17 00:00:00 2001 From: Amaury Danis Cousandier Date: Thu, 11 Jan 2024 17:46:27 +0100 Subject: [PATCH] liked handled properly --- front/components/V2/SearchBar.tsx | 1 + front/views/V2/SearchView.tsx | 19 ++++++++----------- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/front/components/V2/SearchBar.tsx b/front/components/V2/SearchBar.tsx index 3478be7..961e4b1 100644 --- a/front/components/V2/SearchBar.tsx +++ b/front/components/V2/SearchBar.tsx @@ -156,6 +156,7 @@ const SearchBarComponent = (props: { onValidate: (searchData: searchProps) => vo key={index} name={artist.name} onPress={() => { + props.onValidate({artist: artist.id, genre: genresQuery.data?.find((g) => g.name === genre)?.id ?? undefined, query: query}) setArtist(artist.name); }} /> diff --git a/front/views/V2/SearchView.tsx b/front/views/V2/SearchView.tsx index c495569..8f11539 100644 --- a/front/views/V2/SearchView.tsx +++ b/front/views/V2/SearchView.tsx @@ -13,6 +13,7 @@ import API from '../../API'; import { useMutation } from 'react-query'; import LoadingComponent from '../../components/Loading'; import ErrorView from '../ErrorView'; +import { useLikeSongMutation } from '../../utils/likeSongMutation'; export type searchProps = { artist: number | undefined; @@ -96,15 +97,12 @@ const SearchView = (props: RouteProps<{}>) => { const [searchQuery, setSearchQuery] = React.useState({} as searchProps); const rawResult = useQuery(API.searchSongs(searchQuery)); const userQuery = useQuery(API.getUserInfo()); - let result: any[] = []; + const likedSongs = useQuery(API.getLikedSongs()); + const { mutateAsync } = useLikeSongMutation(); - const { mutate } = useMutation((songId: number) => API.addLikedSong(songId), { - onSuccess: () => { - rawResult.refetch(); - }, - }); + let result: any[] = [];; - if (userQuery.isLoading) { + if (userQuery.isLoading || likedSongs.isLoading || userQuery.isLoading) { return ; } @@ -123,11 +121,10 @@ const SearchView = (props: RouteProps<{}>) => { level: song?.difficulties.chordcomplexity, lastScore: song?.lastScore, bestScore: song?.bestScore, - liked: - song?.likedByUsers?.some((user) => user.userId === userQuery.data.id) ?? false, + liked: likedSongs.data?.some(x => x.songId === song.id) ?? false, onLike: () => { - mutate(song.id); - }, + mutateAsync({ songId: song.id, like: false }).then(() => likedSongs.refetch()); + }, onPlay: () => navigation.navigate('Play', { songId: song.id }), })) ?? []; }