2019-12-19 00:50:27 +05:30

153 lines
3.8 KiB
Vue

<template>
<ds-form v-model="form" @submit="handleSubmit" class="comment-form">
<template slot-scope="{ errors }">
<ds-card>
<!-- with client-only the content is not shown -->
<hc-editor ref="editor" :users="users" :value="form.content" @input="updateEditorContent" />
<div class="buttons">
<base-button :disabled="disabled && !update" @click="handleCancel">
{{ $t('actions.cancel') }}
</base-button>
<base-button type="submit" :loading="loading" :disabled="disabled || errors" primary>
{{ $t('post.comment.submit') }}
</base-button>
</div>
</ds-card>
</template>
</ds-form>
</template>
<script>
import HcEditor from '~/components/Editor/Editor'
import { COMMENT_MIN_LENGTH } from '../../constants/comment'
import { minimisedUserQuery } from '~/graphql/User'
import CommentMutations from '~/graphql/CommentMutations'
export default {
components: {
HcEditor,
},
props: {
update: { type: Boolean, default: () => false },
post: { type: Object, default: () => {} },
comment: {
type: Object,
default: () => {},
},
},
data() {
return {
disabled: true,
loading: false,
form: {
content: !this.update || !this.comment.content ? '' : this.comment.content,
},
users: [],
}
},
methods: {
updateEditorContent(value) {
const sanitizedContent = this.$filters.removeHtml(value, false)
if (!this.update) {
if (sanitizedContent.length < COMMENT_MIN_LENGTH) {
this.disabled = true
} else {
this.disabled = false
}
} else {
this.disabled =
value === this.comment.content || sanitizedContent.length < COMMENT_MIN_LENGTH
}
this.form.content = value
},
clear() {
this.$refs.editor.clear()
},
closeEditWindow() {
this.$emit('showEditCommentMenu', false)
},
handleCancel() {
console.log('handle cancel')
if (!this.update) {
this.clear()
} else {
this.closeEditWindow()
}
},
handleSubmit() {
let mutateParams
if (!this.update) {
mutateParams = {
mutation: CommentMutations(this.$i18n).CreateComment,
variables: {
postId: this.post.id,
content: this.form.content,
},
}
} else {
mutateParams = {
mutation: CommentMutations(this.$i18n).UpdateComment,
variables: {
id: this.comment.id,
content: this.form.content,
},
}
}
this.loading = true
this.disabled = true
this.$apollo
.mutate(mutateParams)
.then(res => {
this.loading = false
if (!this.update) {
const {
data: { CreateComment },
} = res
this.$emit('createComment', CreateComment)
this.clear()
this.$toast.success(this.$t('post.comment.submitted'))
this.disabled = false
} else {
const {
data: { UpdateComment },
} = res
this.$emit('updateComment', UpdateComment)
this.$emit('collapse')
this.$toast.success(this.$t('post.comment.updated'))
this.disabled = false
this.closeEditWindow()
}
})
.catch(err => {
this.$toast.error(err.message)
})
},
},
apollo: {
User: {
query() {
return minimisedUserQuery()
},
result(result) {
this.users = result.data.User
},
},
},
}
</script>
<style lang="scss">
.comment-form {
.buttons {
display: flex;
justify-content: flex-end;
margin: $space-small 0;
> .base-button {
margin-left: $space-x-small;
}
}
}
</style>