2019-03-02 09:35:40 +00: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 18:06:07 +00:00
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import React from 'react';
|
2016-11-09 17:52:14 +00: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 09:35:40 +00:00
|
|
|
const now = new Date();
|
|
|
|
if (Math.abs(differenceInMilliseconds(now, this.props.projectSavedTime) < 10000)) {
|
2016-11-09 17:52:14 +00:00
|
|
|
return 'Saved: just now';
|
2019-03-02 09:35:40 +00:00
|
|
|
} else if (differenceInMilliseconds(now, this.props.projectSavedTime) < 20000) {
|
2016-11-09 17:52:14 +00:00
|
|
|
return 'Saved: 15 seconds ago';
|
2019-03-02 09:35:40 +00:00
|
|
|
} else if (differenceInMilliseconds(now, this.props.projectSavedTime) < 30000) {
|
2016-11-09 17:52:14 +00:00
|
|
|
return 'Saved: 25 seconds ago';
|
2019-03-02 09:35:40 +00:00
|
|
|
} else if (differenceInMilliseconds(now, this.props.projectSavedTime) < 46000) {
|
2016-11-09 17:52:14 +00:00
|
|
|
return 'Saved: 35 seconds ago';
|
|
|
|
}
|
2019-03-02 09:35:40 +00:00
|
|
|
return `Saved: ${distanceInWordsToNow(this.props.projectSavedTime, {
|
|
|
|
includeSeconds: true
|
|
|
|
})} ago`;
|
2016-11-09 17:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
2017-01-13 16:55:13 +00:00
|
|
|
const timerClass = classNames({
|
|
|
|
'timer__saved-time': true,
|
|
|
|
'timer__saved-time--notOwner': !this.props.isUserOwner
|
|
|
|
});
|
2016-11-09 17:52:14 +00:00
|
|
|
return (
|
2017-01-13 16:55:13 +00:00
|
|
|
<span className={timerClass}>
|
2016-11-09 17:52:14 +00:00
|
|
|
{this.props.projectSavedTime !== '' ? this.showSavedTime() : null}
|
|
|
|
</span>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Timer.propTypes = {
|
2017-01-13 16:55:13 +00:00
|
|
|
projectSavedTime: PropTypes.string.isRequired,
|
|
|
|
isUserOwner: PropTypes.bool
|
2016-11-09 17:52:14 +00:00
|
|
|
};
|
|
|
|
|
2017-02-22 19:29:35 +00:00
|
|
|
Timer.defaultProps = {
|
|
|
|
isUserOwner: false
|
|
|
|
};
|
|
|
|
|
2016-11-09 17:52:14 +00:00
|
|
|
export default Timer;
|