Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 19 additions & 14 deletions src/pages/blocks/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -145,15 +145,19 @@ export const blocksRowSections = (block: IBlock): IRowSection[] => {
const Blocks: React.FC<PropsWithChildren<IBlocks>> = () => {
const blocksWatcherInterval = 4 * 1000; // 4 secs
const [blocksInterval, setBlocksInterval] = useState(0);
const { data: blocksStatsToday, isLoading: isLoadingBlocksStatsToday } =
useQuery({
queryKey: ['statisticsCall'],
queryFn: totalStatisticsCall,
});
const {
data: blocksStatsToday,
isLoading: isLoadingBlocksStatsToday,
refetch: refetchTotal,
} = useQuery({
queryKey: ['statisticsCall'],
queryFn: totalStatisticsCall,
});
const {
data: blocksStatsYesterday,
refetch,
refetch: refetchYesterday,
isLoading: isLoadingBlocksStatsYesterday,
dataUpdatedAt,
} = useQuery({
queryKey: ['yesterdayStatisticsCall'],
queryFn: yesterdayStatisticsCall,
Expand All @@ -171,7 +175,8 @@ const Blocks: React.FC<PropsWithChildren<IBlocks>> = () => {
useEffect(() => {
if (blocksInterval) {
const intervalId = setInterval(() => {
refetch();
refetchYesterday();
refetchTotal();
}, blocksWatcherInterval);
return () => clearInterval(intervalId);
}
Expand Down Expand Up @@ -250,21 +255,21 @@ const Blocks: React.FC<PropsWithChildren<IBlocks>> = () => {
title,
headers,
values,
dataUpdatedAt,
}) => {
const [uptime] = useState(new Date().getTime());
const [age, setAge] = useState(getAge(new Date()));
const [age, setAge] = useState(
getAge(new Date(dataUpdatedAt ?? Date.now())),
);

useEffect(() => {
const interval = setInterval(() => {
const newAge = getAge(new Date(uptime));

setAge(newAge);
setAge(getAge(new Date(dataUpdatedAt ?? Date.now())));
}, 1 * 1000); // 1 sec

return () => {
clearInterval(interval);
};
}, []);
}, [dataUpdatedAt]);

return (
<Card>
Expand Down Expand Up @@ -308,7 +313,7 @@ const Blocks: React.FC<PropsWithChildren<IBlocks>> = () => {

<CardContainer>
{cards.map((card, index) => (
<CardContent key={index} {...card} />
<CardContent key={index} {...card} dataUpdatedAt={dataUpdatedAt} />
))}
</CardContainer>

Expand Down
19 changes: 9 additions & 10 deletions src/services/apiCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,17 @@ import { IBlockResponse, IBlockStats } from '@/types/blocks';
export const blockCall = async (
page = 1,
limit = 10,
): Promise<IBlockResponse> =>
new Promise<IBlockResponse>(async (resolve, reject) => {
const res = await api.get({
route: `block/list?page=${page}&limit=${limit}`,
});
): Promise<IBlockResponse> => {
const res = await api.get({
route: `block/list?page=${page}&limit=${limit}`,
});

if (!res.error || res.error === '') {
resolve(res);
}
if (!res.error || res.error === '') {
return res;
}

reject(res.error);
});
throw new Error(res.error);
};

export const yesterdayStatisticsCall = async (): Promise<IBlockStats> => {
const res = await api.get({
Expand Down
1 change: 1 addition & 0 deletions src/types/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,5 @@ export interface ICard {
title: string;
headers: string[];
values: (string | React.ReactElement)[];
dataUpdatedAt?: number;
}
Loading