diff --git a/index.html b/index.html index 1a1ad4e..7e4d6be 100644 --- a/index.html +++ b/index.html @@ -4,7 +4,7 @@ @@ -12,16 +12,16 @@ - + - + diff --git a/public/omdb-card.webp b/public/omdb-card.webp new file mode 100644 index 0000000..10582df Binary files /dev/null and b/public/omdb-card.webp differ diff --git a/src/api/film.ts b/src/api/film.ts index 285a963..013ed63 100644 --- a/src/api/film.ts +++ b/src/api/film.ts @@ -56,6 +56,7 @@ export const searchFilms = query( count: 0, }; } + return maybeFilms.data; } ); diff --git a/src/pages/explore/index.tsx b/src/pages/explore/index.tsx index de3b6d4..6ac9e6c 100644 --- a/src/pages/explore/index.tsx +++ b/src/pages/explore/index.tsx @@ -1,4 +1,4 @@ -import { useCallback, useEffect, useMemo, useState, type FC } from "react"; +import { useCallback, useEffect, useMemo, useState, type FC, type SetStateAction } from "react"; import { useSearchParams } from "react-router-dom"; import useQuery, { preload } from "swr"; import { searchFilms } from "../../api/film"; @@ -13,8 +13,14 @@ import PageControls from "./PageControls"; const ExplorePage: FC = () => { const [params, setParams] = useSearchParams(); - const [q, setQ] = useState(params.get("q") ?? ""); - const [page, setPage] = useState(1); + const q = useMemo( + () => params.get("q") ?? "", + [params] + ); + const page = useMemo( + () => maybeInt.parse(params.get("page")) ?? 1, + [params] + ); const sort = useMemo( () => (params.get("sort") as Sorting) ?? "RELEASED_ASC", [params] @@ -37,6 +43,18 @@ const ExplorePage: FC = () => { } ); + const finalPage = useMemo(() => Math.ceil((data?.count ?? page) / 6), [data, page]) + + const setPage = useCallback((newPage: SetStateAction) => { + setParams(prev => { + if (prev.has("page")) { + prev.delete("page"); + } + prev.append("page", `${typeof newPage === "function" ? newPage(page) : newPage}`); + return prev; + }); + }, [page]); + const setParamByKey = useCallback( (key: string, value: string) => { setParams((prev) => { @@ -70,10 +88,17 @@ const ExplorePage: FC = () => { ); }, [q, sort, page]); + useEffect(() => { + if (page > finalPage) { + setPage(Math.max(1, finalPage)); + } + }, [finalPage, page]) + + return (
- + setParamByKey("q", query)} /> setParamByKey("sort", sort)} @@ -123,7 +148,7 @@ const ExplorePage: FC = () => {