From 1801e4319269bf6b788a470c8a5d8de533f26666 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Tue, 11 Apr 2023 18:33:34 +1000 Subject: [PATCH] Recreate useGetRegionOptions and useGetSortOptions again for getting region and sort options in Filter.tsx --- src/features/Pokedex/Filters/Filters.tsx | 46 +++++++++++++++++++----- src/features/Pokedex/pokedexSlice.ts | 39 ++++++++------------ 2 files changed, 53 insertions(+), 32 deletions(-) diff --git a/src/features/Pokedex/Filters/Filters.tsx b/src/features/Pokedex/Filters/Filters.tsx index ff7734e..4122892 100644 --- a/src/features/Pokedex/Filters/Filters.tsx +++ b/src/features/Pokedex/Filters/Filters.tsx @@ -5,6 +5,8 @@ import { setSelectedType, setSelectedSort, fetchPokemonsInTheRegion, + setRegionOptions, + setSortOptions, } from 'features/Pokedex/pokedexSlice'; import { RegionPokemonRange } from 'features/Pokedex/types/slice'; import { useAppDispatch, useAppSelector } from 'app/hooks'; @@ -25,6 +27,29 @@ export const createRegionPokemonListOptionElements = ( }); }; +const useGetRegionOptions = () => { + const data: RegionPokemonRange[] = [ + { region: 'kanto', startId: 1, endId: 151 }, + { region: 'johto', startId: 152, endId: 251 }, + { region: 'hoenn', startId: 252, endId: 386 }, + { region: 'sinnoh', startId: 387, endId: 493 }, + { region: 'unova', startId: 494, endId: 649 }, + { region: 'kalos', startId: 650, endId: 721 }, + { region: 'alola', startId: 722, endId: 809 }, + { region: 'galar', startId: 810, endId: 898 }, + ]; + return { data: data }; +}; + +const useGetSortOptions = () => { + const data = [ + { name: 'ID', value: 'id' }, + { name: 'Name', value: 'name' }, + ]; + + return { data: data }; +}; + const Filters = () => { const dispatch = useAppDispatch(); const selectedRegion = useAppSelector(state => state.pokedex.selectedRegion); @@ -34,13 +59,18 @@ const Filters = () => { const regionPokemonList = useAppSelector( state => state.pokedex.regionOptions, ); - const sortOptions = useAppSelector(state => state.pokedex.sortOptions); - const { data: typesData, isLoading: typesLoading } = useGetTypeListQuery(); + const { data: fetchedRegionOptions } = useGetRegionOptions(); + const { data: fetchedTypeOptions, isLoading: isFetchingTypeOptions } = + useGetTypeListQuery(); + const { data: fetchedSortOptions } = useGetSortOptions(); useEffect(() => { - dispatch(setSelectedRegion('kanto')); - dispatch(fetchPokemonsInTheRegion('kanto')); + dispatch(setRegionOptions(fetchedRegionOptions)); + dispatch(setSortOptions(fetchedSortOptions)); + + dispatch(setSelectedRegion(fetchedRegionOptions[0].region)); + dispatch(fetchPokemonsInTheRegion(fetchedRegionOptions[0].region)); }, []); const optionElements = @@ -72,10 +102,10 @@ const Filters = () => { onChange={e => dispatch(setSelectedType(e.target.value))} value={selectedType} > - {typesLoading ? ( + {isFetchingTypeOptions ? ( ) : ( - typesData?.results.map(type => ( + fetchedTypeOptions?.results.map(type => ( @@ -89,11 +119,11 @@ const Filters = () => {
SORT BY