Created
November 22, 2016 16:23
-
-
Save hyukkwonepic/40359020d631a74a715c095de0afd1b8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var React = require('react'); | |
var Clock = require('Clock'); | |
var CountdownForm = require('CountdownForm'); | |
var Controls = require('Controls'); | |
var Countdown = React.createClass({ | |
getInitialState: function () { | |
return { | |
count: 0, | |
countdownStatus: 'stopped' | |
}; | |
}, | |
componentDidUpdate: function (prevProps, prevState) { | |
if (this.state.countdownStatus !== prevState.countdownStatus) { | |
switch (this.state.countdownStatus) { | |
case 'started': | |
this.startTimer(); | |
break; | |
case 'stopped': | |
this.setState({ | |
count: 0 | |
}); | |
case 'paused': | |
clearInterval(this.timer); | |
this.timer = undefined; | |
break; | |
} | |
} | |
}, | |
startTimer: function () { | |
this.timer = setInterval(() => { | |
var newCount = this.state.count - 1; | |
this.setState({ | |
count: newCount >= 0 ? newCount : 0 | |
}); | |
}, 1000); | |
}, | |
handleSetCountdown: function (seconds) { | |
this.setState({ | |
count: seconds, | |
countdownStatus: 'started' | |
}); | |
}, | |
handleStatusChange : function (newStatus) { | |
this.setState({ | |
countdownStatus: newStatus | |
}); | |
}, | |
render: function () { | |
var {count, countdownStatus} = this.state; | |
var renderControlArea = () => { | |
if (countdownStatus !== "stopped") { | |
return <Controls countdownStatus={countdownStatus} onStatusChange={this.handleStatusChange}/> | |
} else { | |
return <CountdownForm onSetCountdown={this.handleSetCountdown}/> | |
} | |
}; | |
return ( | |
<div> | |
<Clock totalSeconds={count}/> | |
{renderControlArea()} | |
</div> | |
); | |
} | |
}); | |
module.exports = Countdown; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment