aboutsummaryrefslogtreecommitdiff
path: root/resources/vue/store/avatar.module.js
blob: 7a45576b2834a3e25b365239f11f20cebc1331b4 (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
const getDefaultState = () => {
    return {
        context: null,
        httpClient: null,
        userId: null,
    };
};

const initialState = getDefaultState();

const getters = {
    context(state) {
        return state.context;
    },
    httpClient(state) {
        return state.httpClient;
    },
    userId(state) {
        return state.userId;
    },

    currentAvatar(state, getters, rootState, rootGetters) {
        if (getters.context === null) {
            return null;
        }
        const parent = {
            type: getters.context.type,
            id: getters.context.id,
        };

        const relationship = 'avatar';

        return rootGetters['avatar/related']({ parent, relationship });
    },
    currentUser(state, getters, rootState, rootGetters) {
        const id = getters.userId;
        return rootGetters['users/byId']({ id });
    },
    isCourseAvatar(state, getters) {
        return getters.context?.type === 'courses';
    },
    isInstituteAvatar(state, getters) {
        return getters.context?.type === 'institutes';
    },
    isStudygroupAvatar(state, getters) {
        return getters.context?.type === 'studygroups';
    },
    isUserAvatar(state, getters) {
        return getters.context?.type === 'users';
    },
    isCustomized(state, getters) {
        return getters.currentAvatar.attributes.customized;
    }
};

export const state = { ...initialState };

export const actions = {
    // setters
    setContext({ commit }, context) {
        commit('setContext', context);
    },
    setHttpClient({ commit }, httpClient) {
        commit('setHttpClient', httpClient);
    },
    setUserId({ commit }, userId) {
        commit('setUserId', userId);
    },

    // other actions
    loadAvatar({ dispatch, getters, rootGetters }) {
        const parent = {
            type: getters.context.type,
            id: getters.context.id,
        };

        const relationship = 'avatar';

        return dispatch('avatar/loadRelated', { parent, relationship }, { root: true }).then(() => {
            rootGetters['avatar/related']({ parent, relationship });
        });
    },
};

export const mutations = {
    setContext(state, context) {
        state.context = context;
    },
    setHttpClient(state, httpClient) {
        state.httpClient = httpClient;
    },
    setUserId(state, data) {
        state.userId = data;
    },
};

export default {
    state,
    actions,
    mutations,
    getters,
};