unregister hooks

This commit is contained in:
Belen Curcio
2017-04-12 09:33:24 -03:00
parent 83eb6492c2
commit 72c698231b
2 changed files with 21 additions and 8 deletions
+19 -2
View File
@@ -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 {
<Slot
fill="commentBoxDetail"
addCommentHooks={this.addCommentHooks}
registerHook={this.registerHook}
unregisterHook={this.unregisterHook}
inline
/>
@@ -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');
}
}