From 183ce62f30e714303c3d96347baae74aa26c8cc5 Mon Sep 17 00:00:00 2001 From: Jason Zhu Date: Tue, 11 Apr 2023 17:31:48 +1000 Subject: [PATCH] Changed pokedex state variable name; And remove unnecessary listener middleware --- src/features/Pokedex/Filters/Filters.tsx | 12 ++++++-- src/features/Pokedex/pokedexSlice.ts | 35 ++++-------------------- src/features/Pokedex/types/slice.ts | 6 ++-- 3 files changed, 19 insertions(+), 34 deletions(-) diff --git a/src/features/Pokedex/Filters/Filters.tsx b/src/features/Pokedex/Filters/Filters.tsx index 473504a..b31de1b 100644 --- a/src/features/Pokedex/Filters/Filters.tsx +++ b/src/features/Pokedex/Filters/Filters.tsx @@ -32,12 +32,20 @@ const Filters = () => { const selectedSort = useAppSelector(state => state.pokedex.selectedSort); const regionPokemonList = useAppSelector( - state => state.pokedex.regionPokemonIdsList, + state => state.pokedex.regionOptions, ); - const sortOptions = useAppSelector(state => state.pokedex.sortList); + const sortOptions = useAppSelector(state => state.pokedex.sortOptions); const { data: typesData, isLoading: typesLoading } = useGetTypeListQuery(); + useEffect(() => { + console.log('Filters.tsx useEffect'); + }, []); + + useEffect(() => { + console.log('REGION changed'); + }, [selectedRegion]); + const optionElements = createRegionPokemonListOptionElements(regionPokemonList); diff --git a/src/features/Pokedex/pokedexSlice.ts b/src/features/Pokedex/pokedexSlice.ts index f72ca72..0886061 100644 --- a/src/features/Pokedex/pokedexSlice.ts +++ b/src/features/Pokedex/pokedexSlice.ts @@ -54,12 +54,12 @@ export const fetchPokemonsInTheRegion = createAsyncThunk( ); const initialState: PokedexState = { + regionOptions: regionPokemonRange, + typeOptions: [], + sortOptions: sortOptions, selectedRegion: '', - regionPokemonIdsList: regionPokemonRange, selectedType: '', - typeList: [], selectedSort: '', - sortList: sortOptions, isLoadingPokemons: true, pokemonList: [], }; @@ -83,7 +83,7 @@ export const pokedexSlice: Slice = createSlice({ state, action: PayloadAction, ) => { - state.regionPokemonIdsList = action.payload; + state.regionOptions = action.payload; }, setIsLoadingPokemons: (state, action: PayloadAction) => { state.isLoadingPokemons = action.payload; @@ -92,7 +92,7 @@ export const pokedexSlice: Slice = createSlice({ state.pokemonList = action.payload; }, setTypeList: (state, action: PayloadAction) => { - state.typeList = action.payload; + state.typeOptions = action.payload; }, }, extraReducers: builder => { @@ -106,7 +106,7 @@ export const pokedexSlice: Slice = createSlice({ (state, action) => { if (action.payload && action.payload.results.length > 0) { const regionListResults = action.payload.results; - state.typeList = regionListResults.map(region => region.name); + state.typeOptions = regionListResults.map(region => region.name); state.selectedType = action.payload.results[0].name; } @@ -124,26 +124,3 @@ export const { } = pokedexSlice.actions; export default pokedexSlice.reducer; - -// Use second option of organizing listeners method in RTK doc -startAppListening({ - actionCreator: setSelectedRegion, - effect: async (action, { dispatch, getState }) => { - const selectedRegion = getState().pokedex.selectedRegion; - const regionPokemonList = getState().pokedex.regionPokemonIdsList; - console.log('regionPokemonList', regionPokemonList); - const { startId, endId } = getStartAndEndIdsForRegion( - selectedRegion, - regionPokemonList, - ); - console.log('startId', startId); - console.log('endId', endId); - const pokemonIdsToFetch = Array.from( - { length: endId - startId + 1 }, - (_, i) => i + startId, - ); - console.log('pokemonIdsToFetch', pokemonIdsToFetch); - - dispatch(setIsLoadingPokemons(true)); - }, -}); diff --git a/src/features/Pokedex/types/slice.ts b/src/features/Pokedex/types/slice.ts index 7e97c60..6a65986 100644 --- a/src/features/Pokedex/types/slice.ts +++ b/src/features/Pokedex/types/slice.ts @@ -2,11 +2,11 @@ import { PokemonResponseData } from './api'; export type PokedexState = { selectedRegion: string; - regionPokemonIdsList: RegionPokemonRange[]; + regionOptions: RegionPokemonRange[]; selectedType: string; - typeList: string[]; + typeOptions: string[]; selectedSort: string; - sortList: { name: string; value: string }[]; + sortOptions: { name: string; value: string }[]; isLoadingPokemons: boolean; pokemonList: PokemonResponseData[]; };