Skip to content

Commit

Permalink
Merge pull request #29903 from Expensify/jack-includeSelectedParticip…
Browse files Browse the repository at this point in the history
…antsSearch

Update getOptions to exclude selected options on FAB
  • Loading branch information
stitesExpensify authored Oct 18, 2023
2 parents 843b955 + ccd2f3c commit 67d3a9a
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 23 deletions.
15 changes: 13 additions & 2 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,7 @@ function getOptions(
tags = {},
recentlyUsedTags = [],
canInviteUser = true,
includeSelectedOptions = false,
},
) {
if (includeCategories) {
Expand Down Expand Up @@ -1110,8 +1111,15 @@ function getOptions(
allPersonalDetailsOptions = lodashOrderBy(allPersonalDetailsOptions, [(personalDetail) => personalDetail.text && personalDetail.text.toLowerCase()], 'asc');
}

// Always exclude already selected options and the currently logged in user
const optionsToExclude = [...selectedOptions, {login: currentUserLogin}];
// Exclude the current user from the personal details list
const optionsToExclude = [{login: currentUserLogin}];

// If we're including selected options from the search results, we only want to exclude them if the search input is empty
// This is because on certain pages, we show the selected options at the top when the search input is empty
// This prevents the issue of seeing the selected option twice if you have them as a recent chat and select them
if (!includeSelectedOptions || searchInputValue === '') {
optionsToExclude.push(...selectedOptions);
}

_.each(excludeLogins, (login) => {
optionsToExclude.push({login});
Expand Down Expand Up @@ -1357,6 +1365,7 @@ function getIOUConfirmationOptionsFromParticipants(participants, amountText) {
* @param {Object} [tags]
* @param {Array<String>} [recentlyUsedTags]
* @param {boolean} [canInviteUser]
* @param {boolean} [includeSelectedOptions]
* @returns {Object}
*/
function getFilteredOptions(
Expand All @@ -1375,6 +1384,7 @@ function getFilteredOptions(
tags = {},
recentlyUsedTags = [],
canInviteUser = true,
includeSelectedOptions = false,
) {
return getOptions(reports, personalDetails, {
betas,
Expand All @@ -1393,6 +1403,7 @@ function getFilteredOptions(
tags,
recentlyUsedTags,
canInviteUser,
includeSelectedOptions,
});
}

Expand Down
57 changes: 47 additions & 10 deletions src/pages/NewChatPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,13 +71,16 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, translate, i
const sectionsList = [];
let indexOffset = 0;

sectionsList.push({
title: undefined,
data: selectedOptions,
shouldShow: !_.isEmpty(selectedOptions),
indexOffset,
});
indexOffset += selectedOptions.length;
// Only show the selected participants if the search is empty
if (searchTerm === '') {
sectionsList.push({
title: undefined,
data: selectedOptions,
shouldShow: !_.isEmpty(selectedOptions),
indexOffset,
});
indexOffset += selectedOptions.length;
}

if (maxParticipantsReached) {
return sectionsList;
Expand Down Expand Up @@ -109,7 +112,7 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, translate, i
}

return sectionsList;
}, [translate, filteredPersonalDetails, filteredRecentReports, filteredUserToInvite, maxParticipantsReached, selectedOptions]);
}, [translate, filteredPersonalDetails, filteredRecentReports, filteredUserToInvite, maxParticipantsReached, selectedOptions, searchTerm]);

/**
* Removes a selected option from list if already selected. If not already selected add this option to the list.
Expand All @@ -130,7 +133,24 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, translate, i
recentReports,
personalDetails: newChatPersonalDetails,
userToInvite,
} = OptionsListUtils.getFilteredOptions(reports, personalDetails, betas, searchTerm, newSelectedOptions, excludedGroupEmails);
} = OptionsListUtils.getFilteredOptions(
reports,
personalDetails,
betas,
searchTerm,
newSelectedOptions,
isGroupChat ? excludedGroupEmails : [],
false,
true,
false,
{},
[],
false,
{},
[],
true,
true,
);

setSelectedOptions(newSelectedOptions);
setFilteredRecentReports(recentReports);
Expand Down Expand Up @@ -165,7 +185,24 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, translate, i
recentReports,
personalDetails: newChatPersonalDetails,
userToInvite,
} = OptionsListUtils.getFilteredOptions(reports, personalDetails, betas, searchTerm, selectedOptions, isGroupChat ? excludedGroupEmails : []);
} = OptionsListUtils.getFilteredOptions(
reports,
personalDetails,
betas,
searchTerm,
selectedOptions,
isGroupChat ? excludedGroupEmails : [],
false,
true,
false,
{},
[],
false,
{},
[],
true,
true,
);
setFilteredRecentReports(recentReports);
setFilteredPersonalDetails(newChatPersonalDetails);
setFilteredUserToInvite(userToInvite);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,16 +104,19 @@ function MoneyRequestParticipantsSelector({
const newSections = [];
let indexOffset = 0;

newSections.push({
title: undefined,
data: _.map(participants, (participant) => {
const isPolicyExpenseChat = lodashGet(participant, 'isPolicyExpenseChat', false);
return isPolicyExpenseChat ? OptionsListUtils.getPolicyExpenseReportOption(participant) : OptionsListUtils.getParticipantsOption(participant, personalDetails);
}),
shouldShow: true,
indexOffset,
});
indexOffset += participants.length;
// Only show the selected participants if the search is empty
if (searchTerm === '') {
newSections.push({
title: undefined,
data: _.map(participants, (participant) => {
const isPolicyExpenseChat = lodashGet(participant, 'isPolicyExpenseChat', false);
return isPolicyExpenseChat ? OptionsListUtils.getPolicyExpenseReportOption(participant) : OptionsListUtils.getParticipantsOption(participant, personalDetails);
}),
shouldShow: true,
indexOffset,
});
indexOffset += participants.length;
}

if (maxParticipantsReached) {
return newSections;
Expand Down Expand Up @@ -148,7 +151,7 @@ function MoneyRequestParticipantsSelector({
}

return newSections;
}, [maxParticipantsReached, newChatOptions, participants, personalDetails, translate]);
}, [maxParticipantsReached, newChatOptions, participants, personalDetails, translate, searchTerm]);

/**
* Adds a single participant to the request
Expand Down Expand Up @@ -227,6 +230,14 @@ function MoneyRequestParticipantsSelector({

// We don't want to include any P2P options like personal details or reports that are not workspace chats for certain features.
!isDistanceRequest,
false,
{},
[],
false,
{},
[],
true,
true,
);
setNewChatOptions({
recentReports: chatOptions.recentReports,
Expand Down

0 comments on commit 67d3a9a

Please sign in to comment.