aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/structural-element/CoursewareTreeItemAdder.vue
blob: 1bd4c6f9a7218b2417567b44c74a17291c424508 (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
<template>
    <li class="cw-tree-item cw-tree-item-adder">
        <div class="cw-tree-item-wrapper">
            <form v-if="showForm" class="cw-tree-item-adder-form" @submit.prevent="">
                <input type="text" v-model="elementTitle" :placeholder="$gettext('Titel')" />
                <button class="button accept" :title="$gettext('Seite erstellen')" @click="createElement"></button>
                <button class="button cancel" :title="$gettext('Abbrechen')" @click="closeForm"></button>
            </form>
            <button class="add-element" v-else :title="$gettext('Seite hinzufügen')" @click="showForm = true">
                <studip-icon shape="add" />
            </button>
        </div>
    </li>
</template>

<script>
import { mapActions, mapGetters } from 'vuex';

export default {
    name: 'courseware-tree-item-adder',
    props: {
        parentId: {
            type: String,
            required: true,
        },
    },
    data() {
        return {
            showForm: false,
            elementTitle: '',
        };
    },
    computed: {
        ...mapGetters({
            lastCreatedStructuralElement: 'courseware-structural-elements/lastCreated',
            structuralElementById: 'courseware-structural-elements/byId',
            currentElement: 'currentElement',
        }),
    },
    methods: {
        ...mapActions({
            createStructuralElementWithTemplate: 'createStructuralElementWithTemplate',
            loadStructuralElementById: 'courseware-structural-elements/loadById',
            companionError: 'companionError',
            companionInfo: 'companionInfo',
        }),
        closeForm() {
            this.showForm = false;
            this.elementTitle = '';
        },
        async createElement() {
            this.elementTitle = this.elementTitle.trim();
            if (this.elementTitle === '') {
                this.companionInfo({ info: this.$gettext('Bitte geben Sie einen Titel für die neue Seite ein.') });

                return;
            }

            const element = {
                attributes: {
                    title: this.elementTitle,
                    purpose: 'content',
                    payload: {
                        description: '',
                        color: 'studip-blue',
                        license_type: '',
                        required_time: '',
                        difficulty_start: '',
                        difficulty_end: '',
                    },
                },
                templateId: null,
                parentId: this.parentId,
                currentId: this.currentElement,
            };

            this.closeForm();

            try {
                await this.createStructuralElementWithTemplate(element);
            } catch (e) {
                let errorMessage = this.$gettext(
                    'Es ist ein Fehler aufgetreten. Die Seite konnte nicht erstellt werden.'
                );
                if (e.status === 403) {
                    errorMessage = this.$gettext(
                        'Die Seite konnte nicht erstellt werden. Sie haben nicht die notwendigen Schreibrechte.'
                    );
                }

                this.companionError({ info: errorMessage });
                return;
            }

            const newCreated = this.lastCreatedStructuralElement;
            await this.loadStructuralElementById({ id: newCreated.id });
            const newElement = this.structuralElementById({ id: newCreated.id });

            this.$router.push(newElement.id);
        },
    },
};
</script>