import React, { Component } from 'react'; import { Button, Container, Row, Col } from 'reactstrap'; import './Timer.css'; import logo from '../logo.svg'; import Notification from 'react-web-notification'; import sound from '../sound.mp3'; class Timer extends Component { constructor(props) { super(props); this.defaultSeconds = 4; this.defaultLogoSpin = 'App-logo-rotation'; this.state = { seconds: this.defaultSeconds, started: false, logoSpin: '', ignore: true, title: '' }; // binding this.startTimer = this.startTimer.bind(this); this.stopTimer = this.stopTimer.bind(this); this.resetTimer = this.resetTimer.bind(this); this.handlePermissionGranted = this.handlePermissionGranted.bind(this); this.handlePermissionDenied = this.handlePermissionDenied.bind(this); this.handleNotSupported = this.handleNotSupported.bind(this); this.sendNotification = this.sendNotification.bind(this); this.handleNotificationOnShow = this.handleNotificationOnShow.bind(this); this.playSound = this.playSound.bind(this); } formatMinute(s){ return ( s - ( s%=60 )) / 60 + ( 9 < s ?':':':0') +s; } // handleNotification handlePermissionGranted() { this.setState({ ignore: false }); } handlePermissionDenied() { this.setState({ ignore: true }); } handleNotSupported() { this.setState({ ignore: true }); } handleNotificationOnError(e, tag){ console.log(e, 'Notification error tag:' + tag); } playSound(){ document.getElementById('sound').play(); } handleNotificationOnShow() { this.playSound(); } sendNotification() { if (this.state.ignore) { return; } const title = 'tomadoro'; const body = 'Time is Up! 🍅'; const tag = Date.now(); const options = { tag: tag, body: body, lang: 'en', sound: {sound} }; this.setState({ title: title, options: options }); } tick() { this.setState(state => ({ seconds: state.seconds -1 })); if (this.state.seconds <= 0) { this.sendNotification(); this.stopTimer(); this.resetTimer(); } } startTimer() { this.setState({ started: true, logoSpin: this.defaultLogoSpin }); this.interval = setInterval(() => this.tick(), 1000); } stopTimer() { this.setState({ started: false, logoSpin: null }); clearInterval(this.interval); } resetTimer() { this.stopTimer(); this.setState({ seconds: this.defaultSeconds}); } render() { return (
Tomato

{this.formatMinute(this.state.seconds)}

); } } export default Timer;