Last active
March 11, 2024 03:27
-
-
Save rollendxavier/7fe9e8af1176c0f2917d8c179bb0aed9 to your computer and use it in GitHub Desktop.
Price Tracker - Handling Coin Selection
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const handleCoinSelect = (id) => { | |
setLoading(true); | |
const selected = coins.find(coin => coin.id === id); | |
setSelectedCoin(selected); | |
// Fetch historical data for the selected date range | |
const promises = []; | |
const date = new Date(startDate); | |
while (date <= endDate) { | |
const formattedDate = `${date.getDate()}-${date.getMonth() + 1}-${date.getFullYear()}`; | |
const promise = axios.get(`http://localhost:3001/api/v3/coins/${id}/history?date=${formattedDate}`, { | |
headers: { | |
'X-CoinAPI-Key': api_key | |
} | |
}) | |
.then(response => { | |
const price = response.data.market_data.current_price.usd; | |
return [Date.parse(formattedDate), price]; | |
}) | |
.catch(error => { | |
console.error('Error fetching historical data', error); | |
return [Date.parse(formattedDate), 0]; // Return a default value | |
}); | |
promises.push(promise); | |
date.setDate(date.getDate() + 1); // Increment the date | |
} | |
Promise.all(promises) | |
.then(history => { | |
setHistory(history); | |
setLoading(false); | |
}); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment