diff --git a/Back/src/application_controller.js b/Back/src/application_controller.js index b16233c..678ae1f 100644 --- a/Back/src/application_controller.js +++ b/Back/src/application_controller.js @@ -29,11 +29,11 @@ router.get('/coins/markets', function (req, res) { router.get('/count', function (req, res) { let url = config.coingecko.api_url + '/global'; -// console.log("url: ", url); + console.log("url: ", url); api_helper.REMOTE_API_call(url) .then(response => { - res.json(response.data.active_cryptocurrencies); + res.json({'count': response.data.active_cryptocurrencies}); }) .catch(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); + }) +}) diff --git a/Front/src/App.tsx b/Front/src/App.tsx index 58bdd0d..f3f684e 100644 --- a/Front/src/App.tsx +++ b/Front/src/App.tsx @@ -10,7 +10,6 @@ import * as React from 'react'; import { ThemeProvider, createTheme } from '@mui/material/styles'; import Grid from '@mui/material/Grid'; import Box from '@mui/material/Box'; -import { styled } from '@mui/material/styles'; import Container from '@mui/material/Container'; const ColorModeContext = React.createContext({ toggleColorMode: () => { } }); diff --git a/Front/src/Header.tsx b/Front/src/Header.tsx index 89ecbec..1a9b577 100644 --- a/Front/src/Header.tsx +++ b/Front/src/Header.tsx @@ -15,7 +15,12 @@ import { Link } from "react-router-dom"; 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 colorMode = React.useContext(ColorModeContext); return ( diff --git a/Front/src/coinApi.ts b/Front/src/coinApi.ts index 46f8365..c811e11 100644 --- a/Front/src/coinApi.ts +++ b/Front/src/coinApi.ts @@ -8,22 +8,52 @@ const globalUrl = '/global'; const coinInfoUrl = (id:string) => `/coin/${id}`; 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({ baseQuery: fetchBaseQuery({ baseUrl: baseUrl }), endpoints: (builder) => ({ - getGlobal: builder.query({ + getGlobal: builder.query({ query: () => globalUrl, }), - getCoinsList: builder.query({ - query: (payload: {page: number, per_page: number}) => `${marketsUrl}?per_page=${payload.per_page}&page=${payload.page}`, + getCoinsList: builder.query({ + query: (payload: IGetCoinsChartRequest) => `${marketsUrl}?per_page=${payload.per_page}&page=${payload.page}`, }), - getCoinsCount: builder.query({ + getCoinsCount: builder.query({ query: () => countUrl, }), - getCoinInfo: builder.query({ + getCoinInfo: builder.query({ query: (id: string) => coinInfoUrl(id), }), - getCoinChart: builder.query({ + getCoinChart: builder.query({ query: (id: string) => coinChartUrl(id), }), }), diff --git a/Front/src/coinDetails/coinChart.tsx b/Front/src/coinDetails/coinChart.tsx index 71a5128..00dac39 100644 --- a/Front/src/coinDetails/coinChart.tsx +++ b/Front/src/coinDetails/coinChart.tsx @@ -1,91 +1,116 @@ import { - Chart as ChartJS, - CategoryScale, - LinearScale, - PointElement, - LineElement, - Title, - Tooltip, - Filler, - Legend, + Chart as ChartJS, + CategoryScale, + LinearScale, + PointElement, + LineElement, + Title, + Tooltip, + Filler, + Legend, } from 'chart.js'; import { Line } from 'react-chartjs-2'; import { useGetCoinChartQuery } from '../coinApi'; import { format } from 'date-fns'; ChartJS.register( - CategoryScale, - LinearScale, - PointElement, - LineElement, - Title, - Tooltip, - Filler, - Legend + CategoryScale, + LinearScale, + PointElement, + LineElement, + Title, + Tooltip, + Filler, + Legend ); export const options = { - responsive: true, - plugins: { - legend: { - position: 'top' as const, - }, - title: { - display: true, - text: 'Chart.js Line Chart', - }, + responsive: true, + plugins: { + legend: { + position: 'top' as const, }, + title: { + display: true, + text: 'Chart.js Line Chart', + }, + }, }; -const CoinChart = (props: any): JSX.Element => { - // console.log("props: ", props); - // get chart data ------------------------------------------- - const { - data, - isSuccess: isSuccessChart - } = useGetCoinChartQuery(props.coin || 'btc'); // Todo: 404 +export interface ICoinChartProps { + coin: string; +} - // format chart data ---------------------------------------- - function formatChartData(data: any): any { - const chartData = data['prices'].map(function (value: any) { - // return value[1]; - return { - x: new Date(value[0]), - y: value[1], - }; - }); - console.log('formatChartData: ', chartData) - return chartData; - } +// export interface IChartData { +// [key: string]: any; +// } - // format data ---------------------------------------------- - function formatChartLabels(data: any): any { - const labels = data['prices'].map((value: (string | number | Date)[]) => format(new Date(value[0]), 'MM/dd/yyyy')); - console.log('formatChartLabels: ', labels) - return labels; - } - return ( + +// ---------------------------------------- IN +export interface IChartDataItem { + [index: number]: number, +} + +export interface IChartData { + market_caps: Array; + prices: Array; + total_volumes: Array; +} + +// ---------------------------------------- OUT +export interface IChartDataFormattedItem { + x: Date; + y: number; +} + +export interface IChartDataFormatted extends Array{}; + + + + +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 { + x: new Date(value[0]), + y: value[1], + }; + }); + console.log('formatChartData: ', chartData) + return chartData; + } + + 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')); + // console.log('formatChartLabels: ', labels) + return labels; + } + + return ( +
+ { + data &&
- { - data && -
- -
- } - - {/* ; */} +
- ) + } +
+ ) } export default CoinChart; diff --git a/Front/src/coinDetails/coinDetails.tsx b/Front/src/coinDetails/coinDetails.tsx index 9699a22..cdcb450 100644 --- a/Front/src/coinDetails/coinDetails.tsx +++ b/Front/src/coinDetails/coinDetails.tsx @@ -4,34 +4,30 @@ import CoinChart from './coinChart'; const CoinDetails = (): JSX.Element => { const { id } = useParams(); - console.log("id: ", id) - - // get info data const { data: dataInfo, error: errorInfo, isLoading: isLoadingInfo, isSuccess: isSuccessInfo, refetch: refetchInfo - } = useGetCoinInfoQuery(id || 'btc'); // Todo: 404 + } = useGetCoinInfoQuery(id ? id : ''); return (
{ - dataInfo && isSuccessInfo && -
-
ID: {dataInfo.id}
-
-
Homepage: {dataInfo.links.homepage}
+ dataInfo && isSuccessInfo && <> +
+
ID: {dataInfo.id}
+
+
Homepage: {dataInfo.links.homepage}
+
+
Description{dataInfo.description.en}
-
Description{dataInfo.description.en}
-
- } - { -
- -
+
+ +
+ }
diff --git a/Front/src/coinList/coinList.tsx b/Front/src/coinList/coinList.tsx index 85d4b89..24e96be 100644 --- a/Front/src/coinList/coinList.tsx +++ b/Front/src/coinList/coinList.tsx @@ -1,8 +1,6 @@ -// Route import { useSearchParams } from 'react-router-dom'; import { Link } from "react-router-dom"; -// Material UI import Table from '@mui/material/Table'; import TableBody from '@mui/material/TableBody'; import TableCell from '@mui/material/TableCell'; @@ -10,10 +8,8 @@ import TableContainer from '@mui/material/TableContainer'; import TableHead from '@mui/material/TableHead'; import TableRow from '@mui/material/TableRow'; -// Redux import { useGetCoinsListQuery, useGetGlobalQuery } from '../coinApi'; -// components import Pager from './coinListPager'; @@ -27,98 +23,99 @@ const CoinList = (): JSX.Element => { // For Query const payload = { page, per_page } - const { data, error, isLoading, isSuccess } = useGetCoinsListQuery(payload); - const { data: globalData, error: globalError, isLoading: globalIsLoading, isSuccess: lobalSuccess } = useGetGlobalQuery(); + const { data, isLoading, isSuccess } = useGetCoinsListQuery(payload); + const { data: globalData, isLoading: globalIsLoading, isSuccess: lobalSuccess } = useGetGlobalQuery(); return (
- {isLoading &&
Loading...
} + {isLoading || globalIsLoading &&
Loading...
} { globalData && - {`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`}
} - {data && isSuccess && - - - - - - # - - Coin - Current Price - - Low (24h) - - - Hight (24) - - Change (24h) - - - - - { - data.map((row: any, index: any) => ( - +
+ + + - + Coin + Current Price + + Low (24h) + + + Hight (24) + + Change (24h) + + + + + { + data.map((row: any, index: any) => ( + - {row.market_cap_rank} - - - - - {row.name} - {row.symbol} - - - ${row.current_price} - - ${row.low_24h} - - - ${row.high_24h} - - - 0 ? 'green' : 'red'}> - {row.price_change_percentage_24h}% - - - - ))} - -
-
+ + {row.market_cap_rank} + + + + + {row.name} + {row.symbol} + + + ${row.current_price} + + ${row.low_24h} + + + ${row.high_24h} + + + 0 ? 'green' : 'red'}> + {row.price_change_percentage_24h}% + + + + ))} + + + }
diff --git a/Front/src/coinList/coinListPager.tsx b/Front/src/coinList/coinListPager.tsx index 842d229..73a2906 100644 --- a/Front/src/coinList/coinListPager.tsx +++ b/Front/src/coinList/coinListPager.tsx @@ -15,23 +15,27 @@ const Pager = (props:any): JSX.Element => { const { data, error, isLoading, isSuccess, refetch } = useGetCoinsCountQuery(); const navigate = useNavigate(); - const lastPage = Math.ceil(data / per_page) + const lastPage = (data: number) => Math.ceil(data / per_page) function handlePager(page: number) { const url = `/?page=${page}` navigate(url) } - return ( - - - - - Page {page} from {lastPage} - - - - + return (<> + { + data && isSuccess && + + + + + Page {page} from {lastPage(data.count)} + + + + + } + ) }