Skip to content

Commit 4a3d86e

Browse files
authored
Merge pull request #149 from divyansh884/patch-3
Refactor CountDown component to use elapsedTime
2 parents a9cff07 + 96269b0 commit 4a3d86e

1 file changed

Lines changed: 35 additions & 18 deletions

File tree

src/components/CountDown/CountDown.jsx

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ if (typeof window !== "undefined") {
1313

1414
// Import FlipClock CSS and JS with correct paths (use the exported path from package.json)
1515
import "flipclock/themes/flipclock";
16-
import { flipClock, countDown, theme } from "flipclock";
16+
import { flipClock, elapsedTime, theme } from "flipclock";
1717

1818
const CountDown = () => {
1919
const messageRef = useRef(null);
@@ -87,7 +87,7 @@ const CountDown = () => {
8787
}
8888
};
8989

90-
const initializeFlipClock = (endTime, shouldCount = true) => {
90+
const initializeFlipClock = (startTime, endTime, shouldCount = true) => {
9191
if (!isFlipClockReady) {
9292
console.warn("FlipClock is not ready yet");
9393
return;
@@ -108,25 +108,29 @@ const CountDown = () => {
108108
// Clear the container
109109
flipClockRef.current.innerHTML = "";
110110

111-
let countdownFace;
111+
let elapsedTimeFace;
112112

113113
if (shouldCount) {
114-
// Countdown mode - count down to end time
115-
countdownFace = countDown({
116-
to: new Date(endTime), // Count down to this time
114+
// Countdown mode - swap from and to to count DOWN (from future to past)
115+
// This shows REMAINING time by counting from endTime back to current time
116+
elapsedTimeFace = elapsedTime({
117+
from: new Date(endTime), // Start from the END time
118+
to: new Date(startTime), // Count back to START time
117119
format: "hh:mm:ss",
118120
});
119121
} else {
120-
// Static mode - show 00:00:00
121-
countdownFace = countDown({
122-
to: new Date(), // Already passed, shows 00:00:00
122+
// Static clock - show 00:00:00 when stopped
123+
const now = Date.now();
124+
elapsedTimeFace = elapsedTime({
125+
from: new Date(now),
126+
to: new Date(now), // Same time = 00:00:00
123127
format: "hh:mm:ss",
124128
});
125129
}
126130

127-
// Create the FlipClock with the countdown face and theme
131+
// Create the FlipClock with the elapsed time face and theme
128132
const fc = flipClock({
129-
face: countdownFace,
133+
face: elapsedTimeFace,
130134
theme: theme(),
131135
parent: flipClockRef.current,
132136
autoStart: shouldCount, // Only auto-start if we want counting
@@ -145,7 +149,7 @@ const CountDown = () => {
145149
const currentTime = Date.now();
146150
const endTime = currentTime + countdownDuration;
147151
await setCounterData(true, currentTime, endTime);
148-
initializeFlipClock(endTime, true); // true = start counting down
152+
initializeFlipClock(currentTime, endTime, true); // true = start counting down
149153
if (startButtonRef.current)
150154
startButtonRef.current.style.display = "none";
151155
} else {
@@ -182,8 +186,12 @@ const CountDown = () => {
182186
if (startButtonRef.current)
183187
startButtonRef.current.style.display = "none";
184188

185-
// Initialize clock with countdown to end time
186-
initializeFlipClock(counterData.endTime, true);
189+
// Initialize clock with countdown (remaining time)
190+
initializeFlipClock(
191+
counterData.startTime,
192+
counterData.endTime,
193+
true
194+
);
187195

188196
// Set up interval to check if time has ended
189197
intervalId = setInterval(async () => {
@@ -197,7 +205,11 @@ const CountDown = () => {
197205
}
198206

199207
// Reinitialize clock to show 00:00:00 (static)
200-
initializeFlipClock(counterData.endTime, false);
208+
initializeFlipClock(
209+
counterData.startTime,
210+
counterData.endTime,
211+
false
212+
);
201213

202214
// Clear the interval
203215
clearInterval(intervalId);
@@ -214,16 +226,21 @@ const CountDown = () => {
214226
startButtonRef.current.style.display = "none";
215227

216228
// Show 00:00:00 (static, not counting)
217-
initializeFlipClock(counterData.endTime, false);
229+
initializeFlipClock(
230+
counterData.startTime,
231+
counterData.endTime,
232+
false
233+
);
218234
}
219235
} else {
220236
// No countdown has been started yet - show button and clock at 28:00:00
221237
if (startButtonRef.current)
222238
startButtonRef.current.style.display = "block";
223239

224240
// Show static clock at 28:00:00 when no countdown is active
225-
const futureTime = Date.now() + countdownDuration;
226-
initializeFlipClock(futureTime, false);
241+
const now = Date.now();
242+
const futureTime = now + countdownDuration;
243+
initializeFlipClock(now, futureTime, false); // false = don't count
227244
}
228245
})();
229246

0 commit comments

Comments
 (0)