fix(../V2/SearchView): actual music list used + minor fixes

This commit is contained in:
Amaury Danis Cousandier
2024-01-14 16:13:14 +01:00
parent f2ad34c8ab
commit 4ba4303b1e
3 changed files with 10 additions and 44 deletions

View File

@@ -702,17 +702,18 @@ export default class API {
return `${API.baseUrl}/song/${songId}/assets/partition`; return `${API.baseUrl}/song/${songId}/assets/partition`;
} }
public static searchSongs(query: searchProps): Query<Song[]> { public static searchSongs(query: searchProps, include?: SongInclude[]): Query<Song[]> {
const queryParams: string[] = []; const queryParams: string[] = [];
if (query.query) queryParams.push(`q=${encodeURIComponent(query.query)}`); if (query.query) queryParams.push(`q=${encodeURIComponent(query.query)}`);
if (query.artist) queryParams.push(`artistId=${query.artist}`); if (query.artist) queryParams.push(`artistId=${query.artist}`);
if (query.genre) queryParams.push(`genreId=${query.genre}`); if (query.genre) queryParams.push(`genreId=${query.genre}`);
if (include) queryParams.push(`include=${include.join(',')}`)
const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : ''; const queryString = queryParams.length > 0 ? `?${queryParams.join('&')}` : '';
return { return {
key: ['search', query.query, query.artist, query.genre], key: ['search', query.query, query.artist, query.genre, include],
exec: () => { exec: () => {
return API.fetch( return API.fetch(
{ {

View File

@@ -29,9 +29,9 @@ const ArtistChipComponent = (props: ArtistChipProps) => {
}} }}
> >
{props.selected ? ( {props.selected ? (
<CloseCircle size="32" color={'#ED4A51'} /> <CloseCircle size="24" color={'#ED4A51'} />
) : ( ) : (
<AddSquare size="32" color={'#6075F9'} /> <AddSquare size="24" color={'#6075F9'} />
)} )}
<Text>{props.name}</Text> <Text>{props.name}</Text>
</View> </View>
@@ -82,7 +82,7 @@ const SearchBarComponent = (props: { onValidate: (searchData: searchProps) => vo
maxWidth: '100%', maxWidth: '100%',
}} }}
> >
{artist && ( {!!artist && (
<ArtistChipComponent <ArtistChipComponent
onPress={() => setArtist('')} onPress={() => setArtist('')}
name={artist} name={artist}

View File

@@ -12,6 +12,7 @@ import API from '../../API';
import LoadingComponent from '../../components/Loading'; import LoadingComponent from '../../components/Loading';
import ErrorView from '../ErrorView'; import ErrorView from '../ErrorView';
import { useLikeSongMutation } from '../../utils/likeSongMutation'; import { useLikeSongMutation } from '../../utils/likeSongMutation';
import MusicListCC from '../../components/UI/MusicList';
export type searchProps = { export type searchProps = {
artist: number | undefined; artist: number | undefined;
@@ -88,12 +89,10 @@ const MusicListNoOpti = ({ list }: { list: any[] }) => {
); );
}; };
// eslint-disable-next-line @typescript-eslint/ban-types
const SearchView = () => { const SearchView = () => {
const navigation = useNavigation();
const artistsQuery = useQuery(API.getAllArtists()); const artistsQuery = useQuery(API.getAllArtists());
const [searchQuery, setSearchQuery] = React.useState({} as searchProps); const [searchQuery, setSearchQuery] = React.useState({} as searchProps);
const rawResult = useQuery(API.searchSongs(searchQuery), { const rawResult = useQuery(API.searchSongs(searchQuery, ['artist']), {
enabled: !!searchQuery.query || !!searchQuery.artist || !!searchQuery.genre, enabled: !!searchQuery.query || !!searchQuery.artist || !!searchQuery.genre,
onSuccess() { onSuccess() {
const artist = const artist =
@@ -103,49 +102,15 @@ const SearchView = () => {
if (artist != 'unknown artist') API.createSearchHistoryEntry(artist, 'artist'); if (artist != 'unknown artist') API.createSearchHistoryEntry(artist, 'artist');
}, },
}); });
const userQuery = useQuery(API.getUserInfo());
const likedSongs = useQuery(API.getLikedSongs());
const { mutateAsync } = useLikeSongMutation();
let result: any[] = []; if (artistsQuery.isLoading) {
if (userQuery.isLoading || likedSongs.isLoading || artistsQuery.isLoading) {
return <LoadingComponent />; return <LoadingComponent />;
} }
if (userQuery.isError) {
return <ErrorView />;
}
if (userQuery.isSuccess) {
result =
rawResult.data?.map((song) => {
const isLiked = likedSongs.data?.some((x) => x.songId == song.id) ?? false;
return {
artist:
artistsQuery?.data?.find(({ id }) => id == song?.artistId)?.name ??
'unknown artist',
song: song?.name,
image: song?.cover,
level: song?.difficulties.chordcomplexity,
lastScore: song?.lastScore,
bestScore: song?.bestScore,
liked: isLiked,
onLike: () => {
mutateAsync({ songId: song.id, like: !isLiked }).then(() =>
likedSongs.refetch()
);
},
onPlay: () => navigation.navigate('Play', { songId: song.id }),
};
}) ?? [];
}
return ( return (
<View style={{ display: 'flex', gap: 20 }}> <View style={{ display: 'flex', gap: 20 }}>
<SearchBarComponent onValidate={(query) => setSearchQuery(query)} /> <SearchBarComponent onValidate={(query) => setSearchQuery(query)} />
{result.length != 0 ? <MusicListNoOpti list={result} /> : <SearchHistory />} {rawResult.isSuccess ? <MusicListCC musics={rawResult.data} isFetching={rawResult.isFetching} refetch={rawResult.refetch} /> : <SearchHistory />}
</View> </View>
); );
}; };