Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 | import { Link, useLocation } from "react-router-dom";
import { useTranslation } from "react-i18next";
import useInfiniteMovieQuery from "@/queries/useInfiniteMovieQuery";
import { IMovie, IApiFunction, IPage } from "@/interfaces";
import { LoadingSpinner, ErrorMessage } from "@/components/atoms";
import { Movie } from "@/components/molecules";
import emptyResults from "@/assets/empty-results.svg";
const SearchResults = ({ apiFunctions }: { apiFunctions: IApiFunction }) => {
const location = useLocation();
const { i18n } = useTranslation();
const searchQuery = new URLSearchParams(location.search).get("query");
const {
data: searchResults,
error,
fetchNextPage,
hasNextPage,
isFetchingNextPage,
status,
} = useInfiniteMovieQuery([apiFunctions.searchMovies.key, searchQuery], ({ pageParam }: { pageParam: number }) =>
apiFunctions.searchMovies.func(searchQuery, i18n.language, pageParam)
);
return (
<div className="px-4 sm:px-8 py-4">
{error ? (
<ErrorMessage />
) : (
<div className="w-full md:w-4/5 m-auto">
{status === "pending" ? (
<div className="flex justify-center">
<LoadingSpinner />
</div>
) : (
<>
{searchResults.pages.every((p: IPage) => p.results.length === 0) ? (
<div className="flex flex-col items-center">
<img className="w-[300px] " src={emptyResults} />
</div>
) : (
<div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-7">
{searchResults.pages.map((page: IPage) =>
page.results.map((movie: IMovie) => (
<Link key={movie.id} to={`/movie/${movie.id}`}>
<Movie movie={movie} />
</Link>
))
)}
</div>
)}
</>
)}
<div className="flex justify-center">
{status === "success" && (
<button
className="bg-[#172554] px-4 py-2 rounded-md text-white mt-4"
onClick={() => fetchNextPage()}
disabled={!hasNextPage || isFetchingNextPage}
>
{isFetchingNextPage ? "Loading more..." : hasNextPage ? "Load More" : "Nothing more to load"}
</button>
)}
</div>
</div>
)}
</div>
);
};
export default SearchResults;
|