diff --git a/backend/src/middleware/validation/index.js b/backend/src/middleware/validation/index.js index de9be72e9..faf95beaf 100644 --- a/backend/src/middleware/validation/index.js +++ b/backend/src/middleware/validation/index.js @@ -22,10 +22,29 @@ const validateUrl = async (resolve, root, args, context, info) => { } } +const validateComment = async (resolve, root, args, context, info) => { + const COMMENT_MIN_LENGTH = 1 + const content = args.content.replace(/<(?:.|\n)*?>/gm, '').trim() + if (!args.content || content.length < COMMENT_MIN_LENGTH) { + throw new UserInputError( + `Comment must be at least ${COMMENT_MIN_LENGTH} character long!` + ) + } + const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!' + const { postId } = args + if (!postId) { + throw new UserInputError(NO_POST_ERR_MESSAGE) + } + + return await resolve(root, args, context, info) +} + export default { Mutation: { CreateUser: validateUsername, UpdateUser: validateUsername, - CreateSocialMedia: validateUrl + CreateSocialMedia: validateUrl, + CreateComment: validateComment, + UpdateComment: validateComment } } diff --git a/backend/src/resolvers/comments.js b/backend/src/resolvers/comments.js index 6dea8e51b..f3cfea378 100644 --- a/backend/src/resolvers/comments.js +++ b/backend/src/resolvers/comments.js @@ -1,13 +1,9 @@ import { neo4jgraphql } from 'neo4j-graphql-js' import { UserInputError } from 'apollo-server' -const COMMENT_MIN_LENGTH = 1 -const NO_POST_ERR_MESSAGE = 'Comment cannot be created without a post!' - export default { Mutation: { CreateComment: async (object, params, context, resolveInfo) => { - const content = params.content.replace(/<(?:.|\n)*?>/gm, '').trim() const { postId } = params // Adding relationship from comment to post by passing in the postId, // but we do not want to create the comment with postId as an attribute @@ -15,15 +11,6 @@ export default { // before comment creation. delete params.postId - if (!params.content || content.length < COMMENT_MIN_LENGTH) { - throw new UserInputError( - `Comment must be at least ${COMMENT_MIN_LENGTH} character long!` - ) - } - if (!postId.trim()) { - throw new UserInputError(NO_POST_ERR_MESSAGE) - } - const session = context.driver.session() const postQueryRes = await session.run( ` @@ -65,38 +52,6 @@ export default { return comment }, UpdateComment: async (object, params, context, resolveInfo) => { - // Strip element tags and remove leading/trailing white spaces from content - const content = params.content.replace(/<(?:.|\n)*?>/gm, '').trim() - const { id } = params - // Check length of content - if (!params.content || content.length < COMMENT_MIN_LENGTH) { - throw new UserInputError( - `Comment must be at least ${COMMENT_MIN_LENGTH} character long!` - ) - } - - // Check if comment exists - const session = context.driver.session() - const commentQueryRes = await session.run( - ` - MATCH (comment: Comment { id:$id}) - RETURN comment`, - { - id - } - ) - - // Destructure content from session results array - const [comment] = commentQueryRes.records.map(record => { - return record.get('comment') - }) - - // Send error message if cannot find a matching comment. - if (!comment) { - throw new UserInputError(NO_COMMENT_ERR_MESSAGE) - } - - // Update comment. const commentRev = await neo4jgraphql( object, params, @@ -104,20 +59,6 @@ export default { resolveInfo, false ) - - await session.run( - ` - MATCH (comment: Comment { id:$id}) - SET comment.content = $content - `, - { - id, - content - } - ) - session.close() - - return commentRev } } } diff --git a/webapp/components/Comment.vue b/webapp/components/Comment.vue index 13edc9c0d..906575880 100644 --- a/webapp/components/Comment.vue +++ b/webapp/components/Comment.vue @@ -22,15 +22,20 @@ :resource="comment" style="float-right" :is-owner="isAuthor(author.id)" + v-on:showEditCommentMenu="editCommentMenu" /> - -
+ +
+ +
+
@@ -39,13 +44,22 @@ import { mapGetters } from 'vuex' import HcUser from '~/components/User' import ContentMenu from '~/components/ContentMenu' +import HcEditCommentForm from '~/components/comments/EditCommentForm' +import gql from 'graphql-tag' export default { components: { HcUser, - ContentMenu + ContentMenu, + HcEditCommentForm + }, + data() { + return { + openEditCommentMenu: false + } }, props: { + post: { type: Object, default: () => {} }, comment: { type: Object, default() { @@ -69,6 +83,9 @@ export default { methods: { isAuthor(id) { return this.user.id === id + }, + editCommentMenu(showMenu) { + this.openEditCommentMenu = showMenu } } } diff --git a/webapp/components/ContentMenu.vue b/webapp/components/ContentMenu.vue index 29473d6b2..5f4cd54e7 100644 --- a/webapp/components/ContentMenu.vue +++ b/webapp/components/ContentMenu.vue @@ -90,7 +90,7 @@ export default { name: this.$t(`comment.edit`), callback: () => { /* eslint-disable-next-line no-console */ - console.log('EDIT COMMENT') + this.$emit('showEditCommentMenu', true) }, icon: 'edit' }) diff --git a/webapp/components/comments/CommentList/index.vue b/webapp/components/comments/CommentList/index.vue index 57b720087..5a682bba6 100644 --- a/webapp/components/comments/CommentList/index.vue +++ b/webapp/components/comments/CommentList/index.vue @@ -12,23 +12,11 @@ >{{ comments.length }}  Comments - -
- + +
+
- +