coin list global text

This commit is contained in:
Ste Vaidis 2022-12-10 20:08:33 +02:00
parent 4f9882a389
commit 46b167a8f3
6 changed files with 117 additions and 86 deletions

View File

@ -37,6 +37,7 @@ function App() {
return ( return (
<div className="App"> <div className="App">
<BrowserRouter>
<ColorModeContext.Provider value={colorMode}> <ColorModeContext.Provider value={colorMode}>
<ThemeProvider theme={theme}> <ThemeProvider theme={theme}>
<Header colorMode={colorMode} theme={theme} /> <Header colorMode={colorMode} theme={theme} />
@ -47,19 +48,16 @@ function App() {
direction="column" direction="column"
justifyContent="center" justifyContent="center"
> >
{/* <Grid item xs={12}> */}
<BrowserRouter>
<Routes> <Routes>
<Route path="/" element={<CoinList />} /> <Route path="/" element={<CoinList />} />
<Route path="/coin/:id" element={<CoinDetails />} /> <Route path="/coin/:id" element={<CoinDetails />} />
</Routes> </Routes>
</BrowserRouter>
{/* </Grid> */}
</Grid> </Grid>
</Box> </Box>
</Container> </Container>
</ThemeProvider> </ThemeProvider>
</ColorModeContext.Provider> </ColorModeContext.Provider>
</BrowserRouter>
</div> </div>
); );
} }

View File

@ -11,7 +11,7 @@ import MenuIcon from '@mui/icons-material/Menu';
import { useTheme } from '@mui/material/styles'; import { useTheme } from '@mui/material/styles';
import Brightness4Icon from '@mui/icons-material/Brightness4'; import Brightness4Icon from '@mui/icons-material/Brightness4';
import Brightness7Icon from '@mui/icons-material/Brightness7'; import Brightness7Icon from '@mui/icons-material/Brightness7';
import { nodeModuleNameResolver } from 'typescript'; import { Link } from "react-router-dom";
const ColorModeContext = React.createContext({ toggleColorMode: () => {} }); const ColorModeContext = React.createContext({ toggleColorMode: () => {} });
@ -19,11 +19,13 @@ const Header = (props: any): JSX.Element => {
const theme = useTheme(); const theme = useTheme();
const colorMode = React.useContext(ColorModeContext); const colorMode = React.useContext(ColorModeContext);
return ( return (
<Box sx={{ flexGrow: 1 }}> <Box style={{marginBottom: 60}} sx={{ flexGrow: 1 }}>
<AppBar position="static"> <AppBar position="static">
<Toolbar> <Toolbar>
<Typography variant="h6" component="div" sx={{ flexGrow: 1 }}> <Typography variant="h6" component="div" sx={{ flexGrow: 1 }}>
<Link to={'/'}>
CoinGecko Data CoinGecko Data
</Link>
</Typography> </Typography>
<IconButton sx={{ ml: 1 }} onClick={props.colorMode.toggleColorMode} color="inherit"> <IconButton sx={{ ml: 1 }} onClick={props.colorMode.toggleColorMode} color="inherit">
{props.theme.palette.mode === 'dark' ? <Brightness7Icon /> : <Brightness4Icon />} {props.theme.palette.mode === 'dark' ? <Brightness7Icon /> : <Brightness4Icon />}

View File

@ -4,12 +4,16 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'
const baseUrl = "http://127.0.0.1:8080"; const baseUrl = "http://127.0.0.1:8080";
const marketsUrl = '/coins/markets'; const marketsUrl = '/coins/markets';
const countUrl = '/count'; const countUrl = '/count';
const globalUrl = '/global';
const coinInfoUrl = (id:string) => `/coin/${id}`; const coinInfoUrl = (id:string) => `/coin/${id}`;
const coinChartUrl = (id:string) => `/coin/${id}/chart`; const coinChartUrl = (id:string) => `/coin/${id}/chart`;
export const coinListApi = createApi({ export const coinListApi = createApi({
baseQuery: fetchBaseQuery({ baseUrl: baseUrl }), baseQuery: fetchBaseQuery({ baseUrl: baseUrl }),
endpoints: (builder) => ({ endpoints: (builder) => ({
getGlobal: builder.query<any, number | void>({
query: () => globalUrl,
}),
getCoinsList: builder.query<any, any>({ getCoinsList: builder.query<any, any>({
query: (payload: {page: number, per_page: number}) => `${marketsUrl}?per_page=${payload.per_page}&page=${payload.page}`, query: (payload: {page: number, per_page: number}) => `${marketsUrl}?per_page=${payload.per_page}&page=${payload.page}`,
}), }),
@ -26,6 +30,7 @@ export const coinListApi = createApi({
}) })
export const { export const {
useGetGlobalQuery,
useGetCoinsListQuery, useGetCoinsListQuery,
useGetCoinsCountQuery, useGetCoinsCountQuery,
useGetCoinInfoQuery, useGetCoinInfoQuery,

View File

@ -1,8 +1,6 @@
// Route // Route
import { useSearchParams } from 'react-router-dom'; import { useSearchParams } from 'react-router-dom';
import { Link } from "react-router-dom";
// Redux
import { useGetCoinsListQuery } from '../coinApi';
// Material UI // Material UI
import Table from '@mui/material/Table'; import Table from '@mui/material/Table';
@ -12,6 +10,9 @@ import TableContainer from '@mui/material/TableContainer';
import TableHead from '@mui/material/TableHead'; import TableHead from '@mui/material/TableHead';
import TableRow from '@mui/material/TableRow'; import TableRow from '@mui/material/TableRow';
// Redux
import { useGetCoinsListQuery, useGetGlobalQuery } from '../coinApi';
// components // components
import Pager from './coinListPager'; import Pager from './coinListPager';
@ -26,14 +27,24 @@ const CoinList = (): JSX.Element => {
// For Query // For Query
const payload = { page, per_page } const payload = { page, per_page }
const { data, error, isLoading, isSuccess, refetch } = useGetCoinsListQuery(payload); const { data, error, isLoading, isSuccess } = useGetCoinsListQuery(payload);
const { data: globalData, error: globalError, isLoading: globalIsLoading, isSuccess: lobalSuccess } = useGetGlobalQuery();
return ( return (
<div> <div>
{isLoading && <div>Loading...</div>} {isLoading && <div>Loading...</div>}
{
globalData &&
<span className="globalText">
{`The global cryptocurrency market has currently ${globalData.data.active_cryptocurrencies} active cryptocurrencies, and ${globalData.data.markets} markets`}
<br />
</span>
}
{data && isSuccess && {data && isSuccess &&
<TableContainer > <TableContainer style={{ marginTop: 20 }}>
<Table size="small" sx={{ marginTop: 10, marginBottom: 10 }} aria-label="simple table"> <Table size="small" aria-label="simple table">
<TableHead> <TableHead>
<TableRow> <TableRow>
<TableCell <TableCell
@ -76,9 +87,11 @@ const CoinList = (): JSX.Element => {
{row.market_cap_rank} {row.market_cap_rank}
</TableCell> </TableCell>
<TableCell align="left"> <TableCell align="left">
<Link to={`coin/${row.id}`}>
<img src={row.image} width="20" height="20" /> <img src={row.image} width="20" height="20" />
<span className="CoinName">{row.name}</span> <span className="coinName">{row.name}</span>
<span className="CoinSymbol">{row.symbol}</span> <span className="coinSymbol">{row.symbol}</span>
</Link>
</TableCell> </TableCell>
<TableCell align="right">${row.current_price}</TableCell> <TableCell align="right">${row.current_price}</TableCell>
<TableCell <TableCell
@ -107,7 +120,6 @@ const CoinList = (): JSX.Element => {
</Table> </Table>
</TableContainer> </TableContainer>
} }
<Pager page={page} per_page={per_page} setSearchParams={setSearchParams} /> <Pager page={page} per_page={per_page} setSearchParams={setSearchParams} />
</div> </div>
) )

View File

@ -23,7 +23,7 @@ const Pager = (props:any): JSX.Element => {
} }
return ( return (
<Grid container justifyContent="center"> <Grid container justifyContent="center" style={{marginTop: 60}}>
<ButtonGroup variant="contained" aria-label="outlined primary button group"> <ButtonGroup variant="contained" aria-label="outlined primary button group">
<Button disabled={page === 1} onClick={() => navigate("/")}>First</Button> <Button disabled={page === 1} onClick={() => navigate("/")}>First</Button>
<Button disabled={page === 1} onClick={() => handlePager(page - 1)}>Prev</Button> <Button disabled={page === 1} onClick={() => handlePager(page - 1)}>Prev</Button>

View File

@ -7,9 +7,14 @@ body {
-moz-osx-font-smoothing: grayscale; -moz-osx-font-smoothing: grayscale;
} }
code { a {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New', color: #444;
monospace; text-decoration: none;
}
header a {
color: #fff;
text-decoration: none;
} }
.green { .green {
@ -20,15 +25,24 @@ code {
color: #e15241 color: #e15241
} }
.CoinName { .coinName {
font-weight: bold; font-weight: bold;
margin-left: 10px; margin-left: 10px;
} }
.CoinSymbol { .coinSymbol {
font-weight: normal; font-weight: normal;
font-size: 0.8em; font-size: 0.8em;
margin-left: 5px; margin-left: 5px;
color: #777777; color: #777777;
text-transform: uppercase; text-transform: uppercase;
} }
.globalText {
font-weight: normal;
font-size: 0.9em;
font-style: italic;
margin-left: 5px;
color: #777777;
margin-bottom: 20px;
}