Put filter pokemonCard logic into redux
This commit is contained in:
parent
9cb23006ed
commit
e7e4e391d5
@ -3,44 +3,12 @@ import PokemonCard, { PokemonCardProps } from 'components/PokemonCard';
|
|||||||
import Loading from 'components/Loading';
|
import Loading from 'components/Loading';
|
||||||
|
|
||||||
import { useAppSelector, useAppDispatch } from 'app/hooks';
|
import { useAppSelector, useAppDispatch } from 'app/hooks';
|
||||||
import { fetchPokemonsInTheRegion } from './pokedexSlice';
|
import {
|
||||||
|
fetchPokemonsInTheRegion,
|
||||||
|
searchedSortedFilteredPokemonCardList,
|
||||||
|
} from './pokedexSlice';
|
||||||
import { fetchSelectedPokemonInfo } from 'features/InfoDialog/infoDialogSlice';
|
import { fetchSelectedPokemonInfo } from 'features/InfoDialog/infoDialogSlice';
|
||||||
|
|
||||||
export const filterPokemonCardsByType = (
|
|
||||||
pokemonList: PokemonCardProps[],
|
|
||||||
selectedType: string,
|
|
||||||
) => {
|
|
||||||
return pokemonList.filter(
|
|
||||||
pokemon =>
|
|
||||||
selectedType === 'All Types' ||
|
|
||||||
pokemon.types.some(type => type === selectedType),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export const sortPokemonCardsByIdOrName = (
|
|
||||||
pokemonList: PokemonCardProps[],
|
|
||||||
selectedSort: string,
|
|
||||||
) => {
|
|
||||||
return pokemonList.sort((a, b) => {
|
|
||||||
if (selectedSort === 'id') {
|
|
||||||
return a.id - b.id;
|
|
||||||
} else if (selectedSort === 'name') {
|
|
||||||
return a.name.localeCompare(b.name);
|
|
||||||
} else {
|
|
||||||
return 0;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
export const searchPokemonCardsByName = (
|
|
||||||
pokemonList: PokemonCardProps[],
|
|
||||||
searchInput: string,
|
|
||||||
) => {
|
|
||||||
return pokemonList.filter(pokemon =>
|
|
||||||
pokemon.name.toLowerCase().includes(searchInput.toLowerCase()),
|
|
||||||
);
|
|
||||||
};
|
|
||||||
|
|
||||||
export interface PokedexProps {
|
export interface PokedexProps {
|
||||||
selectedRegion: string;
|
selectedRegion: string;
|
||||||
selectedType: string;
|
selectedType: string;
|
||||||
@ -59,19 +27,8 @@ const Pokedex = ({
|
|||||||
const isLoadingPokemons = useAppSelector(
|
const isLoadingPokemons = useAppSelector(
|
||||||
state => state.pokedex.isLoadingPokemons,
|
state => state.pokedex.isLoadingPokemons,
|
||||||
);
|
);
|
||||||
const pokemonList = useAppSelector(state => state.pokedex.pokemonCardList);
|
const pokemonCardListForRendering = searchedSortedFilteredPokemonCardList(
|
||||||
|
useAppSelector(state => state),
|
||||||
const filteredPokemonList = filterPokemonCardsByType(
|
|
||||||
pokemonList,
|
|
||||||
selectedType,
|
|
||||||
);
|
|
||||||
const sortedFilteredPokemonCardList = sortPokemonCardsByIdOrName(
|
|
||||||
filteredPokemonList,
|
|
||||||
selectedSort,
|
|
||||||
);
|
|
||||||
const searchedPokemonCardList = searchPokemonCardsByName(
|
|
||||||
sortedFilteredPokemonCardList,
|
|
||||||
searchInput,
|
|
||||||
);
|
);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
@ -84,7 +41,7 @@ const Pokedex = ({
|
|||||||
<Loading />
|
<Loading />
|
||||||
) : (
|
) : (
|
||||||
<div className="all__pokemons">
|
<div className="all__pokemons">
|
||||||
{searchedPokemonCardList.map(pokemonCard => (
|
{pokemonCardListForRendering.map(pokemonCard => (
|
||||||
<PokemonCard
|
<PokemonCard
|
||||||
key={pokemonCard.id}
|
key={pokemonCard.id}
|
||||||
id={pokemonCard.id}
|
id={pokemonCard.id}
|
||||||
|
@ -2,7 +2,7 @@ import {
|
|||||||
filterPokemonCardsByType,
|
filterPokemonCardsByType,
|
||||||
sortPokemonCardsByIdOrName,
|
sortPokemonCardsByIdOrName,
|
||||||
searchPokemonCardsByName,
|
searchPokemonCardsByName,
|
||||||
} from 'features/Pokedex/Pokedex';
|
} from 'features/Pokedex/pokedexSlice';
|
||||||
import { PokemonCardProps } from 'components/PokemonCard';
|
import { PokemonCardProps } from 'components/PokemonCard';
|
||||||
import pokemon3_venusaur_card from 'features/Pokedex/__test__/pokemon3_venusaur_Card.json';
|
import pokemon3_venusaur_card from 'features/Pokedex/__test__/pokemon3_venusaur_Card.json';
|
||||||
import pokemon4_charmander_card from 'features/Pokedex/__test__/pokemon4_charmandar_Card.json';
|
import pokemon4_charmander_card from 'features/Pokedex/__test__/pokemon4_charmandar_Card.json';
|
@ -7,6 +7,7 @@ import { getStartAndEndIdsForRegion } from './utils';
|
|||||||
import { PokemonResponseData } from 'types/api';
|
import { PokemonResponseData } from 'types/api';
|
||||||
import { pokeRestApi } from 'app/services/pokeRestApi';
|
import { pokeRestApi } from 'app/services/pokeRestApi';
|
||||||
import { RootState } from 'app/store';
|
import { RootState } from 'app/store';
|
||||||
|
import { PokemonCardProps } from 'components/PokemonCard';
|
||||||
|
|
||||||
export const fetchPokemonsInTheRegion = createAsyncThunk<
|
export const fetchPokemonsInTheRegion = createAsyncThunk<
|
||||||
PokemonResponseData[],
|
PokemonResponseData[],
|
||||||
@ -66,3 +67,57 @@ export const pokedexSlice: Slice<PokedexStateProps> = createSlice({
|
|||||||
export const { setIsLoadingPokemons } = pokedexSlice.actions;
|
export const { setIsLoadingPokemons } = pokedexSlice.actions;
|
||||||
|
|
||||||
export default pokedexSlice.reducer;
|
export default pokedexSlice.reducer;
|
||||||
|
|
||||||
|
/// selectors
|
||||||
|
export const filterPokemonCardsByType = (
|
||||||
|
pokemonList: PokemonCardProps[],
|
||||||
|
selectedType: string,
|
||||||
|
) => {
|
||||||
|
return pokemonList.filter(
|
||||||
|
pokemon =>
|
||||||
|
selectedType === 'All Types' ||
|
||||||
|
pokemon.types.some(type => type === selectedType),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const sortPokemonCardsByIdOrName = (
|
||||||
|
pokemonList: PokemonCardProps[],
|
||||||
|
selectedSort: string,
|
||||||
|
) => {
|
||||||
|
return pokemonList.sort((a, b) => {
|
||||||
|
if (selectedSort === 'id') {
|
||||||
|
return a.id - b.id;
|
||||||
|
} else if (selectedSort === 'name') {
|
||||||
|
return a.name.localeCompare(b.name);
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
export const searchPokemonCardsByName = (
|
||||||
|
pokemonList: PokemonCardProps[],
|
||||||
|
searchInput: string,
|
||||||
|
) => {
|
||||||
|
return pokemonList.filter(pokemon =>
|
||||||
|
pokemon.name.toLowerCase().includes(searchInput.toLowerCase()),
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export const filteredPokemonListByType = (state: RootState) =>
|
||||||
|
filterPokemonCardsByType(
|
||||||
|
state.pokedex.pokemonCardList,
|
||||||
|
state.filter.selectedType,
|
||||||
|
);
|
||||||
|
|
||||||
|
export const sortedFilteredPokemonCardList = (state: RootState) =>
|
||||||
|
sortPokemonCardsByIdOrName(
|
||||||
|
filteredPokemonListByType(state),
|
||||||
|
state.filter.selectedSort,
|
||||||
|
);
|
||||||
|
|
||||||
|
export const searchedSortedFilteredPokemonCardList = (state: RootState) =>
|
||||||
|
searchPokemonCardsByName(
|
||||||
|
sortedFilteredPokemonCardList(state),
|
||||||
|
state.filter.searchInput,
|
||||||
|
);
|
||||||
|
Loading…
x
Reference in New Issue
Block a user