Files
hpos-web/src/components/Card.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

2023-12-18 10:39:31 +05:30
import React, { useEffect, useState } from 'react';
2023-12-18 23:46:55 +05:30
import Odometer from 'react-odometerjs';
2023-12-18 10:39:31 +05:30
import './Card.css'; // Import your CSS file
2023-12-18 23:46:55 +05:30
import 'odometer/themes/odometer-theme-default.css';
2023-12-18 10:39:31 +05:30
const Card = ({ title, counter, color, backgroundColor }) => {
const [animatedCounter, setAnimatedCounter] = useState(0);
useEffect(() => {
const updateCounter = () => {
// Set the maximum number of iterations for the animation
const maxIterations = 50;
// Calculate the step value for each iteration
const step = Math.ceil(counter / maxIterations);
// Use setInterval to increment the counter gradually
const intervalId = setInterval(() => {
setAnimatedCounter((prevCounter) => {
const newCounter = prevCounter + step;
// Stop the animation when the counter reaches its final value
if (newCounter >= counter) {
clearInterval(intervalId);
return counter;
}
return newCounter;
});
}, 30); // Adjust the duration of each iteration as needed
// Clear the interval when the component is unmounted
return () => clearInterval(intervalId);
};
// Call the updateCounter function when the component mounts or the counter value changes
updateCounter();
}, [counter]);
return (
<div className='numeric-card' style={{ color, backgroundColor }}>
<div className='card-title'>{title}</div>
<div className='card-data'>
2023-12-18 23:46:55 +05:30
{/* <div className='odometer'>
2023-12-18 10:39:31 +05:30
<div>{animatedCounter}</div>
2023-12-18 23:46:55 +05:30
</div> */}
<Odometer value={counter} style={{ cursor: 'pointer' }} />
2023-12-18 10:39:31 +05:30
</div>
</div>
);
};
export default Card;