1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
|
<template>
<section
class="cw-structural-element-comments"
:class="[emptyComments ? 'cw-structural-element-comments-empty' : '']"
>
<span class="sr-only" aria-live="polite">{{ srMessage }}</span>
<div class="cw-structural-element-comments-items" v-show="!emptyComments" ref="commentsRef">
<courseware-talk-bubble
v-for="comment in comments"
:key="comment.id"
:payload="buildPayload(comment)"
@delete="deleteComment(comment)"
/>
</div>
<div class="cw-structural-element-comment-create">
<textarea v-model="createComment" :placeholder="placeHolder" spellcheck="true"></textarea>
<button class="button" @click="postComment">{{ $gettext('Senden') }}</button>
</div>
</section>
</template>
<script>
import CoursewareTalkBubble from '../layouts/CoursewareTalkBubble.vue';
import { mapActions, mapGetters } from 'vuex';
export default {
name: 'courseware-structural-element-comments',
emits: ['hasComments'],
components: {
CoursewareTalkBubble,
},
props: {
structuralElement: Object,
},
data() {
return {
createComment: '',
placeHolder: this.$gettext('Stellen Sie eine Frage oder kommentieren Sie...'),
srMessage: ''
};
},
computed: {
...mapGetters({
relatedUser: 'users/related',
userId: 'userId',
getComments: 'courseware-structural-element-comments/related',
}),
comments() {
const parent = {
type: this.structuralElement.type,
id: this.structuralElement.id,
};
return this.getComments({ parent, relationship: 'comments' });
},
emptyComments() {
if (this.comments === null || this.comments.length === 0) {
return true;
}
return false;
}
},
methods: {
...mapActions({
createComments: 'courseware-structural-element-comments/create',
loadRelatedComments: 'courseware-structural-element-comments/loadRelated',
deleteElementComment: 'courseware-structural-element-comments/delete'
}),
async loadComments() {
const parent = {
type: this.structuralElement.type,
id: this.structuralElement.id,
};
await this.loadRelatedComments({
parent,
relationship: 'comments',
options: {
include: 'user',
},
});
},
async postComment() {
this.updateSrMessage(this.$gettext('Kommentar gesendet'));
const data = {
attributes: {
comment: this.createComment
},
relationships: {
'structural-element': {
data: {
id: this.structuralElement.id,
type: this.structuralElement.type
}
}
}
};
await this.createComments(data);
this.loadComments();
this.createComment = '';
},
buildPayload(comment) {
const commenter = this.relatedUser({
parent: { id: comment.id, type: comment.type },
relationship: 'user',
});
const payload = {
id: comment.id,
own: comment.relationships.user.data.id === this.userId,
content: comment.attributes.comment,
chdate: comment.attributes.chdate,
mkdate: comment.attributes.mkdate,
user_id: commenter.id,
user_formatted_name: commenter.attributes['formatted-name'],
username: commenter?.attributes?.username ?? '',
user_avatar: commenter.meta.avatar.small,
};
return payload;
},
deleteComment(comment) {
this.deleteElementComment({ id: comment.id, type: comment.type });
},
updateSrMessage(message) {
this.srMessage = '';
this.srMessage = message;
}
},
mounted() {
this.loadComments();
},
updated() {
let ref = this.$refs["commentsRef"];
ref.scrollTop = ref.scrollHeight;
},
watch: {
comments() {
if (this.comments && this.comments.length > 0) {
this.$emit('hasComments');
}
}
}
}
</script>
|