turkmentv_front/components/vote/Countdown.tsx

109 lines
3.7 KiB
TypeScript
Raw Permalink Normal View History

import { Dispatch, SetStateAction, useEffect, useState } from "react";
import { differenceInSeconds, parseISO, addHours } from "date-fns";
import GradientTitle from "./GradientTitle";
2024-08-19 12:44:56 +00:00
interface CountdownProps {
startsAt: string;
endsAt: string;
2024-09-03 12:35:46 +00:00
setVoteStatus: Dispatch<SetStateAction<string | undefined>>;
2024-10-17 13:31:55 +00:00
setEventStatus: Dispatch<SetStateAction<string>>;
eventStatus: string;
2024-08-19 12:44:56 +00:00
}
2024-10-17 13:31:55 +00:00
const Countdown: React.FC<CountdownProps> = ({
startsAt,
endsAt,
setVoteStatus,
setEventStatus,
eventStatus,
}) => {
const [timeLeft, setTimeLeft] = useState<string>("");
2024-08-19 12:44:56 +00:00
useEffect(() => {
// Parsing the start and end times to Date objects in the correct format
const startsAtDate = addHours(parseISO(startsAt.replace(" ", "T")), 5); // Adjusting start time to UTC+5
const endsAtDate = addHours(parseISO(endsAt.replace(" ", "T")), 5); // Adjusting end time to UTC+5
2024-08-19 12:44:56 +00:00
// Function to calculate time left
const calculateTimeLeft = () => {
const now = new Date();
const nowUTC5 = addHours(now, 5); // Adjusting current time to UTC+5
let difference;
if (nowUTC5 < startsAtDate) {
setEventStatus("Not started");
2024-08-19 12:44:56 +00:00
difference = differenceInSeconds(startsAtDate, nowUTC5);
} else if (nowUTC5 < endsAtDate) {
setEventStatus("Started");
2024-08-19 12:44:56 +00:00
difference = differenceInSeconds(endsAtDate, nowUTC5);
} else {
setEventStatus("Finished");
setVoteStatus("closed");
2024-08-19 12:44:56 +00:00
return;
}
const hours = Math.floor(difference / 3600);
const minutes = Math.floor((difference % 3600) / 60);
const seconds = difference % 60;
setTimeLeft(
`${hours < 10 ? "0" + hours : hours}:${
minutes < 10 ? "0" + minutes : minutes
}:${seconds < 10 ? "0" + seconds : seconds}`
2024-08-19 12:44:56 +00:00
);
};
const timer = setInterval(calculateTimeLeft, 1000);
return () => clearInterval(timer);
}, [startsAt, endsAt]);
return (
<div>
{eventStatus === "Finished" ? (
<GradientTitle title={"Netijeler"} size="big" />
) : eventStatus === "Started" ? (
2024-08-19 12:44:56 +00:00
<div className="flex flex-col justify-center items-center">
<div className="text-[44px] sm:text-[100px] leading-[100%] flex items-end font-bold bg-fancyTitle bg-clip-text text-transparent text-center">
<h2 className="flex flex-col items-center justify-center">
{timeLeft.split(":")[0]}
</h2>
:
<h2 className="flex flex-col items-center justify-center">
{timeLeft.split(":")[1]}
</h2>
:
<h2 className="flex flex-col items-center justify-center">
{timeLeft.split(":")[2]}
</h2>
2024-08-19 12:44:56 +00:00
</div>
</div>
) : eventStatus === "Not started" ? (
2024-08-19 12:44:56 +00:00
<div className="flex flex-col justify-center items-center">
<h1 className="font-semibold text-[24px] uppercase text-fillNavyBlue">
Ses bermeklik
</h1>
2024-08-19 12:44:56 +00:00
<div className="text-[44px] sm:text-[100px] leading-[100%] flex items-end font-bold bg-fancyTitle bg-clip-text text-transparent text-center">
<h2 className="flex flex-col items-center justify-center">
{timeLeft.split(":")[0]}
</h2>
:
<h2 className="flex flex-col items-center justify-center">
{timeLeft.split(":")[1]}
</h2>
:
<h2 className="flex flex-col items-center justify-center">
{timeLeft.split(":")[2]}
</h2>
2024-08-19 12:44:56 +00:00
</div>
<h1 className="font-semibold text-[24px] uppercase text-fillNavyBlue mt-[16px]">
sagatdan başlaýar
</h1>
</div>
) : null}
</div>
);
};
export default Countdown;