Compare commits

...

2 Commits

4 changed files with 36 additions and 11 deletions

View File

@ -3,13 +3,24 @@ import './App.css';
import Header from 'components/Header'; import Header from 'components/Header';
import Pokedex from 'features/Pokedex'; import Pokedex from 'features/Pokedex';
import Filters from 'features/Filters'; import Filters from 'features/Filters';
import { useAppSelector } from 'app/hooks';
function App() { function App() {
const selectedRegion = useAppSelector(state => state.filter.selectedRegion);
const selectedType = useAppSelector(state => state.filter.selectedType);
const selectedSort = useAppSelector(state => state.filter.selectedSort);
const selectedSearchInput = useAppSelector(state => state.filter.searchInput);
return ( return (
<div className="App app_container"> <div className="App app_container">
<Header /> <Header />
<Filters /> <Filters />
<Pokedex /> <Pokedex
selectedRegion={selectedRegion}
selectedType={selectedType}
selectedSort={selectedSort}
searchInput={selectedSearchInput}
/>
</div> </div>
); );
} }

View File

@ -1,12 +1,12 @@
import { import {
useGetRegionPokemons, useGetRegionOptions,
createRegionPokemonListOptionElements, createRegionPokemonListOptionElements,
} from './Filters'; } from './Filters';
describe('Filters', () => { describe('Filters', () => {
describe('test utility functions', () => { describe('test utility functions', () => {
test('createOptionElements works correctly', () => { test('createOptionElements works correctly', () => {
const { data } = useGetRegionPokemons(); const { data } = useGetRegionOptions();
const optionElements = createRegionPokemonListOptionElements(data); const optionElements = createRegionPokemonListOptionElements(data);
expect(optionElements[0].props.children).toBe('Kanto (1-151)'); expect(optionElements[0].props.children).toBe('Kanto (1-151)');
expect(optionElements[1].props.children).toBe('Johto (152-251)'); expect(optionElements[1].props.children).toBe('Johto (152-251)');

View File

@ -31,7 +31,7 @@ export const createRegionPokemonListOptionElements = (
}); });
}; };
const useGetRegionOptions = () => { export const useGetRegionOptions = () => {
const data: RegionPokemonRange[] = [ const data: RegionPokemonRange[] = [
{ region: 'kanto', startId: 1, endId: 151 }, { region: 'kanto', startId: 1, endId: 151 },
{ region: 'johto', startId: 152, endId: 251 }, { region: 'johto', startId: 152, endId: 251 },
@ -73,7 +73,6 @@ const Filters = () => {
dispatch(setSortOptions(fetchedSortOptions)); dispatch(setSortOptions(fetchedSortOptions));
dispatch(setSelectedRegion(fetchedRegionOptions[0].region)); dispatch(setSelectedRegion(fetchedRegionOptions[0].region));
dispatch(fetchPokemonsInTheRegion(fetchedRegionOptions[0].region));
dispatch(setSelectedSort(fetchedSortOptions[0].value)); dispatch(setSelectedSort(fetchedSortOptions[0].value));
}, []); }, []);

View File

@ -1,8 +1,9 @@
import React from 'react'; import React, { useEffect } from 'react';
import PokemonCard, { PokemonCardProps } from 'components/PokemonCard'; import PokemonCard, { PokemonCardProps } from 'components/PokemonCard';
import Loading from 'components/Loading'; import Loading from 'components/Loading';
import { useAppSelector } from 'app/hooks'; import { useAppSelector, useAppDispatch } from 'app/hooks';
import { fetchPokemonsInTheRegion } from './pokedexSlice';
export const filterPokemonCardsByType = ( export const filterPokemonCardsByType = (
pokemonList: PokemonCardProps[], pokemonList: PokemonCardProps[],
@ -39,10 +40,20 @@ export const searchPokemonCardsByName = (
); );
}; };
const Pokedex = () => { export interface PokedexProps {
const selectedType = useAppSelector(state => state.filter.selectedType); selectedRegion: string;
const selectedSort = useAppSelector(state => state.filter.selectedSort); selectedType: string;
const searchInput = useAppSelector(state => state.filter.searchInput); selectedSort: string;
searchInput: string;
}
const Pokedex = ({
selectedRegion,
selectedType,
selectedSort,
searchInput,
}: PokedexProps) => {
const dispatch = useAppDispatch();
const isLoadingPokemons = useAppSelector( const isLoadingPokemons = useAppSelector(
state => state.pokedex.isLoadingPokemons, state => state.pokedex.isLoadingPokemons,
@ -62,6 +73,10 @@ const Pokedex = () => {
searchInput, searchInput,
); );
useEffect(() => {
dispatch(fetchPokemonsInTheRegion(selectedRegion));
}, [selectedRegion]);
return ( return (
<> <>
{isLoadingPokemons ? ( {isLoadingPokemons ? (