summaryrefslogtreecommitdiffstats
path: root/webapp/components/suggestion/switch_channel_provider.jsx
diff options
context:
space:
mode:
Diffstat (limited to 'webapp/components/suggestion/switch_channel_provider.jsx')
-rw-r--r--webapp/components/suggestion/switch_channel_provider.jsx105
1 files changed, 66 insertions, 39 deletions
diff --git a/webapp/components/suggestion/switch_channel_provider.jsx b/webapp/components/suggestion/switch_channel_provider.jsx
index 94622b536..8921399ce 100644
--- a/webapp/components/suggestion/switch_channel_provider.jsx
+++ b/webapp/components/suggestion/switch_channel_provider.jsx
@@ -1,15 +1,18 @@
// Copyright (c) 2015 Mattermost, Inc. All Rights Reserved.
// See License.txt for license information.
-import React from 'react';
+import Suggestion from './suggestion.jsx';
import ChannelStore from 'stores/channel_store.jsx';
-import SuggestionStore from 'stores/suggestion_store.jsx';
-import Suggestion from './suggestion.jsx';
-import Constants from 'utils/constants.jsx';
-import StatusIcon from 'components/status_icon.jsx';
+
+import {autocompleteUsersInTeam} from 'actions/user_actions.jsx';
+
+import AppDispatcher from 'dispatcher/app_dispatcher.jsx';
+import {Constants, ActionTypes} from 'utils/constants.jsx';
import * as Utils from 'utils/utils.jsx';
+import React from 'react';
+
class SwitchChannelSuggestion extends Suggestion {
render() {
const {item, isSelection} = this.props;
@@ -31,8 +34,6 @@ class SwitchChannelSuggestion extends Suggestion {
icon = <div className='status'><i className='fa fa-globe'/></div>;
} else if (item.type === Constants.PRIVATE_CHANNEL) {
icon = <div className='status'><i className='fa fa-lock'/></div>;
- } else {
- icon = <StatusIcon status={item.status}/>;
}
return (
@@ -48,46 +49,72 @@ class SwitchChannelSuggestion extends Suggestion {
}
export default class SwitchChannelProvider {
+ constructor() {
+ this.timeoutId = '';
+ }
+
+ componentWillUnmount() {
+ clearTimeout(this.timeoutId);
+ }
+
handlePretextChanged(suggestionId, channelPrefix) {
if (channelPrefix) {
const allChannels = ChannelStore.getAll();
const channels = [];
- for (const id of Object.keys(allChannels)) {
- const channel = allChannels[id];
- if (channel.display_name.toLowerCase().startsWith(channelPrefix.toLowerCase())) {
- channels.push(channel);
- }
-
- // TODO: Fix with auto-complete refactor
- /*else if (channel.type === Constants.DM_CHANNEL && Utils.getDirectTeammate(channel.id).username.startsWith(channelPrefix.toLowerCase())) {
- // New channel to not modify existing channel
- const otherUser = Utils.getDirectTeammate(channel.id);
- const newChannel = {
- display_name: otherUser.username,
- name: otherUser.username + ' ' + Utils.localizeMessage('channel_switch_modal.dm', '(Direct Message)'),
- type: Constants.DM_CHANNEL,
- status: UserStore.getStatus(otherUser.id) || 'offline'
- };
- channels.push(newChannel);
- }*/
- }
+ function autocomplete() {
+ autocompleteUsersInTeam(
+ channelPrefix,
+ (data) => {
+ const users = data.in_team;
- channels.sort((a, b) => {
- if (a.display_name === b.display_name) {
- if (a.type !== Constants.DM_CHANNEL && b.type === Constants.DM_CHANNEL) {
- return -1;
- } else if (a.type === Constants.DM_CHANNEL && b.type !== Constants.DM_CHANNEL) {
- return 1;
- }
- return a.name.localeCompare(b.name);
- }
- return a.display_name.localeCompare(b.display_name);
- });
+ for (const id of Object.keys(allChannels)) {
+ const channel = allChannels[id];
+ if (channel.display_name.toLowerCase().startsWith(channelPrefix.toLowerCase())) {
+ channels.push(channel);
+ }
+ }
+
+ for (let i = 0; i < users.length; i++) {
+ const user = users[i];
+ const newChannel = {
+ display_name: user.username,
+ name: user.username + ' ' + Utils.localizeMessage('channel_switch_modal.dm', '(Direct Message)'),
+ type: Constants.DM_CHANNEL
+ };
+ channels.push(newChannel);
+ }
- const channelNames = channels.map((channel) => channel.name);
+ channels.sort((a, b) => {
+ if (a.display_name === b.display_name) {
+ if (a.type !== Constants.DM_CHANNEL && b.type === Constants.DM_CHANNEL) {
+ return -1;
+ } else if (a.type === Constants.DM_CHANNEL && b.type !== Constants.DM_CHANNEL) {
+ return 1;
+ }
+ return a.name.localeCompare(b.name);
+ }
+ return a.display_name.localeCompare(b.display_name);
+ });
+
+ const channelNames = channels.map((channel) => channel.name);
+
+ AppDispatcher.handleServerAction({
+ type: ActionTypes.SUGGESTION_RECEIVED_SUGGESTIONS,
+ id: suggestionId,
+ matchedPretext: channelPrefix,
+ terms: channelNames,
+ items: channels,
+ component: SwitchChannelSuggestion
+ });
+ }
+ );
+ }
- SuggestionStore.addSuggestions(suggestionId, channelNames, channels, SwitchChannelSuggestion, channelPrefix);
+ this.timeoutId = setTimeout(
+ autocomplete.bind(this),
+ Constants.AUTOCOMPLETE_TIMEOUT
+ );
}
}
}