File size: 4,299 Bytes
1e92f2d |
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 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
import { LinearProgress, makeStyles, Typography } from "@material-ui/core";
import axios from "axios";
import { useEffect, useState } from "react";
import { useParams } from "react-router-dom";
import ReactHtmlParser from "react-html-parser";
import CoinInfo from "../components/CoinInfo";
import { SingleCoin } from "../config/api";
import { numberWithCommas } from "../components/CoinsTable";
import { CryptoState } from "../CryptoContext";
const CoinPage = () => {
const { id } = useParams();
const [coin, setCoin] = useState();
const { currency, symbol } = CryptoState();
const fetchCoin = async () => {
const { data } = await axios.get(SingleCoin(id));
setCoin(data);
};
useEffect(() => {
fetchCoin();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
const useStyles = makeStyles((theme) => ({
container: {
display: "flex",
[theme.breakpoints.down("md")]: {
flexDirection: "column",
alignItems: "center",
},
},
sidebar: {
width: "30%",
[theme.breakpoints.down("md")]: {
width: "100%",
},
display: "flex",
flexDirection: "column",
alignItems: "center",
marginTop: 25,
borderRight: "2px solid grey",
},
heading: {
fontWeight: "bold",
marginBottom: 20,
fontFamily: "Montserrat",
},
description: {
width: "100%",
fontFamily: "Montserrat",
padding: 25,
paddingBottom: 15,
paddingTop: 0,
textAlign: "justify",
},
marketData: {
alignSelf: "start",
padding: 25,
paddingTop: 10,
width: "100%",
[theme.breakpoints.down("md")]: {
display: "flex",
justifyContent: "space-around",
},
[theme.breakpoints.down("sm")]: {
flexDirection: "column",
alignItems: "center",
},
[theme.breakpoints.down("xs")]: {
alignItems: "start",
},
},
}));
const classes = useStyles();
if (!coin) return <LinearProgress style={{ backgroundColor: "gold" }} />;
return (
<div className={classes.container}>
<div className={classes.sidebar}>
<img
src={coin?.image.large}
alt={coin?.name}
height="200"
style={{ marginBottom: 20 }}
/>
<Typography variant="h3" className={classes.heading}>
{coin?.name}
</Typography>
<Typography variant="subtitle1" className={classes.description}>
{ReactHtmlParser(coin?.description.en.split(". ")[0])}.
</Typography>
<div className={classes.marketData}>
<span style={{ display: "flex" }}>
<Typography variant="h5" className={classes.heading}>
Rank:
</Typography>
<Typography
variant="h5"
style={{
fontFamily: "Montserrat",
}}
>
{numberWithCommas(coin?.market_cap_rank)}
</Typography>
</span>
<span style={{ display: "flex" }}>
<Typography variant="h5" className={classes.heading}>
Current Price:
</Typography>
<Typography
variant="h5"
style={{
fontFamily: "Montserrat",
}}
>
{symbol}{" "}
{numberWithCommas(
coin?.market_data.current_price[currency.toLowerCase()]
)}
</Typography>
</span>
<span style={{ display: "flex" }}>
<Typography variant="h5" className={classes.heading}>
Market Cap:
</Typography>
<Typography
variant="h5"
style={{
fontFamily: "Montserrat",
}}
>
{symbol}{" "}
{numberWithCommas(
coin?.market_data.market_cap[currency.toLowerCase()]
.toString()
.slice(0, -6)
)}
M
</Typography>
</span>
</div>
</div>
<CoinInfo coin={coin} />
</div>
);
};
export default CoinPage;
|