-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Evolutions display as chains, but not as trees
RIP Eevee
- Loading branch information
1 parent
970cdfd
commit e5b59c2
Showing
7 changed files
with
84 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
//////////////////////////////////////////////////////////////////////////////// | ||
export const getIdFromUrl = (url: string): number => parseInt(url.replace(/^.*\/(\d+)\//, '$1')); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,47 @@ | ||
import {useEvolutionsByIdQuery} from '@/redux/slices/pokeapi.slice.ts'; | ||
import {FunctionComponent, useEffect} from 'react'; | ||
import {BasicPokemonInfo} from '@/types/pokemon.type.ts'; | ||
import {getIdFromUrl} from '@/utilities/get-id-from-url.function.ts'; | ||
import {ChainLink} from 'pokenode-ts'; | ||
import {Fragment, FunctionComponent, useEffect, useState} from 'react'; | ||
import {Link} from 'react-router-dom'; | ||
|
||
//////////////////////////////////////////////////////////////////////////////// | ||
export const EvolutionsViewer: FunctionComponent<{id: number;}> = props => { | ||
const {data: evolutions, error, isLoading: loading} = useEvolutionsByIdQuery(props.id); | ||
useEffect(() => console.debug(evolutions), [evolutions]); | ||
|
||
// // // // // // // // // // // // // // // // // // // // | ||
const {data: evolutions} = useEvolutionsByIdQuery(props.id); | ||
// useEffect(() => console.debug(evolutions), [evolutions]); | ||
const [chain, setChain] = useState([] as Array<BasicPokemonInfo>); | ||
|
||
/** Convert the raw chain information into something we can actually display. */ | ||
const compileChain = (): void => { | ||
if(!evolutions) return; | ||
const newChain: Array<BasicPokemonInfo> = []; | ||
|
||
const addToChain = (link: ChainLink): void => { | ||
newChain.push({ | ||
id: getIdFromUrl(link.species.url), | ||
name: link.species.name, | ||
}); | ||
if(link.evolves_to?.[0]) addToChain(link.evolves_to[0]); //FIXME: This won't work for the Eeveelutions. | ||
}; | ||
addToChain(evolutions.chain); | ||
setChain(newChain); | ||
}; | ||
useEffect(compileChain, [evolutions]); | ||
|
||
// // // // // // // // // // // // // // // // // // // // | ||
return <> | ||
{loading ? <> | ||
{/* Load silently. */} | ||
</> : error ? <> | ||
{/* Fail silently. */} | ||
</> : !evolutions ? <> | ||
{/* Fail silently. */} | ||
</> : <> | ||
<ul className="evolutions-viewer"> | ||
{/* TODO */} | ||
</ul> | ||
</>} | ||
<span className="evolutions-viewer"> | ||
{chain.map((link, index) => ( | ||
<Fragment key={index}> | ||
<Link to={`/pokemon?id=${link.id}`}> | ||
{link.name} | ||
</Link> | ||
{index < chain.length - 1 ? ', ' : ''} | ||
{index === chain.length - 1 && chain.length > 1 ? '.' : ''} | ||
</Fragment> | ||
))} | ||
</span> | ||
</>; | ||
}; |