import { useState, useEffect } from "react"; import { Line } from "react-chartjs-2"; import { Chart as ChartJS, CategoryScale, LinearScale, PointElement, LineElement, Tooltip, Legend, TimeScale, } from "chart.js"; import "chartjs-adapter-date-fns"; import { useCoinPricesStore, fetchCoinPrices } from "../store/coins-prices"; import Spinner from "./spinner"; ChartJS.register( CategoryScale, LinearScale, PointElement, LineElement, Tooltip, Legend, TimeScale ); const CoinChart = ({ coinId }) => { const [chartData, setChartData] = useState(null); const error = useCoinPricesStore(state => state.error); const coinPrices = useCoinPricesStore(state => state.coinPrices); const loading = useCoinPricesStore(state => state.loading); useEffect(() => { fetchCoinPrices(coinId); }, [coinId]) useEffect(() => { if (coinPrices) { const quotes = coinPrices.prices.map(price => { return { x: price[0], y: price[1], } }); setChartData({ datasets: [ { label: "Price (USD)", data: quotes, fill: true, borderColor: "#007bff", backgroundColor: "rgba(0, 123, 255, 0.1)", pointRadius: 0, tension: 0.3, } ] }); } }, [coinPrices]) if (!chartData) return

Loading

; return ( <> {loading && } {!loading && error &&
Loading Price Data Error
} {!loading && !error && (
`$${val.toLocaleString()}`, }, }, }, }} />
)} ); } export default CoinChart;