summaryrefslogtreecommitdiffstats
path: root/web/react/stores/preference_store.jsx
blob: 79eab4fe11e16910fe9f26cf11e677cf303a9bce (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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.

import Constants from '../utils/constants.jsx';
const ActionTypes = Constants.ActionTypes;
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
import BrowserStore from './browser_store.jsx';
import EventEmitter from 'events';
import UserStore from '../stores/user_store.jsx';

const CHANGE_EVENT = 'change';

function getPreferenceKey(category, name) {
    return `${category}-${name}`;
}

function getPreferenceKeyForModel(preference) {
    return `${preference.category}-${preference.name}`;
}

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

        this.getAllPreferences = this.getAllPreferences.bind(this);
        this.get = this.get.bind(this);
        this.getBool = this.getBool.bind(this);
        this.getInt = this.getInt.bind(this);
        this.getPreference = this.getPreference.bind(this);
        this.getCategory = this.getCategory.bind(this);
        this.getPreferencesWhere = this.getPreferencesWhere.bind(this);
        this.setAllPreferences = this.setAllPreferences.bind(this);
        this.setPreference = this.setPreference.bind(this);

        this.emitChange = this.emitChange.bind(this);
        this.addChangeListener = this.addChangeListener.bind(this);
        this.removeChangeListener = this.removeChangeListener.bind(this);

        this.handleEventPayload = this.handleEventPayload.bind(this);
        this.dispatchToken = AppDispatcher.register(this.handleEventPayload);
    }

    getAllPreferences() {
        return new Map(BrowserStore.getItem('preferences', []));
    }

    get(category, name, defaultValue = '') {
        const preference = this.getAllPreferences().get(getPreferenceKey(category, name));

        if (!preference) {
            return defaultValue;
        }

        return preference.value || defaultValue;
    }

    getBool(category, name, defaultValue = false) {
        const preference = this.getAllPreferences().get(getPreferenceKey(category, name));

        if (!preference) {
            return defaultValue;
        }

        // prevent a non-false default value from being returned instead of an actual false value
        if (preference.value === 'false') {
            return false;
        }

        return (preference.value !== 'false') || defaultValue;
    }

    getInt(category, name, defaultValue = 0) {
        const preference = this.getAllPreferences().get(getPreferenceKey(category, name));

        if (!preference) {
            return defaultValue;
        }

        // prevent a non-zero default value from being returned instead of an actual 0 value
        if (preference.value === '0') {
            return 0;
        }

        return parseInt(preference.value, 10) || defaultValue;
    }

    getPreference(category, name, defaultValue = {}) {
        return this.getAllPreferences().get(getPreferenceKey(category, name)) || defaultValue;
    }

    getCategory(category) {
        return this.getPreferencesWhere((preference) => (preference.category === category));
    }

    getPreferencesWhere(pred) {
        const all = this.getAllPreferences();
        const preferences = [];

        for (const [, preference] of all) {
            if (pred(preference)) {
                preferences.push(preference);
            }
        }

        return preferences;
    }

    setAllPreferences(preferences) {
        // note that we store the preferences as an array of key-value pairs so that we can deserialize
        // it as a proper Map instead of an object
        BrowserStore.setItem('preferences', [...preferences]);
    }

    setPreference(category, name, value) {
        const preferences = this.getAllPreferences();

        const key = getPreferenceKey(category, name);
        let preference = preferences.get(key);

        if (!preference) {
            preference = {
                user_id: UserStore.getCurrentId(),
                category,
                name
            };
        }
        preference.value = value;

        preferences.set(key, preference);

        this.setAllPreferences(preferences);

        return preference;
    }

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

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

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

    handleEventPayload(payload) {
        const action = payload.action;

        switch (action.type) {
        case ActionTypes.RECIEVED_PREFERENCE: {
            const preference = action.preference;
            this.setPreference(preference.category, preference.name, preference.value);
            this.emitChange();
            break;
        }
        case ActionTypes.RECIEVED_PREFERENCES: {
            const preferences = this.getAllPreferences();

            for (const preference of action.preferences) {
                preferences.set(getPreferenceKeyForModel(preference), preference);
            }

            this.setAllPreferences(preferences);
            this.emitChange();
            break;
        }
        }
    }
}

const PreferenceStore = new PreferenceStoreClass();
export default PreferenceStore;
window.PreferenceStore = PreferenceStore;