summaryrefslogtreecommitdiffstats
path: root/webapp/stores/analytics_store.jsx
blob: 63c8a5bee27d94e291733426a572e1db0e5994de (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
// Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import EventEmitter from 'events';

const CHANGE_EVENT = 'change';

import store from 'stores/redux_store.jsx';

class AnalyticsStoreClass extends EventEmitter {
    constructor() {
        super();

        this.entities = {};

        store.subscribe(() => {
            const newEntities = store.getState().entities.admin;

            if (newEntities.analytics !== this.entities.analytics) {
                this.emitChange();
            }

            if (newEntities.teamAnalytics !== this.entities.teamAnalytics) {
                this.emitChange();
            }

            this.entities = newEntities;
        });
    }

    emitChange() {
        this.emit(CHANGE_EVENT);
    }

    addChangeListener(callback) {
        this.on(CHANGE_EVENT, callback);
    }

    removeChangeListener(callback) {
        this.removeListener(CHANGE_EVENT, callback);
    }

    getAllSystem() {
        return JSON.parse(JSON.stringify(store.getState().entities.admin.analytics));
    }

    getAllTeam(id) {
        const teamStats = store.getState().entities.admin.teamAnalytics[id];
        if (teamStats) {
            return JSON.parse(JSON.stringify(teamStats));
        }

        return {};
    }
}

var AnalyticsStore = new AnalyticsStoreClass();
export default AnalyticsStore;