-
-
Save kirubz/009ca63ef6593f94f88a278089788e19 to your computer and use it in GitHub Desktop.
Revisions
-
johndavedecano revised this gist
Feb 23, 2017 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -19,7 +19,7 @@ class TimeAgo extends Component { } getDifference() { return differenceInSeconds(new Date(), this.props.date); } getInterval() { @@ -60,8 +60,8 @@ TimeAgo.defaultProps = { date: new Date(), className: undefined, isLive: true, addSuffix: true, includeSeconds: true, }; const allowedDateTypes = [ -
johndavedecano revised this gist
Feb 23, 2017 . 1 changed file with 3 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -39,11 +39,10 @@ class TimeAgo extends Component { return 'now'; } else { const options = { addSuffix: this.props.addSuffix, includeSeconds: this.props.includeSeconds, }; return distanceInWordsToNow(this.props.date, options); } } -
johndavedecano created this gist
Feb 23, 2017 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,83 @@ import React, {Component} from 'react'; import differenceInSeconds from 'date-fns/difference_in_seconds'; import distanceInWordsToNow from 'date-fns/distance_in_words_to_now'; class TimeAgo extends Component { componentDidMount() { if (this.props.isLive) { this.updateTime(); } } updateTime = () => { const interval = this.getInterval(); if (interval > 0) { setTimeout(this.updateTime, interval); this.forceUpdate(); } } getDifference() { return differenceInSeconds(this.props.date, new Date()); } getInterval() { const diff = this.getDifference(); if (diff < 3600) { return 60000; } else if (diff >= 3600 && diff <= 86400) { return 3600000; } else { return 0; } } getParsedDate() { const diff = this.getDifference(); if (diff < 30) { return 'now'; } else { const options = { addSuffix: false, includeSeconds: false, }; const timeago = distanceInWordsToNow(this.props.date, options); return timeago; } } render() { return React.createElement( this.props.element, {className: (this.props.className) ? this.props.className : ''}, this.getParsedDate() ); } } TimeAgo.defaultProps = { element: 'p', date: new Date(), className: undefined, isLive: true, addSuffix: false, includeSeconds: false, }; const allowedDateTypes = [ React.PropTypes.string, React.PropTypes.number, React.PropTypes.instanceOf(Date) ]; TimeAgo.propTypes = { element: React.PropTypes.string, date: React.PropTypes.oneOfType(allowedDateTypes), className: React.PropTypes.string, isLive: React.PropTypes.bool, addSuffix: React.PropTypes.bool, includeSeconds: React.PropTypes.bool }; export default TimeAgo;