2021-01-01 00:00:39 +01:00
|
|
|
import React, { Component } from 'react'
|
|
|
|
|
import './index.scss'
|
2018-12-31 20:53:32 +01:00
|
|
|
|
2021-01-01 00:00:39 +01:00
|
|
|
const _second = 1000
|
|
|
|
|
const _minute = _second * 60
|
|
|
|
|
const _hour = _minute * 60
|
|
|
|
|
const _day = _hour * 24
|
2018-12-31 20:53:32 +01:00
|
|
|
|
|
|
|
|
class App extends Component {
|
|
|
|
|
constructor() {
|
2021-01-01 00:00:39 +01:00
|
|
|
super()
|
2018-12-31 20:53:32 +01:00
|
|
|
|
2021-01-01 00:00:39 +01:00
|
|
|
const now = new Date()
|
|
|
|
|
let imWaitingForDrinking = true // of course default to true
|
|
|
|
|
let year = now.getFullYear() + 1
|
2018-12-31 20:53:32 +01:00
|
|
|
let countdownData = {}
|
|
|
|
|
|
|
|
|
|
if (now.getMonth() === 0 && now.getDate() === 1) {
|
2021-01-01 00:00:39 +01:00
|
|
|
imWaitingForDrinking = false
|
|
|
|
|
year = now.getFullYear()
|
|
|
|
|
} else {
|
|
|
|
|
countdownData = this.getMissingTime(now)
|
2018-12-31 20:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
|
isNewYear: !imWaitingForDrinking,
|
|
|
|
|
newYear: year,
|
|
|
|
|
countdownID: null,
|
2021-01-01 00:00:39 +01:00
|
|
|
countdownData: countdownData
|
|
|
|
|
}
|
2018-12-31 20:53:32 +01:00
|
|
|
}
|
|
|
|
|
|
2021-01-01 00:00:39 +01:00
|
|
|
getMissingTime = (now) => {
|
|
|
|
|
const newYear = now.getFullYear() + 1
|
|
|
|
|
|
|
|
|
|
if (this.state && this.state.newYear && this.state.newYear === now.getFullYear()) {
|
|
|
|
|
this.setState({
|
|
|
|
|
isNewYear: true
|
|
|
|
|
})
|
2018-12-31 20:53:32 +01:00
|
|
|
|
2021-01-01 00:00:39 +01:00
|
|
|
return this.state.countdownData
|
|
|
|
|
} else {
|
|
|
|
|
const midnight = new Date(`${newYear}-01-01 00:00`)
|
|
|
|
|
const delta = midnight - now
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
days: Math.floor(delta / _day),
|
|
|
|
|
hours: Math.floor((delta % _day) / _hour),
|
|
|
|
|
minutes: Math.floor((delta % _hour) / _minute),
|
|
|
|
|
seconds: Math.floor((delta % _minute) / _second)
|
|
|
|
|
}
|
2018-12-31 20:53:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
updateCountdown = () => {
|
|
|
|
|
this.setState({
|
|
|
|
|
countdownData: this.getMissingTime(new Date())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
|
this.setState({
|
|
|
|
|
countdownID: setInterval(this.updateCountdown, 1000)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
|
clearInterval(this.state.countdownID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
render() {
|
|
|
|
|
const { isNewYear, newYear, countdownData } = this.state
|
|
|
|
|
|
|
|
|
|
const getCountdownMarkup = (data, label) => {
|
|
|
|
|
if (data === 0) {
|
|
|
|
|
return null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let number = data.toString()
|
|
|
|
|
if (number.length < 2) {
|
|
|
|
|
number = `0${number}`
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<span className={`countdown-item ${label}`} key={label}>
|
|
|
|
|
{`${number} ${label}`}
|
|
|
|
|
</span>
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className="app-wrapper">
|
|
|
|
|
{isNewYear && (
|
|
|
|
|
<React.Fragment>
|
|
|
|
|
<div className="text-wrapper im-drinking">
|
|
|
|
|
<p>Happy New Year!</p>
|
|
|
|
|
<p className="welcome">
|
2021-01-01 00:00:39 +01:00
|
|
|
Welcome to <span className="welcome-newyear">{newYear}</span>!
|
2018-12-31 20:53:32 +01:00
|
|
|
</p>
|
|
|
|
|
</div>
|
|
|
|
|
<div className="pyro">
|
2021-01-01 00:00:39 +01:00
|
|
|
<div className="before" />
|
|
|
|
|
<div className="after" />
|
2018-12-31 20:53:32 +01:00
|
|
|
</div>
|
|
|
|
|
</React.Fragment>
|
|
|
|
|
)}
|
|
|
|
|
{!isNewYear && (
|
|
|
|
|
<div className="text-wrapper">
|
|
|
|
|
<p>Countdown to {newYear}</p>
|
|
|
|
|
<div className="countdown">
|
2021-01-01 00:00:39 +01:00
|
|
|
{getCountdownMarkup(countdownData['days'], 'd')}
|
2018-12-31 20:53:32 +01:00
|
|
|
{getCountdownMarkup(countdownData['hours'], 'h')}
|
|
|
|
|
{getCountdownMarkup(countdownData['minutes'], 'm')}
|
|
|
|
|
{getCountdownMarkup(countdownData['seconds'], 's')}
|
|
|
|
|
</div>
|
|
|
|
|
</div>
|
|
|
|
|
)}
|
|
|
|
|
</div>
|
2021-01-01 00:00:39 +01:00
|
|
|
)
|
2018-12-31 20:53:32 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-01 00:00:39 +01:00
|
|
|
export default App
|