Skip to content

Commit

Permalink
Clean up the utility method
Browse files Browse the repository at this point in the history
  • Loading branch information
thienlnam committed Oct 19, 2023
1 parent a3d3afb commit 39afd7b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
61 changes: 29 additions & 32 deletions src/libs/OptionsListUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -1600,33 +1600,35 @@ function formatSectionsFromSearchTerm(searchTerm, selectedOptions, filteredRecen
// We show the selected participants at the top of the list when there is no search term
// However, if there is a search term we remove the selected participants from the top of the list unless they are part of the search results
// This clears up space on mobile views, where if you create a group with 4+ people you can't see the selected participants and the search results at the same time
let newIndexOffset = indexOffset;
const sections = [];
if (searchTerm === '') {
sections.push({
title: undefined,
data: shouldGetOptionDetails
? _.map(selectedOptions, (participant) => {
const isPolicyExpenseChat = lodashGet(participant, 'isPolicyExpenseChat', false);
return isPolicyExpenseChat ? getPolicyExpenseReportOption(participant) : getParticipantsOption(participant, personalDetails);
})
: selectedOptions,
shouldShow: !_.isEmpty(selectedOptions),
indexOffset,
});
newIndexOffset += selectedOptions.length;
} else {
// If you select a new user you don't have a contact for, they won't get returned as part of a recent report or personal details
// This will add them to the list of options, deduping them if they already exist in the other lists
const selectedParticipantsWithoutDetails = _.filter(selectedOptions, (participant) => {
const accountID = lodashGet(participant, 'accountID', null);
const isPartOfSearchTerm = participant.searchText.toLowerCase().includes(searchTerm.trim().toLowerCase());
const isReportInRecentReports = _.some(filteredRecentReports, (report) => report.accountID === accountID);
const isReportInPersonalDetails = _.some(filteredPersonalDetails, (personalDetail) => personalDetail.accountID === accountID);
return isPartOfSearchTerm && !isReportInRecentReports && !isReportInPersonalDetails;
});
return {
section: {
title: undefined,
data: shouldGetOptionDetails
? _.map(selectedOptions, (participant) => {
const isPolicyExpenseChat = lodashGet(participant, 'isPolicyExpenseChat', false);
return isPolicyExpenseChat ? getPolicyExpenseReportOption(participant) : getParticipantsOption(participant, personalDetails);
})
: selectedOptions,
shouldShow: !_.isEmpty(selectedOptions),
indexOffset,
},
newIndexOffset: indexOffset + selectedOptions.length,
};
}

sections.push({
// If you select a new user you don't have a contact for, they won't get returned as part of a recent report or personal details
// This will add them to the list of options, deduping them if they already exist in the other lists
const selectedParticipantsWithoutDetails = _.filter(selectedOptions, (participant) => {
const accountID = lodashGet(participant, 'accountID', null);
const isPartOfSearchTerm = participant.searchText.toLowerCase().includes(searchTerm.trim().toLowerCase());
const isReportInRecentReports = _.some(filteredRecentReports, (report) => report.accountID === accountID);
const isReportInPersonalDetails = _.some(filteredPersonalDetails, (personalDetail) => personalDetail.accountID === accountID);
return isPartOfSearchTerm && !isReportInRecentReports && !isReportInPersonalDetails;
});

return {
section: {
title: undefined,
data: shouldGetOptionDetails
? _.map(selectedParticipantsWithoutDetails, (participant) => {
Expand All @@ -1636,13 +1638,8 @@ function formatSectionsFromSearchTerm(searchTerm, selectedOptions, filteredRecen
: selectedParticipantsWithoutDetails,
shouldShow: !_.isEmpty(selectedParticipantsWithoutDetails),
indexOffset,
});
newIndexOffset += selectedParticipantsWithoutDetails.length;
}

return {
sectionList: sections,
newIndexOffset,
},
newIndexOffset: indexOffset + selectedParticipantsWithoutDetails.length,
};
}

Expand Down
6 changes: 3 additions & 3 deletions src/pages/NewChatPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ function NewChatPage({betas, isGroupChat, personalDetails, reports, translate, i
const isOptionsDataReady = ReportUtils.isReportDataReady() && OptionsListUtils.isPersonalDetailsReady(personalDetails);

const sections = useMemo(() => {
let sectionsList = [];
const sectionsList = [];
let indexOffset = 0;

const formatResults = OptionsListUtils.formatSectionsFromSearchTerm(searchTerm, selectedOptions, filteredRecentReports, filteredPersonalDetails, {}, false, indexOffset);
sectionsList = sectionsList.concat(formatResults.sectionList);
indexOffset = formatResults.indexOffset;
sectionsList.push(formatResults.section);
indexOffset = formatResults.newIndexOffset;

if (maxParticipantsReached) {
return sectionsList;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ function MoneyRequestParticipantsSelector({
* @returns {Array}
*/
const sections = useMemo(() => {
let newSections = [];
const newSections = [];
let indexOffset = 0;

const formatResults = OptionsListUtils.formatSectionsFromSearchTerm(
Expand All @@ -113,8 +113,8 @@ function MoneyRequestParticipantsSelector({
true,
indexOffset,
);
indexOffset = formatResults.indexOffset;
newSections = newSections.concat(formatResults.sectionList);
newSections.push(formatResults.section);
indexOffset = formatResults.newIndexOffset;

if (maxParticipantsReached) {
return newSections;
Expand Down

0 comments on commit 39afd7b

Please sign in to comment.