28 lines
1.1 KiB
TypeScript
28 lines
1.1 KiB
TypeScript
import movieAdvisor from '../clients/movieAdvisor';
|
|
import mapMoviesSeriesResponseToShows from '../dto/Show';
|
|
|
|
export type SearchQueryProps = { query: string };
|
|
|
|
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 }) =>
|
|
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)
|
|
});
|
|
|
|
export default searchQuery;
|