| 1 |
import { useEffect } from '@wordpress/element'; |
| 2 |
|
| 3 |
import { withState } from '@wordpress/compose'; |
| 4 |
import GutenifyCountDownInspectorControls from './GutenifyCountDownInspectorControls'; |
| 5 |
import GutenifyCountDownBlock from './Block'; |
| 6 |
import { getTimeRemaining } from '../../../helpers/clock' |
| 7 |
|
| 8 |
const edit = withState({ |
| 9 |
isCalenderVisible: false, |
| 10 |
isTimerStart: false, |
| 11 |
timerInterval: false, |
| 12 |
dateTime: new Date(), |
| 13 |
timerDays: '00', |
| 14 |
timerHour: '00', |
| 15 |
timerMinute: '00', |
| 16 |
timerSecond: '00', |
| 17 |
})((props) => { |
| 18 |
const initializeCountDown = (dateTime) => { |
| 19 |
|
| 20 |
let $this = props; |
| 21 |
function updateClock() { |
| 22 |
var t = getTimeRemaining(dateTime); |
| 23 |
|
| 24 |
$this.setState({ |
| 25 |
timerDays: t.days, |
| 26 |
timerHour: ('0' + t.hours).slice(-2), |
| 27 |
timerMinute: ('0' + t.minutes).slice(-2), |
| 28 |
timerSecond: ('0' + t.seconds).slice(-2), |
| 29 |
}); |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
if (t.total <= 0 || isNaN(t.total)) { |
| 34 |
$this.setState({ |
| 35 |
timerDays: '00', |
| 36 |
timerHour: '00', |
| 37 |
timerMinute: '00', |
| 38 |
timerSecond: '00', |
| 39 |
}); |
| 40 |
clearInterval(timeinterval); |
| 41 |
} |
| 42 |
} |
| 43 |
updateClock(); |
| 44 |
var timeinterval = setInterval(updateClock, 1000); |
| 45 |
|
| 46 |
|
| 47 |
props.setState({ |
| 48 |
timerInterval: timeinterval |
| 49 |
}); |
| 50 |
} |
| 51 |
useEffect(() => { |
| 52 |
// console.log(props.attributes.dateTime); |
| 53 |
|
| 54 |
// props.setState({ |
| 55 |
// dateTime: new Date( props.attributes.dateTime ) |
| 56 |
// }) |
| 57 |
|
| 58 |
initializeCountDown( props.attributes.dateTime ); |
| 59 |
|
| 60 |
}, []); |
| 61 |
const { showDays, showHour, showMinute, showSecond, showLabel, customLabel, customLabelDays, customLabelHours, customLabelMinutes, customLabelSeconds, digitAttrs, titleAttrs, borderAttrs, containerWidth, backgroundAttrs, counterPosition } = props.attributes; |
| 62 |
return ( |
| 63 |
<div className="gutenify-count-down-wrapper"> |
| 64 |
<GutenifyCountDownInspectorControls {...props} /> |
| 65 |
<div style={{ |
| 66 |
width: `${containerWidth.desktop.value ? containerWidth.desktop.value : 100}${containerWidth.desktop.unit}`, |
| 67 |
marginTop: 0, |
| 68 |
marginBottom: 0, |
| 69 |
marginLeft: 'auto',// 'center' === counterPosition || 'right' === counterPosition ? 'auto' : 0, |
| 70 |
marginRight: 'auto'//'center' === counterPosition || 'left' === counterPosition ? 'auto' : 0, |
| 71 |
}}> |
| 72 |
<GutenifyCountDownBlock {...props} /> |
| 73 |
</div> |
| 74 |
</div> |
| 75 |
); |
| 76 |
}); |
| 77 |
|
| 78 |
export default edit; |