wikijs-fork/client/components/comments.vue

246 lines
6.1 KiB
Vue
Raw Normal View History

2020-05-17 22:38:23 +00:00
<template lang="pug">
div(v-intersect.once='onIntersect')
2020-05-17 22:38:23 +00:00
v-textarea#discussion-new(
2020-05-23 22:49:10 +00:00
outlined
2020-05-17 22:38:23 +00:00
flat
placeholder='Write a new comment...'
auto-grow
dense
rows='3'
hide-details
2020-05-23 22:49:10 +00:00
v-model='newcomment'
color='blue-grey darken-2'
:background-color='$vuetify.theme.dark ? `grey darken-5` : `white`'
v-if='permissions.write'
2020-05-17 22:38:23 +00:00
)
v-row.mt-2(dense, v-if='!isAuthenticated && permissions.write')
v-col(cols='12', lg='6')
v-text-field(
outlined
color='blue-grey darken-2'
:background-color='$vuetify.theme.dark ? `grey darken-5` : `white`'
placeholder='Your Name'
hide-details
dense
autocomplete='name'
v-model='guestName'
)
v-col(cols='12', lg='6')
v-text-field(
outlined
color='blue-grey darken-2'
:background-color='$vuetify.theme.dark ? `grey darken-5` : `white`'
placeholder='Your Email Address'
hide-details
type='email'
dense
autocomplete='email'
v-model='guestEmail'
)
.d-flex.align-center.pt-3(v-if='permissions.write')
2020-05-17 22:38:23 +00:00
v-icon.mr-1(color='blue-grey') mdi-language-markdown-outline
.caption.blue-grey--text Markdown Format
v-spacer
.caption.mr-3(v-if='isAuthenticated') Posting as #[strong {{userDisplayName}}]
2020-05-17 22:38:23 +00:00
v-btn(
dark
2020-05-23 22:49:10 +00:00
color='blue-grey darken-2'
@click='postComment'
depressed
2020-05-17 22:38:23 +00:00
)
v-icon(left) mdi-comment
2020-05-23 22:49:10 +00:00
span.text-none Post Comment
v-divider.mt-3(v-if='permissions.write')
2020-05-23 22:49:10 +00:00
.pa-5.d-flex.align-center.justify-center(v-if='isLoading')
v-progress-circular(
indeterminate
size='20'
width='1'
color='blue-grey'
)
.caption.blue-grey--text.pl-3: em Loading comments...
2020-05-17 22:38:23 +00:00
v-timeline(
dense
2020-05-23 22:49:10 +00:00
v-else-if='comments && comments.length > 0'
2020-05-17 22:38:23 +00:00
)
v-timeline-item(
color='pink darken-4'
large
2020-05-23 22:49:10 +00:00
v-for='cm of comments'
:key='`comment-` + cm.id'
2020-05-17 22:38:23 +00:00
)
template(v-slot:icon)
v-avatar
v-img(src='http://i.pravatar.cc/64')
v-card.elevation-1
v-card-text
.caption: strong {{cm.authorName}}
2020-05-17 22:38:23 +00:00
.overline.grey--text 3 minutes ago
2020-05-23 22:49:10 +00:00
.mt-3 {{cm.render}}
.pt-5.text-center.body-2.blue-grey--text(v-else-if='permissions.write') Be the first to comment.
.text-center.body-2.blue-grey--text(v-else) No comments yet.
2020-05-17 22:38:23 +00:00
</template>
<script>
2020-05-23 22:49:10 +00:00
import gql from 'graphql-tag'
import { get } from 'vuex-pathify'
import validate from 'validate.js'
import _ from 'lodash'
2020-05-23 22:49:10 +00:00
2020-05-17 22:38:23 +00:00
export default {
2020-05-23 22:49:10 +00:00
data () {
return {
newcomment: '',
isLoading: true,
canFetch: false,
comments: [],
guestName: '',
guestEmail: ''
2020-05-23 22:49:10 +00:00
}
},
computed: {
pageId: get('page/id'),
permissions: get('page/commentsPermissions'),
isAuthenticated: get('user/authenticated'),
userDisplayName: get('user/name')
2020-05-23 22:49:10 +00:00
},
methods: {
onIntersect (entries, observer, isIntersecting) {
if (isIntersecting) {
this.isLoading = true
this.canFetch = true
}
2020-05-23 22:49:10 +00:00
},
async postComment () {
let rules = {
comment: {
presence: {
allowEmpty: false
},
length: {
minimum: 2
}
}
}
if (!this.isAuthenticated && this.permissions.write) {
rules.name = {
presence: {
allowEmpty: false
},
length: {
minimum: 2,
maximum: 255
}
}
rules.email = {
presence: {
allowEmpty: false
},
email: true
}
}
const validationResults = validate({
comment: this.newcomment,
name: this.guestName,
email: this.guestEmail
}, rules, { format: 'flat' })
if (validationResults) {
this.$store.commit('showNotification', {
style: 'red',
message: validationResults[0],
icon: 'alert'
})
return
}
2020-05-17 22:38:23 +00:00
const resp = await this.$apollo.mutate({
mutation: gql`
mutation (
$pageId: Int!
$replyTo: Int
$content: String!
$guestName: String
$guestEmail: String
) {
comments {
create (
pageId: $pageId
replyTo: $replyTo
content: $content
guestName: $guestName
guestEmail: $guestEmail
) {
responseResult {
succeeded
errorCode
slug
message
}
}
}
}
`,
variables: {
pageId: this.pageId,
replyTo: 0,
content: this.newcomment,
guestName: this.guestName,
guestEmail: this.guestEmail
}
})
if (_.get(resp, 'data.comments.create.responseResult.succeeded', false)) {
this.$store.commit('showNotification', {
style: 'success',
message: 'New comment posted successfully.',
icon: 'check'
})
this.newcomment = ''
} else {
this.$store.commit('showNotification', {
style: 'red',
message: _.get(resp, 'data.comments.create.responseResult.message', 'An unexpected error occured.'),
icon: 'alert'
})
}
2020-05-23 22:49:10 +00:00
}
},
apollo: {
comments: {
query: gql`
query ($pageId: Int!) {
comments {
list(pageId: $pageId) {
id
render
authorName
createdAt
updatedAt
}
}
}
`,
variables() {
return {
pageId: this.pageId
}
},
skip () {
return !this.canFetch
},
fetchPolicy: 'cache-and-network',
update: (data) => data.comments.list,
watchLoading (isLoading) {
this.isLoading = isLoading
}
}
}
2020-05-17 22:38:23 +00:00
}
</script>
<style lang="scss">
</style>