Complete InfoDialog feature, now we can click info icon on card to show modal

develop
Jason Zhu 2023-05-18 22:46:23 +10:00
parent 8607a8f1ad
commit ff05a9bf83
3 changed files with 56 additions and 47 deletions

View File

@ -3,6 +3,7 @@ import './App.css';
import Header from 'components/Header'; import Header from 'components/Header';
import Pokedex from 'features/Pokedex'; import Pokedex from 'features/Pokedex';
import Filters from 'features/Filters'; import Filters from 'features/Filters';
import InfoDialog from 'features/InfoDialog';
import { useAppSelector } from 'app/hooks'; import { useAppSelector } from 'app/hooks';
function App() { function App() {
@ -21,6 +22,7 @@ function App() {
selectedSort={selectedSort} selectedSort={selectedSort}
searchInput={selectedSearchInput} searchInput={selectedSearchInput}
/> />
<InfoDialog />
</div> </div>
); );
} }

View File

@ -1,23 +1,13 @@
import { useEffect } from 'react'; import { useAppSelector } from 'app/hooks';
import { useAppDispatch, useAppSelector } from 'app/hooks';
import InfoDialogComponent from 'components/InfoDialogComponent'; import InfoDialogComponent from 'components/InfoDialogComponent';
import {
useGetPokemonQuery,
useGetPokemonSpeciesQuery,
useGetEvolutionChainQuery,
} from 'app/services/pokeApi';
import { setPokemonSpeciesIdToFetch } from './infoDialogSlice';
export interface InfoDialogProps { export interface InfoDialogProps {
open: boolean; open: boolean;
pokemonId: string | number; pokemonId: string | number;
} }
const InfoDialog = ({ pokemonId }: InfoDialogProps) => { const InfoDialog = () => {
const dispatch = useAppDispatch();
const isOpen = useAppSelector(state => state.infoDialog.isOpen); const isOpen = useAppSelector(state => state.infoDialog.isOpen);
const selectedInfoDialogDetails = useAppSelector( const selectedInfoDialogDetails = useAppSelector(
state => state.infoDialog.InfoDialogDetails, state => state.infoDialog.InfoDialogDetails,

View File

@ -115,48 +115,65 @@ export const constructPokemonInfoFromResponses = (
export const fetchSelectedPokemonInfo = createAsyncThunk( export const fetchSelectedPokemonInfo = createAsyncThunk(
'infoDialog/fetchSelectedPokemonInfo', 'infoDialog/fetchSelectedPokemonInfo',
async (pokemonId: number, thunkAPI) => { async (pokemonId: number, thunkAPI) => {
const { data: selectedPokemon } = await pokeApi.useGetPokemonQuery( const dispatch = thunkAPI.dispatch;
pokemonId,
try {
const selectedPokemon = await dispatch(
pokeApi.endpoints.getPokemon.initiate(pokemonId),
); );
if (selectedPokemon && selectedPokemon.species) {
const { data: selectedPokemonSpecies } = if (selectedPokemon.data) {
await pokeApi.useGetPokemonSpeciesFromUrlQuery( const selectedPokemonSpecies = await dispatch(
selectedPokemon.species.url, pokeApi.endpoints.getPokemonSpeciesFromUrl.initiate(
selectedPokemon.data.species.url,
),
); );
if (selectedPokemonSpecies && selectedPokemonSpecies.evolution_chain) {
const { data: selectedPokemonEvolutionChain } = if (selectedPokemonSpecies.data) {
await pokeApi.useGetEvolutionChainFromUrlQuery( const selectedPokemonEvolutionChain = await dispatch(
selectedPokemonSpecies.evolution_chain.url, pokeApi.endpoints.getEvolutionChainFromUrl.initiate(
selectedPokemonSpecies.data.evolution_chain.url,
),
); );
if (selectedPokemonEvolutionChain) {
const evolutionChainNames = constructEvolutionChainFromResponse( if (selectedPokemonEvolutionChain.data) {
selectedPokemonEvolutionChain, const evolutionChainName = constructEvolutionChainFromResponse(
selectedPokemonEvolutionChain.data,
); );
// for each name in evolutionChain, fetch the pokemon // for each name in evolutionChain, fetch the pokemon
const evolutionChain: EvolutionSpeciesProps[] = []; const evolutionChain: EvolutionSpeciesProps[] = [];
evolutionChainNames.map(async name => { await Promise.all(
const { data: evolutionChainPokemon } = evolutionChainName.map(async name => {
await pokeApi.useGetPokemonQuery(name); const evolutionChainPokemon = await dispatch(
if (evolutionChainPokemon) { pokeApi.endpoints.getPokemon.initiate(name),
);
if (evolutionChainPokemon.data) {
evolutionChain.push({ evolutionChain.push({
types: evolutionChainPokemon.types.map(type => type.type.name), types: evolutionChainPokemon.data.types.map(
name: evolutionChainPokemon.name, type => type.type.name,
),
name: evolutionChainPokemon.data.name,
image_url: image_url:
evolutionChainPokemon.sprites.other.dream_world.front_default, evolutionChainPokemon.data.sprites.other.dream_world
.front_default,
}); });
} }
}); }),
const selectedPokemonInfo = constructPokemonInfoFromResponses(
selectedPokemon,
selectedPokemonSpecies,
evolutionChain,
); );
const selectedPokemonInfo = constructPokemonInfoFromResponses(
selectedPokemon.data,
selectedPokemonSpecies.data,
evolutionChain,
);
return selectedPokemonInfo; return selectedPokemonInfo;
} }
} }
} }
} catch (err) {
return thunkAPI.rejectWithValue(err);
}
return null; return null;
}, },