All files / src/pages/upcomingMovies upcomingMovies.tsx

0% Statements 0/8
0% Branches 0/12
0% Functions 0/5
0% Lines 0/8

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                                                                                                                                               
import { Link } from "react-router-dom";
import { useTranslation } from "react-i18next";
 
import useInfiniteMovieQuery from "@/queries/useInfiniteMovieQuery";
import { IApiFunction, IMovie, IPage } from "@/interfaces";
 
import { LoadingSpinner, ErrorMessage } from "@/components/atoms";
import { Movie } from "@/components/molecules";
 
export const UpcomingMovies = ({ apiFunctions }: { apiFunctions: IApiFunction }) => {
  const { i18n, t } = useTranslation();
 
  const {
    data: upcomingMovies,
    error,
    fetchNextPage,
    hasNextPage,
    isFetchingNextPage,
    status,
  } = useInfiniteMovieQuery(
    [apiFunctions.getUpcomingMovies.key, i18n.language],
    ({ pageParam }: { pageParam: number }) =>
      apiFunctions.getUpcomingMovies.func(i18n.language, pageParam)
  );
 
  return (
    <div className="mt-8">
      <div className="px-4 sm:px-8">
        <span className="text-2xl font-semibold">{t("upcoming_movies")}</span>
      </div>
 
      {error ? (
        <ErrorMessage />
      ) : (
        <div className="w-full sm:w-[80%] px-4 sm:px-8 py-4 m-auto">
          {status === "pending" ? (
            <div className="flex justify-center">
              <LoadingSpinner />
            </div>
          ) : (
            <div className="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-7">
              {upcomingMovies.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
                  ? t("loading_more")
                  : hasNextPage
                  ? t("load_more")
                  : t("nothing_to_load")}
              </button>
            )}
          </div>
        </div>
      )}
    </div>
  );
};