2016-11-09 18:52:14 +01:00
|
|
|
import React, { PropTypes } from 'react';
|
|
|
|
import moment from 'moment';
|
2017-01-13 17:55:13 +01:00
|
|
|
import classNames from 'classnames';
|
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() {
|
|
|
|
if (Math.abs(moment().diff(this.props.projectSavedTime)) < 10000) {
|
|
|
|
return 'Saved: just now';
|
|
|
|
} else if (Math.abs(moment().diff(this.props.projectSavedTime)) < 20000) {
|
|
|
|
return 'Saved: 15 seconds ago';
|
|
|
|
} else if (Math.abs(moment().diff(this.props.projectSavedTime)) < 30000) {
|
|
|
|
return 'Saved: 25 seconds ago';
|
|
|
|
} else if (Math.abs(moment().diff(this.props.projectSavedTime)) < 46000) {
|
|
|
|
return 'Saved: 35 seconds ago';
|
|
|
|
}
|
|
|
|
return `Saved: ${moment(this.props.projectSavedTime).fromNow()}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
isUserOwner: PropTypes.bool
|
2016-11-09 18:52:14 +01:00
|
|
|
};
|
|
|
|
|
2017-02-22 20:29:35 +01:00
|
|
|
Timer.defaultProps = {
|
|
|
|
isUserOwner: false
|
|
|
|
};
|
|
|
|
|
2016-11-09 18:52:14 +01:00
|
|
|
export default Timer;
|