Filter games by platform

This commit is contained in:
Mosh Hamedani 2023-03-02 11:25:18 -08:00
parent 577c8c75a6
commit 5c1f779e11
4 changed files with 33 additions and 14 deletions

View file

@ -4,11 +4,13 @@ import GameGrid from "./components/GameGrid";
import GenreList from "./components/GenreList";
import NavBar from "./components/NavBar";
import PlatformSelector from "./components/PlatformSelector";
import { Platform } from "./hooks/useGames";
import { Genre } from "./hooks/useGenres";
function App() {
const [selectedGenre, setSelectedGenre] = useState<Genre | null>(null);
const [selectedPlatform, setSelectedPlatform] = useState<Platform | null>(null);
return (
<Grid
templateAreas={{
@ -29,8 +31,8 @@ function App() {
</GridItem>
</Show>
<GridItem area="main">
<PlatformSelector />
<GameGrid selectedGenre={selectedGenre} />
<PlatformSelector selectedPlatform={selectedPlatform} onSelectPlatform={(platform) => setSelectedPlatform(platform) } />
<GameGrid selectedPlatform={selectedPlatform} selectedGenre={selectedGenre} />
</GridItem>
</Grid>
);

View file

@ -1,16 +1,17 @@
import { SimpleGrid, Text } from "@chakra-ui/react";
import useGames from "../hooks/useGames";
import useGames, { Platform } from "../hooks/useGames";
import { Genre } from "../hooks/useGenres";
import GameCard from "./GameCard";
import GameCardContainer from "./GameCardContainer";
import GameCardSkeleton from "./GameCardSkeleton";
interface Props {
selectedGenre: Genre | null
selectedGenre: Genre | null;
selectedPlatform: Platform | null;
}
const GameGrid = ({ selectedGenre }: Props) => {
const { data, error, isLoading } = useGames(selectedGenre);
const GameGrid = ({ selectedGenre, selectedPlatform }: Props) => {
const { data, error, isLoading } = useGames(selectedGenre, selectedPlatform);
const skeletons = [1, 2, 3, 4, 5, 6];
return (

View file

@ -1,8 +1,14 @@
import { Button, Menu, MenuButton, MenuItem, MenuList } from "@chakra-ui/react";
import { BsChevronDown } from "react-icons/bs";
import { Platform } from "../hooks/useGames";
import usePlatforms from "../hooks/usePlatforms";
const PlatformSelector = () => {
interface Props {
onSelectPlatform: (platform: Platform) => void;
selectedPlatform: Platform | null;
}
const PlatformSelector = ({ onSelectPlatform, selectedPlatform }: Props) => {
const { data, error } = usePlatforms();
if (error) return null;
@ -10,10 +16,10 @@ const PlatformSelector = () => {
return (
<Menu>
<MenuButton as={Button} rightIcon={<BsChevronDown />}>
Platforms
{selectedPlatform?.name || 'Platforms'}
</MenuButton>
<MenuList>
{data.map(platform => <MenuItem key={platform.id}>{platform.name}</MenuItem>)}
{data.map(platform => <MenuItem onClick={() => onSelectPlatform(platform)} key={platform.id}>{platform.name}</MenuItem>)}
</MenuList>
</Menu>
);

View file

@ -15,9 +15,19 @@ export interface Game {
metacritic: number;
}
const useGames = (selectedGenre: Genre | null) =>
useData<Game>("/games", { params: { genres: selectedGenre?.id } }, [
selectedGenre?.id,
]);
const useGames = (
selectedGenre: Genre | null,
selectedPlatform: Platform | null
) =>
useData<Game>(
"/games",
{
params: {
genres: selectedGenre?.id,
platforms: selectedPlatform?.id,
},
},
[selectedGenre?.id, selectedPlatform?.id]
);
export default useGames;