types pending
This commit is contained in:
parent
46b167a8f3
commit
a5ed7ade2c
@ -29,11 +29,11 @@ router.get('/coins/markets', function (req, res) {
|
|||||||
|
|
||||||
router.get('/count', function (req, res) {
|
router.get('/count', function (req, res) {
|
||||||
let url = config.coingecko.api_url + '/global';
|
let url = config.coingecko.api_url + '/global';
|
||||||
// console.log("url: ", url);
|
console.log("url: ", url);
|
||||||
|
|
||||||
api_helper.REMOTE_API_call(url)
|
api_helper.REMOTE_API_call(url)
|
||||||
.then(response => {
|
.then(response => {
|
||||||
res.json(response.data.active_cryptocurrencies);
|
res.json({'count': response.data.active_cryptocurrencies});
|
||||||
})
|
})
|
||||||
.catch(error => {
|
.catch(error => {
|
||||||
console.log("error: ", error);
|
console.log("error: ", error);
|
||||||
@ -71,8 +71,18 @@ router.get('/coin/:id/chart', function (req, res) {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
|
router.get('/global', function (req, res) {
|
||||||
|
let url = config.coingecko.api_url + '/global';
|
||||||
|
|
||||||
|
api_helper.REMOTE_API_call(url)
|
||||||
|
.then(response => {
|
||||||
|
res.json(response.data);
|
||||||
|
})
|
||||||
|
.catch(error => {
|
||||||
|
console.log("error: ", error);
|
||||||
|
res.send(error);
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -10,7 +10,6 @@ import * as React from 'react';
|
|||||||
import { ThemeProvider, createTheme } from '@mui/material/styles';
|
import { ThemeProvider, createTheme } from '@mui/material/styles';
|
||||||
import Grid from '@mui/material/Grid';
|
import Grid from '@mui/material/Grid';
|
||||||
import Box from '@mui/material/Box';
|
import Box from '@mui/material/Box';
|
||||||
import { styled } from '@mui/material/styles';
|
|
||||||
import Container from '@mui/material/Container';
|
import Container from '@mui/material/Container';
|
||||||
|
|
||||||
const ColorModeContext = React.createContext({ toggleColorMode: () => { } });
|
const ColorModeContext = React.createContext({ toggleColorMode: () => { } });
|
||||||
|
|||||||
@ -15,7 +15,12 @@ import { Link } from "react-router-dom";
|
|||||||
|
|
||||||
const ColorModeContext = React.createContext({ toggleColorMode: () => {} });
|
const ColorModeContext = React.createContext({ toggleColorMode: () => {} });
|
||||||
|
|
||||||
const Header = (props: any): JSX.Element => {
|
export interface HeaderProps {
|
||||||
|
colorMode: any;
|
||||||
|
theme: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
const Header = (props: HeaderProps): JSX.Element => {
|
||||||
const theme = useTheme();
|
const theme = useTheme();
|
||||||
const colorMode = React.useContext(ColorModeContext);
|
const colorMode = React.useContext(ColorModeContext);
|
||||||
return (
|
return (
|
||||||
|
|||||||
@ -8,22 +8,52 @@ 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 interface IGetGlobalResponse {
|
||||||
|
active_cryptocurrencies: number;
|
||||||
|
markets: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGetCoinsListResponse {
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGetCoinsCountResponse {
|
||||||
|
count: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGetCoinsInfoResponse {
|
||||||
|
[key: string]: any;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGetCoinsInfoRequest {
|
||||||
|
id: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGetCoinsChartRequest {
|
||||||
|
page: number;
|
||||||
|
per_page: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IGetCoinsChartResponse {
|
||||||
|
[key: string]: any ;
|
||||||
|
}
|
||||||
|
|
||||||
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>({
|
getGlobal: builder.query<IGetGlobalResponse, void>({
|
||||||
query: () => globalUrl,
|
query: () => globalUrl,
|
||||||
}),
|
}),
|
||||||
getCoinsList: builder.query<any, any>({
|
getCoinsList: builder.query<IGetCoinsListResponse, IGetCoinsChartRequest>({
|
||||||
query: (payload: {page: number, per_page: number}) => `${marketsUrl}?per_page=${payload.per_page}&page=${payload.page}`,
|
query: (payload: IGetCoinsChartRequest) => `${marketsUrl}?per_page=${payload.per_page}&page=${payload.page}`,
|
||||||
}),
|
}),
|
||||||
getCoinsCount: builder.query<any, number | void>({
|
getCoinsCount: builder.query<IGetCoinsCountResponse, void>({
|
||||||
query: () => countUrl,
|
query: () => countUrl,
|
||||||
}),
|
}),
|
||||||
getCoinInfo: builder.query<any, string>({
|
getCoinInfo: builder.query<IGetCoinsInfoResponse, string>({
|
||||||
query: (id: string) => coinInfoUrl(id),
|
query: (id: string) => coinInfoUrl(id),
|
||||||
}),
|
}),
|
||||||
getCoinChart: builder.query<any, string>({
|
getCoinChart: builder.query<IGetCoinsChartResponse, string>({
|
||||||
query: (id: string) => coinChartUrl(id),
|
query: (id: string) => coinChartUrl(id),
|
||||||
}),
|
}),
|
||||||
}),
|
}),
|
||||||
|
|||||||
@ -37,19 +37,46 @@ export const options = {
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
const CoinChart = (props: any): JSX.Element => {
|
|
||||||
// console.log("props: ", props);
|
|
||||||
|
|
||||||
// get chart data -------------------------------------------
|
export interface ICoinChartProps {
|
||||||
const {
|
coin: string;
|
||||||
data,
|
}
|
||||||
isSuccess: isSuccessChart
|
|
||||||
} = useGetCoinChartQuery(props.coin || 'btc'); // Todo: 404
|
|
||||||
|
|
||||||
// format chart data ----------------------------------------
|
// export interface IChartData {
|
||||||
function formatChartData(data: any): any {
|
// [key: string]: any;
|
||||||
const chartData = data['prices'].map(function (value: any) {
|
// }
|
||||||
// return value[1];
|
|
||||||
|
|
||||||
|
|
||||||
|
// ---------------------------------------- IN
|
||||||
|
export interface IChartDataItem {
|
||||||
|
[index: number]: number,
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IChartData {
|
||||||
|
market_caps: Array<IChartDataItem>;
|
||||||
|
prices: Array<IChartDataItem>;
|
||||||
|
total_volumes: Array<IChartDataItem>;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------- OUT
|
||||||
|
export interface IChartDataFormattedItem {
|
||||||
|
x: Date;
|
||||||
|
y: number;
|
||||||
|
}
|
||||||
|
|
||||||
|
export interface IChartDataFormatted extends Array<IChartDataFormattedItem>{};
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
const CoinChart = (props: ICoinChartProps): JSX.Element => {
|
||||||
|
console.log("props: ", props);
|
||||||
|
|
||||||
|
const { data, isSuccess: isSuccessChart } = useGetCoinChartQuery(props.coin);
|
||||||
|
|
||||||
|
function formatChartData(data: IChartData): IChartDataFormatted {
|
||||||
|
const chartData = data['prices'].map(function (value: IChartDataItem) {
|
||||||
return {
|
return {
|
||||||
x: new Date(value[0]),
|
x: new Date(value[0]),
|
||||||
y: value[1],
|
y: value[1],
|
||||||
@ -59,10 +86,10 @@ const CoinChart = (props: any): JSX.Element => {
|
|||||||
return chartData;
|
return chartData;
|
||||||
}
|
}
|
||||||
|
|
||||||
// format data ----------------------------------------------
|
|
||||||
function formatChartLabels(data: any): any {
|
function formatChartLabels(data: any): any {
|
||||||
|
console.log('data: ', data)
|
||||||
const labels = data['prices'].map((value: (string | number | Date)[]) => format(new Date(value[0]), 'MM/dd/yyyy'));
|
const labels = data['prices'].map((value: (string | number | Date)[]) => format(new Date(value[0]), 'MM/dd/yyyy'));
|
||||||
console.log('formatChartLabels: ', labels)
|
// console.log('formatChartLabels: ', labels)
|
||||||
return labels;
|
return labels;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -82,8 +109,6 @@ const CoinChart = (props: any): JSX.Element => {
|
|||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
{/* <Line options={options} data={data} />; */}
|
|
||||||
</div>
|
</div>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -4,21 +4,18 @@ import CoinChart from './coinChart';
|
|||||||
|
|
||||||
const CoinDetails = (): JSX.Element => {
|
const CoinDetails = (): JSX.Element => {
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
console.log("id: ", id)
|
|
||||||
|
|
||||||
// get info data
|
|
||||||
const {
|
const {
|
||||||
data: dataInfo,
|
data: dataInfo,
|
||||||
error: errorInfo,
|
error: errorInfo,
|
||||||
isLoading: isLoadingInfo,
|
isLoading: isLoadingInfo,
|
||||||
isSuccess: isSuccessInfo,
|
isSuccess: isSuccessInfo,
|
||||||
refetch: refetchInfo
|
refetch: refetchInfo
|
||||||
} = useGetCoinInfoQuery(id || 'btc'); // Todo: 404
|
} = useGetCoinInfoQuery(id ? id : '');
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{
|
{
|
||||||
dataInfo && isSuccessInfo &&
|
dataInfo && isSuccessInfo && <>
|
||||||
<div className="info">
|
<div className="info">
|
||||||
<div>ID: {dataInfo.id}</div>
|
<div>ID: {dataInfo.id}</div>
|
||||||
<div className="links">
|
<div className="links">
|
||||||
@ -26,12 +23,11 @@ const CoinDetails = (): JSX.Element => {
|
|||||||
</div>
|
</div>
|
||||||
<div>Description{dataInfo.description.en}</div>
|
<div>Description{dataInfo.description.en}</div>
|
||||||
</div>
|
</div>
|
||||||
}
|
|
||||||
|
|
||||||
{
|
|
||||||
<div className="chart">
|
<div className="chart">
|
||||||
<CoinChart coin={'flow'} />
|
<CoinChart coin={dataInfo.id} />
|
||||||
</div>
|
</div>
|
||||||
|
</>
|
||||||
}
|
}
|
||||||
|
|
||||||
</div>
|
</div>
|
||||||
|
|||||||
@ -1,8 +1,6 @@
|
|||||||
// Route
|
|
||||||
import { useSearchParams } from 'react-router-dom';
|
import { useSearchParams } from 'react-router-dom';
|
||||||
import { Link } from "react-router-dom";
|
import { Link } from "react-router-dom";
|
||||||
|
|
||||||
// Material UI
|
|
||||||
import Table from '@mui/material/Table';
|
import Table from '@mui/material/Table';
|
||||||
import TableBody from '@mui/material/TableBody';
|
import TableBody from '@mui/material/TableBody';
|
||||||
import TableCell from '@mui/material/TableCell';
|
import TableCell from '@mui/material/TableCell';
|
||||||
@ -10,10 +8,8 @@ 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';
|
import { useGetCoinsListQuery, useGetGlobalQuery } from '../coinApi';
|
||||||
|
|
||||||
// components
|
|
||||||
import Pager from './coinListPager';
|
import Pager from './coinListPager';
|
||||||
|
|
||||||
|
|
||||||
@ -27,22 +23,23 @@ const CoinList = (): JSX.Element => {
|
|||||||
|
|
||||||
// For Query
|
// For Query
|
||||||
const payload = { page, per_page }
|
const payload = { page, per_page }
|
||||||
const { data, error, isLoading, isSuccess } = useGetCoinsListQuery(payload);
|
const { data, isLoading, isSuccess } = useGetCoinsListQuery(payload);
|
||||||
const { data: globalData, error: globalError, isLoading: globalIsLoading, isSuccess: lobalSuccess } = useGetGlobalQuery();
|
const { data: globalData, isLoading: globalIsLoading, isSuccess: lobalSuccess } = useGetGlobalQuery();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div>
|
<div>
|
||||||
{isLoading && <div>Loading...</div>}
|
{isLoading || globalIsLoading && <div>Loading...</div>}
|
||||||
|
|
||||||
{
|
{
|
||||||
globalData &&
|
globalData &&
|
||||||
<span className="globalText">
|
<span className="globalText">
|
||||||
{`The global cryptocurrency market has currently ${globalData.data.active_cryptocurrencies} active cryptocurrencies, and ${globalData.data.markets} markets`}
|
{`The global cryptocurrency market has currently ${globalData.active_cryptocurrencies} active cryptocurrencies, and ${globalData.markets} markets`}
|
||||||
<br />
|
<br />
|
||||||
</span>
|
</span>
|
||||||
}
|
}
|
||||||
|
|
||||||
{data && isSuccess &&
|
{
|
||||||
|
data && isSuccess &&
|
||||||
<TableContainer style={{ marginTop: 20 }}>
|
<TableContainer style={{ marginTop: 20 }}>
|
||||||
<Table size="small" aria-label="simple table">
|
<Table size="small" aria-label="simple table">
|
||||||
<TableHead>
|
<TableHead>
|
||||||
|
|||||||
@ -15,23 +15,27 @@ const Pager = (props:any): JSX.Element => {
|
|||||||
const { data, error, isLoading, isSuccess, refetch } = useGetCoinsCountQuery();
|
const { data, error, isLoading, isSuccess, refetch } = useGetCoinsCountQuery();
|
||||||
const navigate = useNavigate();
|
const navigate = useNavigate();
|
||||||
|
|
||||||
const lastPage = Math.ceil(data / per_page)
|
const lastPage = (data: number) => Math.ceil(data / per_page)
|
||||||
|
|
||||||
function handlePager(page: number) {
|
function handlePager(page: number) {
|
||||||
const url = `/?page=${page}`
|
const url = `/?page=${page}`
|
||||||
navigate(url)
|
navigate(url)
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (<>
|
||||||
|
{
|
||||||
|
data && isSuccess &&
|
||||||
<Grid container justifyContent="center" style={{marginTop: 60}}>
|
<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>
|
||||||
<Typography style={{ padding: 10}}>Page {page} from {lastPage}</Typography>
|
<Typography style={{ padding: 10}}>Page {page} from {lastPage(data.count)}</Typography>
|
||||||
<Button disabled={page === lastPage} onClick={() => handlePager(page + 1)}>Next</Button>
|
<Button disabled={page === lastPage(data.count)} onClick={() => handlePager(page + 1)}>Next</Button>
|
||||||
<Button disabled={page === lastPage} onClick={() => handlePager(lastPage)}>Last</Button>
|
<Button disabled={page === lastPage(data.count)} onClick={() => handlePager(lastPage(data.count))}>Last</Button>
|
||||||
</ButtonGroup>
|
</ButtonGroup>
|
||||||
</Grid>
|
</Grid>
|
||||||
|
}
|
||||||
|
</>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user