From 3281629dcf1312ec4ab339daaa7555ad58838804 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Mon, 17 Apr 2023 22:52:54 +1000 Subject: [PATCH] Implemented search in the filter bar --- src/features/Pokedex/Filters/Filters.tsx | 10 +++++++ src/features/Pokedex/Pokedex.tsx | 16 +++++++++- src/features/Pokedex/__test__/pokedex.test.ts | 29 +++++++++++++++++++ src/features/Pokedex/pokedexSlice.ts | 5 ++++ src/features/Pokedex/types/slice.ts | 7 +++-- 5 files changed, 63 insertions(+), 4 deletions(-) diff --git a/src/features/Pokedex/Filters/Filters.tsx b/src/features/Pokedex/Filters/Filters.tsx index 0be10bc..ced5313 100644 --- a/src/features/Pokedex/Filters/Filters.tsx +++ b/src/features/Pokedex/Filters/Filters.tsx @@ -8,6 +8,7 @@ import { setRegionOptions, setSortOptions, setTypeOptions, + setSearchInput, } from 'features/Pokedex/pokedexSlice'; import { RegionPokemonRange } from 'features/Pokedex/types/slice'; import { useAppDispatch, useAppSelector } from 'app/hooks'; @@ -141,6 +142,15 @@ const Filters = () => { +
+
+
SEARCH
+ dispatch(setSearchInput(e.target.value))} + /> +
+
); diff --git a/src/features/Pokedex/Pokedex.tsx b/src/features/Pokedex/Pokedex.tsx index 921a2f4..fae58c7 100644 --- a/src/features/Pokedex/Pokedex.tsx +++ b/src/features/Pokedex/Pokedex.tsx @@ -31,12 +31,22 @@ export const sortPokemonCardsByIdOrName = ( }); }; +export const searchPokemonCardsByName = ( + pokemonList: PokemonCardProps[], + searchInput: string, +) => { + return pokemonList.filter(pokemon => + pokemon.name.toLowerCase().includes(searchInput.toLowerCase()), + ); +}; + const Pokedex = () => { const isLoadingPokemons = useAppSelector( state => state.pokedex.isLoadingPokemons, ); const selectedType = useAppSelector(state => state.pokedex.selectedType); const selectedSort = useAppSelector(state => state.pokedex.selectedSort); + const searchInput = useAppSelector(state => state.pokedex.searchInput); const pokemonList = useAppSelector(state => state.pokedex.pokemonCardList); @@ -48,6 +58,10 @@ const Pokedex = () => { filteredPokemonList, selectedSort, ); + const searchedPokemonCardList = searchPokemonCardsByName( + sortedFilteredPokemonCardList, + searchInput, + ); return ( <> @@ -56,7 +70,7 @@ const Pokedex = () => { ) : (
- {sortedFilteredPokemonCardList.map(pokemonCard => ( + {searchedPokemonCardList.map(pokemonCard => ( { expect(sortedList).toEqual([pokemon4_Charmander, pokemon3_Venusaur]); }); }); + + describe('searchPokemonByName works correctly', () => { + beforeEach(() => { + store = configureStore({ + reducer: { + pokedex: pokedexSlice.reducer, + [pokedexApi.reducerPath]: pokedexApi.reducer, + }, + middleware: getDefaultMiddleware => + getDefaultMiddleware().concat( + pokedexApi.middleware, + listenerMiddleware.middleware, + ), + }); + }); + + const pokemonList: PokemonResponseData[] = [ + pokemon3_Venusaur, + pokemon4_Charmander, + ]; + + it('should search by name correctly', () => { + const searchName = 'char'; + const searchedList = searchPokemonCardsByName(pokemonList, searchName); + expect(searchedList).toHaveLength(1); + expect(searchedList[0]).toEqual(pokemon4_Charmander); + }); + }); }); diff --git a/src/features/Pokedex/pokedexSlice.ts b/src/features/Pokedex/pokedexSlice.ts index fb42c25..aa345f3 100644 --- a/src/features/Pokedex/pokedexSlice.ts +++ b/src/features/Pokedex/pokedexSlice.ts @@ -50,6 +50,7 @@ const initialState: PokedexState = { selectedRegion: '', selectedType: '', selectedSort: '', + searchInput: '', isLoadingPokemons: true, pokemonCardList: [], }; @@ -79,6 +80,9 @@ export const pokedexSlice: Slice = createSlice({ ) => { state.sortOptions = action.payload; }, + setSearchInput: (state, action: PayloadAction) => { + state.searchInput = action.payload; + }, setIsLoadingPokemons: (state, action: PayloadAction) => { state.isLoadingPokemons = action.payload; }, @@ -118,6 +122,7 @@ export const { setRegionOptions, setTypeOptions, setSortOptions, + setSearchInput, setIsLoadingPokemons, } = pokedexSlice.actions; diff --git a/src/features/Pokedex/types/slice.ts b/src/features/Pokedex/types/slice.ts index d4f899c..2782b77 100644 --- a/src/features/Pokedex/types/slice.ts +++ b/src/features/Pokedex/types/slice.ts @@ -2,12 +2,13 @@ import { PokemonResponseData } from './api'; import { PokemonCardProps } from '../PokemonCard'; export type PokedexState = { - selectedRegion: string; regionOptions: RegionPokemonRange[]; - selectedType: string; typeOptions: string[]; - selectedSort: string; sortOptions: { name: string; value: string }[]; + selectedRegion: string; + selectedType: string; + selectedSort: string; + searchInput: string; isLoadingPokemons: boolean; pokemonCardList: PokemonCardProps[]; };