chinwagsocial/app/assets/javascripts/components/components/relative_timestamp.jsx

60 lines
993 B
React
Raw Normal View History

import moment from 'moment';
import PureRenderMixin from 'react-addons-pure-render-mixin';
2016-08-25 05:08:00 +10:00
moment.updateLocale('en', {
relativeTime : {
future: "in %s",
2016-09-01 00:48:21 +10:00
past: "%s",
s: "%ds",
m: "1m",
2016-08-25 05:08:00 +10:00
mm: "%dm",
2016-09-01 00:48:21 +10:00
h: "1h",
2016-08-25 05:08:00 +10:00
hh: "%dh",
2016-09-01 00:48:21 +10:00
d: "1d",
2016-08-25 05:08:00 +10:00
dd: "%dd",
2016-09-01 00:48:21 +10:00
M: "1mo",
MM: "%dmo",
2016-09-01 00:48:21 +10:00
y: "1y",
2016-08-25 05:08:00 +10:00
yy: "%dy"
}
});
const RelativeTimestamp = React.createClass({
2016-08-25 05:08:00 +10:00
getInitialState () {
return {
text: ''
};
},
propTypes: {
timestamp: React.PropTypes.string.isRequired
},
mixins: [PureRenderMixin],
2016-08-25 05:08:00 +10:00
componentWillMount () {
this._updateMomentText();
this.interval = setInterval(this._updateMomentText, 60000);
2016-08-25 05:08:00 +10:00
},
componentWillUnmount () {
clearInterval(this.interval);
},
_updateMomentText () {
this.setState({ text: moment(this.props.timestamp).fromNow() });
},
render () {
return (
2016-09-01 06:58:10 +10:00
<span>
2016-08-25 05:08:00 +10:00
{this.state.text}
</span>
);
}
});
export default RelativeTimestamp;