Complete InfoDialog feature, now we can click info icon on card to show modal
parent
8607a8f1ad
commit
ff05a9bf83
|
@ -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>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
@ -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,
|
||||||
|
|
|
@ -115,47 +115,64 @@ 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 {
|
||||||
if (selectedPokemon && selectedPokemon.species) {
|
const selectedPokemon = await dispatch(
|
||||||
const { data: selectedPokemonSpecies } =
|
pokeApi.endpoints.getPokemon.initiate(pokemonId),
|
||||||
await pokeApi.useGetPokemonSpeciesFromUrlQuery(
|
);
|
||||||
selectedPokemon.species.url,
|
|
||||||
|
if (selectedPokemon.data) {
|
||||||
|
const selectedPokemonSpecies = await dispatch(
|
||||||
|
pokeApi.endpoints.getPokemonSpeciesFromUrl.initiate(
|
||||||
|
selectedPokemon.data.species.url,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
if (selectedPokemonSpecies && selectedPokemonSpecies.evolution_chain) {
|
|
||||||
const { data: selectedPokemonEvolutionChain } =
|
|
||||||
await pokeApi.useGetEvolutionChainFromUrlQuery(
|
|
||||||
selectedPokemonSpecies.evolution_chain.url,
|
|
||||||
);
|
|
||||||
if (selectedPokemonEvolutionChain) {
|
|
||||||
const evolutionChainNames = constructEvolutionChainFromResponse(
|
|
||||||
selectedPokemonEvolutionChain,
|
|
||||||
);
|
|
||||||
// for each name in evolutionChain, fetch the pokemon
|
|
||||||
const evolutionChain: EvolutionSpeciesProps[] = [];
|
|
||||||
evolutionChainNames.map(async name => {
|
|
||||||
const { data: evolutionChainPokemon } =
|
|
||||||
await pokeApi.useGetPokemonQuery(name);
|
|
||||||
if (evolutionChainPokemon) {
|
|
||||||
evolutionChain.push({
|
|
||||||
types: evolutionChainPokemon.types.map(type => type.type.name),
|
|
||||||
name: evolutionChainPokemon.name,
|
|
||||||
image_url:
|
|
||||||
evolutionChainPokemon.sprites.other.dream_world.front_default,
|
|
||||||
});
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
const selectedPokemonInfo = constructPokemonInfoFromResponses(
|
if (selectedPokemonSpecies.data) {
|
||||||
selectedPokemon,
|
const selectedPokemonEvolutionChain = await dispatch(
|
||||||
selectedPokemonSpecies,
|
pokeApi.endpoints.getEvolutionChainFromUrl.initiate(
|
||||||
evolutionChain,
|
selectedPokemonSpecies.data.evolution_chain.url,
|
||||||
|
),
|
||||||
);
|
);
|
||||||
|
|
||||||
return selectedPokemonInfo;
|
if (selectedPokemonEvolutionChain.data) {
|
||||||
|
const evolutionChainName = constructEvolutionChainFromResponse(
|
||||||
|
selectedPokemonEvolutionChain.data,
|
||||||
|
);
|
||||||
|
|
||||||
|
// for each name in evolutionChain, fetch the pokemon
|
||||||
|
const evolutionChain: EvolutionSpeciesProps[] = [];
|
||||||
|
await Promise.all(
|
||||||
|
evolutionChainName.map(async name => {
|
||||||
|
const evolutionChainPokemon = await dispatch(
|
||||||
|
pokeApi.endpoints.getPokemon.initiate(name),
|
||||||
|
);
|
||||||
|
if (evolutionChainPokemon.data) {
|
||||||
|
evolutionChain.push({
|
||||||
|
types: evolutionChainPokemon.data.types.map(
|
||||||
|
type => type.type.name,
|
||||||
|
),
|
||||||
|
name: evolutionChainPokemon.data.name,
|
||||||
|
image_url:
|
||||||
|
evolutionChainPokemon.data.sprites.other.dream_world
|
||||||
|
.front_default,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
|
||||||
|
const selectedPokemonInfo = constructPokemonInfoFromResponses(
|
||||||
|
selectedPokemon.data,
|
||||||
|
selectedPokemonSpecies.data,
|
||||||
|
evolutionChain,
|
||||||
|
);
|
||||||
|
return selectedPokemonInfo;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
return thunkAPI.rejectWithValue(err);
|
||||||
}
|
}
|
||||||
|
|
||||||
return null;
|
return null;
|
||||||
|
|
Loading…
Reference in New Issue