-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.jsx
138 lines (121 loc) · 3.54 KB
/
index.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
import React, { useState } from 'react'
import Page from '@components/page'
import Image from 'next/image'
import styles from '@styles/Home.module.css'
import { useQuery } from 'react-query'
import { ArrowRight, ArrowLeft } from '@components/icons'
import Link from 'next/link'
const Home = (props) => {
const initialPokemons = props.data
const [page, setPage] = useState(0)
const [pokeImage, setPokeImage] = useState(0)
const [pageCount, setPageCount] = useState(1)
const pokemonReq = async () => {
const res = await fetch(`https://pokeapi.co/api/v2/pokemon?offset=${page}&limit=20`)
return res.json()
}
const { data, status, refetch } = useQuery('req', pokemonReq, {
initialData: initialPokemons,
staleTime: false,
cachetime: false,
})
const decrement = async (e) => {
e.preventDefault()
try {
if (page > 0) {
setPokeImage(Number(pokeImage - 2))
setPage(page - 19)
setPageCount(pageCount - 1)
}
refetch()
} catch (error) {
console.error(error)
}
}
const increment = async (e) => {
e.preventDefault()
try {
setPage(Number(page + 19))
setPokeImage(Number(pokeImage + 2))
setPageCount(pageCount + 1)
refetch()
} catch (error) {
console.error(error)
}
}
return (
<Page title="Home" description="PokeDex Home Page">
<div>
<div className={styles.container}>
<div className={styles.wrapper}>
<div className={styles.menu}>
<button
type="button"
aria-label="Previous Page"
onClick={decrement}
disabled={page === 0}
>
<span aria-hidden="true" >
<ArrowLeft />
</span>
</button>
<button
type="button"
aria-label="Next Page"
onClick={increment}
>
<span aria-hidden="true" >
<ArrowRight />
</span>
</button>
</div>
</div>
<main className={styles.main}>
<h1 className={styles.title}>
PokèDex
</h1>
<p className={styles.description}>
page: {pageCount}
</p>
<div className={styles.grid}>
{status === 'success' && data.results.map((pokemon, i) => (
<Link href={`/${pokemon.name}`} key={pokemon.name}>
<a className={styles.card} >
<Image src={`https://assets.pokemon.com/assets/cms2/img/pokedex/detail/${(('00' + pokeImage + ++i)).slice(-3)}.png`}
width="215px"
height="215px"
alt={`${pokemon.name}`}
/>
<h2>{pokemon.name} →</h2>
</a>
</Link>
))}
</div>
</main>
<footer className={styles.footer}>
<p>
Powered by{' '}
<a
href="https://xis.li"
target="_blank"
rel="noopener noreferrer"
>
ξ
</a>
</p>
</footer>
</div>
</div>
</Page>
);
}
export default Home;
export const getStaticProps = async () => {
const res = await fetch('https://pokeapi.co/api/v2/pokemon?offset=0&limit=20')
const pokeMons = await res.json()
return {
props: {
data: pokeMons
}
}
}