Skip to content

Commit e01efe8

Browse files
authored
Merge pull request #2165 from lindapaiste/refactor/timer
Convert Timer to a function component.
2 parents 8c1b869 + 79cc493 commit e01efe8

File tree

2 files changed

+40
-54
lines changed

2 files changed

+40
-54
lines changed

client/modules/IDE/components/Editor.jsx

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ import UnsavedChangesDotIcon from '../../../images/unsaved-changes-dot.svg';
5151
import RightArrowIcon from '../../../images/right-arrow.svg';
5252
import LeftArrowIcon from '../../../images/left-arrow.svg';
5353
import { getHTMLFile } from '../reducers/files';
54-
import { getIsUserOwner } from '../selectors/users';
5554

5655
import * as FileActions from '../actions/files';
5756
import * as IDEActions from '../actions/ide';
@@ -420,10 +419,7 @@ class Editor extends React.Component {
420419
) : null}
421420
</span>
422421
</span>
423-
<Timer
424-
projectSavedTime={this.props.projectSavedTime}
425-
isUserOwner={this.props.isUserOwner}
426-
/>
422+
<Timer />
427423
</div>
428424
</header>
429425
<article
@@ -474,7 +470,6 @@ Editor.propTypes = {
474470
isPlaying: PropTypes.bool.isRequired,
475471
theme: PropTypes.string.isRequired,
476472
unsavedChanges: PropTypes.bool.isRequired,
477-
projectSavedTime: PropTypes.string.isRequired,
478473
files: PropTypes.arrayOf(
479474
PropTypes.shape({
480475
id: PropTypes.string.isRequired,
@@ -485,7 +480,6 @@ Editor.propTypes = {
485480
isExpanded: PropTypes.bool.isRequired,
486481
collapseSidebar: PropTypes.func.isRequired,
487482
expandSidebar: PropTypes.func.isRequired,
488-
isUserOwner: PropTypes.bool.isRequired,
489483
clearConsole: PropTypes.func.isRequired,
490484
// showRuntimeErrorWarning: PropTypes.func.isRequired,
491485
hideRuntimeErrorWarning: PropTypes.func.isRequired,
@@ -516,9 +510,7 @@ function mapStateToProps(state) {
516510
...state.ide,
517511
...state.project,
518512
...state.editorAccessibility,
519-
isExpanded: state.ide.sidebarIsExpanded,
520-
projectSavedTime: state.project.updatedAt,
521-
isUserOwner: getIsUserOwner(state)
513+
isExpanded: state.ide.sidebarIsExpanded
522514
};
523515
}
524516

Lines changed: 38 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,46 @@
11
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';
55

66
import dates from '../../../utils/formatDate';
7+
import useInterval from '../hooks/useInterval';
8+
import { getIsUserOwner } from '../selectors/users';
79

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);
4113

42-
Timer.propTypes = {
43-
projectSavedTime: PropTypes.string.isRequired,
44-
isUserOwner: PropTypes.bool,
45-
t: PropTypes.func.isRequired
46-
};
14+
const { t } = useTranslation();
4715

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+
);
5044
};
5145

52-
export default withTranslation()(Timer);
46+
export default Timer;

0 commit comments

Comments
 (0)