1
- import { useState , useReducer } from "react" ;
1
+ import { useState , useReducer , useEffect } from "react" ;
2
2
import GameCard from "./components/GameCard" ;
3
3
import "./App.css" ;
4
4
import { Navbar , Nav , NavDropdown , Row , Col , Container } from "react-bootstrap" ;
5
+ import axios from "./axios" ;
6
+ import { propTypes } from "react-bootstrap/esm/Image" ;
5
7
6
8
const defaultPokedex = [
7
9
{
@@ -71,8 +73,9 @@ function App() {
71
73
const [ score , setScore ] = useState ( 0 ) ;
72
74
const [ highScore , setHS ] = useState ( 0 ) ;
73
75
const [ pokedex , setPokedex ] = useState ( defaultPokedex ) ;
74
- // const [pokimonToPlay, setPokimonToPlay ] = useState();
76
+ const [ imageCache , setImageCache ] = useState ( { } ) ;
75
77
const [ pokemonToDisplay , setPokemonToDisply ] = useState ( defaultPokedex ) ;
78
+ const [ isLoading , setLoading ] = useState ( null ) ;
76
79
77
80
function isHighScore ( newScore ) {
78
81
if ( newScore > highScore ) setHS ( newScore ) ;
@@ -95,6 +98,25 @@ function App() {
95
98
setPokemonToDisply ( array ) ;
96
99
forceUpdate ( ) ;
97
100
}
101
+ useEffect ( ( ) => {
102
+ async function cacheImages ( ) {
103
+ try {
104
+ let cacheImages = { } ;
105
+ pokedex . map ( async ( pokemon ) => {
106
+ const response = await axios . get ( pokemon . name ) ;
107
+ const img_url = response . data . sprites . other [ "official-artwork" ] ;
108
+ cacheImages [ pokemon . name ] = img_url . front_default ;
109
+ console . log ( img_url . front_default ) ;
110
+ console . log ( cacheImages [ pokemon . name ] ) ;
111
+ } ) ;
112
+ setImageCache ( cacheImages ) ;
113
+ } catch ( error ) {
114
+ console . error ( error ) ;
115
+ }
116
+ }
117
+ cacheImages ( ) ;
118
+ setLoading ( false ) ;
119
+ } , [ pokedex ] )
98
120
99
121
const handleClick = ( pokemonID ) => {
100
122
let newState = ( pokedex . map ( pokemon => {
@@ -113,10 +135,10 @@ function App() {
113
135
< >
114
136
< Navbar expand = "lg" className = "bg-body-tertiary" pb = { 4 } >
115
137
< Container >
116
- < Navbar . Brand href = "#home" > React-Bootstrap </ Navbar . Brand >
138
+ < Navbar . Brand href = "#home" > Pokemon Memorization </ Navbar . Brand >
117
139
< Navbar . Toggle aria-controls = "basic-navbar-nav" />
118
140
< Navbar . Collapse id = "basic-navbar-nav" >
119
- < Nav className = "me-auto" >
141
+ { /* <Nav className="me-auto">
120
142
<Nav.Link href="#home">Home</Nav.Link>
121
143
<Nav.Link href="#link">Link</Nav.Link>
122
144
<NavDropdown title="Dropdown" id="basic-nav-dropdown">
@@ -130,7 +152,7 @@ function App() {
130
152
Separated link
131
153
</NavDropdown.Item>
132
154
</NavDropdown>
133
- </ Nav >
155
+ </Nav> */ }
134
156
</ Navbar . Collapse >
135
157
< Navbar . Collapse className = "justify-content-end" >
136
158
< Col >
@@ -150,13 +172,15 @@ function App() {
150
172
151
173
</ Navbar >
152
174
< Container >
153
- < Row className = "justify-content-md-center" >
175
+ < Row className = "justify-content-md-center pt-5 " >
154
176
{ pokemonToDisplay . map ( ( pokemon , index ) => (
155
177
< GameCard
156
178
key = { index }
157
179
name = { pokemon . name }
158
180
handleClick = { handleClick }
181
+ images = { imageCache }
159
182
id = { pokemon . id }
183
+ isLoading = { isLoading }
160
184
/>
161
185
) ) }
162
186
</ Row >
0 commit comments