aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/plugins/blubber.js
blob: 1f41767409fc136693d3e916f57351183512e560 (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
import axios from 'axios';
import { mapResourceModules } from '@/assets/javascripts/lib/reststate-vuex.js';
import JSUpdater from '@/assets/javascripts/lib/jsupdater.js';
import blubberModule from '../store/blubber.js';
import * as components from '../components/blubber/components.js';

const JSONAPI_PATH = 'jsonapi.php/v1';

export const BlubberPlugin = {
    install(app, options = {}) {
        if (!('store' in options)) {
            throw new Error('You must provide the vuex store via the options argument');
        }

        this.enhanceStore(options.store);
        this.registerComponents(app);
        this.registerUpdater(options.store);
    },
    enhanceStore(store) {
        const httpClient = getHttpClient(window.STUDIP.URLHelper.getURL(JSONAPI_PATH, {}, true));
        initializeStore(store, httpClient);
    },
    registerComponents(app) {
        Object.entries(components).forEach(([name, component]) => {
            const exists = app.component(name);
            if (!exists) {
                app.component(name, component);
            }
        });
    },
    registerUpdater(store) {
        registerUpdater(JSUpdater, store);
    },
};

function getHttpClient(baseURL) {
    return axios.create({ baseURL, headers: { 'Content-Type': 'application/vnd.api+json' } });
}

function initializeStore(store, httpClient) {
    const modules = mapResourceModules({ names: ['blubber-threads', 'blubber-comments', 'users'], httpClient });
    Object.entries(modules).forEach(([name, module]) => {
        if (!store.hasModule(name)) {
            store.registerModule(name, module);
        }
    });
    if (!store.hasModule(['studip'])) {
        store.registerModule(['studip'], { namespaced: true });
    }
    if (!store.hasModule(['studip', 'blubber'])) {
        store.registerModule(['studip', 'blubber'], blubberModule);
    }
}

function registerUpdater(updater, store) {
    if (!updater.isRegistered('blubber')) {
        updater.register(
            'blubber',
            (datagram) => store.dispatch('studip/blubber/updateState', datagram),
            store.getters['studip/blubber/pollingParams']
        );
    }
}