import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { defineMessages, injectIntl } from 'react-intl';
import Service from './service';
import injectWbResizeEvent from '/imports/ui/components/presentation/resize-wrapper/component';
import Toggle from '/imports/ui/components/common/switch/component';
import Styled from './styles';
const intlMessages = defineMessages({
hideTimerLabel: {
id: 'app.timer.hideTimerLabel',
description: 'Label for hiding timer button',
},
title: {
id: 'app.timer.title',
description: 'Title for timer',
},
stopwatch: {
id: 'app.timer.button.stopwatch',
description: 'Stopwatch switch button',
},
timer: {
id: 'app.timer.button.timer',
description: 'Timer switch button',
},
start: {
id: 'app.timer.button.start',
description: 'Timer start button',
},
stop: {
id: 'app.timer.button.stop',
description: 'Timer stop button',
},
reset: {
id: 'app.timer.button.reset',
description: 'Timer reset button',
},
hours: {
id: 'app.timer.hours',
description: 'Timer hours label',
},
minutes: {
id: 'app.timer.minutes',
description: 'Timer minutes label',
},
seconds: {
id: 'app.timer.seconds',
description: 'Timer seconds label',
},
music: {
id: 'app.timer.music',
description: 'Music toggle label',
},
});
const propTypes = {
intl: PropTypes.shape({
formatMessage: PropTypes.func.isRequired,
}).isRequired,
};
class Timer extends Component {
constructor(props) {
super(props);
this.timeRef = React.createRef();
this.interval = null;
this.updateTime = this.updateTime.bind(this);
}
componentDidMount() {
const { timer } = this.props;
const { running } = timer;
const { current } = this.timeRef;
if (current && running) {
this.interval = setInterval(this.updateTime, Service.getInterval());
}
}
componentDidUpdate(prevProps) {
const { timer } = this.props;
const { timer: prevTimer } = prevProps;
this.updateInterval(prevTimer, timer);
}
componentWillUnmount() {
clearInterval(this.interval);
}
updateInterval(prevTimer, timer) {
const { running } = timer;
const { running: prevRunning } = prevTimer;
if (!prevRunning && running) {
this.interval = setInterval(this.updateTime, Service.getInterval());
}
if (prevRunning && !running) {
clearInterval(this.interval);
}
}
getTime() {
const {
timer,
timeOffset,
} = this.props;
const {
stopwatch,
running,
time,
accumulated,
timestamp,
} = timer;
const elapsedTime = Service.getElapsedTime(running, timestamp, timeOffset, accumulated);
let updatedTime;
if (stopwatch) {
updatedTime = elapsedTime;
} else {
updatedTime = Math.max(time - elapsedTime, 0);
}
return Service.getTimeAsString(updatedTime, stopwatch);
}
updateTime() {
const { current } = this.timeRef;
if (current) {
current.textContent = this.getTime();
}
}
handleControlClick() {
const { timer } = this.props;
if (timer.running) {
Service.stopTimer();
} else {
Service.startTimer();
}
}
handleOnHoursChange(event) {
const { timer } = this.props;
const { target } = event;
if (target && target.value) {
const hours = parseInt(target.value);
Service.setHours(hours, timer.time);
}
}
handleOnMinutesChange(event) {
const { timer } = this.props;
const { target } = event;
if (target && target.value) {
const minutes = parseInt(target.value);
Service.setMinutes(minutes, timer.time);
}
}
handleOnSecondsChange(event) {
const { timer } = this.props;
const { target } = event;
if (target && target.value) {
const seconds = parseInt(target.value);
Service.setSeconds(seconds, timer.time);
}
}
handleSwitchToStopwatch() {
const { timer } = this.props;
if (!timer.stopwatch) {
Service.switchTimer(true);
}
}
handleSwitchToTimer() {
const { timer } = this.props;
if (timer.stopwatch) {
Service.switchTimer(false);
}
}
handleOnMusicChange() {
const { isMusicActive } = this.props;
Service.setMusic(!isMusicActive);
}
renderControls() {
const {
intl,
timer,
} = this.props;
const { running } = timer;
const label = running ? intlMessages.stop : intlMessages.start;
const color = running ? 'danger' : 'primary';
return (