Skip to content

Instantly share code, notes, and snippets.

@DragonOsman
Created February 27, 2023 01:37
Show Gist options
  • Save DragonOsman/5c5ba4dcdaf12bf47eace5e05ad5b5dd to your computer and use it in GitHub Desktop.
Save DragonOsman/5c5ba4dcdaf12bf47eace5e05ad5b5dd to your computer and use it in GitHub Desktop.
import React from "react";
import "./App.css";
import CountdownTimer from "./CountdownTimer";
const App = () => {
const THREE_DAYS_IN_MS = 3 * 24 * 60 * 60 * 1000;
const NOW_IN_MS = new Date().getTime();
const dateTimeAfterThreeDays = NOW_IN_MS + THREE_DAYS_IN_MS;
return (
<div>
<h1>Countdown Timer</h1>
<CountdownTimer targetDate={dateTimeAfterThreeDays} />
</div>
);
};
export default App;
import React from "react";
import { useCountdown } from "./hooks/useCountdown";
import DateTimeDisplay from "./DateTimeDisplay";
const ExpiredNotice = () => {
return (
<div className="expired-notice">
<span>Expired!!!</span>
<p>Please select a future date and time.</p>
</div>
);
};
const ShowCounter = ({ days, hours, minutes, seconds }) => {
return (
<div className="show-counter">
<a
href="https://tapasadhikary.com"
target="_blank"
rel="noopener noreferrer"
className="countdown-link"
>
<DateTimeDisplay value={days} type={"Days"} isDanger={days <= 3} />
<p>:</p>
<DateTimeDisplay value={hours} type={"Hours"} isDanger={false} />
<p>:</p>
<DateTimeDisplay value={minutes} type={"Minutes"} isDanger={false} />
<p>:</p>
<DateTimeDisplay value={seconds} type={"Seconds"} isDanger={false} />
</a>
</div>
);
};
const CountdownTimer = ({ targetTime }) => {
const [days, hours, minutes, seconds] = useCountdown(targetTime);
if (days + hours + minutes + seconds <= 0) {
return <ExpiredNotice />;
} else {
return (
<ShowCounter
days={days}
hours={hours}
minutes={minutes}
seconds={seconds}
/>
);
}
};
export default CountdownTimer;
import React from "react";
const DateTimeDisplay = ({ value, type, isDanger }) => {
return (
<div className={isDanger ? "countdown-danger" : "countdown"}>
<p>{value}</p>
<span>{type}</span>
</div>
);
};
export default DateTimeDisplay;
import { useEffect, useState } from "react";
const useCountdown = (targetDate) => {
console.log(targetDate);
const countDownDate = new Date(targetDate).getTime();
const [countDown, setCountDown] = useState(
countDownDate - new Date().getTime()
);
useEffect(() => {
const interval = setInterval(() => {
setCountDown(countDownDate - new Date().getTime());
});
return () => clearInterval(interval);
}, [countDownDate]);
return getReturnValues(countDown);
};
const getReturnValues = (countDown) => {
const days = Math.floor(countDown / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(countDown % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((countDown % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((countDown % (1000 * 60)) / 1000);
return [days, hours, minutes, seconds];
};
export { useCountdown };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment