mirror of
https://github.com/mosh-hamedani/game-hub.git
synced 2026-05-21 12:14:32 +02:00
Refactor: Extract a query object
This commit is contained in:
parent
5c1f779e11
commit
08cbda491b
15
src/App.tsx
15
src/App.tsx
|
|
@ -7,9 +7,14 @@ import PlatformSelector from "./components/PlatformSelector";
|
|||
import { Platform } from "./hooks/useGames";
|
||||
import { Genre } from "./hooks/useGenres";
|
||||
|
||||
|
||||
export interface GameQuery {
|
||||
genre: Genre | null;
|
||||
platform: Platform | null;
|
||||
}
|
||||
|
||||
function App() {
|
||||
const [selectedGenre, setSelectedGenre] = useState<Genre | null>(null);
|
||||
const [selectedPlatform, setSelectedPlatform] = useState<Platform | null>(null);
|
||||
const [gameQuery, setGameQuery] = useState<GameQuery>({} as GameQuery);
|
||||
|
||||
return (
|
||||
<Grid
|
||||
|
|
@ -27,12 +32,12 @@ function App() {
|
|||
</GridItem>
|
||||
<Show above="lg">
|
||||
<GridItem area="aside" paddingX={5}>
|
||||
<GenreList selectedGenre={selectedGenre} onSelectGenre={(genre) => setSelectedGenre(genre)} />
|
||||
<GenreList selectedGenre={gameQuery.genre} onSelectGenre={(genre) => setGameQuery({ ...gameQuery, genre})} />
|
||||
</GridItem>
|
||||
</Show>
|
||||
<GridItem area="main">
|
||||
<PlatformSelector selectedPlatform={selectedPlatform} onSelectPlatform={(platform) => setSelectedPlatform(platform) } />
|
||||
<GameGrid selectedPlatform={selectedPlatform} selectedGenre={selectedGenre} />
|
||||
<PlatformSelector selectedPlatform={gameQuery.platform} onSelectPlatform={(platform) => setGameQuery({ ...gameQuery, platform}) } />
|
||||
<GameGrid gameQuery={gameQuery} />
|
||||
</GridItem>
|
||||
</Grid>
|
||||
);
|
||||
|
|
|
|||
|
|
@ -1,4 +1,5 @@
|
|||
import { SimpleGrid, Text } from "@chakra-ui/react";
|
||||
import { GameQuery } from "../App";
|
||||
import useGames, { Platform } from "../hooks/useGames";
|
||||
import { Genre } from "../hooks/useGenres";
|
||||
import GameCard from "./GameCard";
|
||||
|
|
@ -6,12 +7,11 @@ import GameCardContainer from "./GameCardContainer";
|
|||
import GameCardSkeleton from "./GameCardSkeleton";
|
||||
|
||||
interface Props {
|
||||
selectedGenre: Genre | null;
|
||||
selectedPlatform: Platform | null;
|
||||
gameQuery: GameQuery
|
||||
}
|
||||
|
||||
const GameGrid = ({ selectedGenre, selectedPlatform }: Props) => {
|
||||
const { data, error, isLoading } = useGames(selectedGenre, selectedPlatform);
|
||||
const GameGrid = ({ gameQuery }: Props) => {
|
||||
const { data, error, isLoading } = useGames(gameQuery);
|
||||
const skeletons = [1, 2, 3, 4, 5, 6];
|
||||
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
import { GameQuery } from "../App";
|
||||
import useData from "./useData";
|
||||
import { Genre } from "./useGenres";
|
||||
|
||||
|
|
@ -15,19 +16,16 @@ export interface Game {
|
|||
metacritic: number;
|
||||
}
|
||||
|
||||
const useGames = (
|
||||
selectedGenre: Genre | null,
|
||||
selectedPlatform: Platform | null
|
||||
) =>
|
||||
const useGames = (gameQuery: GameQuery) =>
|
||||
useData<Game>(
|
||||
"/games",
|
||||
{
|
||||
params: {
|
||||
genres: selectedGenre?.id,
|
||||
platforms: selectedPlatform?.id,
|
||||
genres: gameQuery.genre?.id,
|
||||
platforms: gameQuery.platform?.id,
|
||||
},
|
||||
},
|
||||
[selectedGenre?.id, selectedPlatform?.id]
|
||||
[gameQuery]
|
||||
);
|
||||
|
||||
export default useGames;
|
||||
|
|
|
|||
Loading…
Reference in a new issue