feat(search): unified movie+series search, results ranked by rating
This commit is contained in:
1 parent
75880e5ac7
commit
6cff846f94
4 files changed
+22
-13
No files matched your search
+18
-6
@@ -1,14 +1,26 @@
|
||||
import movieAdvisor from '../clients/movieAdvisor';
|
||||
import mapMoviesSeriesResponseToShows from '../dto/Show';
|
||||
|
||||
export type SearchQueryProps = { query: string; type: Show['type'] };
|
||||
export type SearchQueryProps = { query: string };
|
||||
|
||||
const searchQuery = ({ query, type }: SearchQueryProps) => ({
|
||||
queryKey: ['search', query, type],
|
||||
const byRating = (a: Show, b: Show) => b.rating - a.rating || b.votes - a.votes || a.title.localeCompare(b.title);
|
||||
|
||||
const searchQuery = ({ query }: SearchQueryProps) => ({
|
||||
queryKey: ['search', query],
|
||||
queryFn: ({ pageParam }: { pageParam: string }) =>
|
||||
movieAdvisor
|
||||
.get<TMDBPagination<Movie>>(`/search/${type}`, { params: { page: pageParam, query } })
|
||||
.then((response) => mapMoviesSeriesResponseToShows(response, type)),
|
||||
Promise.all([
|
||||
movieAdvisor.get<TMDBPagination<Movie>>('/search/movie', { params: { page: pageParam, query } }),
|
||||
movieAdvisor.get<TMDBPagination<Series>>('/search/tv', { params: { page: pageParam, query } })
|
||||
]).then(([movies, series]) => {
|
||||
const moviesPage = mapMoviesSeriesResponseToShows(movies, 'movie');
|
||||
const seriesPage = mapMoviesSeriesResponseToShows(series, 'tv');
|
||||
|
||||
return {
|
||||
page: Math.max(moviesPage.page, seriesPage.page),
|
||||
total: Math.max(moviesPage.total, seriesPage.total),
|
||||
results: [...moviesPage.results, ...seriesPage.results].sort(byRating)
|
||||
};
|
||||
}),
|
||||
enabled: Boolean(query)
|
||||
});
|
||||
|
||||
|
||||
Reference in new issue
Block a user