-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbitcoin.tsx
61 lines (55 loc) · 1.17 KB
/
bitcoin.tsx
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
import { GetStaticProps } from "next";
import useSWR from "swr";
type Result = {
chartName: string;
time: {
updated: string;
};
bpi: {
USD: {
code: string;
rate: number;
};
GBP: {
code: string;
rate: number;
};
EUR: {
code: string;
rate: number;
};
};
};
const fetcher = async (): Promise<Result> => {
const res = await fetch(`https://api.coindesk.com/v1/bpi/currentprice.json`);
return res.json();
};
export const getStaticProps: GetStaticProps = async () => {
const result = await fetcher();
return { props: { result }, revalidate: 60 };
};
export default function Bitcoin({ result }: { result: Result }) {
const { data } = useSWR("/bitcoin", fetcher, {
initialData: result,
});
if (!data) {
return <h1>Loading...</h1>;
}
return (
<div>
<h1>{data.chartName}</h1>
<h2>{data.time.updated}</h2>
<ul>
<li>
{data.bpi.USD.code}: ${data.bpi.USD.rate}
</li>
<li>
{data.bpi.GBP.code}: ${data.bpi.GBP.rate}
</li>
<li>
{data.bpi.EUR.code}: ${data.bpi.EUR.rate}
</li>
</ul>
</div>
);
}