Skip to content

Commit 7fd4c3e

Browse files
committed
refactor(vue3): clear comment form on submit
Signed-off-by: grnd-alt <git@belakkaf.net>
1 parent 51ae85e commit 7fd4c3e

5 files changed

Lines changed: 52 additions & 41 deletions

File tree

‎src/components/ActivityEntry.vue‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,12 @@ import moment from '@nextcloud/moment'
2323
import DOMPurify from 'dompurify'
2424
import relativeDate from '../mixins/relativeDate.js'
2525
import formatReadableDate from '../mixins/readableDate.js'
26+
import { h } from 'vue'
2627
28+
// Vue 2's `{ functional: true, render(createElement, context) }` signature no
29+
// longer exists in Vue 3, so this is a regular component instead.
2730
const InternalLink = {
2831
name: 'InternalLink',
29-
functional: true,
3032
props: {
3133
href: {
3234
type: String,
@@ -37,8 +39,8 @@ const InternalLink = {
3739
default: '',
3840
},
3941
},
40-
render(createElement, context) {
41-
return createElement('a', { attrs: { href: context.props.href }, style: { 'font-weight': 600 } }, context.props.name)
42+
render() {
43+
return h('a', { href: this.href, style: { 'font-weight': 600 } }, this.name)
4244
},
4345
}
4446
export default {

‎src/components/card/CardSidebarTabComments.vue‎

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
- SPDX-License-Identifier: AGPL-3.0-or-later
44
-->
55
<template>
6-
<div>
6+
<div class="comments" v-v-infinite-scroll="[infiniteHandler, {distance: 10, canLoadMore: () => commentStore.hasMoreComments(card.id)}]">
77
<div class="comment--header">
88
<NcAvatar :user="currentUser.uid" />
99
<span class="username">
@@ -16,15 +16,13 @@
1616
:reply="true"
1717
:preview="true"
1818
@cancel="cancelReply" />
19-
<CommentForm v-model="newComment" @submit="createComment" />
19+
<CommentForm v-model="newComment" @submit="createComment($event)" />
2020

2121
<ul v-if="commentStore.getCommentsForCard(card.id).length > 0" id="commentsFeed">
22-
<div v-v-infinite-scroll="[infiniteHandler, {canLoadMore: () => commentStore.hasMoreComments(card.id)}]">
23-
<CommentItem v-for="comment in commentStore.getCommentsForCard(card.id)"
24-
:key="comment.id"
25-
:comment="comment"
26-
@do-reload="loadComments" />
27-
</div>
22+
<CommentItem v-for="comment in commentStore.getCommentsForCard(card.id)"
23+
:key="comment.id"
24+
:comment="comment"
25+
@do-reload="loadComments" />
2826
<!-- <InfiniteLoading :identifier="card.id" @infinite="infiniteHandler">
2927
<template #spinner>
3028
<div class="icon-loading" />
@@ -101,19 +99,13 @@ export default {
10199
},
102100
},
103101
methods: {
104-
async infiniteHandler($state) {
102+
async infiniteHandler() {
105103
this.error = null
106104
try {
107105
await this.loadMore()
108-
if (this.commentStore.hasMoreComments(this.card.id)) {
109-
$state.loaded()
110-
} else {
111-
$state.complete()
112-
}
113106
} catch (e) {
114107
console.error('Failed to fetch more comments during infinite loading', e)
115108
this.error = t('deck', 'Failed to load comments')
116-
$state.complete()
117109
}
118110
},
119111
async loadComments() {
@@ -132,14 +124,13 @@ export default {
132124
this.error = t('deck', 'Failed to load comments')
133125
}
134126
},
135-
async createComment(content) {
127+
async createComment(comment) {
136128
const commentObj = {
137129
cardId: this.card.id,
138-
comment: content,
130+
comment,
139131
}
140132
await this.commentStore.createComment(commentObj)
141133
this.commentStore.setReplyTo(null)
142-
this.newComment = ''
143134
await this.loadComments()
144135
},
145136
async loadMore() {

‎src/components/card/CommentForm.vue‎

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,29 @@ export default {
4141
NcRichContenteditable,
4242
},
4343
props: {
44-
value: {
44+
modelValue: {
4545
type: String,
4646
default: '',
4747
},
4848
},
49-
emits: ['input', 'submit'],
49+
emits: ['update:modelValue', 'submit'],
5050
data() {
5151
return {
52-
commentText: this.value,
5352
error: null,
5453
}
5554
},
5655
computed: {
5756
...mapState(useBoardStore, {
5857
currentBoard: 'currentBoard',
5958
}),
59+
commentText: {
60+
get() {
61+
return this.modelValue
62+
},
63+
set(value) {
64+
this.$emit('update:modelValue', value)
65+
},
66+
},
6067
members() {
6168
const obj = {}
6269
this.currentBoard.users.forEach(user => {
@@ -70,12 +77,7 @@ export default {
7077
return obj
7178
},
7279
hasContent() {
73-
return this.commentText.trim().length > 0
74-
},
75-
},
76-
watch: {
77-
value(val) {
78-
this.commentText = val
80+
return this.modelValue.trim().length > 0
7981
},
8082
},
8183
methods: {
@@ -84,7 +86,7 @@ export default {
8486
},
8587
validate(submit) {
8688
this.error = null
87-
const content = this.commentText
89+
const content = this.modelValue
8890
if (submit && content.length === 0) {
8991
this.error = t('deck', 'The comment cannot be empty.')
9092
}
@@ -101,7 +103,7 @@ export default {
101103
const temp = document.createElement('div')
102104
temp.innerHTML = content
103105
const text = temp.textContent || temp.innerText || ''
104-
this.$emit('input', text)
106+
this.$emit('update:modelValue', '')
105107
this.$emit('submit', text)
106108
}
107109
},

‎src/components/card/CommentItem.vue‎

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<CommentForm v-if="edit"
7373
v-model="commentMsg"
7474
dir="auto"
75-
@submit="updateComment" />
75+
@submit="updateComment($event)" />
7676
</li>
7777
</template>
7878

@@ -84,17 +84,28 @@ import md5 from 'blueimp-md5'
8484
import relativeDate from '../../mixins/relativeDate.js'
8585
import ReplyIcon from 'vue-material-design-icons/ReplyOutline.vue'
8686
import moment from 'moment'
87+
import { h } from 'vue'
8788
import { useCommentStore } from '../../stores/comment.js'
8889
90+
// Vue 2's `{ functional: true, render(createElement, context) }` signature no
91+
// longer exists in Vue 3, so this is a regular component instead.
8992
const AtMention = {
9093
name: 'AtMention',
91-
functional: true,
92-
render(createElement, context) {
93-
const { user, displayName } = context.props
94-
return createElement(
94+
props: {
95+
user: {
96+
type: String,
97+
required: true,
98+
},
99+
displayName: {
100+
type: String,
101+
default: '',
102+
},
103+
},
104+
render() {
105+
return h(
95106
'span',
96-
{ attrs: { 'data-at-embedded': true, contenteditable: false } },
97-
[createElement(NcUserBubble, { props: { user, displayName }, attrs: { 'data-mention-id': user } })],
107+
{ 'data-at-embedded': true, 'data-mention-id': this.user, contenteditable: false },
108+
[h(NcUserBubble, { user: this.user, displayName: this.displayName })],
98109
)
99110
},
100111
}
@@ -195,9 +206,9 @@ export default {
195206
this.commentMsg = ''
196207
this.edit = false
197208
},
198-
async updateComment() {
209+
async updateComment(message) {
199210
const data = {
200-
comment: { id: this.comment.id, message: this.commentMsg },
211+
comment: { id: this.comment.id, message },
201212
cardId: this.comment.objectId,
202213
}
203214
await this.commentStore.updateComment(data)

‎src/css/comments.scss‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,11 @@
22
* SPDX-FileCopyrightText: 2020 Nextcloud GmbH and Nextcloud contributors
33
* SPDX-License-Identifier: AGPL-3.0-or-later
44
*/
5+
6+
.comments {
7+
height: 100%;
8+
overflow: auto;
9+
}
510
.comment-form form {
611
display: flex;
712
flex-grow: 1;

0 commit comments

Comments
 (0)