Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update getOptions to exclude selected options on FAB #29903

Merged
merged 8 commits into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should refractor getFilteredOptions in future to avoid sending this much params

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agreed. Does this JS have named params like PHP? Where we can jsut pass the params to a function by variable name?

{},
[],
false,
{},
[],
true,
true,
);
setNewChatOptions({
recentReports: chatOptions.recentReports,
Expand Down