implement shouldComponentUpdate for relative_timestamp (#3320)

This commit is contained in:
Nolan Lawson 2017-05-25 18:25:41 -07:00 committed by Eugen Rochko
parent a098d08d12
commit 7c67cb5997
1 changed files with 33 additions and 12 deletions

View File

@ -2,19 +2,40 @@ import React from 'react';
import { injectIntl, FormattedRelative } from 'react-intl'; import { injectIntl, FormattedRelative } from 'react-intl';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
const RelativeTimestamp = ({ intl, timestamp }) => { const dateFormatOptions = {
hour12: false,
year: 'numeric',
month: 'short',
day: '2-digit',
hour: '2-digit',
minute: '2-digit',
};
class RelativeTimestamp extends React.Component {
static propTypes = {
intl: PropTypes.object.isRequired,
timestamp: PropTypes.string.isRequired,
};
shouldComponentUpdate (nextProps) {
// As of right now the locale doesn't change without a new page load,
// but we might as well check in case that ever changes.
return this.props.timestamp !== nextProps.timestamp ||
this.props.intl.locale !== nextProps.intl.locale;
}
render () {
const { timestamp, intl } = this.props;
const date = new Date(timestamp); const date = new Date(timestamp);
return ( return (
<time dateTime={timestamp} title={intl.formatDate(date, { hour12: false, year: 'numeric', month: 'short', day: '2-digit', hour: '2-digit', minute: '2-digit' })}> <time dateTime={timestamp} title={intl.formatDate(date, dateFormatOptions)}>
<FormattedRelative value={date} /> <FormattedRelative value={date} />
</time> </time>
); );
}; }
RelativeTimestamp.propTypes = { }
intl: PropTypes.object.isRequired,
timestamp: PropTypes.string.isRequired,
};
export default injectIntl(RelativeTimestamp); export default injectIntl(RelativeTimestamp);