From b4aa0bf9dae9fbef95fe1aa3547c8505c995e80f Mon Sep 17 00:00:00 2001 From: Kakabay <2kakabayashyrberdyew@gmail.com> Date: Wed, 11 Dec 2024 18:11:44 +0500 Subject: [PATCH] lottery countdown allert component added --- .../LotteryCountDownAllert.tsx | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 components/lottery/countDown/countDownAllert/LotteryCountDownAllert.tsx diff --git a/components/lottery/countDown/countDownAllert/LotteryCountDownAllert.tsx b/components/lottery/countDown/countDownAllert/LotteryCountDownAllert.tsx new file mode 100644 index 0000000..bba1f74 --- /dev/null +++ b/components/lottery/countDown/countDownAllert/LotteryCountDownAllert.tsx @@ -0,0 +1,76 @@ +'use client'; + +import { calculateTimeLeft } from '@/lib/hooks/useCalculateTimeLeft'; +import React, { useState, useEffect, Dispatch, SetStateAction } from 'react'; + +interface LotteryCountDownAllertProps { + startDate: string; // Event start date in "YYYY-MM-DD HH:mm:ss" format + endDate: string; // Event end date in "YYYY-MM-DD HH:mm:ss" format + lotteryStatus: string; + setLotteryStatus: Dispatch>; +} + +const LotteryCountDownAllert: React.FC = ({ + startDate, + endDate, + lotteryStatus, + setLotteryStatus, +}) => { + const [timeLeft, setTimeLeft] = useState({ + hours: 0, + minutes: 0, + seconds: 0, + }); + + useEffect(() => { + const timer = setInterval(() => { + if (lotteryStatus === 'not-started') { + const timeToStart = calculateTimeLeft(startDate); + setTimeLeft(timeToStart); + + if (timeToStart.hours === 0 && timeToStart.minutes === 0 && timeToStart.seconds === 0) { + setLotteryStatus('started'); // Update status to "started" + } + } else if (lotteryStatus === 'started') { + const timeToEnd = calculateTimeLeft(endDate); + setTimeLeft(timeToEnd); + + if (timeToEnd.hours === 0 && timeToEnd.minutes === 0 && timeToEnd.seconds === 0) { + setLotteryStatus('ended'); // Update status to "finished" + } + } + }, 1000); + + return () => clearInterval(timer); // Clean up interval on component unmount + }, [startDate, endDate, lotteryStatus, setLotteryStatus]); + + return ( +
+
+ + + + + {lotteryStatus !== 'ended' ? ( + + Bije {timeLeft.hours}:{timeLeft.minutes}:{timeLeft.seconds}-dan(den) tamamlanýar + + ) : ( + Bije tamamlandy + )} +
+
+ ); +}; + +export default LotteryCountDownAllert;