aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/store/MyCoursesStore.js
blob: af8e0fc464471f9d8540c6b63c34cc04db46ea8f (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
const configMapping = {
    view_settings: value => {
        return {
            MY_COURSES_VIEW_SETTINGS: value
        };
    },
    open_groups: value => {
        return {
            MY_COURSES_OPEN_GROUPS: value,
        };
    },
};

export default {
    namespaced: true,

    state: () => ({
        courses: {},
        groups: {},
        userid: null,
        config: {},
    }),

    getters: {
        isGroupOpen: (state) => (group) => {
            if (state.config.group_by === 'sem_number') {
                return true;
            }
            return state.config.open_groups.includes(group.id);
        },
        getConfig: (state) => (...keys) => {
            let config = state.config;
            for (const key of keys) {
                config = config[key];
            }
            return config;
        },
    },

    mutations: {
        setCourses (state, courses) {
            state.courses = courses;
        },
        setGroups (state, groups) {
            state.groups = groups;
        },
        setUserId (state, userid) {
            state.userid = userid;
        },
        setConfig (state, config) {
            state.config = config;
        },
    },

    actions: {
        updateConfigValue({ commit, state }, { key, value }) {
            commit('setConfig', { ...state.config, [key]: value });

            // do we have to store this on the server?
            if (!configMapping[key]) {
                return Promise.resolve(null);
            }

            const configValue = configMapping[key](value);
            const configKey = Object.keys(configValue)[0];
            const documentId = `${state.userid}_${configKey}`;

            const data = {
                id: documentId,
                type: 'config-values',
                attributes: { value: configValue[configKey] }
            };

            return STUDIP.jsonapi.withPromises().patch(`config-values/${documentId}`, { data: { data } })
        },
        toggleOpenGroup ({ state, dispatch }, group) {
            let open_groups = [ ...state.config.open_groups ];
            if (open_groups.includes(group.id)) {
                open_groups = open_groups.filter(item => item != group.id);
            } else {
                open_groups.push(group.id);
            }
            return dispatch('updateConfigValue', { key: 'open_groups', value: open_groups });
        }
    }
}