aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/forum/posts/PostEditForm.vue
blob: 84a3d0ac7a93e5d969cca014cec7b6b8782a8b70 (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
<script setup>
import {onMounted, onUnmounted, ref} from "vue";
import {$gettext} from "@/assets/javascripts/lib/gettext";
import {useForumConfig} from "../../../store/pinia/forum/ForumConfig";
import {useForumPost} from "../../../store/pinia/forum/ForumPost";
import StudipIcon from "@/vue/components/StudipIcon.vue";
import StudipSwitch from "@/vue/components/StudipSwitch.vue";
import StudipWysiwyg from "@/vue/components/StudipWysiwyg.vue";
import {deserializeJSONAPIResponse} from "../../../../assets/javascripts/lib/jsonapiUtils";

const forumDiscussionPost = useForumPost();
const forumConfig = useForumConfig();
const emit = defineEmits(['canceled', 'updated']);
const props = defineProps({
    auth_user: {
        type: Object,
        required: true,
    },
    post: {
        type: Object,
        required: true,
    }
});

const anonymous = ref(props.post.anonymous);
const content = ref(props.post.content);
const isLoading = ref(false);

const getPostJSONAPIObject = () => ({
    data: {
        id: props.post.id,
        type: 'forum-postings',
        attributes: {
            content: content.value,
            anonymous: anonymous.value
        }
    }
})


const updatePost = async () => {
    try {
        isLoading.value = true;

        const response = await STUDIP.jsonapi.withPromises().PATCH(
            `forum-postings/${props.post.id}`,
            { data: getPostJSONAPIObject }
        );

        const post = await deserializeJSONAPIResponse(response);

        const updatedPost = {
            ...props.post,
            content: post.content,
            anonymous: content.value,
            chdate: post.chdate
        };

        forumDiscussionPost.updatePost(updatedPost);
        content.value = "";
        emit("updated", updatedPost);

        STUDIP.Report.success($gettext("Die Änderungen wurde gespeichert."));
    } catch (error) {
        STUDIP.Report.error(error.statusText);
    } finally {
        isLoading.value = false;
    }
}

onMounted(() => {
    if (window.location.hash) {
        window.history.pushState('', document.title, window.location.href.split('#')[0]);
    }
})

onUnmounted(() => {
    if (window.location.hash) {
        window.history.pushState('', document.title, window.location.href.split('#')[0]);
    }
})
</script>

<template>
    <form @submit.prevent="updatePost" class="default post-form forum-quote">
        {{ content }}
        <StudipWysiwyg required="required" v-model="content" />
        <div v-if="forumConfig.anonymousPost" class="mt-10">
            <StudipSwitch name="anonymous" v-model="anonymous" :label="$gettext('Anonym')" />
        </div>
        <div class="post-form__footer">
            <button
                type="submit"
                :disabled="isLoading || !content"
                class="button button--icon-label"
                :value="$gettext('Speichern')"
                :title="$gettext('Speichern')"
            >
                <StudipIcon shape="reply" :size="20" aria-hidden="true" />
                {{ $gettext('Speichern') }}
            </button>
            <button
                type="button"
                class="button button--icon-label"
                :title="$gettext('Abbrechen')"
                @click="$emit('canceled')"
            >
                <StudipIcon shape="decline" :size="20" aria-hidden="true"/>
                {{ $gettext('Abbrechen') }}
            </button>
        </div>
    </form>
</template>