From 72c698231bb377589380edf868e15fb327ca49f6 Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Wed, 12 Apr 2017 09:33:24 -0300 Subject: [PATCH] unregister hooks --- client/coral-plugin-commentbox/CommentBox.js | 21 +++++++++++++++++-- .../client/components/OffTopicCheckbox.js | 8 ++----- 2 files changed, 21 insertions(+), 8 deletions(-) diff --git a/client/coral-plugin-commentbox/CommentBox.js b/client/coral-plugin-commentbox/CommentBox.js index 729472856..e8c257899 100644 --- a/client/coral-plugin-commentbox/CommentBox.js +++ b/client/coral-plugin-commentbox/CommentBox.js @@ -101,7 +101,7 @@ class CommentBox extends Component { this.setState({body: ''}); } - addCommentHooks = (hooks = {}) => { + registerHook = (hooks = {}) => { if (typeof hooks === 'object') { this.setState(() => ({ hooks: merge(this.state.hooks, hooks) @@ -110,6 +110,22 @@ class CommentBox extends Component { return this.state.hooks; } + unregisterHook = (hookType = '', hook = '') => { + const hooks = this.state.hooks; + + if (hooks[hookType]) { + if (hooks[hookType][hook]) { + delete hooks[hookType][hook]; + } else { + console.warn(`${hook} is invalid. Cannot unregister ${hook} Hook `); + } + } else { + console.warn(`${hookType} does not exist. Cannot unregister ${hook} Hook `); + } + + this.setState(() => ({hooks})); + } + render () { const {styles, isReply, authorId, charCount} = this.props; let {cancelButtonClicked} = this.props; @@ -148,7 +164,8 @@ class CommentBox extends Component { diff --git a/plugins/coral-plugin-offtopic/client/components/OffTopicCheckbox.js b/plugins/coral-plugin-offtopic/client/components/OffTopicCheckbox.js index 0856e0a93..df87ff290 100644 --- a/plugins/coral-plugin-offtopic/client/components/OffTopicCheckbox.js +++ b/plugins/coral-plugin-offtopic/client/components/OffTopicCheckbox.js @@ -7,7 +7,7 @@ class OffTopicCheckbox extends React.Component { handleChange = (e) => { if (e.target.checked) { - this.props.addCommentHooks({ + this.props.registerHook({ postSubmit: { addTag: (data) => { const {comment} = data.createComment; @@ -19,11 +19,7 @@ class OffTopicCheckbox extends React.Component { } }) } else { - this.props.addCommentHooks({ - postSubmit: { - addTag: () => {} - } - }) + this.props.unregisterHook('postSubmit', 'addTag'); } }