Skip to content

Commit 454f949

Browse files
committed
Host on GH
1 parent fd1dd5d commit 454f949

File tree

3 files changed

+59
-26
lines changed

3 files changed

+59
-26
lines changed

src/App.jsx

+30-6
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1-
import { useState, useReducer } from "react";
1+
import { useState, useReducer, useEffect } from "react";
22
import GameCard from "./components/GameCard";
33
import "./App.css";
44
import { Navbar, Nav, NavDropdown, Row, Col, Container } from "react-bootstrap";
5+
import axios from "./axios";
6+
import { propTypes } from "react-bootstrap/esm/Image";
57

68
const defaultPokedex = [
79
{
@@ -71,8 +73,9 @@ function App() {
7173
const [score, setScore] = useState(0);
7274
const [highScore, setHS] = useState(0);
7375
const [pokedex, setPokedex] = useState(defaultPokedex);
74-
// const [pokimonToPlay, setPokimonToPlay] = useState();
76+
const [imageCache, setImageCache] = useState({});
7577
const [pokemonToDisplay, setPokemonToDisply] = useState(defaultPokedex);
78+
const [isLoading, setLoading] = useState(null);
7679

7780
function isHighScore(newScore) {
7881
if (newScore > highScore) setHS(newScore);
@@ -95,6 +98,25 @@ function App() {
9598
setPokemonToDisply(array);
9699
forceUpdate();
97100
}
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])
98120

99121
const handleClick = (pokemonID) => {
100122
let newState = (pokedex.map(pokemon => {
@@ -113,10 +135,10 @@ function App() {
113135
<>
114136
<Navbar expand="lg" className="bg-body-tertiary" pb={4}>
115137
<Container>
116-
<Navbar.Brand href="#home">React-Bootstrap</Navbar.Brand>
138+
<Navbar.Brand href="#home">Pokemon Memorization</Navbar.Brand>
117139
<Navbar.Toggle aria-controls="basic-navbar-nav" />
118140
<Navbar.Collapse id="basic-navbar-nav">
119-
<Nav className="me-auto">
141+
{/* <Nav className="me-auto">
120142
<Nav.Link href="#home">Home</Nav.Link>
121143
<Nav.Link href="#link">Link</Nav.Link>
122144
<NavDropdown title="Dropdown" id="basic-nav-dropdown">
@@ -130,7 +152,7 @@ function App() {
130152
Separated link
131153
</NavDropdown.Item>
132154
</NavDropdown>
133-
</Nav>
155+
</Nav> */}
134156
</Navbar.Collapse>
135157
<Navbar.Collapse className="justify-content-end">
136158
<Col>
@@ -150,13 +172,15 @@ function App() {
150172

151173
</Navbar>
152174
<Container>
153-
<Row className="justify-content-md-center">
175+
<Row className="justify-content-md-center pt-5">
154176
{pokemonToDisplay.map((pokemon, index) => (
155177
<GameCard
156178
key={index}
157179
name={pokemon.name}
158180
handleClick={handleClick}
181+
images={imageCache}
159182
id={pokemon.id}
183+
isLoading={isLoading}
160184
/>
161185
))}
162186
</Row>

src/components/GameCard.jsx

+28-19
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,52 @@
11
import { useState, useEffect } from "react";
2-
import { Card, Col } from "react-bootstrap";
3-
import axios from "../axios";
2+
import { Card, Col, Placeholder } from "react-bootstrap";
3+
// import axios from "../axios";
44

55
//Display picture of pokemon
66
//Display pokemon name
77
export default function GameCard(props) {
88
const [sprite, setSprite] = useState([]);
99
const [isLoading, setLoading] = useState(null);
1010

11-
useEffect(() => {
12-
async function fetchData() {
13-
try {
14-
const response = await axios.get(props.name);
15-
setSprite(response.data.sprites.other["official-artwork"]);
16-
} catch (error) {
17-
console.error(error);
18-
}
19-
}
20-
fetchData();
11+
// useEffect(() => {
12+
// async function fetchData() {
13+
// try {
14+
// const response = await axios.get(props.name);
15+
// setSprite(response.data.sprites.other["official-artwork"]);
16+
// } catch (error) {
17+
// console.error(error);
18+
// }
19+
// }
20+
// fetchData();
2121

22-
setLoading(false);
23-
}, [props.name]);
22+
// setLoading(false);
23+
// }, [props.name]);
2424

25-
if (isLoading) {
25+
if (props.isLoading) {
2626
return <p>Loading...</p>;
2727
} else {
2828
return (
2929
<Col md={"auto"}>
30-
<Card
31-
style={{ width: "14rem", height: "15rem" }}
30+
{props.isLoading ? (
31+
<Card style={{ width: '18rem' }}>
32+
<Card.Img variant="top" src="holder.js/100px180" />
33+
<Card.Body>
34+
<Placeholder as={Card.Title} animation="glow">
35+
<Placeholder xs={6} />
36+
</Placeholder>
37+
</Card.Body>
38+
</Card> ) : (
39+
<Card style={{ width: "14rem", height: "15rem" }}
3240
onClick={() => {
3341
props.handleClick(props.id);
3442
}}
3543
>
36-
<Card.Img variant="top" src={sprite.front_default} />
44+
{console.log(props.images[props.name])}
45+
<Card.Img variant="top" src={props.images[props.name]} />
3746
<Card.Body>
3847
<Card.Title>{props.name}</Card.Title>
3948
</Card.Body>
40-
</Card>
49+
</Card>)}
4150
</Col>
4251
);
4352
}

vite.config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,5 +11,5 @@ export default defineConfig({
1111
host: true,
1212
strictPort: true,
1313
port: 5173,
14-
}
14+
},
1515
})

0 commit comments

Comments
 (0)