Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added timer component for time sensitive promotions. #108

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/countDownTimer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import React, { Component } from 'react';

class CountDownTimer extends Component {
// counts down from the GMT time given in format ex{ Mon, 1 Jan 2022 08:00:00 GMT }
state = {
days: 0,
hours: '00',
minutes: '00',
seconds: '00',
finished: false
}

componentDidMount () {
setInterval(() => {
const endDate = new Date(this.props.date);
const timeLeft = endDate - new Date();
if (timeLeft < 1) {
this.setState({ finished: true });
} else {
const days = Math.floor(timeLeft / (1000 * 60 * 60 * 24));
const hours = Math.floor((timeLeft / (1000 * 60 * 60)) % 24);
const minutes = Math.floor((timeLeft / (1000 * 60)) % 60);
const seconds = Math.floor((timeLeft / (1000)) % 60);
this.setState({
hours: hours > 9 ? hours : `0${hours}`,
minutes: minutes > 9 ? minutes : `0${minutes}`,
seconds: seconds > 9 ? seconds : `0${seconds}`,
days
});
}
}, 1000);
}

PropperOutputContext () {
const { days, hours, minutes, seconds } = this.state;
if (days > 1) {
return `${days} Days : ${hours} Hours : ${minutes} Minutes : ${seconds} Seconds`;
}
if (days === 1) {
return `${days} Day : ${hours} Hours : ${minutes} Minutes : ${seconds} Seconds`;
} else {
return `${hours} Hours : ${minutes} Minutes : ${seconds} Seconds`;
}
}

render () {
const { finished } = this.state;
const countdown = this.PropperOutputContext();
return (!finished) ? (<p>{ `${countdown}` }</p>) : (<p>Keep an eye out for time sensitive promotions!</p>);
}
}

export default CountDownTimer;