From 7fca451263dba47e13573f8a4a30f0d2df9b7bba Mon Sep 17 00:00:00 2001 From: Belen Curcio Date: Wed, 12 Apr 2017 19:21:55 -0300 Subject: [PATCH] Immutable plugins --- .../graphql/mutations/index.js | 4 +- client/coral-plugin-commentbox/CommentBox.js | 143 ++++++++++-------- .../client/components/OffTopicCheckbox.js | 38 +++-- 3 files changed, 107 insertions(+), 78 deletions(-) diff --git a/client/coral-framework/graphql/mutations/index.js b/client/coral-framework/graphql/mutations/index.js index 16bac9b1a..8b1feed66 100644 --- a/client/coral-framework/graphql/mutations/index.js +++ b/client/coral-framework/graphql/mutations/index.js @@ -16,7 +16,7 @@ export const postComment = graphql(POST_COMMENT, { props: ({ownProps, mutate}) => ({ postItem: comment => { const {asset_id, body, parent_id, tags = []} = comment; - + console.log(tags) return mutate({ variables: { comment @@ -77,7 +77,7 @@ export const postComment = graphql(POST_COMMENT, { return updatedAsset; } } - }) + }); } }), }); diff --git a/client/coral-plugin-commentbox/CommentBox.js b/client/coral-plugin-commentbox/CommentBox.js index a25dbc76c..c506da0cc 100644 --- a/client/coral-plugin-commentbox/CommentBox.js +++ b/client/coral-plugin-commentbox/CommentBox.js @@ -8,49 +8,42 @@ const name = 'coral-plugin-commentbox'; class CommentBox extends Component { - static propTypes = { - commentPostedHandler: PropTypes.func, - postItem: PropTypes.func.isRequired, - cancelButtonClicked: PropTypes.func, - assetId: PropTypes.string.isRequired, - parentId: PropTypes.string, - authorId: PropTypes.string.isRequired, - isReply: PropTypes.bool.isRequired, - canPost: PropTypes.bool, - currentUser: PropTypes.object + constructor(props) { + super(props); + + this.state = { + username: '', + comment: { + body: '', + tags: [] + }, + hooks: { + preSubmit: [], + postSubmit: [] + } + }; } - state = { - body: '', - username: '', - hooks: { - preSubmit: [], - postSubmit: [] - } - } + updateComment = cb => this.setState(cb); postComment = () => { const { - commentPostedHandler, - postItem, - assetId, - updateCountCache, isReply, - countCache, + assetId, parentId, + postItem, + countCache, addNotification, - authorId + updateCountCache, + commentPostedHandler, } = this.props; let comment = { - body: this.state.body, asset_id: assetId, - parent_id: parentId + parent_id: parentId, + ...this.state.comment }; - if (this.props.charCount && this.state.body.length > this.props.charCount) { - return; - } !isReply && updateCountCache(assetId, countCache + 1); // Execute preSubmit Hooks @@ -75,33 +68,37 @@ class CommentBox extends Component { commentPostedHandler(); } }) - .catch((err) => console.error(err)); - this.setState({body: ''}); + .catch((err) => console.error(err)); + + this.setState({ + comment: { + ...this.state.comment, + body: '' + } + }); } registerHook = (hookType = '', hook = () => {}) => { - if (typeof hook === 'function') { - if (typeof hookType === 'string') { - this.setState(state => ({ - hooks: { - ...state.hooks, - [hookType]: [ - ...state.hooks[hookType], - hook - ] - } - })); + if (typeof hook !== 'function') { + return console.warn(`Hooks must be functions. Please check your ${hookType} hooks`); + } else if (typeof hookType === 'string') { + this.setState(state => ({ + hooks: { + ...state.hooks, + [hookType]: [ + ...state.hooks[hookType], + hook + ] + } + })); - return { - hookType, - hook - }; + return { + hookType, + hook + }; - } else { - console.warn('hookTypes must be a string. Please check your hooks'); - } } else { - console.warn(`Hooks must be functions. Please check your ${hookType} hooks`); + return console.warn('hookTypes must be a string. Please check your hooks'); } } @@ -129,10 +126,21 @@ class CommentBox extends Component { }); } + handleChange = e => { + this.setState({ + comment: { + ...this.state.comment, + body: e.target.value + } + }); + } + render () { const {styles, isReply, authorId, charCount} = this.props; let {cancelButtonClicked} = this.props; - const length = this.state.body.length; + + const length = this.state.comment.body.length; + const enablePostComment = !length || (charCount && length > charCount); if (isReply && typeof cancelButtonClicked !== 'function') { console.warn('the CommentBox component should have a cancelButtonClicked callback defined if it lives in a Reply'); @@ -151,46 +159,39 @@ class CommentBox extends Component {