aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/feedback/FeedbackEntryCreate.vue
blob: 7833a64b6650461fd7ddfbc941ce00cb18e64b56 (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
<template>
    <div v-if="feedbackElement" class="feedback-entry-create">
        <studip-five-stars-input v-model="rating" />
        <label v-if="isCommentable">
            {{ $gettext('Kommentar') }}
            <textarea v-model="comment"></textarea>
        </label>
        <label v-if="anonymousEntriesEnabled">
            <input type="checkbox" v-model="anonymous" />
            {{ $gettext('Feedback anonym abgeben') }}
        </label>
        <div class="button-wrapper">
            <button class="button accept" @click="submitEntry">
                {{ $gettext('Absenden') }}
            </button>
            <button v-if="hasEntry" class="button cancel" @click="$emit('cancel')">
                {{ $gettext('Abbrechen') }}
            </button>

        </div>
    </div>
</template>

<script>
import StudipFiveStarsInput from './StudipFiveStarsInput.vue';
import { mapActions } from 'vuex';

export default {
    name: 'feedback-entry-create',
    components: {
        StudipFiveStarsInput,
    },
    emits: ['cancel', 'submit'],
    props: {
        feedbackElement: {
            type: Object || null,
        },
        entry: {
            type: Object,
            default: null,
        },
        currentUser: {
            type: Object,
            required: true
        }
    },
    data() {
        return {
            rating: 0,
            comment: '',
            anonymous: false
        };
    },
    computed: {
        hasEntry() {
            return this.entry !== null;
        },
        anonymousEntriesEnabled() {
            return this.feedbackElement?.attributes['anonymous-entries'];
        },
        isCommentable() {
            return this.feedbackElement?.attributes['is-commentable'];
        }
    },
    methods: {
        ...mapActions({
            loadFeedbackEntriesById: 'feedback-entries/byId',
            createFeedbackEntries: 'feedback-entries/create',
            updateFeedbackEntries: 'feedback-entries/update',
        }),
        async submitEntry() {
            let data = {
                attributes: {
                    rating: this.rating,
                },
                relationships: {
                    'feedback-element': {
                        data: {
                            type: 'feedback-elements',
                            id: this.feedbackElement.id,
                        },
                    },
                    author: {
                        data: {
                            id: this.currentUser.id,
                            type: 'users'
                        }
                    }
                },
            };
            if (this.isCommentable) {
                data.attributes.comment = this.comment
            }
            if (this.anonymousEntriesEnabled) {
                data.attributes.anonymous = this.anonymous;
            }
            if (this.hasEntry) {
                data.id = this.entry.id;
                data.type = this.entry.type;
                await this.updateFeedbackEntries(data);
            } else {
                await this.createFeedbackEntries(data);
            }
            this.$emit('submit');
        },
    },
    mounted() {
        if (this.hasEntry) {
            this.rating = parseInt(this.entry.attributes.rating);
            this.comment = this.entry.attributes.comment;
            this.anonymous = this.entry.attributes.anonymous;
        }
    },
};
</script>