Skip to content

Commit

Permalink
Merge pull request #49 from KleeGroup/feature-external-refresh
Browse files Browse the repository at this point in the history
Add callback to force refresh comments
  • Loading branch information
c3dr0x authored Aug 21, 2017
2 parents e6966cd + f54c960 commit d833cc9
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 8 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,5 @@ Please note that you must you React with addons, so choose the appropriate built
- **messageSentCallback** (`function`): a callback to know when a message has been sent
- **timeDisplay** (`string`): the display format, either `ago` or `dateTime`, `ago` by default
- **dateTimeFormat** (`string`): the dateTime format if **timeDisplay** is set to `dateTime`
- **registerRefreshCommentsMethod** (`function`): method to register the refresh comment method into parent
- **unregisterRefreshCommentsMethod** (`function`): method to unregister the refresh comment method from parent
38 changes: 30 additions & 8 deletions src/component/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const propTypes = {
locale: PropTypes.string.isRequired,
messageSentCallback: PropTypes.func,
timeDisplay: PropTypes.oneOf(['ago', 'dateTime']),
dateTimeFormat: PropTypes.string
dateTimeFormat: PropTypes.string,
registerRefreshCommentsMethod: PropTypes.func,
unregisterRefreshCommentsMethod: PropTypes.func
}

const defaultProps = {
Expand All @@ -47,18 +49,27 @@ const defaultProps = {
},
locale: 'en',
timeDisplay: 'ago',
dateTimeFormat: 'DD/MM/YYYY HH:mm'
dateTimeFormat: 'DD/MM/YYYY HH:mm',
registerRefreshCommentsMethod: undefined,
unregisterRefreshCommentsMethod: undefined
}

class Container extends Component {
_refreshComments() {
const { dispatch, concept, conceptId, apiRootUrl } = this.props;
dispatch(getComments(concept, conceptId, apiRootUrl));

constructor(props) {
super(props);

this._refreshComments = this._refreshComments.bind(this);
}

componentWillMount() {
const { dispatch, apiRootUrl, concept, conceptId, locale, registerRefreshCommentsMethod } = this.props;

if (registerRefreshCommentsMethod) {
registerRefreshCommentsMethod(this._refreshComments);
}

moment.locale(this.props.locale);
const { dispatch, apiRootUrl, concept, conceptId } = this.props;
dispatch(getComments(concept, conceptId, apiRootUrl));
}

Expand All @@ -70,10 +81,21 @@ class Container extends Component {
}

componentWillUnmount() {
this.props.dispatch(clearComments());
const { dispatch, unregisterRefreshCommentsMethod } = this.props;

if (unregisterRefreshCommentsMethod) {
unregisterRefreshCommentsMethod();
}

dispatch(clearComments());
clearInterval(this.refreshInterval);
}

_refreshComments() {
const { dispatch, concept, conceptId, apiRootUrl } = this.props;
dispatch(getComments(concept, conceptId, apiRootUrl));
}

render() {
const { comments, dispatch, isLoading, lastUpdate, error, ...otherProps } = this.props;
return (
Expand All @@ -82,7 +104,7 @@ class Container extends Component {
<div data-focus='title'>{this.props.texts.title}</div>
<div data-focus='last-update'>{isLoading ? this.props.texts.loading : `${this.props.texts.lastUpdate} ${moment(lastUpdate).fromNow()}`}</div>
<div data-focus='refresh'>
<button className='mdl-button mdl-js-button mdl-button--fab mdl-button--colored mdl-button--raised' onClick={this._refreshComments.bind(this)}>
<button className='mdl-button mdl-js-button mdl-button--fab mdl-button--colored mdl-button--raised' onClick={this._refreshComments}>
{isLoading ?
<i className="fa fa-circle-o-notch fa-spin"></i>
:
Expand Down

0 comments on commit d833cc9

Please sign in to comment.