Skip to content

Commit

Permalink
Se renderiza vista de detalles
Browse files Browse the repository at this point in the history
  • Loading branch information
DannitaZz committed Feb 1, 2022
1 parent b6202ae commit 827c65b
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 6 deletions.
2 changes: 1 addition & 1 deletion src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ function App() {
<BrowserRouter>
<Routes>
<Route exact path='/' element={<Pokemon favs={state.favIds} actionName={'setMainPage'} data={state.data} page={state.mainPage.page} dispatch={dispatch} />} />
{/* <Route exact path='/details' element={<Details state={state} dispatch={dispatch} />} /> */}
<Route exact path='/details/:id' element={<Details pokemon={state.pokemon} dispatch={dispatch} />} />
<Route exact path='/favorites' element={<Pokemon favs={state.favIds} actionName={'setFavPage'} data={state.favs} page={state.favPage.page} dispatch={dispatch} />} />
{/* <Route exact path='/favorites' element={<Favorites state={state} dispatch={dispatch} />} /> */}
</Routes>
Expand Down
6 changes: 4 additions & 2 deletions src/components/pages/details.jsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import Pokedetails from "../pokedetails"


function Details({state, dispatch}) {

function Details({pokemon, dispatch}) {

return (
<>
<h1>Vista detalles</h1>
<Pokedetails />
<Pokedetails pokemon={pokemon} dispatch={dispatch}/>
</>
)
}
Expand Down
69 changes: 67 additions & 2 deletions src/components/pokedetails.jsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,72 @@
function Pokedetails() {
import { useParams } from 'react-router-dom';
import { useEffect } from "react";
import axios from 'axios';
import { Typography, Card, CardContent, CardMedia } from '@mui/material';


function Pokedetails({ pokemon, dispatch }) {
const { id } = useParams();
function upperFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
const checkState = (state) => {
try {
return state.name
}
catch {
return 'loading'
}
}
useEffect(() => {
async function getPokeData() {
try {
const response = await axios.get(`https://pokeapi.co/api/v2/pokemon/${id}/`);
const fulldata = response.data;
console.log('Pokemon: ', fulldata);
dispatch({ type: 'getPokemon', pokemon: fulldata });
} catch (error) {
console.error(error);
}
}
getPokeData();

}
, [])

return (
<p>Detalles de Pokémon</p>
<>

<div>
<Card sx={{ with: '80vw', height: '92vh', display: 'flex', justifyContent: 'center' }}>
<CardContent sx={{ align: 'center' }}>
<Typography component="div" variant="h4" textAlign='center'>
Pokemon #{id} {upperFirstLetter(pokemon.name)}
</Typography>
<CardMedia
component="img"
sx={{ height: '40vh', align: 'center' }}
image={pokemon && pokemon.sprites.other["official-artwork"].front_default}
alt="pokemon"
/>
<Typography component="div" variant="p" textAlign='center'>

<hr></hr>
Type: {pokemon && pokemon.types.map((type, i)=> {
return (<li key={'t'+ i}>{type.type.name}</li>)
})}
<hr></hr>
STATS: {pokemon && pokemon.stats.map((stat, i) => {
return <>
<li key={'p' + i}>{stat.stat.name}: {stat.base_stat}</li>
</>
})}


</Typography>
</CardContent>
</Card>
</div>
</>
)
}

Expand Down
10 changes: 9 additions & 1 deletion src/components/pokelist.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@ import Button from '@mui/material/Button';
import Typography from '@mui/material/Typography';
import StarBorderSharpIcon from '@mui/icons-material/StarBorderSharp';
import StarIcon from '@mui/icons-material/Star';
import { useNavigate } from "react-router-dom";


function Pokelist({ favs, data, page, pageSize, dispatch }) {

const navigateTo = useNavigate();
const pageData = data.slice((page-1)*pageSize, page*pageSize)

function upperFirstLetter(str) {
return str.charAt(0).toUpperCase() + str.slice(1);
}
function handleClick(e) {
const id = e.currentTarget.value
console.log('id', id)
navigateTo(`details/${id}`);
}
return (
<div style={{ width: '100vw', display: 'flex', flexDirection: 'row', flexFlow: 'row wrap', justifyContent: 'center' }}>
{pageData && pageData.map((pokemon, i) => {
Expand Down Expand Up @@ -42,7 +50,7 @@ function Pokelist({ favs, data, page, pageSize, dispatch }) {
return <StarBorderSharpIcon onClick={(e) => dispatch({ type: 'setFav', value: pokemon.id })} />
}
})()}
<Button size="small">Detalles</Button>
<Button size="small" value={pokemon.id} onClick={(e) => handleClick(e)}>Detalles</Button>
</CardActions>
</Card>
)
Expand Down
10 changes: 10 additions & 0 deletions src/hooks/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ export const initialState = {
data: [],
favs: [],
favIds: [],
pokemon: {
name: 'loading',
types: [{type: {name: 'loading', url: 'url'}}],
stats: [{'base_stat': '50', stat: {name: 'loading', url: 'url'}}, {'base_stat': '50', stat: {name: 'loading', url: 'url'}}, {'base_stat': '50', stat: {name: 'loading', url: 'url'}}, {'base_stat': '50', stat: {name: 'loading', url: 'url'}}, {'base_stat': '50', stat: {name: 'loading', url: 'url'}}, {'base_stat': '50', stat: {name: 'loading', url: 'url'}}],
sprites: {other:{'official-artwork': { front_default: 'loading'}}}
},
mainPage: {
currentData:[],
page: 1,
Expand Down Expand Up @@ -32,6 +38,10 @@ export const reducer = (state, action) => {
return {...state, mainPage: {...state.mainPage, page: action.page}}
case 'setFavPage':
return {...state, favPage: {...state.favPage, page: action.page}}
case 'getPokemon':
const pokemon = action.pokemon;
console.log('El pokemon es: ', pokemon.name)
return {...state, pokemon: pokemon}
case 'setFav':
const id = action.value;
const index = id - 1;
Expand Down

0 comments on commit 827c65b

Please sign in to comment.