import React, { useEffect, useState } from "react"; import { makeStyles } from "@material-ui/core/styles"; import Pagination from "@material-ui/lab/Pagination"; import { Container, createTheme, TableCell, LinearProgress, ThemeProvider, Typography, TextField, TableBody, TableRow, TableHead, TableContainer, Table, Paper, } from "@material-ui/core"; import axios from "axios"; import { CoinList } from "../config/api"; import { useHistory } from "react-router-dom"; import { CryptoState } from "../CryptoContext"; export function numberWithCommas(x) { return x.toString().replace(/\B(?=(\d{3})+(?!\d))/g, ","); } export default function CoinsTable() { const [coins, setCoins] = useState([]); const [loading, setLoading] = useState(false); const [search, setSearch] = useState(""); const [page, setPage] = useState(1); const { currency, symbol } = CryptoState(); const useStyles = makeStyles({ row: { backgroundColor: "#16171a", cursor: "pointer", "&:hover": { backgroundColor: "#131111", }, fontFamily: "Montserrat", }, pagination: { "& .MuiPaginationItem-root": { color: "gold", }, }, }); const classes = useStyles(); const history = useHistory(); const darkTheme = createTheme({ palette: { primary: { main: "#fff", }, type: "dark", }, }); const fetchCoins = async () => { setLoading(true); const { data } = await axios.get(CoinList(currency)); console.log(data); setCoins(data); setLoading(false); }; useEffect(() => { fetchCoins(); // eslint-disable-next-line react-hooks/exhaustive-deps }, [currency]); const handleSearch = () => { return coins.filter( (coin) => coin.name.toLowerCase().includes(search) || coin.symbol.toLowerCase().includes(search) ); }; return ( Cryptocurrency Prices by Market Cap setSearch(e.target.value)} /> {loading ? ( ) : ( {["Coin", "Price", "24h Change", "Market Cap"].map((head) => ( {head} ))} {handleSearch() .slice((page - 1) * 10, (page - 1) * 10 + 10) .map((row) => { const profit = row.price_change_percentage_24h > 0; return ( history.push(`/coins/${row.id}`)} className={classes.row} key={row.name} > {row.name}
{row.symbol} {row.name}
{symbol}{" "} {numberWithCommas(row.current_price.toFixed(2))} 0 ? "rgb(14, 203, 129)" : "red", fontWeight: 500, }} > {profit && "+"} {row.price_change_percentage_24h.toFixed(2)}% {symbol}{" "} {numberWithCommas( row.market_cap.toString().slice(0, -6) )} M
); })}
)}
{/* Comes from @material-ui/lab */} { setPage(value); window.scroll(0, 450); }} />
); }