aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/components/CacheAdministration.vue
blob: a7a1792357d86d4c1cd0ea809484e0a40186ebec (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
<template>
    <StudipMessageBox v-if="!enabled" type="warning" :hide-close="true">
        {{ $gettext('Caching ist systemweit ausgeschaltet, daher kann hier nichts konfiguriert werden.') }}
    </StudipMessageBox>
    <form v-else class="default" :action="actionUrl" method="post" ref="configForm">
        <fieldset>
            <legend>
                <translate>Cachetyp</translate>
            </legend>
            <label>
                <translate>Cachetyp auswählen</translate>

                <select name="cachetype" v-model="selectedCacheType" @change="getCacheConfig">
                    <option v-for="(type) in cacheTypes" :key="type.cache_id" :value="type.class_name">
                        {{ type.display_name }}
                    </option>
                </select>
            </label>
        </fieldset>

        <fieldset>
            <legend>Konfiguration</legend>
            <template v-if="configComponent != null">
                <component :is="configComponent" v-bind="configProps" ref="cacheConfig" @is-valid="setValid"></component>
            </template>
            <template v-else>
                <translate>Für diesen Cachetyp ist keine Konfiguration erforderlich.</translate>
            </template>
        </fieldset>
        <footer data-dialog-button>
            <button class="button accept" @click.prevent="validateConfig" :disabled="!isValid">
                <translate>Speichern</translate>
            </button>
        </footer>
    </form>
</template>

<script>
import FileCacheConfig from './FileCacheConfig.vue'
import MemcachedCacheConfig from './MemcachedCacheConfig.vue'
import RedisCacheConfig from './RedisCacheConfig.vue'
import StudipMessageBox from './StudipMessageBox.vue';

export default {
    name: 'CacheAdministration',
    components: {
        StudipMessageBox,
        FileCacheConfig,
        MemcachedCacheConfig,
        RedisCacheConfig
    },
    props: {
        cacheTypes: {
            type: Array,
            required: true
        },
        currentCache: {
            type: String,
            required: true
        },
        currentConfig: {
            type: Object,
            default() {
                return {
                    component: null,
                    props: []
                };
            }
        },
        enabled: {
            type: Boolean,
            required: true,
        }
    },
    data () {
        return {
            isValid: true,
            selectedCacheType: this.currentCache,
            configComponent: this.currentConfig.component,
            configProps: this.currentConfig.props
        }
    },
    computed: {
        actionUrl () {
            return STUDIP.URLHelper.getURL('dispatch.php/admin/cache/store_settings');
        }
    },
    methods: {
        /**
         * Fetches configuration template for selected cache
         * @param event
         */
        getCacheConfig (event) {
            const url = STUDIP.URLHelper.getURL(
                'dispatch.php/admin/cache/get_config',
                {cache: this.selectedCacheType},
                true
            );
            fetch(url).then((response) => {
                if (!response.ok) {
                    throw response
                }

                response.json().then((json) => {
                    this.configComponent = json.component
                    this.configProps = json.props
                });
            }).catch((error) => {
                console.error(error)
                console.error(error.status + ': ', error.statusText)
            })
        },
        validateConfig () {
            if (this.configComponent == null || this.isValid) {
                this.$refs.configForm.submit()
            }
        },
        setValid (state) {
            this.isValid = state;
        }
    },
    watch: {
        configComponent () {
            this.isValid = true;
        }
    }
}
</script>