2023-05-16 03:46:44 +08:00
|
|
|
import React, { Component } from 'react';
|
2020-04-26 03:03:35 +08:00
|
|
|
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';
|
2021-09-03 20:44:16 +08:00
|
|
|
import Toggle from '/imports/ui/components/common/switch/component';
|
2020-04-26 03:03:35 +08:00
|
|
|
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',
|
|
|
|
},
|
2023-05-16 03:46:44 +08:00
|
|
|
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',
|
|
|
|
},
|
2021-09-03 20:44:16 +08:00
|
|
|
music: {
|
|
|
|
id: 'app.timer.music',
|
|
|
|
description: 'Music toggle label',
|
|
|
|
},
|
2020-04-26 03:03:35 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
const propTypes = {
|
|
|
|
intl: PropTypes.shape({
|
|
|
|
formatMessage: PropTypes.func.isRequired,
|
|
|
|
}).isRequired,
|
|
|
|
};
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
class Timer extends Component {
|
2020-04-26 03:03:35 +08:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
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();
|
|
|
|
}
|
2020-04-26 03:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
handleControlClick() {
|
2023-05-16 03:46:44 +08:00
|
|
|
const { timer } = this.props;
|
2020-04-26 03:03:35 +08:00
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
if (timer.running) {
|
2020-04-26 03:03:35 +08:00
|
|
|
Service.stopTimer();
|
|
|
|
} else {
|
|
|
|
Service.startTimer();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-27 01:44:01 +08:00
|
|
|
handleOnHoursChange(event) {
|
2023-05-16 03:46:44 +08:00
|
|
|
const { timer } = this.props;
|
2020-04-27 01:44:01 +08:00
|
|
|
const { target } = event;
|
|
|
|
|
|
|
|
if (target && target.value) {
|
|
|
|
const hours = parseInt(target.value);
|
2023-05-16 03:46:44 +08:00
|
|
|
Service.setHours(hours, timer.time);
|
2020-04-27 01:44:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOnMinutesChange(event) {
|
2023-05-16 03:46:44 +08:00
|
|
|
const { timer } = this.props;
|
2020-04-27 01:44:01 +08:00
|
|
|
const { target } = event;
|
|
|
|
|
|
|
|
if (target && target.value) {
|
|
|
|
const minutes = parseInt(target.value);
|
2023-05-16 03:46:44 +08:00
|
|
|
Service.setMinutes(minutes, timer.time);
|
2020-04-27 01:44:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleOnSecondsChange(event) {
|
2023-05-16 03:46:44 +08:00
|
|
|
const { timer } = this.props;
|
2020-04-27 01:44:01 +08:00
|
|
|
const { target } = event;
|
|
|
|
|
|
|
|
if (target && target.value) {
|
|
|
|
const seconds = parseInt(target.value);
|
2023-05-16 03:46:44 +08:00
|
|
|
Service.setSeconds(seconds, timer.time);
|
2020-04-27 01:44:01 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-26 03:03:35 +08:00
|
|
|
handleSwitchToStopwatch() {
|
2023-05-16 03:46:44 +08:00
|
|
|
const { timer } = this.props;
|
2020-04-26 03:03:35 +08:00
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
if (!timer.stopwatch) {
|
2020-04-26 03:03:35 +08:00
|
|
|
Service.switchTimer(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
handleSwitchToTimer() {
|
2023-05-16 03:46:44 +08:00
|
|
|
const { timer } = this.props;
|
2020-04-26 03:03:35 +08:00
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
if (timer.stopwatch) {
|
2020-04-26 03:03:35 +08:00
|
|
|
Service.switchTimer(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:44:16 +08:00
|
|
|
handleOnMusicChange() {
|
|
|
|
const { isMusicActive } = this.props;
|
|
|
|
Service.setMusic(!isMusicActive);
|
|
|
|
}
|
|
|
|
|
2020-04-26 03:03:35 +08:00
|
|
|
renderControls() {
|
|
|
|
const {
|
|
|
|
intl,
|
2023-05-16 03:46:44 +08:00
|
|
|
timer,
|
2020-04-26 03:03:35 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
const { running } = timer;
|
2020-04-26 03:03:35 +08:00
|
|
|
|
|
|
|
const label = running ? intlMessages.stop : intlMessages.start;
|
2023-05-16 04:27:39 +08:00
|
|
|
const color = running ? 'danger' : 'primary';
|
2020-04-26 03:03:35 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Styled.TimerControls>
|
|
|
|
<Styled.TimerControlButton
|
2023-05-16 04:27:39 +08:00
|
|
|
color={color}
|
2020-04-26 03:03:35 +08:00
|
|
|
label={intl.formatMessage(label)}
|
2023-05-16 03:46:44 +08:00
|
|
|
onClick={() => this.handleControlClick()}
|
2020-04-26 03:03:35 +08:00
|
|
|
/>
|
|
|
|
<Styled.TimerControlButton
|
|
|
|
label={intl.formatMessage(intlMessages.reset)}
|
|
|
|
onClick={() => Service.resetTimer()}
|
|
|
|
/>
|
|
|
|
</Styled.TimerControls>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-09-03 20:44:16 +08:00
|
|
|
renderToggle() {
|
|
|
|
const {
|
|
|
|
intl,
|
|
|
|
timer,
|
|
|
|
isMusicActive,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {
|
|
|
|
stopwatch,
|
|
|
|
} = timer;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<Styled.TimerSongsWrapper>
|
|
|
|
<Styled.TimerRow>
|
|
|
|
<Styled.TimerCol
|
|
|
|
disabled={stopwatch}
|
|
|
|
>
|
|
|
|
{intl.formatMessage(intlMessages.music)}
|
|
|
|
</Styled.TimerCol>
|
2021-09-14 02:46:59 +08:00
|
|
|
<Styled.TimerCol>
|
2021-09-03 20:44:16 +08:00
|
|
|
<Toggle
|
|
|
|
onChange={() => this.handleOnMusicChange()}
|
|
|
|
icons={false}
|
|
|
|
disabled={stopwatch}
|
|
|
|
checked={isMusicActive}
|
|
|
|
/>
|
|
|
|
</Styled.TimerCol>
|
|
|
|
</Styled.TimerRow>
|
|
|
|
</Styled.TimerSongsWrapper>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-04-26 03:03:35 +08:00
|
|
|
renderTimer() {
|
2023-05-16 03:46:44 +08:00
|
|
|
const {
|
|
|
|
intl,
|
|
|
|
timer,
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const {
|
|
|
|
time,
|
|
|
|
stopwatch,
|
|
|
|
} = timer;
|
2020-04-27 01:44:01 +08:00
|
|
|
|
|
|
|
const timeArray = Service.getTimeAsString(time).split(':');
|
|
|
|
|
|
|
|
const hasHours = timeArray.length === 3;
|
|
|
|
|
|
|
|
const hours = hasHours ? timeArray[0] : '00';
|
|
|
|
const minutes = hasHours ? timeArray[1] : timeArray[0];
|
|
|
|
const seconds = hasHours ? timeArray[2] : timeArray[1];
|
|
|
|
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<Styled.StopwatchTime>
|
|
|
|
<input
|
|
|
|
type="number"
|
2023-05-16 03:46:44 +08:00
|
|
|
disabled={stopwatch}
|
2020-04-27 01:44:01 +08:00
|
|
|
value={hours}
|
|
|
|
maxLength="2"
|
|
|
|
max={Service.getMaxHours()}
|
|
|
|
min="0"
|
2023-05-16 03:46:44 +08:00
|
|
|
onChange={(event) => this.handleOnHoursChange(event)}
|
2020-04-27 01:44:01 +08:00
|
|
|
/>
|
|
|
|
<Styled.StopwatchTimeColon>:</Styled.StopwatchTimeColon>
|
|
|
|
<input
|
|
|
|
type="number"
|
2023-05-16 03:46:44 +08:00
|
|
|
disabled={stopwatch}
|
2020-04-27 01:44:01 +08:00
|
|
|
value={minutes}
|
|
|
|
maxLength="2"
|
|
|
|
max="59"
|
|
|
|
min="0"
|
2023-05-16 03:46:44 +08:00
|
|
|
onChange={(event) => this.handleOnMinutesChange(event)}
|
2020-04-27 01:44:01 +08:00
|
|
|
/>
|
|
|
|
<Styled.StopwatchTimeColon>:</Styled.StopwatchTimeColon>
|
|
|
|
<input
|
|
|
|
type="number"
|
2023-05-16 03:46:44 +08:00
|
|
|
disabled={stopwatch}
|
2020-04-27 01:44:01 +08:00
|
|
|
value={seconds}
|
|
|
|
maxLength="2"
|
|
|
|
max="59"
|
|
|
|
min="0"
|
2023-05-16 03:46:44 +08:00
|
|
|
onChange={(event) => this.handleOnSecondsChange(event)}
|
2020-04-27 01:44:01 +08:00
|
|
|
/>
|
|
|
|
</Styled.StopwatchTime>
|
2021-09-03 20:44:16 +08:00
|
|
|
{ Service.isMusicEnabled()
|
|
|
|
? this.renderToggle() : null}
|
2020-04-27 01:44:01 +08:00
|
|
|
{this.renderControls()}
|
|
|
|
</div>
|
|
|
|
);
|
2020-04-26 03:03:35 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
renderContent() {
|
|
|
|
const {
|
|
|
|
intl,
|
2023-05-16 03:46:44 +08:00
|
|
|
timer,
|
2020-04-26 03:03:35 +08:00
|
|
|
} = this.props;
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
const { stopwatch } = timer;
|
2020-04-26 03:03:35 +08:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Styled.TimerContent>
|
2023-05-16 03:46:44 +08:00
|
|
|
<Styled.TimerCurrent>
|
|
|
|
{this.getTime()}
|
|
|
|
</Styled.TimerCurrent>
|
2020-04-26 03:03:35 +08:00
|
|
|
<Styled.TimerType>
|
|
|
|
<Styled.TimerSwitchButton
|
|
|
|
color={stopwatch ? 'primary' : 'default'}
|
|
|
|
label={intl.formatMessage(intlMessages.stopwatch)}
|
2023-05-16 03:46:44 +08:00
|
|
|
onClick={() => this.handleSwitchToStopwatch()}
|
2020-04-26 03:03:35 +08:00
|
|
|
/>
|
|
|
|
<Styled.TimerSwitchButton
|
|
|
|
color={stopwatch ? 'default' : 'primary'}
|
|
|
|
label={intl.formatMessage(intlMessages.timer)}
|
2023-05-16 03:46:44 +08:00
|
|
|
onClick={() => this.handleSwitchToTimer()}
|
2020-04-26 03:03:35 +08:00
|
|
|
/>
|
|
|
|
</Styled.TimerType>
|
2023-05-16 03:46:44 +08:00
|
|
|
{this.renderTimer()}
|
2020-04-26 03:03:35 +08:00
|
|
|
</Styled.TimerContent>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const {
|
|
|
|
intl,
|
|
|
|
isRTL,
|
|
|
|
isActive,
|
|
|
|
isModerator,
|
2020-04-27 01:44:01 +08:00
|
|
|
layoutContextDispatch,
|
2023-05-16 03:46:44 +08:00
|
|
|
timer,
|
2020-04-26 03:03:35 +08:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
if (!isActive || !isModerator) {
|
2020-04-27 01:44:01 +08:00
|
|
|
Service.closePanel(layoutContextDispatch)
|
2020-04-26 03:03:35 +08:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2023-05-16 03:46:44 +08:00
|
|
|
const { stopwatch } = timer;
|
|
|
|
const message = stopwatch ? intlMessages.stopwatch : intlMessages.timer;
|
|
|
|
|
2020-04-26 03:03:35 +08:00
|
|
|
return (
|
|
|
|
<Styled.TimerSidebarContent
|
|
|
|
data-test="timer"
|
|
|
|
>
|
|
|
|
<Styled.TimerHeader>
|
|
|
|
<Styled.TimerTitle
|
|
|
|
data-test="timerTitle"
|
|
|
|
>
|
|
|
|
<Styled.TimerMinimizeButton
|
2020-04-27 01:44:01 +08:00
|
|
|
onClick={() => Service.closePanel(layoutContextDispatch)}
|
2020-04-26 03:03:35 +08:00
|
|
|
aria-label={intl.formatMessage(intlMessages.hideTimerLabel)}
|
2023-05-16 03:46:44 +08:00
|
|
|
label={intl.formatMessage(message)}
|
2020-04-26 03:03:35 +08:00
|
|
|
icon={isRTL ? "right_arrow" : "left_arrow"}
|
|
|
|
/>
|
|
|
|
</Styled.TimerTitle>
|
|
|
|
</Styled.TimerHeader>
|
|
|
|
{this.renderContent()}
|
|
|
|
</Styled.TimerSidebarContent>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Timer.propTypes = propTypes;
|
|
|
|
|
|
|
|
export default injectWbResizeEvent(injectIntl(Timer));
|