Commit f79a5820 authored by Sudheer's avatar Sudheer Committed by Joram Wilander

MM-13387 Collapse and expand slash commands not working (#2163)

* MM-13387 Collapse and expand slash commands not working

 * Preference_store is initiated in the app anymore
   so dispatching redux action

* Remove preference global events
parent ffbd0e25
......@@ -271,39 +271,6 @@ export function emitClearSuggestions(suggestionId) {
});
}
export function emitPreferenceChangedEvent(preference) {
AppDispatcher.handleServerAction({
type: Constants.ActionTypes.RECEIVED_PREFERENCE,
preference,
});
if (addedNewDmUser(preference)) {
loadProfilesForSidebar();
}
}
export function emitPreferencesChangedEvent(preferences) {
AppDispatcher.handleServerAction({
type: Constants.ActionTypes.RECEIVED_PREFERENCES,
preferences,
});
if (preferences.findIndex(addedNewDmUser) !== -1) {
loadProfilesForSidebar();
}
}
function addedNewDmUser(preference) {
return preference.category === Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW && preference.value === 'true';
}
export function emitPreferencesDeletedEvent(preferences) {
AppDispatcher.handleServerAction({
type: Constants.ActionTypes.DELETED_PREFERENCES,
preferences,
});
}
export function sendEphemeralPost(message, channelId, parentId) {
const timestamp = Utils.getTimestamp();
const post = {
......
......@@ -12,6 +12,7 @@ import {
GeneralTypes,
AdminTypes,
IntegrationTypes,
PreferenceTypes,
} from 'mattermost-redux/action_types';
import {WebsocketEvents, General} from 'mattermost-redux/constants';
import {
......@@ -41,6 +42,7 @@ import * as GlobalActions from 'actions/global_actions.jsx';
import {handleNewPost} from 'actions/post_actions.jsx';
import * as StatusActions from 'actions/status_actions.jsx';
import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
import {loadProfilesForSidebar} from 'actions/user_actions.jsx';
import store from 'stores/redux_store.jsx';
import WebSocketClient from 'client/web_websocket_client.jsx';
import {loadPlugin, loadPluginsIfNecessary, removePlugin} from 'plugins';
......@@ -726,17 +728,27 @@ function handleChannelDeletedEvent(msg) {
function handlePreferenceChangedEvent(msg) {
const preference = JSON.parse(msg.data.preference);
GlobalActions.emitPreferenceChangedEvent(preference);
dispatch({type: PreferenceTypes.RECEIVED_PREFERENCES, data: [preference]});
if (addedNewDmUser(preference)) {
loadProfilesForSidebar();
}
}
function handlePreferencesChangedEvent(msg) {
const preferences = JSON.parse(msg.data.preferences);
GlobalActions.emitPreferencesChangedEvent(preferences);
dispatch({type: PreferenceTypes.RECEIVED_PREFERENCES, data: preferences});
if (preferences.findIndex(addedNewDmUser) !== -1) {
loadProfilesForSidebar();
}
}
function handlePreferencesDeletedEvent(msg) {
const preferences = JSON.parse(msg.data.preferences);
GlobalActions.emitPreferencesDeletedEvent(preferences);
dispatch({type: PreferenceTypes.DELETED_PREFERENCES, data: preferences});
}
function addedNewDmUser(preference) {
return preference.category === Constants.Preferences.CATEGORY_DIRECT_CHANNEL_SHOW && preference.value === 'true';
}
function handleUserTypingEvent(msg) {
......@@ -874,4 +886,4 @@ function handleOpenDialogEvent(msg) {
}
store.dispatch(openModal({modalId: ModalIdentifiers.INTERACTIVE_DIALOG, dialogType: InteractiveDialog}));
}
\ No newline at end of file
}
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
import EventEmitter from 'events';
import {PreferenceTypes} from 'mattermost-redux/action_types';
import * as Selectors from 'mattermost-redux/selectors/entities/preferences';
import store from 'stores/redux_store.jsx';
import Constants from 'utils/constants.jsx';
import AppDispatcher from '../dispatcher/app_dispatcher.jsx';
const ActionTypes = Constants.ActionTypes;
const CHANGE_EVENT = 'change';
class PreferenceStore extends EventEmitter {
constructor() {
super();
this.handleEventPayload = this.handleEventPayload.bind(this);
this.dispatchToken = AppDispatcher.register(this.handleEventPayload);
this.preferences = new Map();
this.entities = Selectors.getMyPreferences(store.getState());
Object.keys(this.entities).forEach((key) => {
this.preferences.set(key, this.entities[key].value);
});
store.subscribe(() => {
const newEntities = Selectors.getMyPreferences(store.getState());
const entities = this.entities;
this.entities = newEntities;
if (entities !== newEntities) {
this.preferences = new Map();
Object.keys(newEntities).forEach((key) => {
this.preferences.set(key, newEntities[key].value);
});
this.emitChange();
}
});
this.setMaxListeners(600);
}
getKey(category, name) {
return `${category}--${name}`;
}
get(category, name, defaultValue = '') {
const key = this.getKey(category, name);
if (!this.preferences.has(key)) {
return defaultValue;
}
return this.preferences.get(key);
}
getBool(category, name, defaultValue = false) {
const key = this.getKey(category, name);
if (!this.preferences.has(key)) {
return defaultValue;
}
return this.preferences.get(key) !== 'false';
}
getInt(category, name, defaultValue = 0) {
const key = this.getKey(category, name);
if (!this.preferences.has(key)) {
return defaultValue;
}
return parseInt(this.preferences.get(key), 10);
}
getObject(category, name, defaultValue = null) {
const key = this.getKey(category, name);
if (!this.preferences.has(key)) {
return defaultValue;
}
return JSON.parse(this.preferences.get(key));
}
getCategory(category) {
const prefix = category + '--';
const preferences = new Map();
for (const [key, value] of this.preferences) {
if (key.startsWith(prefix)) {
preferences.set(key.substring(prefix.length), value);
}
}
return preferences;
}
setPreference(category, name, value) {
store.dispatch({
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: [{category, name, value}],
});
}
setPreferencesFromServer(newPreferences) {
store.dispatch({
type: PreferenceTypes.RECEIVED_PREFERENCES,
data: newPreferences,
});
}
deletePreference(preference) {
store.dispatch({
type: PreferenceTypes.DELETED_PREFERENCES,
data: [preference],
});
}
emitChange(category) {
this.emit(CHANGE_EVENT, category);
}
addChangeListener(callback) {
this.on(CHANGE_EVENT, callback);
}
removeChangeListener(callback) {
this.removeListener(CHANGE_EVENT, callback);
}
getTheme() {
return Selectors.getTheme(store.getState());
}
handleEventPayload(payload) {
const action = payload.action;
switch (action.type) {
case ActionTypes.RECEIVED_PREFERENCE: {
const preference = action.preference;
this.setPreference(preference.category, preference.name, preference.value);
this.emitChange(preference.category);
break;
}
case ActionTypes.RECEIVED_PREFERENCES:
this.setPreferencesFromServer(action.preferences);
this.emitChange();
break;
case ActionTypes.DELETED_PREFERENCES:
for (const preference of action.preferences) {
this.deletePreference(preference);
}
this.emitChange();
break;
case ActionTypes.CLICK_CHANNEL:
this.setPreference(action.team_id, 'channel', action.id);
break;
}
}
}
global.PreferenceStore = new PreferenceStore();
export default global.PreferenceStore;
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment