-
Notifications
You must be signed in to change notification settings - Fork 0
/
CryptoContext.js
93 lines (86 loc) · 2.7 KB
/
CryptoContext.js
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
import { useEffect, useState } from "react";
import { createContext } from "react";
import { useAsyncError } from "react-router-dom";
export const CryptoContext = createContext({});
export const CryptoProvider = ({ children }) => {
const [cryptoData, setCryptoData] = useState(null);
const [searchData, setSearchData] = useState(null);
const [coinSearch, setCoinSearch] = useState("");
const [currency, setCurrency] = useState("inr");
const [sortBy, setSortBy] = useState("market_cap_desc");
const [currentPage, setCurrentPage] = useState(1);
const [tPage, setTotalPage] = useState(250);
const [perPage, setPerPage] = useState(10);
const [coinData, setCoinData] = useState(null);
useEffect(() => {
fetchData();
}, [coinSearch, currency, currentPage, sortBy, perPage]);
const defaults = () => {
setCurrentPage(1);
setCoinSearch("");
};
async function fetchData() {
setTotalPage(13220);
try {
const response = await fetch(
`https://api.coingecko.com/api/v3/coins/markets?vs_currency=${currency}&ids=${coinSearch}&order=${sortBy}&per_page=${perPage}&page=${currentPage}&sparkline=false&price_change_percentage=1h%2C24h%2C7d&locale=en`
);
const data = await response.json();
//console.log(data);
setCryptoData(data);
} catch (error) {
/*console.log(
"Please try after sometime as the API is called maximum time"
);
*/
// Handle the error here
}
}
async function getCryptoData(text) {
const data = await fetch(
`https://api.coingecko.com/api/v3/search?query=${text}`
);
const json = await data.json();
setSearchData(json.coins);
//console.log(searchData);
}
const getCoinData = async (coinid) => {
try {
const response = await fetch(
`https://api.coingecko.com/api/v3/coins/${coinid}?localization=false&tickers=false&market_data=true&community_data=false&developer_data=true&sparkline=false`
);
const data = await response.json();
setCoinData(data);
} catch (error) {
//console.log("Error fetching data:", error);
// Handle the error here
}
};
return (
<CryptoContext.Provider
value={{
cryptoData,
searchData,
currency,
currentPage,
tPage,
perPage,
coinData,
setCoinSearch,
setSortBy,
setCryptoData,
getCryptoData,
setCurrency,
setSearchData,
setCurrentPage,
setTotalPage,
setPerPage,
setCoinData,
defaults,
getCoinData,
}}
>
{children}
</CryptoContext.Provider>
);
};