2018-02-05 11:40:33 -08:00
|
|
|
import React, { Component } from 'react';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
|
|
|
interface Props {
|
|
|
|
initialTime: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
interface State {
|
2018-02-08 09:30:30 -08:00
|
|
|
timeDisplay: string;
|
2018-02-05 11:40:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
class CountDown extends Component<Props, State> {
|
2018-02-08 09:30:30 -08:00
|
|
|
public state = { timeDisplay: '' };
|
2018-02-05 11:40:33 -08:00
|
|
|
|
2018-02-08 09:30:30 -08:00
|
|
|
public componentDidMount() {
|
2018-02-05 11:40:33 -08:00
|
|
|
this.startCountDown();
|
|
|
|
}
|
|
|
|
|
|
|
|
public render() {
|
2018-02-08 09:30:30 -08:00
|
|
|
return <p>{this.state.timeDisplay}</p>;
|
2018-02-05 11:40:33 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
private startCountDown = () => {
|
2018-02-08 09:30:30 -08:00
|
|
|
const time = moment(this.props.initialTime);
|
2018-03-07 15:36:05 -08:00
|
|
|
let intervalId: number;
|
2018-02-08 09:30:30 -08:00
|
|
|
|
|
|
|
const setTimeDisplay = () => {
|
|
|
|
const diff = moment.duration(time.diff(moment()));
|
|
|
|
let timeDisplay;
|
2018-02-05 11:40:33 -08:00
|
|
|
|
2018-02-08 09:30:30 -08:00
|
|
|
if (diff) {
|
|
|
|
const pieces = [
|
|
|
|
diff.days() > 0 && `${diff.days()} days`,
|
|
|
|
diff.hours() > 0 && `${diff.hours()} hours`,
|
|
|
|
diff.minutes() > 0 && `${diff.minutes()} minutes`,
|
|
|
|
diff.seconds() > 0 && `${diff.seconds()} seconds`
|
|
|
|
].filter(piece => !!piece);
|
|
|
|
timeDisplay = `in ${pieces.join(', ')}`;
|
|
|
|
} else {
|
|
|
|
clearInterval(intervalId);
|
|
|
|
timeDisplay = 'Auction is over!';
|
2018-02-05 11:40:33 -08:00
|
|
|
}
|
|
|
|
|
2018-02-08 09:30:30 -08:00
|
|
|
this.setState({ timeDisplay });
|
|
|
|
};
|
|
|
|
|
2018-03-07 15:36:05 -08:00
|
|
|
intervalId = window.setInterval(setTimeDisplay, 1000);
|
2018-02-08 09:30:30 -08:00
|
|
|
setTimeDisplay();
|
2018-02-05 11:40:33 -08:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ITime {
|
|
|
|
text: string;
|
|
|
|
time: number;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ENSTime: React.SFC<ITime> = ({ text, time }) => (
|
2018-02-08 09:30:30 -08:00
|
|
|
<section>
|
2018-02-05 11:40:33 -08:00
|
|
|
<p>{text}</p>
|
2018-02-08 09:30:30 -08:00
|
|
|
<h4>{moment(time).format('LLLL')}</h4>
|
2018-02-05 11:40:33 -08:00
|
|
|
<CountDown initialTime={time} />
|
|
|
|
</section>
|
|
|
|
);
|
|
|
|
|
|
|
|
export default ENSTime;
|