import { CountdownTimerProps , InjectedCountdownTimerProps , CountdownTimerState , ProgressBarCountdownProps , TwoPhaseProgressBarCountdownProps , } from "./types" ; import { TextCountdownTimerComponent } from "./TextCountdownTimer" ; import { ProgressBarCountdownTimerComponent } from "./ProgressBarCountdownTimer" ; import { TwoPhaseProgressBarCountdownTimerComponent } from "./TwoPhaseProgressBarCountdownTimer" ; import { SmallProgressBarCountdownTimerComponent } from "./SmallProgressBarCountdownTimer" ; import * as React from "react" ; const connectCountdownTimer = ( ) => < TCountdownTimerProps extends CountdownTimerProps > ( CountdownTimerComponent ) => { return class HOCountdownTimerContainer extends React . Component < TCountdownTimerProps , CountdownTimerState > { public timer ? : number ; constructor ( props ) { super ( props ) ; this . state = { secondsRemaining : 0 , } ; } public render ( ) { return < CountdownTimerComponent { ... this . props } { ... this . state } />; } public async componentDidMount ( ) { return this . initCountdown ( ) ; } public componentWillUnmount ( ) { if ( this . timer ) { window . clearInterval ( this . timer ) ; } } public updateTimeRemaining = ( ) => { const expiry = this . props . endTime ; const currentTime = parseInt ( ( Date . now ( ) / 1000 ) . toString ( ) , 10 ) ; const secondsRemaining = expiry - currentTime ; this . setState ( { secondsRemaining } ) ; return secondsRemaining ; } ; public initCountdown = async ( ) => { const timeRemaining = this . updateTimeRemaining ( ) ; if ( timeRemaining > 0 ) { this . timer = window . setInterval ( this . updateTimeRemaining , 1000 ) ; } else { window . clearInterval ( this . timer ) ; } } ; } ; } ; export const TextCountdownTimer = connectCountdownTimer ( ) ( TextCountdownTimerComponent ) ; export const ProgressBarCountdownTimer = connectCountdownTimer ( ) ( ProgressBarCountdownTimerComponent ) ; export const SmallProgressBarCountdownTimer = connectCountdownTimer ( ) ( SmallProgressBarCountdownTimerComponent ) ; export const TwoPhaseProgressBarCountdownTimer = connectCountdownTimer ( ) ( TwoPhaseProgressBarCountdownTimerComponent ) ; export * from "./types" ; export * from "./constants" ;