-
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.
feat(app): move table & add pokemon card
- Loading branch information
Facundo Ledesma
authored and
Facundo Ledesma
committed
Dec 30, 2022
1 parent
af7bf7f
commit 67f4b05
Showing
15 changed files
with
355 additions
and
218 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#root { | ||
.app { | ||
width: 80%; | ||
margin: 0 auto; | ||
padding: 2rem; | ||
padding: 2rem 0; | ||
text-align: center; | ||
} |
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,78 +1,56 @@ | ||
import React from 'react'; | ||
import { useSinglePokemon } from '@components/hooks/useSinglePokemon'; | ||
import { Indeterminate } from '@faculedesma/ledesma-lib'; | ||
import React, { useState } from 'react'; | ||
import { ISinglePokemonParsed } from 'src/types'; | ||
import './card.scss'; | ||
|
||
interface IPokemonCardProps { | ||
pokemonId: string; | ||
interface ICardProps { | ||
pokemon: ISinglePokemonParsed; | ||
} | ||
|
||
export const PokemonCard = ({ pokemonId }: IPokemonCardProps): JSX.Element => { | ||
const { data, isLoading, isError } = useSinglePokemon(pokemonId); | ||
export const Card: React.FC<ICardProps> = ({ pokemon }): JSX.Element => { | ||
const [isImageLoaded, setIsImageLoaded] = useState<boolean>(false); | ||
|
||
if (isLoading) { | ||
return <div>Loading...</div>; | ||
} | ||
const handleImageLoaded = (): void => setIsImageLoaded(true); | ||
|
||
if (isError) { | ||
return <div>Error!</div>; | ||
} | ||
|
||
const pokemon = data; | ||
|
||
return ( | ||
<div className={`card`}> | ||
<div className={`card-header`}> | ||
return pokemon !== undefined ? ( | ||
<div className="card"> | ||
<div className="card-header"> | ||
<div className="image-container"> | ||
<img src={pokemon?.imageSrc} alt="header-card" /> | ||
<img | ||
src={pokemon?.imageSrc} | ||
alt="poke-default" | ||
onLoad={handleImageLoaded} | ||
/> | ||
{!isImageLoaded && <Indeterminate />} | ||
</div> | ||
</div> | ||
<div className={`card-content`}> | ||
<div> | ||
<div className="card-content"> | ||
<div className="top"> | ||
<b>{pokemon?.name}</b> | ||
<span style={{ marginLeft: 10, opacity: 0.5 }}> | ||
{pokemon?.stats.hp}Hp | ||
</span> | ||
<span className="hp">{pokemon?.stats.hp}Hp</span> | ||
</div> | ||
<div> | ||
<span style={{ transform: 'translateY(40%)', opacity: 0.5 }}> | ||
{pokemon?.stats.experience}Exp | ||
</span> | ||
<div className="bottom"> | ||
<span className="exp">{pokemon?.stats.experience}Exp</span> | ||
</div> | ||
</div> | ||
<div className={`card-footer`}> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center' | ||
}} | ||
> | ||
<b style={{ fontSize: 16 }}>{pokemon?.stats.attack}K</b> | ||
<p style={{ fontSize: 12, opacity: 0.5 }}>Attack</p> | ||
<div className="card-footer"> | ||
<div className="stat"> | ||
<b>{pokemon?.stats.attack}K</b> | ||
<p>Attack</p> | ||
</div> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center' | ||
}} | ||
> | ||
<b style={{ fontSize: 16 }}>{pokemon?.stats.specialAttack}K</b> | ||
<p style={{ fontSize: 12, opacity: 0.5 }}>Special</p> | ||
<div className="stat"> | ||
<b>{pokemon?.stats.specialAttack}K</b> | ||
<p>Special</p> | ||
</div> | ||
<div | ||
style={{ | ||
display: 'flex', | ||
flexDirection: 'column', | ||
alignItems: 'center' | ||
}} | ||
> | ||
<b style={{ fontSize: 16 }}>{pokemon?.stats.defense}K</b> | ||
<p style={{ fontSize: 12, opacity: 0.5 }}>Defense</p> | ||
<div className="stat"> | ||
<b>{pokemon?.stats.defense}K</b> | ||
<p>Defense</p> | ||
</div> | ||
</div> | ||
</div> | ||
) : ( | ||
<>No pokemon selected</> | ||
); | ||
}; | ||
|
||
export default PokemonCard; | ||
export default Card; |
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,110 +1,19 @@ | ||
import React, { useState } from 'react'; | ||
import { | ||
Button, | ||
Table, | ||
TableRow, | ||
TableHeaderCell, | ||
TableCell | ||
} from '@faculedesma/ledesma-lib'; | ||
import PokemonCard from '@components/card/Card'; | ||
import { usePokemons } from '@components/hooks/usePokemons'; | ||
import { capitalizeFirstLetter } from '@utils/utils'; | ||
import HomeTable from './HomeTable'; | ||
import HomeCard from './HomeCard'; | ||
import './home.scss'; | ||
|
||
const getPokemonIdFromURL = (url: string): string => { | ||
const parts = url.split('/'); | ||
return parts[parts.length - 2]; | ||
}; | ||
|
||
const Home = (): JSX.Element => { | ||
const [offset, setOffset] = useState<number>(0); | ||
const [selectedPokemonId, setSelectedPokemonId] = useState< | ||
string | undefined | ||
>(undefined); | ||
|
||
const { data, isLoading, isError } = usePokemons(offset); | ||
|
||
if (isLoading) { | ||
return <div>Loading...</div>; | ||
} | ||
|
||
if (isError) { | ||
return <div>Error!</div>; | ||
} | ||
|
||
const handleSelectRow = (id: string): void => setSelectedPokemonId(id); | ||
|
||
const handlePrev = (): void => setOffset(offset - 10); | ||
|
||
const handleNext = (): void => setOffset(offset + 10); | ||
|
||
if (data !== undefined) { | ||
const columns = Object.keys(data.results[0]).map((column) => | ||
capitalizeFirstLetter(column) | ||
); | ||
const rows = data.results.map((row: any) => [ | ||
capitalizeFirstLetter(row.name), | ||
row.url | ||
]); | ||
const Home: React.FC = (): JSX.Element => { | ||
const [selectedPokemonId, setSelectedPokemonId] = useState<string>(''); | ||
|
||
return ( | ||
<div className="home"> | ||
<Table> | ||
<> | ||
<thead> | ||
<TableRow> | ||
<> | ||
{columns?.map((name, columnIndex) => { | ||
return ( | ||
<TableHeaderCell key={columnIndex}> | ||
{name} | ||
</TableHeaderCell> | ||
); | ||
})} | ||
</> | ||
</TableRow> | ||
</thead> | ||
<tbody> | ||
{rows?.map((row: any, rowIndex: number) => { | ||
return ( | ||
<TableRow | ||
key={rowIndex} | ||
onClick={() => handleSelectRow(getPokemonIdFromURL(row[1]))} | ||
> | ||
<> | ||
{row.map((data: any, rowDataIndex: number) => { | ||
return <TableCell key={rowDataIndex}>{data}</TableCell>; | ||
})} | ||
</> | ||
</TableRow> | ||
); | ||
})} | ||
</tbody> | ||
</> | ||
</Table> | ||
<div> | ||
<Button onClick={handlePrev} disabled={offset === 0}> | ||
Previous | ||
</Button> | ||
<Button | ||
onClick={handleNext} | ||
disabled={offset === Math.trunc(data.count / 10) + 1} | ||
> | ||
Next | ||
</Button> | ||
<span>Page: {offset / 10 + 1}</span> | ||
<span>Pages: {Math.trunc(data.count / 10) + 1}</span> | ||
</div> | ||
{selectedPokemonId != null ? ( | ||
<div> | ||
<PokemonCard pokemonId={selectedPokemonId} /> | ||
</div> | ||
) : null} | ||
</div> | ||
); | ||
} | ||
const handleSelectPokemonId = (id: string): void => setSelectedPokemonId(id); | ||
|
||
return <></>; | ||
return ( | ||
<div className="home"> | ||
<HomeTable setPokemonId={handleSelectPokemonId} /> | ||
<HomeCard pokemonId={selectedPokemonId} /> | ||
</div> | ||
); | ||
}; | ||
|
||
export default Home; |
Oops, something went wrong.