mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
117 lines (116 loc) • 4.92 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
Object.defineProperty(exports, "__esModule", { value: true });
exports.deletePreferences = deletePreferences;
exports.getMyPreferences = getMyPreferences;
exports.getUserPreferences = getUserPreferences;
exports.setCustomStatusInitialisationState = setCustomStatusInitialisationState;
exports.savePreferences = savePreferences;
exports.saveTheme = saveTheme;
exports.deleteTeamSpecificThemes = deleteTeamSpecificThemes;
const action_types_1 = require("mattermost-redux/action_types");
const client_1 = require("mattermost-redux/client");
const preferences_1 = require("mattermost-redux/selectors/entities/preferences");
const users_1 = require("mattermost-redux/selectors/entities/users");
const preference_utils_1 = require("mattermost-redux/utils/preference_utils");
const helpers_1 = require("./helpers");
const constants_1 = require("../constants");
function deletePreferences(userId, preferences) {
return async (dispatch, getState) => {
const state = getState();
const myPreferences = (0, preferences_1.getMyPreferences)(state);
const currentPreferences = preferences.map((pref) => myPreferences[(0, preference_utils_1.getPreferenceKey)(pref.category, pref.name)]);
(async function deletePreferencesWrapper() {
try {
dispatch({
type: action_types_1.PreferenceTypes.DELETED_PREFERENCES,
data: preferences,
});
await client_1.Client4.deletePreferences(userId, preferences);
}
catch {
dispatch({
type: action_types_1.PreferenceTypes.RECEIVED_PREFERENCES,
data: currentPreferences,
});
}
}());
return { data: true };
};
}
function getMyPreferences() {
return (0, helpers_1.bindClientFunc)({
clientFunc: client_1.Client4.getMyPreferences,
onSuccess: action_types_1.PreferenceTypes.RECEIVED_ALL_PREFERENCES,
});
}
// used for fetching some other user's preferences other than current user
function getUserPreferences(userID) {
return (0, helpers_1.bindClientFunc)({
clientFunc: () => client_1.Client4.getUserPreferences(userID),
onSuccess: action_types_1.PreferenceTypes.RECEIVED_USER_ALL_PREFERENCES,
});
}
function setCustomStatusInitialisationState(initializationState) {
return async (dispatch, getState) => {
const state = getState();
const currentUserId = (0, users_1.getCurrentUserId)(state);
const preference = {
user_id: currentUserId,
category: constants_1.Preferences.CATEGORY_CUSTOM_STATUS,
name: constants_1.Preferences.NAME_CUSTOM_STATUS_TUTORIAL_STATE,
value: JSON.stringify(initializationState),
};
await dispatch(savePreferences(currentUserId, [preference]));
};
}
function savePreferences(userId, preferences) {
return async (dispatch, getState) => {
(async function savePreferencesWrapper() {
const state = getState();
const currentUserId = (0, users_1.getCurrentUserId)(state);
const actionType = userId === currentUserId ? action_types_1.PreferenceTypes.RECEIVED_PREFERENCES : action_types_1.PreferenceTypes.RECEIVED_USER_PREFERENCES;
try {
dispatch({
type: actionType,
data: preferences,
});
await client_1.Client4.savePreferences(userId, preferences);
}
catch {
dispatch({
type: action_types_1.PreferenceTypes.DELETED_PREFERENCES,
data: preferences,
});
}
}());
return { data: true };
};
}
function saveTheme(teamId, theme) {
return async (dispatch, getState) => {
const state = getState();
const currentUserId = (0, users_1.getCurrentUserId)(state);
const preference = {
user_id: currentUserId,
category: constants_1.Preferences.CATEGORY_THEME,
name: teamId || '',
value: JSON.stringify(theme),
};
await dispatch(savePreferences(currentUserId, [preference]));
return { data: true };
};
}
function deleteTeamSpecificThemes() {
return async (dispatch, getState) => {
const state = getState();
const themePreferences = (0, preferences_1.getThemePreferences)(state);
const currentUserId = (0, users_1.getCurrentUserId)(state);
const toDelete = themePreferences.filter((pref) => pref.name !== '');
if (toDelete.length > 0) {
await dispatch(deletePreferences(currentUserId, toDelete));
}
return { data: true };
};
}