tomadoro/src/components/Timer.js

157 lines
3.1 KiB
JavaScript
Raw Normal View History

2019-03-14 16:53:56 +01:00
import React, { Component } from 'react';
2019-03-21 11:22:41 +01:00
import { Container, Row, Col } from 'reactstrap';
2019-03-21 14:58:26 +01:00
import Notification from './Notification';
2019-03-21 11:22:41 +01:00
import ButtonBox from './ButtonBox';
2019-03-21 12:00:36 +01:00
import LogoSpin from './LogoSpin';
2019-03-14 16:53:56 +01:00
import './Timer.css';
2019-03-15 10:33:22 +01:00
class Timer extends Component {
2019-03-14 16:53:56 +01:00
constructor(props) {
super(props);
2019-03-21 12:00:36 +01:00
this.state = {
2019-03-30 18:53:12 +01:00
seconds: 0,
2019-03-15 10:33:22 +01:00
started: false,
2019-03-30 18:53:12 +01:00
break: false,
sendNotification: false
2019-03-14 16:53:56 +01:00
};
2019-03-15 15:24:47 +01:00
2019-03-21 14:58:26 +01:00
this.handleNotification = this.handleNotification.bind(this);
2019-03-14 16:53:56 +01:00
this.startTimer = this.startTimer.bind(this);
this.stopTimer = this.stopTimer.bind(this);
this.resetTimer = this.resetTimer.bind(this);
2019-03-30 18:53:12 +01:00
this.finishedTimer = this.finishedTimer.bind(this);
this.switchMode = this.switchMode.bind(this);
this.pomodoroMode = this.pomodoroMode.bind(this);
this.breakMode = this.breakMode.bind(this);
}
componentWillMount() {
this.pomodoroMode();
2019-03-15 15:24:47 +01:00
}
2019-03-30 18:53:12 +01:00
2019-03-21 14:58:26 +01:00
handleNotification(flag) {
2019-03-15 15:24:47 +01:00
this.setState({
2019-03-30 18:53:12 +01:00
sendNotification: flag
2019-03-15 15:24:47 +01:00
});
}
2019-03-21 14:58:26 +01:00
formatMinute(s) {
return (s - (s %= 60)) / 60 + (9 < s ? ':' : ':0') + s;
2019-03-15 19:07:55 +01:00
}
2019-03-21 14:58:26 +01:00
2019-03-14 16:53:56 +01:00
tick() {
this.setState(state => ({
2019-03-21 12:00:36 +01:00
seconds: state.seconds - 1
2019-03-14 16:53:56 +01:00
}));
2019-03-15 15:24:47 +01:00
2019-03-30 18:53:12 +01:00
if (this.state.seconds === 0) {
2019-03-15 15:24:47 +01:00
this.stopTimer();
2019-03-30 18:53:12 +01:00
this.finishedTimer();
2019-03-15 15:24:47 +01:00
}
2019-03-14 16:53:56 +01:00
}
startTimer() {
2019-03-21 12:00:36 +01:00
this.setState({
started: true
2019-03-15 10:33:22 +01:00
});
2019-03-30 18:53:12 +01:00
2019-03-14 16:53:56 +01:00
this.interval = setInterval(() => this.tick(), 1000);
}
2019-03-21 12:00:36 +01:00
2019-03-14 16:53:56 +01:00
stopTimer() {
2019-03-21 12:00:36 +01:00
this.setState({
started: false
});
2019-03-30 18:53:12 +01:00
2019-03-14 16:53:56 +01:00
clearInterval(this.interval);
}
resetTimer() {
2019-03-30 18:53:12 +01:00
if(this.state.break) {
this.breakMode();
} else {
this.pomodoroMode();
}
}
finishedTimer() {
this.setState({
sendNotification: true,
});
this.switchMode();
}
switchMode() {
if(this.state.break && !this.state.started) {
this.pomodoroMode();
} else if(!this.state.break && !this.state.started) {
this.breakMode();
}
}
pomodoroMode() {
2019-03-30 19:00:06 +01:00
const pomodoroSeconds = 1500;
2019-03-30 18:53:12 +01:00
this.setState({
seconds: pomodoroSeconds,
break: false
});
}
breakMode() {
2019-03-30 19:00:06 +01:00
const breakSeconds = 300
2019-03-30 18:53:12 +01:00
this.setState({
seconds: breakSeconds,
break: true
})
2019-03-21 11:22:41 +01:00
}
2019-03-15 19:07:55 +01:00
2019-03-21 14:58:26 +01:00
render() {
2019-03-14 16:53:56 +01:00
return (
<div className="App">
2019-03-15 10:33:22 +01:00
<Container>
2019-03-15 19:07:55 +01:00
<Row>
<Col>
2019-03-21 12:00:36 +01:00
<LogoSpin
isStarted={this.state.started}
2019-03-30 18:53:12 +01:00
switchMode={this.switchMode}
2019-03-21 12:00:36 +01:00
/>
2019-03-15 19:07:55 +01:00
</Col>
2019-03-21 12:00:36 +01:00
</Row>
2019-03-15 19:07:55 +01:00
<Row>
<Col>
2019-03-21 12:00:36 +01:00
<p
className="timer"
>
{this.formatMinute(this.state.seconds)}
</p>
2019-03-15 19:07:55 +01:00
</Col>
</Row>
2019-03-21 12:00:36 +01:00
2019-03-15 19:07:55 +01:00
<Row>
2019-03-21 11:22:41 +01:00
<Col>
2019-03-21 12:00:36 +01:00
<ButtonBox
startTimer={this.startTimer}
stopTimer={this.stopTimer}
isStarted={this.state.started}
resetTimer={this.resetTimer}
2019-03-21 14:58:26 +01:00
seconds={this.state.seconds}
2019-03-21 11:22:41 +01:00
/>
</Col>
2019-03-15 19:07:55 +01:00
</Row>
2019-03-15 10:33:22 +01:00
</Container>
2019-03-15 19:07:55 +01:00
2019-03-15 15:24:47 +01:00
<Notification
2019-03-30 18:53:12 +01:00
send={this.state.sendNotification}
2019-03-21 14:58:26 +01:00
handleNotification={this.handleNotification}
/>
2019-03-14 16:53:56 +01:00
</div>
);
}
}
export default Timer;