From efb1f415fcbb823a64f5d6126bb10449e5ba0bf0 Mon Sep 17 00:00:00 2001 From: Sobit Neupane <073bct543.sobit@pcampus.edu.np> Date: Fri, 28 Jan 2022 21:39:50 +0545 Subject: [PATCH 1/4] Separate report name and icon configuration from personal details --- src/libs/actions/PersonalDetails.js | 37 +++------------------------ src/libs/actions/Report.js | 39 ++++++++++++++++++++++++++++- 2 files changed, 42 insertions(+), 34 deletions(-) diff --git a/src/libs/actions/PersonalDetails.js b/src/libs/actions/PersonalDetails.js index 22453fcd4ee9..aaeb85f953d9 100644 --- a/src/libs/actions/PersonalDetails.js +++ b/src/libs/actions/PersonalDetails.js @@ -8,7 +8,6 @@ import CONST from '../../CONST'; import NetworkConnection from '../NetworkConnection'; import * as API from '../API'; import NameValuePair from './NameValuePair'; -import * as ReportUtils from '../reportUtils'; import * as OptionsListUtils from '../OptionsListUtils'; import Growl from '../Growl'; import * as Localize from '../Localize'; @@ -146,6 +145,7 @@ function fetchPersonalDetails() { * Get personal details from report participants. * * @param {Object} reports + * @returns {Promise} */ function getFromReportParticipants(reports) { const participantEmails = _.chain(reports) @@ -155,10 +155,10 @@ function getFromReportParticipants(reports) { .value(); if (participantEmails.length === 0) { - return; + return Promise.resolve({}); } - API.PersonalDetails_GetForEmails({emailList: participantEmails.join(',')}) + return API.PersonalDetails_GetForEmails({emailList: participantEmails.join(',')}) .then((data) => { const existingDetails = _.pick(data, participantEmails); @@ -173,36 +173,7 @@ function getFromReportParticipants(reports) { const formattedPersonalDetails = formatPersonalDetails(details); Onyx.merge(ONYXKEYS.PERSONAL_DETAILS, formattedPersonalDetails); - - // The personalDetails of the participants contain their avatar images. Here we'll go over each - // report and based on the participants we'll link up their avatars to report icons. This will - // skip over default rooms which aren't named by participants. - const reportsToUpdate = {}; - _.each(reports, (report) => { - if (report.participants.length <= 0 && !ReportUtils.isChatRoom(report)) { - return; - } - - const avatars = OptionsListUtils.getReportIcons(report, details); - const reportName = ReportUtils.isChatRoom(report) - ? report.reportName - : _.chain(report.participants) - .filter(participant => participant !== currentUserEmail) - .map(participant => lodashGet( - formattedPersonalDetails, - [participant, 'displayName'], - participant, - )) - .value() - .join(', '); - - reportsToUpdate[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = {icons: avatars, reportName}; - }); - - // We use mergeCollection such that it updates ONYXKEYS.COLLECTION.REPORT in one go. - // Any withOnyx subscribers to this key will also receive the complete updated props just once - // than updating props for each report and re-rendering had merge been used. - Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, reportsToUpdate); + return details; }); } diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index aff30887392d..be133808d517 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -320,6 +320,40 @@ function fetchIOUReportID(debtorEmail) { }); } +function configureReportNameAndIcons(reports, details) { + // The personalDetails of the participants contain their avatar images. Here we'll go over each + // report and based on the participants we'll link up their avatars to report icons. This will + // skip over default rooms which aren't named by participants. + + const reportsToUpdate = {}; + const formattedPersonalDetails = PersonalDetails.formatPersonalDetails(details); + _.each(reports, (report) => { + if (report.participants.length <= 0 && !ReportUtils.isChatRoom(report)) { + return; + } + + const avatars = ReportUtils.isChatRoom(report) ? (['']) : OptionsListUtils.getReportIcons(report, details); + const reportName = ReportUtils.isChatRoom(report) + ? report.reportName + : _.chain(report.participants) + .filter(participant => participant !== currentUserEmail) + .map(participant => lodashGet( + formattedPersonalDetails, + [participant, 'displayName'], + participant, + )) + .value() + .join(', '); + + reportsToUpdate[`${ONYXKEYS.COLLECTION.REPORT}${report.reportID}`] = {icons: avatars, reportName}; + }); + + // We use mergeCollection such that it updates ONYXKEYS.COLLECTION.REPORT in one go. + // Any withOnyx subscribers to this key will also receive the complete updated props just once + // than updating props for each report and re-rendering had merge been used. + Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, reportsToUpdate); +} + /** * Fetches chat reports when provided a list of chat report IDs. * If the shouldRedirectIfInaccessible flag is set, we redirect to the Concierge chat @@ -400,8 +434,11 @@ function fetchChatReportsByIDs(chatList, shouldRedirectIfInaccessible = false) { Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT_IOUS, reportIOUData); Onyx.mergeCollection(ONYXKEYS.COLLECTION.REPORT, simplifiedReports); + const simplifiedReportsList = _.values(simplifiedReports); + // Fetch the personal details if there are any - PersonalDetails.getFromReportParticipants(_.values(simplifiedReports)); + PersonalDetails.getFromReportParticipants(simplifiedReportsList) + .then(details => configureReportNameAndIcons(simplifiedReportsList, details)); return fetchedReports; }) .catch((err) => { From 5e7299cef5c8a7a4372472cb20e9928432e150ca Mon Sep 17 00:00:00 2001 From: Sobit Neupane <073bct543.sobit@pcampus.edu.np> Date: Sat, 29 Jan 2022 13:19:48 +0545 Subject: [PATCH 2/4] Remove use of single-use temporary variable formattedPersonalDetails --- src/libs/actions/Report.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index be133808d517..638b342084a4 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -326,7 +326,6 @@ function configureReportNameAndIcons(reports, details) { // skip over default rooms which aren't named by participants. const reportsToUpdate = {}; - const formattedPersonalDetails = PersonalDetails.formatPersonalDetails(details); _.each(reports, (report) => { if (report.participants.length <= 0 && !ReportUtils.isChatRoom(report)) { return; @@ -338,7 +337,7 @@ function configureReportNameAndIcons(reports, details) { : _.chain(report.participants) .filter(participant => participant !== currentUserEmail) .map(participant => lodashGet( - formattedPersonalDetails, + PersonalDetails.formatPersonalDetails(details), [participant, 'displayName'], participant, )) From 88250fb06e758839237eac3d03577f48d7e33611 Mon Sep 17 00:00:00 2001 From: Sobit Neupane <073bct543.sobit@pcampus.edu.np> Date: Tue, 1 Feb 2022 12:34:59 +0545 Subject: [PATCH 3/4] Use local variable isChatRoom instead of referencing twice --- src/libs/actions/Report.js | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 638b342084a4..130576b9d3f8 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -330,9 +330,15 @@ function configureReportNameAndIcons(reports, details) { if (report.participants.length <= 0 && !ReportUtils.isChatRoom(report)) { return; } + const isChatRoom = ReportUtils.isChatRoom(report); - const avatars = ReportUtils.isChatRoom(report) ? (['']) : OptionsListUtils.getReportIcons(report, details); - const reportName = ReportUtils.isChatRoom(report) + // Chat rooms have a specific avatar so we can return any non-empty array but for 1:1 chats and group chats + // avatars are extracted from avatars of the participants. + const avatars = isChatRoom ? (['']) : OptionsListUtils.getReportIcons(report, details); + + // ReportName is already present in report for chatrooms but for 1:1 chats and group chats + // reportName is extracted from displayNames of the participants. + const reportName = isChatRoom ? report.reportName : _.chain(report.participants) .filter(participant => participant !== currentUserEmail) From e4a80214724a4df0a04f65601eeaed1a6259295b Mon Sep 17 00:00:00 2001 From: Sobit Neupane <073bct543@ioe.edu.np> Date: Wed, 2 Feb 2022 23:46:00 +0545 Subject: [PATCH 4/4] Make syntactic change Co-authored-by: Tim Golen --- src/libs/actions/Report.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/libs/actions/Report.js b/src/libs/actions/Report.js index 130576b9d3f8..8a31e02464bb 100644 --- a/src/libs/actions/Report.js +++ b/src/libs/actions/Report.js @@ -334,7 +334,7 @@ function configureReportNameAndIcons(reports, details) { // Chat rooms have a specific avatar so we can return any non-empty array but for 1:1 chats and group chats // avatars are extracted from avatars of the participants. - const avatars = isChatRoom ? (['']) : OptionsListUtils.getReportIcons(report, details); + const avatars = isChatRoom ? [''] : OptionsListUtils.getReportIcons(report, details); // ReportName is already present in report for chatrooms but for 1:1 chats and group chats // reportName is extracted from displayNames of the participants.