From b02510e26e7e9bc292a1779bd23801014b94aad4 Mon Sep 17 00:00:00 2001 From: Rafi Zadanly Date: Wed, 26 Apr 2023 17:23:52 +0700 Subject: flash sale and countdown --- .../components/elements/CountDown/CountDown.jsx | 55 ++++++++++++++++++++++ 1 file changed, 55 insertions(+) create mode 100644 src/core/components/elements/CountDown/CountDown.jsx (limited to 'src/core/components/elements/CountDown') diff --git a/src/core/components/elements/CountDown/CountDown.jsx b/src/core/components/elements/CountDown/CountDown.jsx new file mode 100644 index 00000000..49a33d77 --- /dev/null +++ b/src/core/components/elements/CountDown/CountDown.jsx @@ -0,0 +1,55 @@ +import { useEffect, useState } from 'react' + +const CountDown = ({ initialTime }) => { + const days = Math.floor(initialTime / 86400) + const hours = Math.floor((initialTime % 86400) / 3600) + const minutes = Math.floor((initialTime % 3600) / 60) + const seconds = initialTime % 60 + + const [timeLeft, setTimeLeft] = useState({ + day: days, + hour: hours, + minute: minutes, + second: seconds + }) + + useEffect(() => { + const timer = setInterval(() => { + const totalSeconds = timeLeft.day * 86400 + timeLeft.hour * 3600 + timeLeft.minute * 60 + timeLeft.second + const secondsLeft = totalSeconds - 1 + if (secondsLeft < 0) { + clearInterval(timer) + } else { + const days = Math.floor(secondsLeft / 86400) + const hours = Math.floor((secondsLeft % 86400) / 3600) + const minutes = Math.floor((secondsLeft % 3600) / 60) + const seconds = secondsLeft % 60 + setTimeLeft({ day: days, hour: hours, minute: minutes, second: seconds }) + } + }, 1000) + return () => clearInterval(timer) + }, [timeLeft]) + + return ( +
+
+ {timeLeft.day} + Hari +
+
+ {timeLeft.hour} + Jam +
+
+ {timeLeft.minute} + Menit +
+
+ {timeLeft.second} + Detik +
+
+ ) +} + +export default CountDown -- cgit v1.2.3