Skip to content

Commit

Permalink
Handle error case when autocompleting channels (mattermost#3917)
Browse files Browse the repository at this point in the history
  • Loading branch information
streamer45 committed Oct 9, 2019
1 parent ec7d2da commit 057da6d
Showing 1 changed file with 52 additions and 47 deletions.
99 changes: 52 additions & 47 deletions components/suggestion/channel_mention_provider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -155,65 +155,70 @@ export default class ChannelMentionProvider extends Provider {
matchedPretext: captured[1],
});

this.autocompleteChannels(
prefix,
(channels) => {
const myMembers = getMyChannelMemberships(store.getState());
if (this.shouldCancelDispatch(prefix)) {
return;
}
const handleChannels = (channels, withError) => {
const myMembers = getMyChannelMemberships(store.getState());
if (this.shouldCancelDispatch(prefix)) {
return;
}

if (channels.length === 0) {
this.lastPrefixWithNoResults = prefix;
}
if (channels.length === 0 && !withError) {
this.lastPrefixWithNoResults = prefix;
}

// Wrap channels in an outer object to avoid overwriting the 'type' property.
const wrappedMoreChannels = [];
const moreChannels = [];
channels.forEach((item) => {
if (item.delete_at > 0 && !myMembers[item.id]) {
return;
}
if (getChannel(store.getState(), item.id)) {
if (!wrappedChannelIds[item.id]) {
wrappedChannelIds[item.id] = true;
wrappedChannels.push({
type: Constants.MENTION_CHANNELS,
channel: item,
});
}
return;
// Wrap channels in an outer object to avoid overwriting the 'type' property.
const wrappedMoreChannels = [];
const moreChannels = [];
channels.forEach((item) => {
if (item.delete_at > 0 && !myMembers[item.id]) {
return;
}
if (getChannel(store.getState(), item.id)) {
if (!wrappedChannelIds[item.id]) {
wrappedChannelIds[item.id] = true;
wrappedChannels.push({
type: Constants.MENTION_CHANNELS,
channel: item,
});
}
return;
}

wrappedMoreChannels.push({
type: Constants.MENTION_MORE_CHANNELS,
channel: item,
});

moreChannels.push(item);
wrappedMoreChannels.push({
type: Constants.MENTION_MORE_CHANNELS,
channel: item,
});

wrappedChannels = wrappedChannels.sort((a, b) => {
//
// MM-12677 When this is migrated this needs to be fixed to pull the user's locale
//
return sortChannelsByTypeAndDisplayName('en', a.channel, b.channel);
});
const wrapped = wrappedChannels.concat(wrappedMoreChannels);
const mentions = wrapped.map((item) => '~' + item.channel.name);
moreChannels.push(item);
});

wrappedChannels = wrappedChannels.sort((a, b) => {
//
// MM-12677 When this is migrated this needs to be fixed to pull the user's locale
//
return sortChannelsByTypeAndDisplayName('en', a.channel, b.channel);
});
const wrapped = wrappedChannels.concat(wrappedMoreChannels);
const mentions = wrapped.map((item) => '~' + item.channel.name);

if (moreChannels.length > 0) {
store.dispatch({
type: ChannelTypes.RECEIVED_CHANNELS,
data: moreChannels,
});

resultCallback({
matchedPretext: captured[1],
terms: mentions,
items: wrapped,
component: ChannelMentionSuggestion,
});
}

resultCallback({
matchedPretext: captured[1],
terms: mentions,
items: wrapped,
component: ChannelMentionSuggestion,
});
};

this.autocompleteChannels(
prefix,
(channels) => handleChannels(channels, false),
() => handleChannels([], true),
);

return true;
Expand Down

0 comments on commit 057da6d

Please sign in to comment.