mattermost-redux
Version:
Common code (API client, Redux stores, logic, utility functions) for building a Mattermost client
90 lines (89 loc) • 3.19 kB
JavaScript
;
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
Object.defineProperty(exports, "__esModule", { value: true });
const redux_1 = require("redux");
const action_types_1 = require("mattermost-redux/action_types");
function getKey(preference) {
return `${preference.category}--${preference.name}`;
}
function setAllPreferences(preferences) {
const nextState = {};
if (preferences) {
for (const preference of preferences) {
nextState[getKey(preference)] = preference;
}
}
return nextState;
}
function setAllUserPreferences(preferences) {
const nextState = {};
if (preferences.length === 0) {
return nextState;
}
const userID = preferences[0].user_id;
nextState[userID] = {};
if (preferences) {
for (const preference of preferences) {
nextState[userID][getKey(preference)] = preference;
}
}
return nextState;
}
function myPreferences(state = {}, action) {
switch (action.type) {
case action_types_1.PreferenceTypes.RECEIVED_ALL_PREFERENCES:
return setAllPreferences(action.data);
case action_types_1.UserTypes.LOGIN: // Used by the mobile app
return setAllPreferences(action.data.preferences);
case action_types_1.PreferenceTypes.RECEIVED_PREFERENCES: {
const nextState = { ...state };
if (action.data) {
for (const preference of action.data) {
nextState[getKey(preference)] = preference;
}
}
return nextState;
}
case action_types_1.PreferenceTypes.DELETED_PREFERENCES: {
const nextState = { ...state };
if (action.data) {
for (const preference of action.data) {
Reflect.deleteProperty(nextState, getKey(preference));
}
}
return nextState;
}
case action_types_1.UserTypes.LOGOUT_SUCCESS:
return {};
default:
return state;
}
}
function userPreferences(state = {}, action) {
switch (action.type) {
case action_types_1.PreferenceTypes.RECEIVED_USER_ALL_PREFERENCES:
return setAllUserPreferences(action.data);
case action_types_1.PreferenceTypes.RECEIVED_USER_PREFERENCES: {
const nextState = { ...state };
const data = action.data;
if (action.data && data.length > 0) {
const userID = data[0].user_id;
nextState[userID] = nextState[userID] ? { ...nextState[userID] } : {};
for (const preference of action.data) {
nextState[preference.user_id][getKey(preference)] = preference;
}
}
return nextState;
}
case action_types_1.UserTypes.LOGOUT_SUCCESS:
return {};
default:
return state;
}
}
exports.default = (0, redux_1.combineReducers)({
// object where the key is the category-name and has the corresponding value
myPreferences,
userPreferences,
});