mirror of
https://github.com/Ocelot-Social-Community/Ocelot-Social.git
synced 2025-12-13 07:46:06 +00:00
Refactor by melting EditCommentForm.vue into CommentForm.vue
This commit is contained in:
parent
3f4e717163
commit
9f203cda3b
@ -29,9 +29,10 @@
|
|||||||
|
|
||||||
<ds-space margin-bottom="small" />
|
<ds-space margin-bottom="small" />
|
||||||
<div v-if="openEditCommentMenu">
|
<div v-if="openEditCommentMenu">
|
||||||
<hc-edit-comment-form
|
<hc-comment-form
|
||||||
:comment="comment"
|
:update="true"
|
||||||
:post="post"
|
:post="post"
|
||||||
|
:comment="comment"
|
||||||
@showEditCommentMenu="editCommentMenu"
|
@showEditCommentMenu="editCommentMenu"
|
||||||
/>
|
/>
|
||||||
</div>
|
</div>
|
||||||
@ -62,7 +63,7 @@ import { mapGetters } from 'vuex'
|
|||||||
import HcUser from '~/components/User/User'
|
import HcUser from '~/components/User/User'
|
||||||
import ContentMenu from '~/components/ContentMenu'
|
import ContentMenu from '~/components/ContentMenu'
|
||||||
import ContentViewer from '~/components/Editor/ContentViewer'
|
import ContentViewer from '~/components/Editor/ContentViewer'
|
||||||
import HcEditCommentForm from '~/components/EditCommentForm/EditCommentForm'
|
import HcCommentForm from '~/components/CommentForm/CommentForm'
|
||||||
import CommentMutations from '~/graphql/CommentMutations'
|
import CommentMutations from '~/graphql/CommentMutations'
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
@ -76,7 +77,7 @@ export default {
|
|||||||
HcUser,
|
HcUser,
|
||||||
ContentMenu,
|
ContentMenu,
|
||||||
ContentViewer,
|
ContentViewer,
|
||||||
HcEditCommentForm,
|
HcCommentForm,
|
||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
post: { type: Object, default: () => {} },
|
post: { type: Object, default: () => {} },
|
||||||
|
|||||||
@ -41,6 +41,9 @@ describe('CommentForm.vue', () => {
|
|||||||
error: jest.fn(),
|
error: jest.fn(),
|
||||||
success: jest.fn(),
|
success: jest.fn(),
|
||||||
},
|
},
|
||||||
|
$filters: {
|
||||||
|
removeHtml: a => a,
|
||||||
|
},
|
||||||
}
|
}
|
||||||
propsData = {
|
propsData = {
|
||||||
post: {
|
post: {
|
||||||
|
|||||||
@ -2,18 +2,18 @@
|
|||||||
<ds-form v-model="form" @submit="handleSubmit">
|
<ds-form v-model="form" @submit="handleSubmit">
|
||||||
<template slot-scope="{ errors }">
|
<template slot-scope="{ errors }">
|
||||||
<ds-card>
|
<ds-card>
|
||||||
<hc-editor
|
<!-- with client-only the content is not shown -->
|
||||||
ref="editor"
|
<hc-editor ref="editor" :users="users" :value="form.content" @input="updateEditorContent" />
|
||||||
:users="users"
|
|
||||||
:hashtags="null"
|
|
||||||
:value="form.content"
|
|
||||||
@input="updateEditorContent"
|
|
||||||
/>
|
|
||||||
<ds-space />
|
<ds-space />
|
||||||
<ds-flex :gutter="{ base: 'small', md: 'small', sm: 'x-large', xs: 'x-large' }">
|
<ds-flex :gutter="{ base: 'small', md: 'small', sm: 'x-large', xs: 'x-large' }">
|
||||||
<ds-flex-item :width="{ base: '0%', md: '50%', sm: '0%', xs: '0%' }" />
|
<ds-flex-item :width="{ base: '0%', md: '50%', sm: '0%', xs: '0%' }" />
|
||||||
<ds-flex-item :width="{ base: '40%', md: '20%', sm: '30%', xs: '30%' }">
|
<ds-flex-item :width="{ base: '40%', md: '20%', sm: '30%', xs: '30%' }">
|
||||||
<ds-button :disabled="disabled" ghost class="cancelBtn" @click.prevent="clear">
|
<ds-button
|
||||||
|
:disabled="disabled && !update"
|
||||||
|
ghost
|
||||||
|
class="cancelBtn"
|
||||||
|
@click.prevent="handleCancel"
|
||||||
|
>
|
||||||
{{ $t('actions.cancel') }}
|
{{ $t('actions.cancel') }}
|
||||||
</ds-button>
|
</ds-button>
|
||||||
</ds-flex-item>
|
</ds-flex-item>
|
||||||
@ -30,6 +30,7 @@
|
|||||||
|
|
||||||
<script>
|
<script>
|
||||||
import HcEditor from '~/components/Editor/Editor'
|
import HcEditor from '~/components/Editor/Editor'
|
||||||
|
import { COMMENT_MIN_LENGTH } from '../../constants/comment'
|
||||||
import { minimisedUserQuery } from '~/graphql/User'
|
import { minimisedUserQuery } from '~/graphql/User'
|
||||||
import PostQuery from '~/graphql/PostQuery'
|
import PostQuery from '~/graphql/PostQuery'
|
||||||
import CommentMutations from '~/graphql/CommentMutations'
|
import CommentMutations from '~/graphql/CommentMutations'
|
||||||
@ -39,36 +40,55 @@ export default {
|
|||||||
HcEditor,
|
HcEditor,
|
||||||
},
|
},
|
||||||
props: {
|
props: {
|
||||||
|
update: { type: Boolean, default: () => false },
|
||||||
post: { type: Object, default: () => {} },
|
post: { type: Object, default: () => {} },
|
||||||
|
comment: {
|
||||||
|
type: Object,
|
||||||
|
default: () => {},
|
||||||
|
},
|
||||||
},
|
},
|
||||||
data() {
|
data() {
|
||||||
return {
|
return {
|
||||||
disabled: true,
|
disabled: true,
|
||||||
loading: false,
|
loading: false,
|
||||||
form: {
|
form: {
|
||||||
content: '',
|
content: !this.update || !this.comment.content ? '' : this.comment.content,
|
||||||
},
|
},
|
||||||
users: [],
|
users: [],
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
updateEditorContent(value) {
|
updateEditorContent(value) {
|
||||||
const content = value.replace(/<(?:.|\n)*?>/gm, '').trim()
|
const sanitizedContent = this.$filters.removeHtml(value, false)
|
||||||
if (content.length < 1) {
|
if (!this.update) {
|
||||||
this.disabled = true
|
if (sanitizedContent.length < COMMENT_MIN_LENGTH) {
|
||||||
|
this.disabled = true
|
||||||
|
} else {
|
||||||
|
this.disabled = false
|
||||||
|
}
|
||||||
} else {
|
} else {
|
||||||
this.disabled = false
|
this.disabled =
|
||||||
|
value === this.comment.content || sanitizedContent.length < COMMENT_MIN_LENGTH
|
||||||
}
|
}
|
||||||
this.form.content = value
|
this.form.content = value
|
||||||
},
|
},
|
||||||
clear() {
|
clear() {
|
||||||
this.$refs.editor.clear()
|
this.$refs.editor.clear()
|
||||||
},
|
},
|
||||||
|
closeEditWindow() {
|
||||||
|
this.$emit('showEditCommentMenu', false)
|
||||||
|
},
|
||||||
|
handleCancel() {
|
||||||
|
if (!this.update) {
|
||||||
|
this.clear()
|
||||||
|
} else {
|
||||||
|
this.closeEditWindow()
|
||||||
|
}
|
||||||
|
},
|
||||||
handleSubmit() {
|
handleSubmit() {
|
||||||
this.loading = true
|
let mutateParams
|
||||||
this.disabled = true
|
if (!this.update) {
|
||||||
this.$apollo
|
mutateParams = {
|
||||||
.mutate({
|
|
||||||
mutation: CommentMutations(this.$i18n).CreateComment,
|
mutation: CommentMutations(this.$i18n).CreateComment,
|
||||||
variables: {
|
variables: {
|
||||||
postId: this.post.id,
|
postId: this.post.id,
|
||||||
@ -82,12 +102,32 @@ export default {
|
|||||||
data.Post[0].comments.push(CreateComment)
|
data.Post[0].comments.push(CreateComment)
|
||||||
await store.writeQuery({ query: PostQuery(this.$i18n), data })
|
await store.writeQuery({ query: PostQuery(this.$i18n), data })
|
||||||
},
|
},
|
||||||
})
|
}
|
||||||
|
} else {
|
||||||
|
mutateParams = {
|
||||||
|
mutation: CommentMutations(this.$i18n).UpdateComment,
|
||||||
|
variables: {
|
||||||
|
content: this.form.content,
|
||||||
|
id: this.comment.id,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
this.loading = true
|
||||||
|
this.disabled = true
|
||||||
|
this.$apollo
|
||||||
|
.mutate(mutateParams)
|
||||||
.then(res => {
|
.then(res => {
|
||||||
this.loading = false
|
this.loading = false
|
||||||
this.clear()
|
if (!this.update) {
|
||||||
this.$toast.success(this.$t('post.comment.submitted'))
|
this.clear()
|
||||||
this.disabled = false
|
this.$toast.success(this.$t('post.comment.submitted'))
|
||||||
|
this.disabled = false
|
||||||
|
} else {
|
||||||
|
this.$toast.success(this.$t('post.comment.updated'))
|
||||||
|
this.disabled = false
|
||||||
|
this.closeEditWindow()
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.catch(err => {
|
.catch(err => {
|
||||||
this.$toast.error(err.message)
|
this.$toast.error(err.message)
|
||||||
|
|||||||
@ -1,96 +0,0 @@
|
|||||||
<template>
|
|
||||||
<ds-form v-model="form" @submit="handleSubmit">
|
|
||||||
<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" />
|
|
||||||
<ds-space />
|
|
||||||
<ds-flex :gutter="{ base: 'small', md: 'small', sm: 'x-large', xs: 'x-large' }">
|
|
||||||
<ds-flex-item :width="{ base: '0%', md: '50%', sm: '0%', xs: '0%' }" />
|
|
||||||
<ds-flex-item :width="{ base: '40%', md: '20%', sm: '30%', xs: '30%' }">
|
|
||||||
<ds-button ghost class="cancelBtn" @click.prevent="closeEditWindow">
|
|
||||||
{{ $t('actions.cancel') }}
|
|
||||||
</ds-button>
|
|
||||||
</ds-flex-item>
|
|
||||||
<ds-flex-item :width="{ base: '40%', md: '20%', sm: '40%', xs: '40%' }">
|
|
||||||
<ds-button type="submit" :loading="loading" :disabled="disabled || errors" primary>
|
|
||||||
{{ $t('post.comment.submit') }}
|
|
||||||
</ds-button>
|
|
||||||
</ds-flex-item>
|
|
||||||
</ds-flex>
|
|
||||||
</ds-card>
|
|
||||||
</template>
|
|
||||||
</ds-form>
|
|
||||||
</template>
|
|
||||||
|
|
||||||
<script>
|
|
||||||
import HcEditor from '~/components/Editor/Editor'
|
|
||||||
import { minimisedUserQuery } from '~/graphql/User'
|
|
||||||
import CommentMutations from '~/graphql/CommentMutations.js'
|
|
||||||
|
|
||||||
export default {
|
|
||||||
components: {
|
|
||||||
HcEditor,
|
|
||||||
},
|
|
||||||
props: {
|
|
||||||
comment: {
|
|
||||||
type: Object,
|
|
||||||
default() {
|
|
||||||
return {}
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
data() {
|
|
||||||
return {
|
|
||||||
disabled: true,
|
|
||||||
loading: false,
|
|
||||||
form: {
|
|
||||||
content: this.comment.content,
|
|
||||||
},
|
|
||||||
users: [],
|
|
||||||
}
|
|
||||||
},
|
|
||||||
methods: {
|
|
||||||
updateEditorContent(value) {
|
|
||||||
const sanitizedContent = value.replace(/<(?:.|\n)*?>/gm, '').trim()
|
|
||||||
this.disabled = value === this.comment.content || sanitizedContent.length < 1
|
|
||||||
this.form.content = value
|
|
||||||
},
|
|
||||||
closeEditWindow() {
|
|
||||||
this.$emit('showEditCommentMenu', false)
|
|
||||||
},
|
|
||||||
handleSubmit() {
|
|
||||||
this.loading = true
|
|
||||||
this.disabled = true
|
|
||||||
this.$apollo
|
|
||||||
.mutate({
|
|
||||||
mutation: CommentMutations(this.$i18n).UpdateComment,
|
|
||||||
variables: {
|
|
||||||
content: this.form.content,
|
|
||||||
id: this.comment.id,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.then(() => {
|
|
||||||
this.loading = false
|
|
||||||
|
|
||||||
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({ data: { User } }) {
|
|
||||||
this.users = User
|
|
||||||
},
|
|
||||||
},
|
|
||||||
},
|
|
||||||
}
|
|
||||||
</script>
|
|
||||||
1
webapp/constants/comment.js
Normal file
1
webapp/constants/comment.js
Normal file
@ -0,0 +1 @@
|
|||||||
|
export const COMMENT_MIN_LENGTH = 1
|
||||||
@ -83,10 +83,13 @@ export default ({ app = {} }) => {
|
|||||||
|
|
||||||
return excerpt
|
return excerpt
|
||||||
},
|
},
|
||||||
removeHtml: content => {
|
removeHtml: (content, replaceLinebreaks = true) => {
|
||||||
if (!content) return ''
|
if (!content) return ''
|
||||||
// replace linebreaks with spaces first
|
let contentExcerpt = content
|
||||||
let contentExcerpt = content.replace(/<br>/gim, ' ').trim()
|
if (replaceLinebreaks) {
|
||||||
|
// replace linebreaks with spaces first
|
||||||
|
contentExcerpt = contentExcerpt.replace(/<br>/gim, ' ').trim()
|
||||||
|
}
|
||||||
// remove the rest of the HTML
|
// remove the rest of the HTML
|
||||||
contentExcerpt = contentExcerpt.replace(/<(?:.|\n)*?>/gm, '').trim()
|
contentExcerpt = contentExcerpt.replace(/<(?:.|\n)*?>/gm, '').trim()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user