From 83ae2f34d789d3f1526180ecb3ba216b3ce88f14 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Thu, 6 Apr 2023 23:57:05 +1000 Subject: [PATCH] Change Redux code style by move all slice initialization process into pokedexSlice, Component are majorly for presentation --- src/features/Pokedex/Filters/Filters.tsx | 88 +++++------------------- src/features/Pokedex/Pokedex.tsx | 24 ++++--- src/features/Pokedex/pokedexSlice.ts | 38 +++++++++- src/features/Pokedex/types/slice.ts | 2 + 4 files changed, 70 insertions(+), 82 deletions(-) diff --git a/src/features/Pokedex/Filters/Filters.tsx b/src/features/Pokedex/Filters/Filters.tsx index 629f0b5..2af4832 100644 --- a/src/features/Pokedex/Filters/Filters.tsx +++ b/src/features/Pokedex/Filters/Filters.tsx @@ -4,33 +4,10 @@ import { setSelectedRegion, setSelectedType, setSelectedSort, - setRegionPokemonIdsList, } from 'features/Pokedex/pokedexSlice'; import { RegionPokemonRange } from 'features/Pokedex/types/slice'; import { useAppDispatch, useAppSelector } from 'app/hooks'; -const useGetSortOptions = () => { - const sortOptions = [ - { name: 'ID', value: 'id' }, - { name: 'Name', value: 'name' }, - ]; - return { data: sortOptions }; -}; - -export const useGetRegionPokemons = () => { - 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 }; -}; - export const createRegionPokemonListOptionElements = ( data: RegionPokemonRange[], ) => { @@ -49,54 +26,20 @@ export const createRegionPokemonListOptionElements = ( const Filters = () => { const dispatch = useAppDispatch(); + const selectedRegion = useAppSelector(state => state.pokedex.selectedRegion); + const selectedType = useAppSelector(state => state.pokedex.selectedType); + const selectedSort = useAppSelector(state => state.pokedex.selectedSort); - const handleRegionChange = (event: React.ChangeEvent) => { - dispatch(setSelectedRegion(event.target.value)); - }; - const handleTypeChange = (event: React.ChangeEvent) => { - dispatch(setSelectedType(event.target.value)); - }; - const handleSortChange = (event: React.ChangeEvent) => { - dispatch(setSelectedSort(event.target.value)); - }; + const regionPokemonList = useAppSelector( + state => state.pokedex.regionPokemonIdsList, + ); + const sortOptions = useAppSelector(state => state.pokedex.sortList); const { data: typesData, isLoading: typesLoading } = useGetTypeListQuery(); - const { data: sortOptions } = useGetSortOptions(); - const { data: regionPokemonListData } = useGetRegionPokemons(); - // Action when loading the component - useEffect(() => { - dispatch(setRegionPokemonIdsList(regionPokemonListData)); + const optionElements = + createRegionPokemonListOptionElements(regionPokemonList); - const initailRegion = Object.keys(regionPokemonListData)[0]; - if (initailRegion) { - const initialEvent = { - target: { value: initailRegion }, - } as React.ChangeEvent; - handleRegionChange(initialEvent); - } - - if (sortOptions && sortOptions.length > 0) { - const initialSortEvent = { - target: { value: sortOptions[0].value }, - } as React.ChangeEvent; - handleSortChange(initialSortEvent); - } - }, []); - - // Send the first type as the default selected type - useEffect(() => { - if (typesData && typesData.results.length > 0) { - const initialTypeEvent = { - target: { value: typesData.results[0].name }, - } as React.ChangeEvent; - handleTypeChange(initialTypeEvent); - } - }, [typesData]); - - const optionElements = createRegionPokemonListOptionElements( - regionPokemonListData, - ); return ( <>
@@ -105,8 +48,8 @@ const Filters = () => {
REGION
@@ -115,7 +58,11 @@ const Filters = () => {
TYPE
- dispatch(setSelectedType(e.target.value))} + value={selectedType} + > {typesLoading ? ( ) : ( @@ -134,7 +81,8 @@ const Filters = () => {