-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.jsx
155 lines (136 loc) · 7.08 KB
/
App.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import React, {useEffect, useState} from 'react'
import Pagination from "./Pagination"
import axios from "axios"
import {
Typography,
AppBar,
Card,
CardActions,
CardContent,
CardMedia,
CssBaseline,
Grid,
Toolbar,
Container,
ButtonGroup, Button, makeStyles
} from '@material-ui/core'
import { CatchingPokemon } from '@mui/icons-material';
import useStyles from './styles'
const App = () => {
const classes = useStyles()
const [pokemon, setPokemon] = useState([])
const [sprites, setSprites] = useState([])
const [currentPageUrl, setCurrentPageUrl] = useState("https://pokeapi.co/api/v2/pokemon")
const [nextPageUrl, setNextPageUrl] = useState()
const [prevPageUrl, setPrevPageUrl] = useState()
const [loading, setLoading] = useState(true)
const [cards, setCards] = useState([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17])
useEffect(() => {
setLoading(true)
let cancel
axios.get(currentPageUrl, {
cancelToken: new axios.CancelToken(c => (cancel = c)),
})
.then(res => {
setLoading(false)
setNextPageUrl(res.data.next)
setPrevPageUrl(res.data.previous)
setPokemon(res.data.results.map(p => p.name))
const spritePromises = res.data.results.map(p => axios.get(p.url))
Promise.all(spritePromises)
.then(responses => {
const spriteArray = responses.map(response => {
const { sprites } = response.data
const frontSprite = sprites.front_default
return frontSprite
})
setSprites(spriteArray)
})
})
.catch(error => {
console.error('Error fetching Pokémon data:', error)
setLoading(false);
})
return () => cancel
}, [currentPageUrl])
function goToNextPage() {
setCurrentPageUrl(nextPageUrl)
}
function goToPrevPage() {
setCurrentPageUrl(prevPageUrl)
}
if(loading) return "Loading..."
return (
<div>
<CssBaseline />
<AppBar position={"relative"} style={{ backgroundColor: '#f6c833' }}>
<Toolbar >
<CatchingPokemon className={classes.icon} style={{ color: '#1386fe' }}/>
<Typography variant={"h6"} style={{ color: '#1386fe' }}>
Pokedex
</Typography>
</Toolbar>
</AppBar>
<main>
<div className={classes.container} style={{ backgroundColor: '#1386fe' }}>
<Container maxWidth={"sm"} >
<Typography variant={"h2"} align={"center"} color={"textPrimary"} style={{ color: 'white', fontWeight: 'bold' }} gutterBottom>
Pokedex
</Typography>
<Typography variant={"h5"} align={"center"} color={"textSecondary"} style={{ color: 'white' }} paragraph>
A cool Pokedex!
</Typography>
<div className={classes.buttons}>
<Grid container spacing={2} justify={"center"}>
<Grid item>
<Button variant={"contained"} color={"primary"} style={{ backgroundColor: '#f6c833', color: '#1386fe' }}>
View by generation
</Button>
</Grid>
</Grid>
</div>
</Container>
</div>
<Container className={classes.cardGrid} maxWidth={"md" } >
<Grid container spacing={10}>
{cards.map((card, index) => (
<Grid item key={card} xs={12} sm={6} md={5} lg={4}>
<Card className={classes.card}>
{sprites[index] ? (
<CardMedia
component={"img"}
className={classes.cardMedia}
image={sprites[index]}
title={"Image title"}
/>
) : (
<div>Loading sprite...</div>
)}
<CardContent className={classes.cardContent} style={{ backgroundColor: '#1386fe', display: 'flex', justifyContent: 'center', paddingTop: '8px', paddingBottom: '8px' }}>
<Typography gutterBottom variant={"h5"} style={{ color: 'white', fontWeight: 'bold', marginTop: '0', marginBottom: '0' }}>
{pokemon[card]}
</Typography>
</CardContent>
<CardActions style={{ backgroundColor: '#1386fe', display: 'flex', justifyContent: 'center', paddingTop: '4px', paddingBottom: '4px' }}>
<Button size={"small"} color={"primary"} style={{ backgroundColor: '#f6c833', width: '100%' }}>
More Info
</Button>
</CardActions>
</Card>
</Grid>
))}
</Grid>
</Container>
</main>
<footer className={classes.footer} style={{ backgroundColor: '#1386fe' }}>
<Typography variant={"h6"} align={"center"} gutterBottom>
<Pagination
goToNextPage={nextPageUrl ? goToNextPage : null}
goToPrevPage={prevPageUrl ? goToPrevPage : null}
/>
</Typography>
</footer>
</div>
)
}
export default App;