aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/CoursewareBlockComments.vue
blob: fb66627a07e35b499f148e0d03ca2ae0afacd58a (plain)
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
<template>
    <section class="cw-block-comments" :class="[emptyComments ? 'cw-block-comments-empty' : '']">
        <div class="cw-block-features-content">
            <div class="cw-block-comments-items" v-show="!emptyComments" ref="commentsRef">
                <courseware-talk-bubble
                    v-for="comment in comments"
                    :key="comment.id"
                    :payload="buildPayload(comment)"
                />
            </div>
            <div class="cw-block-comment-create">
                <textarea v-model="createComment" :placeholder="placeHolder" spellcheck="true"></textarea>
                <button class="button" @click="postComment"><translate>Senden</translate></button>
            </div>
        </div>
    </section>
</template>

<script>
import CoursewareTalkBubble from './CoursewareTalkBubble.vue';
import { mapGetters } from 'vuex';

export default {
    name: 'courseware-block-comments',
    components: {
        CoursewareTalkBubble,
    },
    props: {
        block: Object,
    },
    data() {
        return {
            createComment: '',
            placeHolder: this.$gettext('Stellen Sie eine Frage oder kommentieren Sie...'),
        };
    },
    computed: {
        ...mapGetters({
            relatedUser: 'users/related',
            userId: 'userId',
            getComments: 'courseware-block-comments/related',
        }),
        comments() {
            const parent = {
                type: this.block.type,
                id: this.block.id,
            };

            return this.getComments({ parent, relationship: 'comments' });
        },
        emptyComments() {
            if (this.comments === null || this.comments.length === 0) {
                return true;
            }

            return false;
        }
    },
    methods: {
        async loadComments() {
            const parent = {
                type: this.block.type,
                id: this.block.id,
            };
            await this.$store.dispatch('courseware-block-comments/loadRelated', {
                parent,
                relationship: 'comments',
                options: {
                    include: 'user',
                },
            });
        },
        async postComment() {
            const data = {
                attributes: {
                    comment: this.createComment
                },
                relationships: {
                    block: {
                        data: {
                            id: this.block.id,
                            type: this.block.type
                        }
                    }
                }
            };

            await this.$store.dispatch('courseware-block-comments/create', 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_name: commenter.attributes['formatted-name'],
                user_avatar: commenter.meta.avatar.small,
            };

            return payload;
        },
    },
    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>