Refactored handling of DeleteModal in Comments and Posts

This commit is contained in:
Wolfgang Huß 2019-05-18 12:58:51 +02:00
parent 04deda57ae
commit 9af8c2b46c
8 changed files with 88 additions and 41 deletions

View File

@ -19,8 +19,8 @@
<content-menu <content-menu
placement="bottom-end" placement="bottom-end"
resource-type="comment" resource-type="comment"
:deleteCallback="deleteCallback"
:resource="comment" :resource="comment"
:callbacks="{ confirmCallback, cancelCallback: null }"
style="float-right" style="float-right"
:is-owner="isAuthor(author.id)" :is-owner="isAuthor(author.id)"
/> />
@ -37,6 +37,7 @@
</template> </template>
<script> <script>
import gql from 'graphql-tag'
import { mapGetters } from 'vuex' import { mapGetters } from 'vuex'
import HcUser from '~/components/User' import HcUser from '~/components/User'
import ContentMenu from '~/components/ContentMenu' import ContentMenu from '~/components/ContentMenu'
@ -71,8 +72,27 @@ export default {
isAuthor(id) { isAuthor(id) {
return this.user.id === id return this.user.id === id
}, },
deleteCallback() { async confirmCallback() {
console.log('"deleteCallback" was called !!!') console.log('"confirmCallback" was called !!! ', this.comment.id)
try {
// XXX Make custom mutation and tests in the Backend !!!
var gqlMutation = gql`
mutation($id: ID!) {
DeleteComment(id: $id) {
id
}
}
`
await this.$apollo.mutate({
mutation: gqlMutation,
variables: { id: this.comment.id }
})
this.$toast.success(this.$t(`delete.comment.success`))
this.$emit('deleteComment')
} catch (err) {
this.$toast.error(err.message)
}
} }
} }
} }

View File

@ -60,7 +60,8 @@ export default {
validator: value => { validator: value => {
return value.match(/(contribution|comment|organization|user)/) return value.match(/(contribution|comment|organization|user)/)
} }
} },
callbacks: { type: Object, default: () => {} }
}, },
computed: { computed: {
routes() { routes() {
@ -151,7 +152,8 @@ export default {
name: dialog, name: dialog,
data: { data: {
type: this.resourceType, type: this.resourceType,
resource: this.resource resource: this.resource,
callbacks: this.callbacks
} }
}) })
} }

View File

@ -18,7 +18,8 @@
v-if="open === 'delete'" v-if="open === 'delete'"
:id="data.resource.id" :id="data.resource.id"
:type="data.type" :type="data.type"
:deleteCallback="deleteCallback" :confirm-callback="data.callbacks.confirmCallback"
:cancel-callback="!!data.callbacks.cancelCallback ? data.callbacks.cancelCallback : null"
:name="name" :name="name"
@close="close" @close="close"
/> />
@ -38,10 +39,6 @@ export default {
ReportModal, ReportModal,
DeleteModal DeleteModal
}, },
props: {
deleteCallback: { type: Function, required: true },
cancelCallback: { type: Function, default: null }
},
computed: { computed: {
...mapGetters({ ...mapGetters({
data: 'modal/data', data: 'modal/data',

View File

@ -53,7 +53,7 @@ export default {
props: { props: {
name: { type: String, default: '' }, name: { type: String, default: '' },
type: { type: String, required: true }, type: { type: String, required: true },
deleteCallback: { type: Function, required: true }, confirmCallback: { type: Function, required: true },
cancelCallback: { type: Function, default: null }, cancelCallback: { type: Function, default: null },
id: { type: String, required: true } id: { type: String, required: true }
}, },
@ -78,6 +78,9 @@ export default {
this.isOpen = false this.isOpen = false
setTimeout(() => { setTimeout(() => {
this.$emit('close') this.$emit('close')
if (!!this.cancelCallback) {
this.cancelCallback()
}
}, 1000) }, 1000)
}, },
async confirm() { async confirm() {
@ -87,34 +90,28 @@ export default {
try { try {
switch (this.type) { switch (this.type) {
case 'contribution': case 'contribution':
gqlMutation = gql` // gqlMutation = gql`
mutation($id: ID!) { // mutation($id: ID!) {
DeletePost(id: $id) { // DeletePost(id: $id) {
id // id
} // }
} // }
` // `
// await this.$apollo.mutate({
// mutation: gqlMutation,
// variables: { id: this.id }
// })
// this.$toast.success(this.$t(`delete.${this.type}.success`))
await this.confirmCallback()
break break
case 'comment': case 'comment':
// XXX Make custom mutation and tests in the Backend !!! // XXX Make custom mutation and tests in the Backend !!!
gqlMutation = gql` await this.confirmCallback()
mutation($id: ID!) {
DeleteComment(id: $id) {
id
}
}
`
break break
} }
await this.$apollo.mutate({
mutation: gqlMutation,
variables: { id: this.id }
})
this.success = true this.success = true
this.$toast.success(this.$t(`delete.${this.type}.success`))
setTimeout(() => { setTimeout(() => {
this.isOpen = false this.isOpen = false
// XXX For comment just reload the page !!!
setTimeout(() => { setTimeout(() => {
this.success = false this.success = false
this.$emit('close') this.$emit('close')
@ -122,25 +119,22 @@ export default {
case 'contribution': case 'contribution':
if (this.$router.history.current.name === 'post-id-slug') { if (this.$router.history.current.name === 'post-id-slug') {
// redirect to index // redirect to index
this.$router.history.push('/') // this.$router.history.push('/')
} else { } else {
// reload the page (when deleting from profile or index) // reload the page (when deleting from profile or index)
window.location.assign(window.location.href) // window.location.assign(window.location.href)
} }
break break
case 'comment':
// reload the page
// window.location.assign(window.location.href)
console.log('Remove comment emit !!!')
// this.$parent.$parent.$emit('deleteComment')
this.deleteCallback()
break
} }
}, 500) }, 500)
}, 1500) }, 1500)
} catch (err) { } catch (err) {
this.success = false this.success = false
this.$toast.error(err.message) // switch (this.type) {
// case 'contribution':
// this.$toast.error(err.message)
// break
// }
} finally { } finally {
this.loading = false this.loading = false
} }

View File

@ -64,6 +64,7 @@
<content-menu <content-menu
resource-type="contribution" resource-type="contribution"
:resource="post" :resource="post"
:callbacks="{ confirmCallback, cancelCallback: null }"
:is-owner="isAuthor" :is-owner="isAuthor"
/> />
</no-ssr> </no-ssr>
@ -77,6 +78,7 @@ import HcUser from '~/components/User'
import ContentMenu from '~/components/ContentMenu' import ContentMenu from '~/components/ContentMenu'
import { randomBytes } from 'crypto' import { randomBytes } from 'crypto'
import { mapGetters } from 'vuex' import { mapGetters } from 'vuex'
import Post from '~/mixins/Post'
export default { export default {
name: 'HcPostCard', name: 'HcPostCard',
@ -84,6 +86,7 @@ export default {
HcUser, HcUser,
ContentMenu ContentMenu
}, },
mixins: [Post],
props: { props: {
post: { post: {
type: Object, type: Object,

25
webapp/mixins/Post.js Normal file
View File

@ -0,0 +1,25 @@
import gql from 'graphql-tag'
export default {
methods: {
async confirmCallback() {
try {
var gqlMutation = gql`
mutation($id: ID!) {
DeletePost(id: $id) {
id
}
}
`
await this.$apollo.mutate({
mutation: gqlMutation,
variables: { id: this.post.id }
})
this.$toast.success(this.$t(`delete.contribution.success`))
// this.$emit('deletePost')
} catch (err) {
this.$toast.error(err.message)
}
}
}
}

View File

@ -18,6 +18,7 @@
placement="bottom-end" placement="bottom-end"
resource-type="contribution" resource-type="contribution"
:resource="post" :resource="post"
:callbacks="{ confirmCallback, cancelCallback: null }"
:is-owner="isAuthor(post.author.id)" :is-owner="isAuthor(post.author.id)"
/> />
</no-ssr> </no-ssr>
@ -114,6 +115,7 @@ import HcUser from '~/components/User'
import HcShoutButton from '~/components/ShoutButton.vue' import HcShoutButton from '~/components/ShoutButton.vue'
import HcCommentForm from '~/components/comments/CommentForm' import HcCommentForm from '~/components/comments/CommentForm'
import HcCommentList from '~/components/comments/CommentList' import HcCommentList from '~/components/comments/CommentList'
import Post from '~/mixins/Post'
export default { export default {
transition: { transition: {
@ -129,6 +131,7 @@ export default {
HcCommentForm, HcCommentForm,
HcCommentList HcCommentList
}, },
mixins: [Post],
head() { head() {
return { return {
title: this.title title: this.title

View File

@ -25,6 +25,7 @@
placement="bottom-end" placement="bottom-end"
resource-type="user" resource-type="user"
:resource="user" :resource="user"
:callbacks="{ confirmCallback, cancelCallback: null }"
:is-owner="myProfile" :is-owner="myProfile"
class="user-content-menu" class="user-content-menu"
/> />
@ -327,6 +328,7 @@ import HcBadges from '~/components/Badges.vue'
import HcLoadMore from '~/components/LoadMore.vue' import HcLoadMore from '~/components/LoadMore.vue'
import HcEmpty from '~/components/Empty.vue' import HcEmpty from '~/components/Empty.vue'
import ContentMenu from '~/components/ContentMenu' import ContentMenu from '~/components/ContentMenu'
import Post from '~/mixins/Post'
export default { export default {
components: { components: {
@ -339,6 +341,7 @@ export default {
HcEmpty, HcEmpty,
ContentMenu ContentMenu
}, },
mixins: [Post],
transition: { transition: {
name: 'slide-up', name: 'slide-up',
mode: 'out-in' mode: 'out-in'