aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/CoursewareStructuralElementFeedback.vue
blob: cc079eada2d862a951a42a822079e0756ea64868 (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
128
129
130
<template>
    <section
        v-if="canEdit || userIsTeacher"
        class="cw-structural-element-feedback"
        :class="[emptyFeedback ? 'cw-structural-element-feedback-empty' : '']"
    >
        <div class="cw-structural-element-feedback-items" v-show="!emptyFeedback" ref="feedbacks">
            <courseware-talk-bubble
                v-for="feedback in feedback"
                :key="feedback.id"
                :payload="buildPayload(feedback)"
            />
        </div>
        <courseware-companion-box
                v-if="!userIsTeacher && feedback.length === 0"
                :msgCompanion="$gettext('Es wurde noch kein Feedback abgegeben.')"
                mood="pointing"
            />
        <div v-if="userIsTeacher" class="cw-structural-element-feedback-create">
            <textarea v-model="feedbackText" :placeholder="placeHolder" spellcheck="true"></textarea>
            <button class="button" @click="postFeedback"><translate>Senden</translate></button>
        </div>
    </section>
</template>

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

export default {
    name: 'courseware-structural-element-feedback',
    components: {
        CoursewareCompanionBox,
        CoursewareTalkBubble,
    },
    props: {
        structuralElement: Object,
        canEdit: Boolean,
    },
    data() {
        return {
            feedbackText: '',
            placeHolder: this.$gettext('Schreiben Sie ein Feedback...'),
        };
    },
    computed: {
        ...mapGetters({
            userId: 'userId',
            getRelatedFeedback: 'courseware-structural-element-feedback/related',
            getRelatedUser: 'users/related',
            userIsTeacher: 'userIsTeacher',
        }),
        feedback() {
            const parent = {
                type: this.structuralElement.type,
                id: this.structuralElement.id,
            };

            return this.getRelatedFeedback({ parent, relationship: 'feedback' });
        },
        emptyFeedback() {
            if (this.feedback === null || this.feedback.length === 0) {
                return true;
            }

            return false;
        }
    },
    methods: {
        buildPayload(feedback) {
            const { id, type } = feedback;
            const user = this.getRelatedUser({ parent: { id, type }, relationship: 'user' });

            return {
                own: user.id === this.userId,
                content: feedback.attributes.feedback,
                chdate: feedback.attributes.chdate,
                mkdate: feedback.attributes.mkdate,
                user_name: user?.attributes?.['formatted-name'] ?? '',
                user_avatar: user?.meta?.avatar.small,
            };
        },
        async loadFeedback() {
            const parent = {
                type: this.structuralElement.type,
                id: this.structuralElement.id,
            };
            await this.$store.dispatch('courseware-structural-element-feedback/loadRelated', {
                parent,
                relationship: 'feedback',
                options: {
                    include: 'user',
                },
            });
        },
        async postFeedback() {
            const data = {
                attributes: {
                    feedback: this.feedbackText,
                },
                relationships: {
                    'structural-element': {
                        data: {
                            id: this.structuralElement.id,
                            type: this.structuralElement.type
                        }
                    }
                },
            };
            await this.$store.dispatch('courseware-structural-element-feedback/create', data, { root: true });
            this.feedbackText = '';
            this.loadFeedback();
        }
    },
    async mounted() {
        await this.loadFeedback();
    },
    updated() {
        this.$refs.feedbacks.scrollTop = this.$refs.feedbacks.scrollHeight;
    },
    watch: {
        feedback() {
            if (this.feedback && this.feedback.length > 0) {
                this.$emit('hasFeedback');
            }
        }
    }
}
</script>