Implemented side effect of selectedRegion using app listener

This commit is contained in:
Jason Zhu 2023-04-07 17:01:18 +10:00
parent 83ae2f34d7
commit a09463a2b4
2 changed files with 25 additions and 2 deletions

View File

@ -3,7 +3,6 @@ import {
pokeApiAllPagesCustomBaseQuery,
pokeApiFullListFetchArgs,
} from './paginationBaseQuery';
import { setFetchingRegionPokemonList } from './pokedexSlice';
import {
AreaResponseData,
LocationResponseData,

View File

@ -22,7 +22,8 @@ const sortOptions = [
{ name: 'ID', value: 'id' },
{ name: 'Name', value: 'name' },
];
pokedexApi.endpoints.getTypeList.initiate();
pokedexApi.endpoints.getTypeList.initiate(); // initialize type list fetching
// typesData will be used in Filters.tsx
const initialState: PokedexState = {
selectedRegion: '',
@ -88,3 +89,26 @@ 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));
},
});