From 6f6fe6912908c28590fbf7775216a48d1baeb6c1 Mon Sep 17 00:00:00 2001 From: okbel Date: Thu, 22 Feb 2018 08:59:21 -0300 Subject: [PATCH] Handling edited comments data --- .../components/EditableCommentContent.js | 21 ++++++++++++++++--- plugins/talk-plugin-rte/client/index.js | 18 ++++++++++++++++ plugins/talk-plugin-rte/server/hooks.js | 10 +++++++++ .../talk-plugin-rte/server/typeDefs.graphql | 6 +++++- 4 files changed, 51 insertions(+), 4 deletions(-) diff --git a/client/coral-embed-stream/src/tabs/stream/components/EditableCommentContent.js b/client/coral-embed-stream/src/tabs/stream/components/EditableCommentContent.js index 34f114651..257860342 100644 --- a/client/coral-embed-stream/src/tabs/stream/components/EditableCommentContent.js +++ b/client/coral-embed-stream/src/tabs/stream/components/EditableCommentContent.js @@ -53,6 +53,8 @@ export class EditableCommentContent extends React.Component { this.state = { body: props.comment.body, loadingState: '', + // data: {@object} contains data that might be useful for plugins, metadata, etc + data: {}, }; } componentDidMount() { @@ -72,8 +74,14 @@ export class EditableCommentContent extends React.Component { } } - handleBodyChange = body => { - this.setState({ body }); + handleBodyChange = (body, ...data) => { + this.setState(state => ({ + body, + data: { + ...state.data, + ...data[0], + }, + })); }; handleSubmit = async () => { @@ -88,9 +96,16 @@ export class EditableCommentContent extends React.Component { if (typeof editComment !== 'function') { return; } + + let input = { + body: this.state.body, + ...this.state.data, + }; + let response; + try { - response = await editComment({ body: this.state.body }); + response = await editComment(input); if (!this.unmounted) { this.setState({ loadingState: 'success' }); } diff --git a/plugins/talk-plugin-rte/client/index.js b/plugins/talk-plugin-rte/client/index.js index 0c6370fa0..1f357de77 100644 --- a/plugins/talk-plugin-rte/client/index.js +++ b/plugins/talk-plugin-rte/client/index.js @@ -15,6 +15,13 @@ export default { } } `, + EditCommentResponse: gql` + fragment TalkRTE_EditCommentResponse on EditCommentResponse { + comment { + htmlBody + } + } + `, }, mutations: { PostComment: ({ variables: { input } }) => { @@ -28,5 +35,16 @@ export default { }, }; }, + EditComment: ({ variables: { edit } }) => { + return { + optimisticResponse: { + editComment: { + comment: { + htmlBody: edit.htmlBody, + }, + }, + }, + }; + }, }, }; diff --git a/plugins/talk-plugin-rte/server/hooks.js b/plugins/talk-plugin-rte/server/hooks.js index a7633fb1e..c46a3b10a 100644 --- a/plugins/talk-plugin-rte/server/hooks.js +++ b/plugins/talk-plugin-rte/server/hooks.js @@ -12,5 +12,15 @@ module.exports = { ); }, }, + editComment: { + async pre(_, { edit }, _context, _info) { + edit.metadata = merge( + {}, + { + htmlBody: edit.htmlBody, + } + ); + }, + }, }, }; diff --git a/plugins/talk-plugin-rte/server/typeDefs.graphql b/plugins/talk-plugin-rte/server/typeDefs.graphql index 766189e04..f5db2fd88 100644 --- a/plugins/talk-plugin-rte/server/typeDefs.graphql +++ b/plugins/talk-plugin-rte/server/typeDefs.graphql @@ -2,6 +2,10 @@ input CreateCommentInput { htmlBody: String } +input EditCommentInput { + htmlBody: String +} + type Comment { htmlBody: String -} +} \ No newline at end of file