On change, Group mentions are updated in autocomplete (#4944)

* On change, Group mentions are updated in autocomplete

* Converted getAssociatedGroupsForReference, searchAssociatedGroupsForReferenceLocal functions to reselectors

* Sending only state to getCurrentUserLocale in selector

* Added types to passed params

Co-authored-by: anurag shivarathri <anuragindianbraves@gmail.com>
This commit is contained in:
Anurag Shivarathri 2020-11-13 17:26:35 +05:30 committed by GitHub
parent 48dbb75471
commit 31d5f9eab6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 42 additions and 30 deletions

View file

@ -57,7 +57,7 @@ export default class AtMention extends PureComponent {
}
componentWillReceiveProps(nextProps) {
const {inChannel, outChannel, teamMembers, isSearch, matchTerm, requestStatus} = nextProps;
const {groups, inChannel, outChannel, teamMembers, isSearch, matchTerm, requestStatus} = nextProps;
// Not invoked, render nothing.
if (matchTerm === null) {
@ -83,8 +83,13 @@ export default class AtMention extends PureComponent {
}
// Server request is complete
if (requestStatus !== RequestStatus.STARTED &&
(inChannel !== this.props.inChannel || outChannel !== this.props.outChannel || teamMembers !== this.props.teamMembers)) {
if (
groups !== this.props.groups ||
(
requestStatus !== RequestStatus.STARTED &&
(inChannel !== this.props.inChannel || outChannel !== this.props.outChannel || teamMembers !== this.props.teamMembers)
)
) {
const sections = this.buildSections(nextProps);
this.setState({
sections,

View file

@ -57,34 +57,41 @@ export function getGroupMembers(state: GlobalState, id: string) {
return groupMemberData.members;
}
export function searchAssociatedGroupsForReferenceLocal(state: GlobalState, term: string, teamId: string, channelId: string): Array<Group> {
const groups = getAssociatedGroupsForReference(state, teamId, channelId);
if (!groups || groups.length === 0) {
return emptyList;
}
const filteredGroups = filterGroupsMatchingTerm(groups, term);
return filteredGroups;
}
export const getAssociatedGroupsForReference = reselect.createSelector(
(state: GlobalState, teamId: string, channelId: string) => {
const team = getTeam(state, teamId);
const channel = getChannel(state, channelId);
let groupsForReference = [];
if (team && team.group_constrained && channel && channel.group_constrained) {
const groupsFromChannel = getGroupsAssociatedToChannelForReference(state, channelId);
const groupsFromTeam = getGroupsAssociatedToTeamForReference(state, teamId);
groupsForReference = groupsFromChannel.concat(groupsFromTeam.filter((item) => groupsFromChannel.indexOf(item) < 0));
} else if (team && team.group_constrained) {
groupsForReference = getGroupsAssociatedToTeamForReference(state, teamId);
} else if (channel && channel.group_constrained) {
groupsForReference = getGroupsAssociatedToChannelForReference(state, channelId);
} else {
groupsForReference = getAllAssociatedGroupsForReference(state);
}
return groupsForReference;
},
(state: GlobalState) => getCurrentUserLocale(state),
(groupsForReference: Array<Group>, locale: string) => {
return groupsForReference.sort((groupA: Group, groupB: Group) => groupA.name.localeCompare(groupB.name, locale));
},
);
export function getAssociatedGroupsForReference(state: GlobalState, teamId: string, channelId: string): Array<Group> {
const team = getTeam(state, teamId);
const channel = getChannel(state, channelId);
const locale = getCurrentUserLocale(state);
let groupsForReference = [];
if (team && team.group_constrained && channel && channel.group_constrained) {
const groupsFromChannel = getGroupsAssociatedToChannelForReference(state, channelId);
const groupsFromTeam = getGroupsAssociatedToTeamForReference(state, teamId);
groupsForReference = groupsFromChannel.concat(groupsFromTeam.filter((item) => groupsFromChannel.indexOf(item) < 0));
} else if (team && team.group_constrained) {
groupsForReference = getGroupsAssociatedToTeamForReference(state, teamId);
} else if (channel && channel.group_constrained) {
groupsForReference = getGroupsAssociatedToChannelForReference(state, channelId);
} else {
groupsForReference = getAllAssociatedGroupsForReference(state);
}
return groupsForReference.sort((groupA: Group, groupB: Group) => groupA.name.localeCompare(groupB.name, locale));
}
export const searchAssociatedGroupsForReferenceLocal = reselect.createSelector(
(state: GlobalState, term: string, teamId: string, channelId: string) => getAssociatedGroupsForReference(state, teamId, channelId),
(state: GlobalState, term: string) => term,
(groups: Array<Group>, term: string) => {
if (!groups || groups.length === 0) {
return emptyList;
}
const filteredGroups = filterGroupsMatchingTerm(groups, term);
return filteredGroups;
},
);
export const getAssociatedGroupsForReferenceMap = reselect.createSelector(
getAssociatedGroupsForReference,