Replace deleteComment with a more KISS solution

This commit is contained in:
roschaefer 2019-09-03 00:00:50 +02:00
parent c4ba2c4aeb
commit 491a626031
9 changed files with 112 additions and 79 deletions

View File

@ -179,6 +179,7 @@ export default {
hasOne: { hasOne: {
invitedBy: '<-[:INVITED]-(related:User)', invitedBy: '<-[:INVITED]-(related:User)',
disabledBy: '<-[:DISABLED]-(related:User)', disabledBy: '<-[:DISABLED]-(related:User)',
location: '-[:IS_IN]->(related:Location)',
}, },
hasMany: { hasMany: {
followedBy: '<-[:FOLLOWS]-(related:User)', followedBy: '<-[:FOLLOWS]-(related:User)',

View File

@ -68,7 +68,6 @@ import ContentMenu from '~/components/ContentMenu'
import ContentViewer from '~/components/Editor/ContentViewer' import ContentViewer from '~/components/Editor/ContentViewer'
import HcEditCommentForm from '~/components/EditCommentForm/EditCommentForm' import HcEditCommentForm from '~/components/EditCommentForm/EditCommentForm'
import CommentMutations from '~/graphql/CommentMutations' import CommentMutations from '~/graphql/CommentMutations'
import PostQuery from '~/graphql/PostQuery'
export default { export default {
data: function() { data: function() {
@ -143,26 +142,14 @@ export default {
}, },
async deleteCommentCallback() { async deleteCommentCallback() {
try { try {
await this.$apollo.mutate({ const {
data: { DeleteComment },
} = await this.$apollo.mutate({
mutation: CommentMutations(this.$i18n).DeleteComment, mutation: CommentMutations(this.$i18n).DeleteComment,
variables: { id: this.comment.id }, variables: { id: this.comment.id },
update: async store => {
const data = await store.readQuery({
query: PostQuery(this.$i18n),
variables: { id: this.post.id },
})
const index = data.Post[0].comments.findIndex(
deletedComment => deletedComment.id === this.comment.id,
)
if (index !== -1) {
data.Post[0].comments.splice(index, 1)
}
await store.writeQuery({ query: PostQuery(this.$i18n), data })
},
}) })
this.$toast.success(this.$t(`delete.comment.success`)) this.$toast.success(this.$t(`delete.comment.success`))
this.$emit('deleteComment') this.$emit('deleteComment', DeleteComment)
} catch (err) { } catch (err) {
this.$toast.error(err.message) this.$toast.error(err.message)
} }

View File

@ -18,11 +18,11 @@
<ds-space margin-bottom="large" /> <ds-space margin-bottom="large" />
<div v-if="post.comments && post.comments.length" id="comments" class="comments"> <div v-if="post.comments && post.comments.length" id="comments" class="comments">
<comment <comment
v-for="(comment, index) in post.comments" v-for="comment in post.comments"
:key="comment.id" :key="comment.id"
:comment="comment" :comment="comment"
:post="post" :post="post"
@deleteComment="post.comments.splice(index, 1)" @deleteComment="deleteComment"
/> />
</div> </div>
<hc-empty v-else name="empty" icon="messages" /> <hc-empty v-else name="empty" icon="messages" />
@ -40,5 +40,12 @@ export default {
props: { props: {
post: { type: Object, default: () => {} }, post: { type: Object, default: () => {} },
}, },
methods: {
deleteComment(deleted) {
this.post.comments = this.post.comments.map(comment => {
return comment.id === deleted.id ? deleted : comment
})
},
},
} }
</script> </script>

View File

@ -118,9 +118,11 @@ export default {
methods: { methods: {
async deletePostCallback() { async deletePostCallback() {
try { try {
await this.$apollo.mutate(deletePostMutation(this.post.id)) const {
data: { DeletePost },
} = await this.$apollo.mutate(deletePostMutation(this.post.id))
this.$toast.success(this.$t('delete.contribution.success')) this.$toast.success(this.$t('delete.contribution.success'))
this.$emit('removePostFromList') this.$emit('removePostFromList', DeletePost)
} catch (err) { } catch (err) {
this.$toast.error(err.message) this.$toast.error(err.message)
} }

View File

@ -1,6 +1,7 @@
import gql from 'graphql-tag' import gql from 'graphql-tag'
export default i18n => { export default i18n => {
const lang = i18n.locale().toUpperCase()
return { return {
CreateComment: gql` CreateComment: gql`
mutation($postId: ID!, $content: String!) { mutation($postId: ID!, $content: String!) {
@ -55,6 +56,31 @@ export default i18n => {
mutation($id: ID!) { mutation($id: ID!) {
DeleteComment(id: $id) { DeleteComment(id: $id) {
id id
contentExcerpt
content
createdAt
disabled
deleted
author {
id
slug
name
avatar
disabled
deleted
shoutedCount
contributionsCount
commentedCount
followedByCount
followedByCurrentUser
location {
name: name${lang}
}
badges {
id
icon
}
}
} }
} }
`, `,

View File

@ -128,3 +128,51 @@ export const PostsEmotionsByCurrentUser = () => {
} }
` `
} }
export const relatedContributions = i18n => {
const lang = i18n.locale().toUpperCase()
return gql`query Post($slug: String!) {
Post(slug: $slug) {
id
title
tags {
id
}
categories {
id
name
icon
}
relatedContributions(first: 2) {
id
title
slug
contentExcerpt
shoutedCount
categories {
id
name
icon
}
author {
id
name
slug
avatar
contributionsCount
followedByCount
followedByCurrentUser
commentedCount
location {
name: name${lang}
}
badges {
id
icon
}
}
}
shoutedCount
}
}`
}

View File

@ -20,7 +20,7 @@
<hc-post-card <hc-post-card
:post="post" :post="post"
:width="{ base: '100%', xs: '100%', md: '50%', xl: '33%' }" :width="{ base: '100%', xs: '100%', md: '50%', xl: '33%' }"
@removePostFromList="deletePost(index, post.id)" @removePostFromList="deletePost"
/> />
</masonry-grid-item> </masonry-grid-item>
</template> </template>
@ -164,9 +164,9 @@ export default {
showMoreContributions() { showMoreContributions() {
this.offset += this.pageSize this.offset += this.pageSize
}, },
deletePost(_index, postId) { deletePost(deletedPost) {
this.posts = this.posts.filter(post => { this.posts = this.posts.filter(post => {
return post.id !== postId return post.id !== deletedPost.id
}) })
}, },
}, },

View File

@ -36,11 +36,11 @@
<ds-section style="margin: 0 -1.5rem; padding: 1.5rem;"> <ds-section style="margin: 0 -1.5rem; padding: 1.5rem;">
<ds-flex v-if="post.relatedContributions && post.relatedContributions.length" gutter="small"> <ds-flex v-if="post.relatedContributions && post.relatedContributions.length" gutter="small">
<hc-post-card <hc-post-card
v-for="(relatedPost, index) in post.relatedContributions" v-for="relatedPost in post.relatedContributions"
:key="relatedPost.id" :key="relatedPost.id"
:post="relatedPost" :post="relatedPost"
:width="{ base: '100%', lg: 1 }" :width="{ base: '100%', lg: 1 }"
@removePostFromList="post.relatedContributions.splice(index, 1)" @removePostFromList="removePostFromList"
/> />
</ds-flex> </ds-flex>
<hc-empty v-else margin="large" icon="file" message="No related Posts" /> <hc-empty v-else margin="large" icon="file" message="No related Posts" />
@ -50,9 +50,9 @@
</template> </template>
<script> <script>
import gql from 'graphql-tag'
import HcPostCard from '~/components/PostCard' import HcPostCard from '~/components/PostCard'
import HcEmpty from '~/components/Empty.vue' import HcEmpty from '~/components/Empty.vue'
import { relatedContributions } from '~/graphql/PostQuery'
export default { export default {
transition: { transition: {
@ -68,57 +68,17 @@ export default {
return this.Post ? this.Post[0] || {} : {} return this.Post ? this.Post[0] || {} : {}
}, },
}, },
methods: {
removePostFromList(deletedPost) {
this.post.relatedContributions = this.post.relatedContributions.filter(contribution => {
return contribution.id !== deletedPost.id
})
},
},
apollo: { apollo: {
Post: { Post: {
query() { query() {
return gql` return relatedContributions(this.$i18n)
query Post($slug: String!) {
Post(slug: $slug) {
id
title
tags {
id
name
}
categories {
id
name
icon
}
relatedContributions(first: 2) {
id
title
slug
contentExcerpt
shoutedCount
commentedCount
categories {
id
name
icon
}
author {
id
name
slug
avatar
contributionsCount
followedByCount
followedByCurrentUser
commentedCount
location {
name: name${this.$i18n.locale().toUpperCase()}
}
badges {
id
icon
}
}
}
shoutedCount
}
}
`
}, },
variables() { variables() {
return { return {

View File

@ -220,11 +220,11 @@
</ds-grid-item> </ds-grid-item>
<template v-if="posts.length"> <template v-if="posts.length">
<masonry-grid-item v-for="(post, index) in posts" :key="post.id"> <masonry-grid-item v-for="post in posts" :key="post.id">
<hc-post-card <hc-post-card
:post="post" :post="post"
:width="{ base: '100%', md: '100%', xl: '50%' }" :width="{ base: '100%', md: '100%', xl: '50%' }"
@removePostFromList="removePostFromList(index)" @removePostFromList="removePostFromList"
/> />
</masonry-grid-item> </masonry-grid-item>
</template> </template>
@ -345,8 +345,10 @@ export default {
}, },
}, },
methods: { methods: {
removePostFromList(index) { removePostFromList(deletedPost) {
this.posts.splice(index, 1) this.posts = this.posts.filter(post => {
return post.id !== deletedPost.id
})
}, },
handleTab(tab) { handleTab(tab) {
this.tabActive = tab this.tabActive = tab