aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/courseware/structural-element/CoursewareTreeItemUpdater.vue
blob: 006def158b85940afae430135643363c7265177b (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
<template>
    <form class="cw-tree-item-updater" @submit.prevent="">
        <input type="text" v-model="elementTitle" :placeholder="$gettext('Titel')" />
        <button class="button accept" :title="$gettext('Speichern')" @click="updateElement"></button>
        <button class="button cancel" :title="$gettext('Abbrechen')" @click="close"></button>
    </form>
</template>

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

export default {
    name: 'courseware-tree-item-adder',
    emits: ['childrenUpdated', 'close'],
    props: {
        structuralElement: {
            type: Object,
            required: true,
        },
    },
    data() {
        return {
            elementTitle: '',
        };
    },
    computed: {
        ...mapGetters({
            structuralElementById: 'courseware-structural-elements/byId',
            userId: 'userId',
            userById: 'users/byId',
        }),
    },
    methods: {
        ...mapActions({
            loadStructuralElementById: 'courseware-structural-elements/loadById',
            companionError: 'companionError',
            companionInfo: 'companionInfo',

            updateStructuralElement: 'updateStructuralElement',
            lockObject: 'lockObject',
            unlockObject: 'unlockObject',
            loadUser: 'users/loadById',
        }),
        close() {
            this.$emit('close');
        },
        async updateElement() {
            if (this.elementTitle === '') {
                this.companionInfo({
                    info: this.$gettext('Bitte geben Sie einen Titel für die Seite ein.'),
                });
                return;
            }
            await this.loadStructuralElementById({ id: this.structuralElement.id });
            let element = this.structuralElementById({ id: this.structuralElement.id });
            element.attributes.title = this.elementTitle;
            const blockerData = element?.relationships?.['edit-blocker']?.data;
            const blocked = blockerData !== null && blockerData !== '';
            const blockedByAnotherUser = blocked && blockerData.id !== this.userId;
            if (blockedByAnotherUser) {
                this.close();
                await this.loadUser({ id: blockerData.id });
                const blocker = this.userById({ id: blockerData.id });
                this.companionWarning({
                    info: this.$gettext(
                        'Ihre Änderungen konnten nicht gespeichert werden, da %{blockingUserName} die Bearbeitung übernommen hat.',
                        { blockingUserName: blocker.attributes['formatted-name'] }
                    ),
                });
                return;
            }
            if (!this.blocked) {
                await this.lockObject({ id: this.structuralElement.id, type: 'courseware-structural-elements' });
            }

            await this.lockObject({ id: this.structuralElement.id, type: 'courseware-structural-elements' });
            await this.updateStructuralElement({ element, id: this.structuralElement.id });
            await this.unlockObject({ id: this.structuralElement.id, type: 'courseware-structural-elements' });
            this.$emit('childrenUpdated');
            this.close();
        },
        setTitle() {
            this.elementTitle = this.structuralElement.attributes.title;
        },
    },
    mounted() {
        this.setTitle();
    },
};
</script>