diff --git a/src/App.css b/src/App.css index a936122..1495cb2 100644 --- a/src/App.css +++ b/src/App.css @@ -23,9 +23,9 @@ body.dark { } .title { + margin: 10px; text-align: center; color: #000000; - margin-bottom: 30px; font-size: 2.5rem; } @@ -40,7 +40,7 @@ body.dark { background-color: #2a2a2a; padding: 20px; border-radius: 8px; - margin-bottom: 30px; + margin-bottom: 10px; } .main-input { diff --git a/src/MainPage.tsx b/src/MainPage.tsx index cd398e8..bc55283 100644 --- a/src/MainPage.tsx +++ b/src/MainPage.tsx @@ -1,10 +1,4 @@ -import React, { - useRef, - useState, - useEffect, - useCallback, - useContext, -} from "react"; +import React, { useRef, useEffect, useContext } from "react"; import { useParams, useSearchParams, @@ -12,7 +6,6 @@ import { useNavigate, } from "react-router-dom"; import "./App.css"; -import { PlanetApi } from "./PlanetFetch"; import Planets from "./Planets"; import Button from "./Button"; import Input from "./Input"; @@ -24,23 +17,22 @@ import { useDispatch } from "react-redux"; import { addItem, removeItem, stateItems } from "./store/selectedItemsSlice"; import SelectedPlanets from "./SelectedPlanets/SelectedPlanets.tsx"; import { useAppSelector } from "./hooks/hooks.ts"; +import { useGetPlanetsQuery } from "./PlanetRTKQuery"; + const planetsPerPage = 10; function MainPage(): React.ReactElement { const navigate = useNavigate(); const { pageNumber } = useParams(); + const currentPage = parseInt(pageNumber || "1", 10); const initialRender = useRef(true); const [searchParams] = useSearchParams(); const searchQuery = searchParams.get("search") || ""; - const currentPage = parseInt(pageNumber || "1", 10); const localStorageKey: string = "starWarsQuery"; const [inputValue, setInputValue] = useLocalStorage( localStorageKey, searchQuery, ); - const [searchPlanets, setPlanets] = useState([]); - const [isLoading, setIsLoading] = useState(false); - const [totalPlanets, setTotalPlanets] = useState(0); const { theme } = useContext(ThemeContext) || {}; @@ -48,6 +40,14 @@ function MainPage(): React.ReactElement { const selectedItems = useAppSelector(stateItems); + const { + data: { planets = [], total_records = 0 } = {}, + error, + isLoading, + isFetching, + refetch, + } = useGetPlanetsQuery({ page: currentPage, search: searchQuery }); + const toggleItemSelection = (item: PlanetsListItem) => { if (selectedItems.some((selected) => selected.uid === item.uid)) { dispatch(removeItem(item.uid)); @@ -56,32 +56,6 @@ function MainPage(): React.ReactElement { } }; - const getPlanets = useCallback(async () => { - let errorMessage = "Unknown error"; - setIsLoading(true); - setError(null); - try { - const planets = await PlanetApi.fetchPlanets(searchQuery, currentPage); - const firstPlanetsIndex = (currentPage - 1) * planetsPerPage; - const lastPlanetsIndex = firstPlanetsIndex + planetsPerPage; - const currentPlanets = planets.planets.slice( - firstPlanetsIndex, - lastPlanetsIndex, - ); - setTotalPlanets(planets.count); - setPlanets(currentPlanets); - } catch (error) { - if (error instanceof Error) { - errorMessage = error.message; - } else if (typeof error === "string") { - errorMessage = error; - } - setError(errorMessage); - } finally { - setIsLoading(false); - } - }, [currentPage, searchQuery]); - useEffect(() => { if (initialRender.current) { initialRender.current = false; @@ -91,12 +65,6 @@ function MainPage(): React.ReactElement { } }, [inputValue, searchQuery, setInputValue]); - useEffect(() => { - getPlanets(); - }, [getPlanets, searchQuery]); - - const [error, setError] = useState(null); - function handleSearch() { navigate(`/list/1?search=${encodeURIComponent(inputValue)}`); } @@ -108,15 +76,17 @@ function MainPage(): React.ReactElement { return ( <>

Star Wars Planets

- {error ?
{error}
: null} + {error ? ( +
{JSON.stringify(error)}
+ ) : null}
)} {!isLoading && } diff --git a/src/Pagination.css b/src/Pagination.css index 089db56..5de3264 100644 --- a/src/Pagination.css +++ b/src/Pagination.css @@ -1,7 +1,7 @@ .pagination-container { display: flex; gap: 8px; - justify-content: center; + justify-content: flex-end; align-items: center; background-color: #000; padding: 16px; @@ -31,3 +31,23 @@ background-color: #a0910f; color: #000; } + +.pagination-container button{ + margin-left:20%; + right: 0; + color: #a0910f; + background-color: transparent; + border: 1px solid #a0910f; + border-radius: 4px; + padding: 8px 12px; + cursor: pointer; + min-width: 36px; + text-decoration: none; + text-align: center; + font-weight: bold; +} + +.pagination-container button:hover { + background-color: #a0910f; + color: #000; +} diff --git a/src/Pagination.tsx b/src/Pagination.tsx index 7de7a52..0f91cac 100644 --- a/src/Pagination.tsx +++ b/src/Pagination.tsx @@ -1,12 +1,14 @@ import { NavLink } from "react-router-dom"; import "./Pagination.css"; import { useParams } from "react-router-dom"; +import Button from "./Button"; interface PaginationProps { planetsPerPage: number; totalPlanets: number; currentPage: number; searchQuery: string; + refetch: () => object; } export default function Pagination({ @@ -14,6 +16,7 @@ export default function Pagination({ totalPlanets, currentPage, searchQuery, + refetch, }: PaginationProps): React.ReactElement { const pageNumbers = []; const { planetId } = useParams(); @@ -32,6 +35,7 @@ export default function Pagination({ {number} ))} +
); } diff --git a/src/PlanetCard.tsx b/src/PlanetCard.tsx index 85e9a30..b11cd7b 100644 --- a/src/PlanetCard.tsx +++ b/src/PlanetCard.tsx @@ -1,15 +1,8 @@ -import React, { useEffect, useState } from "react"; -import type { PlanetProperties } from "./types"; import Spinner from "./Spinner"; -import { PlanetApi } from "./PlanetFetch"; import { useParams, useNavigate } from "react-router-dom"; +import { useGetPlanetDetailsQuery } from "./PlanetRTKQuery"; function PlanetCard(): React.ReactElement { - const [error, setError] = useState(null); - const [isLoadingDetail, setIsLoadingDetail] = useState(false); - const [selectedPlanet, setSelectedPlanet] = useState( - null, - ); const { planetId, pageNumber } = useParams(); const navigate = useNavigate(); @@ -17,36 +10,22 @@ function PlanetCard(): React.ReactElement { navigate(pageNumber ? `/list/${pageNumber}` : "/"); }; - useEffect(() => { - const getPlanetSelect = async () => { - let errorMessage = "Unknown error"; - try { - if (!planetId) return null; - setIsLoadingDetail(true); - const planetUrl = `https://swapi.tech/api/planets/${planetId}`; - const planetDetails = await PlanetApi.fetchPlanetDetail(planetUrl); - setSelectedPlanet(planetDetails); - } catch (error) { - if (error instanceof Error) { - errorMessage = error.message; - } else if (typeof error === "string") { - errorMessage = error; - } - setError(errorMessage); - } finally { - setIsLoadingDetail(false); - } - }; - getPlanetSelect(); - }, [planetId]); + const { + data: selectedPlanet, + error, + isLoading, + isFetching, + } = useGetPlanetDetailsQuery(planetId); return (
- {isLoadingDetail ? ( + {isLoading || isFetching ? ( ) : ( <> - {error ?
{error}
: null} + {error ? ( +
{JSON.stringify(error)}
+ ) : null} {selectedPlanet && ( <>