diff options
author | Eugen Rochko <eugen@zeonfederated.com> | 2016-11-16 17:20:52 +0100 |
---|---|---|
committer | Eugen Rochko <eugen@zeonfederated.com> | 2016-11-16 17:20:52 +0100 |
commit | 01e43c3e5799b575a70798056945365ddf51f3ad (patch) | |
tree | 75801dd3733930cc05cd3c26795cef382a4c1e5d /app/assets/javascripts/components/components/relative_timestamp.jsx | |
parent | 546c4718e781f8900ba6498307ccb1e659de5edd (diff) |
Adding react-intl i18n to the frontend. No translations yet
Diffstat (limited to 'app/assets/javascripts/components/components/relative_timestamp.jsx')
-rw-r--r-- | app/assets/javascripts/components/components/relative_timestamp.jsx | 68 |
1 files changed, 20 insertions, 48 deletions
diff --git a/app/assets/javascripts/components/components/relative_timestamp.jsx b/app/assets/javascripts/components/components/relative_timestamp.jsx index 1de44bb95..96f99cca8 100644 --- a/app/assets/javascripts/components/components/relative_timestamp.jsx +++ b/app/assets/javascripts/components/components/relative_timestamp.jsx @@ -1,52 +1,24 @@ -import moment from 'moment'; -import PureRenderMixin from 'react-addons-pure-render-mixin'; - -moment.updateLocale('en', { - relativeTime : { - future: "in %s", - past: "%s", - s: "%ds", - m: "1m", - mm: "%dm", - h: "1h", - hh: "%dh", - d: "1d", - dd: "%dd", - M: "1mo", - MM: "%dmo", - y: "1y", - yy: "%dy" - } -}); - -const RelativeTimestamp = React.createClass({ - - propTypes: { - timestamp: React.PropTypes.string.isRequired, - now: React.PropTypes.any - }, - - mixins: [PureRenderMixin], - - render () { - const timestamp = moment(this.props.timestamp); - const now = this.props.now; - - let string = ''; - - if (timestamp.isAfter(now)) { - string = 'Just now'; - } else { - string = timestamp.from(now); - } - - return ( - <span> - {string} - </span> - ); +import { + FormattedMessage, + FormattedDate, + FormattedRelative +} from 'react-intl'; + +const RelativeTimestamp = ({ timestamp, now }) => { + const diff = (new Date(now)) - (new Date(timestamp)); + + if (diff < 0) { + return <FormattedMessage id='relative_time.just_now' defaultMessage='Just now' /> + } else if (diff > (3600 * 24 * 7 * 1000)) { + return <FormattedDate value={timestamp} /> + } else { + return <FormattedRelative value={timestamp} initialNow={now} updateInterval={0} /> } +}; -}); +RelativeTimestamp.propTypes = { + timestamp: React.PropTypes.string.isRequired, + now: React.PropTypes.any +}; export default RelativeTimestamp; |