Files
talk/plugins/coral-plugin-offtopic/client/components/OffTopicCheckbox.js
T
2017-05-17 11:19:06 -03:00

49 lines
1.2 KiB
JavaScript

import React from 'react';
import {connect} from 'react-redux';
import {bindActionCreators} from 'redux';
import {addTag, removeTag, clearTags} from 'coral-plugin-commentbox/actions';
import styles from './styles.css';
class OffTopicCheckbox extends React.Component {
label = 'OFF_TOPIC';
componentDidMount() {
this.clearTagsHook = this.props.registerHook('postSubmit', (data) => {
this.props.clearTags();
});
}
componentWillUnmount() {
this.props.unregisterHook(this.clearTagsHook);
}
handleChange = (e) => {
if (e.target.checked) {
this.props.addTag(this.label)
} else {
const idx = this.props.commentBox.tags.indexOf(this.label);
this.props.removeTag(idx);
}
}
render() {
return (
<div className={styles.offTopic}>
<label className={styles.offTopicLabel}>
<input type="checkbox" onChange={this.handleChange}/>
Off-Topic
</label>
</div>
)
}
}
const mapStateToProps = ({commentBox}) => ({commentBox});
const mapDispatchToProps = dispatch =>
bindActionCreators({addTag, removeTag, clearTags}, dispatch);
export default connect(mapStateToProps, mapDispatchToProps)(OffTopicCheckbox);