|
1 | 1 | import classNames from 'classnames';
|
2 |
| -import PropTypes from 'prop-types'; |
3 |
| -import React from 'react'; |
4 |
| -import { withTranslation } from 'react-i18next'; |
| 2 | +import React, { useEffect, useState } from 'react'; |
| 3 | +import { useTranslation } from 'react-i18next'; |
| 4 | +import { useSelector } from 'react-redux'; |
5 | 5 |
|
6 | 6 | import dates from '../../../utils/formatDate';
|
| 7 | +import useInterval from '../hooks/useInterval'; |
| 8 | +import { getIsUserOwner } from '../selectors/users'; |
7 | 9 |
|
8 |
| -class Timer extends React.Component { |
9 |
| - constructor(props) { |
10 |
| - super(props); |
11 |
| - this.showSavedTime = this.showSavedTime.bind(this); |
12 |
| - } |
13 |
| - |
14 |
| - componentDidMount() { |
15 |
| - this.interval = setInterval(() => this.forceUpdate(), 10000); |
16 |
| - } |
17 |
| - |
18 |
| - componentWillUnmount() { |
19 |
| - if (this.interval) { |
20 |
| - clearInterval(this.interval); |
21 |
| - } |
22 |
| - } |
23 |
| - |
24 |
| - showSavedTime() { |
25 |
| - const timeAgo = dates.distanceInWordsToNow(this.props.projectSavedTime); |
26 |
| - return this.props.t('Timer.SavedAgo', { timeAgo }); |
27 |
| - } |
28 |
| - |
29 |
| - render() { |
30 |
| - const timerClass = classNames({ |
31 |
| - 'timer__saved-time': true, |
32 |
| - 'timer__saved-time--notOwner': !this.props.isUserOwner |
33 |
| - }); |
34 |
| - return ( |
35 |
| - <span className={timerClass}> |
36 |
| - {this.props.projectSavedTime !== '' ? this.showSavedTime() : null} |
37 |
| - </span> |
38 |
| - ); |
39 |
| - } |
40 |
| -} |
| 10 | +const Timer = () => { |
| 11 | + const projectSavedTime = useSelector((state) => state.project.updatedAt); |
| 12 | + const isUserOwner = useSelector(getIsUserOwner); |
41 | 13 |
|
42 |
| -Timer.propTypes = { |
43 |
| - projectSavedTime: PropTypes.string.isRequired, |
44 |
| - isUserOwner: PropTypes.bool, |
45 |
| - t: PropTypes.func.isRequired |
46 |
| -}; |
| 14 | + const { t } = useTranslation(); |
47 | 15 |
|
48 |
| -Timer.defaultProps = { |
49 |
| - isUserOwner: false |
| 16 | + const [timeAgo, setTimeAgo] = useState(''); |
| 17 | + |
| 18 | + // Update immediately upon saving. |
| 19 | + useEffect(() => { |
| 20 | + setTimeAgo( |
| 21 | + projectSavedTime ? dates.distanceInWordsToNow(projectSavedTime) : '' |
| 22 | + ); |
| 23 | + }, [projectSavedTime]); |
| 24 | + |
| 25 | + // Update every 10 seconds. |
| 26 | + useInterval( |
| 27 | + () => |
| 28 | + setTimeAgo( |
| 29 | + projectSavedTime ? dates.distanceInWordsToNow(projectSavedTime) : '' |
| 30 | + ), |
| 31 | + 10000 |
| 32 | + ); |
| 33 | + |
| 34 | + const timerClass = classNames({ |
| 35 | + 'timer__saved-time': true, |
| 36 | + 'timer__saved-time--notOwner': !isUserOwner |
| 37 | + }); |
| 38 | + |
| 39 | + return ( |
| 40 | + <span className={timerClass}> |
| 41 | + {timeAgo ? t('Timer.SavedAgo', { timeAgo }) : null} |
| 42 | + </span> |
| 43 | + ); |
50 | 44 | };
|
51 | 45 |
|
52 |
| -export default withTranslation()(Timer); |
| 46 | +export default Timer; |
0 commit comments