Skip to content

Commit

Permalink
fix: lint warnings in countdown container
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Sep 2, 2020
1 parent 9c600d7 commit ca14b49
Showing 1 changed file with 26 additions and 30 deletions.
56 changes: 26 additions & 30 deletions src/components/Countdown/CountdownContainer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,60 +3,56 @@ import Countdown from './Countdown';
import PropTypes from 'prop-types';
import styles from './CountdownContainer.module.scss';

const getRemainingTime = (targetDate) => {
const countDownDate = new Date(targetDate).getTime();
const now = new Date().getTime();

const milliseconds = countDownDate - now;

const days = Math.floor(milliseconds / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(milliseconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((milliseconds % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((milliseconds % (1000 * 60)) / 1000);

return { days, hours, minutes, seconds, milliseconds };
};

const CountdownContainer = ({ targetDate, inactiveMessage }) => {
const [countdown, setCountdown] = React.useState(() => getRemainingTime());
let active = countdown.milliseconds > 0;
const [countdown, setCountdown] = React.useState(() =>
getRemainingTime(targetDate)
);
const active = countdown.milliseconds > 0;

useEffect(() => {
const hasRemainingTime = () => {
const { milliseconds } = getRemainingTime();
const { milliseconds } = getRemainingTime(targetDate);
return milliseconds > 0;
};

if (!hasRemainingTime()) {
active = false;
return;
}

const interval = setInterval(() => {
const {
days,
hours,
minutes,
seconds,
milliseconds,
} = getRemainingTime();
const { days, hours, minutes, seconds, milliseconds } = getRemainingTime(
targetDate
);

if (milliseconds < 0) {
active = false;
clearInterval(interval);
} else {
setCountdown({ days, hours, minutes, seconds, milliseconds });
}
}, 1000);

return () => clearInterval(interval);
}, []);

function getRemainingTime() {
const countDownDate = new Date(targetDate).getTime();
const now = new Date().getTime();

const milliseconds = countDownDate - now;

const days = Math.floor(milliseconds / (1000 * 60 * 60 * 24));
const hours = Math.floor(
(milliseconds % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
);
const minutes = Math.floor((milliseconds % (1000 * 60 * 60)) / (1000 * 60));
const seconds = Math.floor((milliseconds % (1000 * 60)) / 1000);

return { days, hours, minutes, seconds, milliseconds };
}
}, [targetDate]);

return (
<div className={styles.container}>
{active === true ? (
{active ? (
<div className={styles.countdownContainer}>
<Countdown countdown={countdown} />
</div>
Expand Down

0 comments on commit ca14b49

Please sign in to comment.