|
3 | 3 | * ---------------------- |
4 | 4 | * Easy: |
5 | 5 | * - [ ] Sort buttons (Market Cap, Price, 24h %) |
6 | | - * - [ ] Show coin symbol & small logo (image URL in API) |
7 | | - * - [ ] Format numbers with utility (abbreviate large caps: 1.2B) |
8 | | - * - [ ] Highlight positive vs negative 24h change (green/red) |
| 6 | + * - [x] Show coin symbol & small logo (image URL in API) |
| 7 | + * - [x] Format numbers with utility (abbreviate large caps: 1.2B) |
| 8 | + * - [x] Highlight positive vs negative 24h change (green/red) |
9 | 9 | * Medium: |
10 | | - * - [ ] Add pagination (Top 50 -> allow next pages) |
| 10 | + * - [x] Add pagination (Top 50 -> allow next pages) |
11 | 11 | * - [ ] Client-side caching with timestamp (avoid re-fetch spam) |
12 | 12 | * - [ ] Mini sparkline (use canvas or simple SVG) |
13 | 13 | * - [ ] Favorites (star) + localStorage persistence |
|
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'; |
25 | | -import formatNumber from '../utilities/numberFormatter.js'; |
| 21 | + |
| 22 | +import { useEffect, useState } from "react"; |
| 23 | +import Loading from "../components/Loading.jsx"; |
| 24 | +import ErrorMessage from "../components/ErrorMessage.jsx"; |
| 25 | +import Card from "../components/Card.jsx"; |
| 26 | +import formatNumber from "../utilities/numberFormatter.js"; |
| 27 | + |
26 | 28 | export default function Crypto() { |
27 | 29 | const [coins, setCoins] = useState([]); |
28 | | - const [query, setQuery] = useState(''); |
| 30 | + const [query, setQuery] = useState(""); |
29 | 31 | const [loading, setLoading] = useState(false); |
30 | 32 | const [error, setError] = useState(null); |
| 33 | + const [page, setPage] = useState(1); |
31 | 34 |
|
32 | | - useEffect(() => { fetchCoins(); }, []); |
| 35 | + useEffect(() => { |
| 36 | + fetchCoins(); |
| 37 | + }, [page]); |
33 | 38 |
|
34 | 39 | async function fetchCoins() { |
35 | 40 | 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'); |
| 41 | + setLoading(true); |
| 42 | + setError(null); |
| 43 | + const res = await fetch( |
| 44 | + `https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&order=market_cap_desc&per_page=50&page=${page}&sparkline=false` |
| 45 | + ); |
| 46 | + if (!res.ok) throw new Error("Failed to fetch"); |
39 | 47 | const json = await res.json(); |
40 | 48 | setCoins(json); |
41 | | - } catch (e) { setError(e); } finally { setLoading(false); } |
| 49 | + } catch (e) { |
| 50 | + setError(e); |
| 51 | + } finally { |
| 52 | + setLoading(false); |
| 53 | + } |
42 | 54 | } |
43 | 55 |
|
44 | | - const filtered = coins.filter(c => c.name.toLowerCase().includes(query.toLowerCase())); |
| 56 | + const filtered = coins.filter((c) => |
| 57 | + c.name.toLowerCase().includes(query.toLowerCase()) |
| 58 | + ); |
45 | 59 |
|
46 | 60 | return ( |
47 | 61 | <div> |
48 | | - <h2>Cryptocurrency Tracker</h2> |
49 | | - <input value={query} onChange={e => setQuery(e.target.value)} placeholder="Search coin" /> |
| 62 | + <h2>💹 Cryptocurrency Tracker</h2> |
| 63 | + <input |
| 64 | + value={query} |
| 65 | + onChange={(e) => setQuery(e.target.value)} |
| 66 | + placeholder="Search coin..." |
| 67 | + style={{ marginBottom: "1rem" }} |
| 68 | + /> |
| 69 | + |
50 | 70 | {loading && <Loading />} |
51 | 71 | <ErrorMessage error={error} /> |
| 72 | + |
52 | 73 | <div className="grid"> |
53 | | - {filtered.map(c => ( |
54 | | - <Card key={c.id} title={c.name} footer={<span>${c.current_price}</span>}> |
55 | | - <p>Market Cap: ${formatNumber(c.market_cap)}</p> |
56 | | - <p>24h: {c.price_change_percentage_24h?.toFixed(2)}%</p> |
57 | | - {/* TODO: Add mini sparkline chart */} |
58 | | - </Card> |
59 | | - ))} |
| 74 | + {filtered.map((c) => { |
| 75 | + const isPositive = c.price_change_percentage_24h >= 0; |
| 76 | + return ( |
| 77 | + <Card |
| 78 | + key={c.id} |
| 79 | + title={ |
| 80 | + <span style={{ display: "flex", alignItems: "center", gap: 8 }}> |
| 81 | + <img |
| 82 | + src={c.image} |
| 83 | + alt={c.symbol} |
| 84 | + style={{ width: 24, height: 24, borderRadius: "50%" }} |
| 85 | + /> |
| 86 | + {c.name} ({c.symbol.toUpperCase()}) |
| 87 | + </span> |
| 88 | + } |
| 89 | + footer={<strong>${c.current_price.toLocaleString()}</strong>} |
| 90 | + > |
| 91 | + <p>Market Cap: ${formatNumber(c.market_cap)}</p> |
| 92 | + <p |
| 93 | + style={{ |
| 94 | + color: isPositive ? "#16a34a" : "#dc2626", |
| 95 | + fontWeight: 600, |
| 96 | + }} |
| 97 | + > |
| 98 | + 24h: {isPositive ? "+" : ""} |
| 99 | + {c.price_change_percentage_24h?.toFixed(2)}% |
| 100 | + </p> |
| 101 | + {/* TODO: Add mini sparkline chart */} |
| 102 | + </Card> |
| 103 | + ); |
| 104 | + })} |
| 105 | + </div> |
| 106 | + |
| 107 | + <div |
| 108 | + className="pagination" |
| 109 | + style={{ |
| 110 | + display: "flex", |
| 111 | + justifyContent: "center", |
| 112 | + alignItems: "center", |
| 113 | + gap: "1rem", |
| 114 | + marginTop: "1.5rem", |
| 115 | + }} |
| 116 | + > |
| 117 | + <button onClick={() => setPage((p) => p - 1)} disabled={page === 1}> |
| 118 | + Previous |
| 119 | + </button> |
| 120 | + |
| 121 | + <span>Page {page}</span> |
| 122 | + |
| 123 | + <button onClick={() => setPage((p) => p + 1)}>Next</button> |
60 | 124 | </div> |
61 | 125 | </div> |
62 | 126 | ); |
|
0 commit comments