Skip to content

Commit

Permalink
feat: chart periods
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasmagnus committed Aug 23, 2023
1 parent 47665e3 commit 08f96d4
Show file tree
Hide file tree
Showing 16 changed files with 486 additions and 76 deletions.
9 changes: 7 additions & 2 deletions frontend/src/app/core/pages/asset-home/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { assetHomeHelper } from 'utils/constants/helpers'
import { AssetActions } from 'components/enums/asset-actions'
import { PathRoute } from 'components/enums/path-route'
import { ActionHelper } from 'components/molecules/action-helper'
import { TChartPeriod } from 'components/molecules/chart-period'
import { ManagementBreadcrumb } from 'components/molecules/management-breadcrumb'
import { MenuActionsAsset } from 'components/organisms/menu-actions-asset'
import { Sidebar } from 'components/organisms/sidebar'
Expand All @@ -18,6 +19,8 @@ export const AssetHome: React.FC = () => {
const [asset, setAsset] = useState<Hooks.UseAssetsTypes.IAssetDto>()
const [paymentsAsset, setPaymentsAsset] =
useState<Hooks.UseDashboardsTypes.IAsset>()
const [chartPeriod, setChartPeriod] = useState<TChartPeriod>('24h')

const { loadingAsset, getAssetById } = useAssets()
const { getPaymentsByAssetId, loadingChart } = useDashboards()
const { id } = useParams()
Expand All @@ -30,11 +33,11 @@ export const AssetHome: React.FC = () => {

useEffect(() => {
if (id) {
getPaymentsByAssetId(id).then(paymentsAsset =>
getPaymentsByAssetId(id, 0, chartPeriod).then(paymentsAsset =>
setPaymentsAsset(paymentsAsset)
)
}
}, [getPaymentsByAssetId, id])
}, [chartPeriod, getPaymentsByAssetId, id])

return (
<Flex>
Expand All @@ -50,6 +53,8 @@ export const AssetHome: React.FC = () => {
asset={asset}
loadingChart={loadingChart}
paymentsAsset={paymentsAsset}
chartPeriod={chartPeriod}
setChartPeriod={setChartPeriod}
/>
)}
</Flex>
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/app/core/pages/burn-asset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ import { FieldValues, UseFormSetValue } from 'react-hook-form'
import { useParams } from 'react-router-dom'

import { useAssets } from 'hooks/useAssets'
import { useDashboards } from 'hooks/useDashboards'
import { burnHelper } from 'utils/constants/helpers'
import { MessagesError } from 'utils/constants/messages-error'

import { AssetActions } from 'components/enums/asset-actions'
import { PathRoute } from 'components/enums/path-route'
import { ActionHelper } from 'components/molecules/action-helper'
import { TChartPeriod } from 'components/molecules/chart-period'
import { ManagementBreadcrumb } from 'components/molecules/management-breadcrumb'
import { MenuActionsAsset } from 'components/organisms/menu-actions-asset'
import { Sidebar } from 'components/organisms/sidebar'
import { BurnAssetTemplate } from 'components/templates/burn-asset'

export const BurnAsset: React.FC = () => {
const [asset, setAsset] = useState<Hooks.UseAssetsTypes.IAssetDto>()
const [mintOperations, setMintOperations] =
useState<Hooks.UseDashboardsTypes.IAsset>()
const [burnOperations, setBurnOperations] =
useState<Hooks.UseDashboardsTypes.IAsset>()
const [chartPeriod, setChartPeriod] = useState<TChartPeriod>('24h')

const { burn, getAssetById, loadingOperation, loadingAsset } = useAssets()
const { loadingChart, getPaymentsByAssetId } = useDashboards()
const { id } = useParams()
const toast = useToast()

Expand Down Expand Up @@ -63,6 +72,22 @@ export const BurnAsset: React.FC = () => {
}
}, [getAssetById, id])

useEffect(() => {
if (id) {
getPaymentsByAssetId(id, 2, chartPeriod).then(paymentsAsset => {
setMintOperations(paymentsAsset)
})
}
}, [chartPeriod, getPaymentsByAssetId, id])

useEffect(() => {
if (id) {
getPaymentsByAssetId(id, 5, chartPeriod).then(paymentsAsset => {
setBurnOperations(paymentsAsset)
})
}
}, [chartPeriod, getPaymentsByAssetId, id])

const toastError = (message: string): void => {
toast({
title: 'Burn error!',
Expand All @@ -85,9 +110,14 @@ export const BurnAsset: React.FC = () => {
) : (
<BurnAssetTemplate
onSubmit={onSubmit}
setChartPeriod={setChartPeriod}
loading={loadingOperation}
asset={asset}
assetData={asset.assetData}
mintOperations={mintOperations}
burnOperations={burnOperations}
loadingChart={loadingChart}
chartPeriod={chartPeriod}
/>
)}
</Flex>
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/app/core/pages/dashboards/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { useDashboards } from 'hooks/useDashboards'
import { useVaults } from 'hooks/useVaults'

import { PathRoute } from 'components/enums/path-route'
import { TChartPeriod } from 'components/molecules/chart-period'
import { Sidebar } from 'components/organisms/sidebar'
import { DashboardsTemplate } from 'components/templates/dashboards'

Expand All @@ -15,6 +16,7 @@ export const Dashboards: React.FC = () => {
useState<Hooks.UseVaultsTypes.IVaultCategory[]>()
const [paymentsAssets, setPaymentsAssets] =
useState<Hooks.UseDashboardsTypes.IAsset[]>()
const [chartPeriod, setChartPeriod] = useState<TChartPeriod>('24h')

const { loadingVaults, getVaults, getVaultCategories } = useVaults()
const { loadingChart, getPayments } = useDashboards()
Expand All @@ -31,8 +33,10 @@ export const Dashboards: React.FC = () => {
}, [getVaultCategories])

useEffect(() => {
getPayments().then(paymentsAssets => setPaymentsAssets(paymentsAssets))
}, [getPayments])
getPayments(chartPeriod).then(paymentsAssets =>
setPaymentsAssets(paymentsAssets)
)
}, [chartPeriod, getPayments])

useEffect(() => {
getAssets()
Expand All @@ -49,6 +53,8 @@ export const Dashboards: React.FC = () => {
vaultCategories={vaultCategories}
assets={assets}
loadingAssets={loadingAssets}
chartPeriod={chartPeriod}
setChartPeriod={setChartPeriod}
/>
</Sidebar>
</Flex>
Expand Down
30 changes: 30 additions & 0 deletions frontend/src/app/core/pages/mint-asset/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,29 @@ import { FieldValues, UseFormSetValue } from 'react-hook-form'
import { useParams } from 'react-router-dom'

import { useAssets } from 'hooks/useAssets'
import { useDashboards } from 'hooks/useDashboards'
import { mintHelper } from 'utils/constants/helpers'
import { MessagesError } from 'utils/constants/messages-error'

import { AssetActions } from 'components/enums/asset-actions'
import { PathRoute } from 'components/enums/path-route'
import { ActionHelper } from 'components/molecules/action-helper'
import { TChartPeriod } from 'components/molecules/chart-period'
import { ManagementBreadcrumb } from 'components/molecules/management-breadcrumb'
import { MenuActionsAsset } from 'components/organisms/menu-actions-asset'
import { Sidebar } from 'components/organisms/sidebar'
import { MintAssetTemplate } from 'components/templates/mint-asset'

export const MintAsset: React.FC = () => {
const [asset, setAsset] = useState<Hooks.UseAssetsTypes.IAssetDto>()
const [mintOperations, setMintOperations] =
useState<Hooks.UseDashboardsTypes.IAsset>()
const [burnOperations, setBurnOperations] =
useState<Hooks.UseDashboardsTypes.IAsset>()
const [chartPeriod, setChartPeriod] = useState<TChartPeriod>('24h')

const { mint, getAssetById, loadingOperation, loadingAsset } = useAssets()
const { loadingChart, getPaymentsByAssetId } = useDashboards()
const { id } = useParams()
const toast = useToast()

Expand Down Expand Up @@ -63,6 +72,22 @@ export const MintAsset: React.FC = () => {
}
}, [getAssetById, id])

useEffect(() => {
if (id) {
getPaymentsByAssetId(id, 2, chartPeriod).then(paymentsAsset => {
setMintOperations(paymentsAsset)
})
}
}, [chartPeriod, getPaymentsByAssetId, id])

useEffect(() => {
if (id) {
getPaymentsByAssetId(id, 5, chartPeriod).then(paymentsAsset => {
setBurnOperations(paymentsAsset)
})
}
}, [chartPeriod, getPaymentsByAssetId, id])

const toastError = (message: string): void => {
toast({
title: 'Mint error!',
Expand All @@ -85,9 +110,14 @@ export const MintAsset: React.FC = () => {
) : (
<MintAssetTemplate
onSubmit={onSubmit}
setChartPeriod={setChartPeriod}
loading={loadingOperation}
asset={asset}
assetData={asset.assetData}
mintOperations={mintOperations}
burnOperations={burnOperations}
loadingChart={loadingChart}
chartPeriod={chartPeriod}
/>
)}
</Flex>
Expand Down
135 changes: 135 additions & 0 deletions frontend/src/components/molecules/chart-mint-burn/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import {
Box,
Container,
Flex,
Skeleton,
Text,
useColorMode,
} from '@chakra-ui/react'
import { Dispatch, SetStateAction } from 'react'
import Chart from 'react-apexcharts'

import { getChartLabels, isEqualLabel } from 'utils/constants/dashboards'
import { toCrypto } from 'utils/formatter'

import { ChartPeriod, TChartPeriod } from '../chart-period'

interface IChartMintBurn {
loadingChart: boolean
mintOperations: Hooks.UseDashboardsTypes.IAsset
burnOperations: Hooks.UseDashboardsTypes.IAsset
chartPeriod: TChartPeriod
setChartPeriod: Dispatch<SetStateAction<TChartPeriod>>
}

export const ChartMintBurn: React.FC<IChartMintBurn> = ({
mintOperations,
burnOperations,
chartPeriod,
loadingChart,
setChartPeriod,
}) => {
const { colorMode } = useColorMode()

const series = [
{
name: 'Mint',
data: getChartLabels(chartPeriod).map(label => {
const index = mintOperations.date?.findIndex(date =>
isEqualLabel(chartPeriod, date, label)
)
return index > -1 ? mintOperations.amount[index] : 0
}),
},
{
name: 'Burn',
data: getChartLabels(chartPeriod).map(label => {
const index = burnOperations.date?.findIndex(date =>
isEqualLabel(chartPeriod, date, label)
)
return index > -1 ? burnOperations.amount[index] : 0
}),
},
]

const options = {
chart: {
id: 'line',
foreColor: colorMode === 'dark' ? 'white' : 'black',
toolbar: {
show: true,
offsetX: 0,
offsetY: 0,
tools: {
download: false,
selection: false,
zoom: true,
zoomin: true,
zoomout: true,
pan: false,
reset: true,
},
},
stroke: {
curve: 'smooth',
},
},
tooltip: {
theme: colorMode,
},
xaxis: {
categories: getChartLabels(chartPeriod),
labels: {
show: true,
formatter: function (value: string): string {
return `${chartPeriod === '24h' ? `${value}h` : `${value}`}`
},
},
},
yaxis: {
labels: {
show: true,
formatter: function (value: number): string {
return `${toCrypto(value)}`
},
},
},
dataLabels: {
enabled: false,
},
}

return (
<Container
variant="primary"
justifyContent="center"
py="0.5rem"
px="0.75rem"
w="full"
maxW="full"
mt="1rem"
>
<Flex justifyContent="space-between" mb="1.25rem">
<Text fontSize="xs" fontWeight="600">
Mint/Burn timeline
</Text>
<Flex>
<ChartPeriod period={chartPeriod} setPeriod={setChartPeriod} />
</Flex>
</Flex>
<Box>
{loadingChart ? (
<Skeleton h="12rem" w="full" />
) : (
<Chart
options={options}
series={series}
type="line"
width="100%"
height="320px"
/>
)}
</Box>
</Container>
)
}
47 changes: 47 additions & 0 deletions frontend/src/components/molecules/chart-period/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import { Button, Container, Flex } from '@chakra-ui/react'
import React, { Dispatch, SetStateAction } from 'react'

export type TChartPeriod = '24h' | '7d' | '30d'

interface IChartPeriod {
period: TChartPeriod
setPeriod: Dispatch<SetStateAction<TChartPeriod>>
}

export const ChartPeriod: React.FC<IChartPeriod> = ({ period, setPeriod }) => {
return (
<Container variant="primary" p="0" h="2rem">
<Flex>
<Button
variant={period === '24h' ? 'menuButtonSelected' : 'menuButton'}
borderStartRadius="0.25rem"
h="2rem"
onClick={(): void => {
setPeriod('24h')
}}
>
Last 24h
</Button>
<Button
variant={period === '7d' ? 'menuButtonSelected' : 'menuButton'}
h="2rem"
onClick={(): void => {
setPeriod('7d')
}}
>
7 days
</Button>
<Button
variant={period === '30d' ? 'menuButtonSelected' : 'menuButton'}
h="2rem"
borderEndRadius="0.25rem"
onClick={(): void => {
setPeriod('30d')
}}
>
30 days
</Button>
</Flex>
</Container>
)
}
Loading

0 comments on commit 08f96d4

Please sign in to comment.