|
18 | 18 | * - [ ] Dark mode adaptive coloring for charts |
19 | 19 | * - [ ] Extract to service + custom hook (useCryptoMarkets) |
20 | 20 | */ |
21 | | -import { useEffect, useState } from 'react'; |
22 | | -import Loading from '../components/Loading.jsx'; |
23 | | -import ErrorMessage from '../components/ErrorMessage.jsx'; |
24 | | -import Card from '../components/Card.jsx'; |
| 21 | +import { useEffect, useState } from "react"; |
| 22 | +import Loading from "../components/Loading.jsx"; |
| 23 | +import ErrorMessage from "../components/ErrorMessage.jsx"; |
| 24 | +import Card from "../components/Card.jsx"; |
25 | 25 |
|
26 | 26 | export default function Crypto() { |
27 | 27 | const [coins, setCoins] = useState([]); |
28 | | - const [query, setQuery] = useState(''); |
| 28 | + const [query, setQuery] = useState(""); |
29 | 29 | const [loading, setLoading] = useState(false); |
30 | 30 | const [error, setError] = useState(null); |
| 31 | + const [page, setPage] = useState(1); |
31 | 32 |
|
32 | | - useEffect(() => { fetchCoins(); }, []); |
| 33 | + useEffect(() => { |
| 34 | + fetchCoins(); |
| 35 | + }, [page]); |
33 | 36 |
|
34 | 37 | async function fetchCoins() { |
35 | 38 | try { |
36 | | - setLoading(true); setError(null); |
37 | | - const res = await fetch('https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=50&page=1&sparkline=false'); |
38 | | - if (!res.ok) throw new Error('Failed to fetch'); |
| 39 | + setLoading(true); |
| 40 | + setError(null); |
| 41 | + const res = await fetch( |
| 42 | + `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=50&page=${page}&sparkline=false` |
| 43 | + ); |
| 44 | + if (!res.ok) throw new Error("Failed to fetch"); |
39 | 45 | const json = await res.json(); |
40 | 46 | setCoins(json); |
41 | | - } catch (e) { setError(e); } finally { setLoading(false); } |
| 47 | + } catch (e) { |
| 48 | + setError(e); |
| 49 | + } finally { |
| 50 | + setLoading(false); |
| 51 | + } |
42 | 52 | } |
43 | 53 |
|
44 | | - const filtered = coins.filter(c => c.name.toLowerCase().includes(query.toLowerCase())); |
| 54 | + const filtered = coins.filter((c) => |
| 55 | + c.name.toLowerCase().includes(query.toLowerCase()) |
| 56 | + ); |
45 | 57 |
|
46 | 58 | return ( |
47 | 59 | <div> |
48 | 60 | <h2>Cryptocurrency Tracker</h2> |
49 | | - <input value={query} onChange={e => setQuery(e.target.value)} placeholder="Search coin" /> |
| 61 | + <input |
| 62 | + value={query} |
| 63 | + onChange={(e) => setQuery(e.target.value)} |
| 64 | + placeholder="Search coin" |
| 65 | + /> |
| 66 | + |
50 | 67 | {loading && <Loading />} |
51 | 68 | <ErrorMessage error={error} /> |
52 | 69 | <div className="grid"> |
53 | | - {filtered.map(c => ( |
54 | | - <Card key={c.id} title={c.name} footer={<span>${c.current_price}</span>}> |
| 70 | + {filtered.map((c) => ( |
| 71 | + <Card |
| 72 | + key={c.id} |
| 73 | + title={c.name} |
| 74 | + footer={<span>${c.current_price}</span>} |
| 75 | + > |
55 | 76 | <p>Market Cap: ${c.market_cap.toLocaleString()}</p> |
56 | 77 | <p>24h: {c.price_change_percentage_24h?.toFixed(2)}%</p> |
57 | 78 | {/* TODO: Add mini sparkline chart */} |
58 | 79 | </Card> |
59 | 80 | ))} |
60 | 81 | </div> |
| 82 | + |
| 83 | + <div className="pagination"> |
| 84 | + <button onClick={() => setPage((p) => p - 1)} disabled={page === 1}> |
| 85 | + Previous |
| 86 | + </button> |
| 87 | + |
| 88 | + <span>Page {page}</span> |
| 89 | + |
| 90 | + <button onClick={() => setPage((p) => p + 1)}>Next</button> |
| 91 | + </div> |
61 | 92 | </div> |
62 | 93 | ); |
63 | 94 | } |
0 commit comments