2019-03-02 10:35:40 +01:00
|
|
|
import classNames from 'classnames';
|
|
|
|
import differenceInMilliseconds from 'date-fns/difference_in_milliseconds';
|
|
|
|
import distanceInWordsToNow from 'date-fns/distance_in_words_to_now';
|
2018-02-07 19:06:07 +01:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2020-08-25 09:20:16 +02:00
|
|
|
import { withTranslation } from 'react-i18next';
|
2016-11-09 18:52:14 +01:00
|
|
|
|
|
|
|
class Timer extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.showSavedTime = this.showSavedTime.bind(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentDidMount() {
|
|
|
|
this.interval = setInterval(() => this.forceUpdate(), 10000);
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
if (this.interval) {
|
|
|
|
clearInterval(this.interval);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
showSavedTime() {
|
2019-03-02 10:35:40 +01:00
|
|
|
const now = new Date();
|
|
|
|
if (Math.abs(differenceInMilliseconds(now, this.props.projectSavedTime) < 10000)) {
|
2020-08-25 09:20:16 +02:00
|
|
|
return this.props.t('Timer.SavedJustNow');
|
2019-03-02 10:35:40 +01:00
|
|
|
} else if (differenceInMilliseconds(now, this.props.projectSavedTime) < 20000) {
|
2020-08-25 09:20:16 +02:00
|
|
|
return this.props.t('Timer.Saved15Seconds');
|
2019-03-02 10:35:40 +01:00
|
|
|
} else if (differenceInMilliseconds(now, this.props.projectSavedTime) < 30000) {
|
2020-08-25 09:20:16 +02:00
|
|
|
return this.props.t('Timer.Saved25Seconds');
|
2019-03-02 10:35:40 +01:00
|
|
|
} else if (differenceInMilliseconds(now, this.props.projectSavedTime) < 46000) {
|
2020-08-25 09:20:16 +02:00
|
|
|
return this.props.t('Timer.Saved35Seconds');
|
2016-11-09 18:52:14 +01:00
|
|
|
}
|
2020-08-25 09:20:16 +02:00
|
|
|
|
|
|
|
const timeAgo = distanceInWordsToNow(this.props.projectSavedTime, {
|
2019-03-02 10:35:40 +01:00
|
|
|
includeSeconds: true
|
2020-08-25 09:20:16 +02:00
|
|
|
});
|
|
|
|
return this.props.t('Timer.SavedAgo', { timeAgo });
|
2016-11-09 18:52:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-01-13 17:55:13 +01:00
|
|
|
const timerClass = classNames({
|
|
|
|
'timer__saved-time': true,
|
|
|
|
'timer__saved-time--notOwner': !this.props.isUserOwner
|
|
|
|
});
|
2016-11-09 18:52:14 +01:00
|
|
|
return (
|
2017-01-13 17:55:13 +01:00
|
|
|
<span className={timerClass}>
|
2016-11-09 18:52:14 +01:00
|
|
|
{this.props.projectSavedTime !== '' ? this.showSavedTime() : null}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer.propTypes = {
|
2017-01-13 17:55:13 +01:00
|
|
|
projectSavedTime: PropTypes.string.isRequired,
|
2020-08-25 09:20:16 +02:00
|
|
|
isUserOwner: PropTypes.bool,
|
|
|
|
t: PropTypes.func.isRequired
|
2016-11-09 18:52:14 +01:00
|
|
|
};
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
Timer.defaultProps = {
|
|
|
|
isUserOwner: false
|
|
|
|
};
|
|
|
|
|
2020-08-25 09:20:16 +02:00
|
|
|
export default withTranslation()(Timer);
|