Skip to content

Commit

Permalink
Merge pull request #22904 from samh-nl/fix/issue-22442
Browse files Browse the repository at this point in the history
fix: optimistically add personal details
  • Loading branch information
yuwenmemon authored Jul 19, 2023
2 parents d3198f2 + 1ee57d8 commit 555621c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
65 changes: 63 additions & 2 deletions src/libs/PersonalDetailsUtils.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,13 @@ import * as Localize from './Localize';
import * as UserUtils from './UserUtils';

let personalDetails = [];
let allPersonalDetails = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => (personalDetails = _.values(val)),
callback: (val) => {
personalDetails = _.values(val);
allPersonalDetails = val;
},
});

/**
Expand Down Expand Up @@ -91,4 +95,61 @@ function getLoginsByAccountIDs(accountIDs) {
);
}

export {getDisplayNameOrDefault, getPersonalDetailsByIDs, getAccountIDsByLogins, getLoginsByAccountIDs};
/**
* Given a list of logins and accountIDs, return Onyx data for users with no existing personal details stored
*
* @param {Array<string>} logins Array of user logins
* @param {Array<number>} accountIDs Array of user accountIDs
* @returns {Object} - Object with optimisticData, successData and failureData (object of personal details objects)
*/
function getNewPersonalDetailsOnyxData(logins, accountIDs) {
const optimisticData = {};
const successData = {};
const failureData = {};

_.each(logins, (login, index) => {
const accountID = accountIDs[index];

if (_.isEmpty(allPersonalDetails[accountID])) {
optimisticData[accountID] = {
login,
accountID,
avatar: UserUtils.getDefaultAvatarURL(accountID),
displayName: login,
};

/**
* Cleanup the optimistic user to ensure it does not permanently persist.
* This is done to prevent duplicate entries (upon success) since the BE will return other personal details with the correct account IDs.
*/
successData[accountID] = null;
failureData[accountID] = null;
}
});

return {
optimisticData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: optimisticData,
},
],
successData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: successData,
},
],
failureData: [
{
onyxMethod: Onyx.METHOD.MERGE,
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
value: failureData,
},
],
};
}

export {getDisplayNameOrDefault, getPersonalDetailsByIDs, getAccountIDsByLogins, getLoginsByAccountIDs, getNewPersonalDetailsOnyxData};
7 changes: 6 additions & 1 deletion src/libs/actions/Policy.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import CONST from '../../CONST';
import * as OptionsListUtils from '../OptionsListUtils';
import * as ErrorUtils from '../ErrorUtils';
import * as ReportUtils from '../ReportUtils';
import * as PersonalDetailsUtils from '../PersonalDetailsUtils';
import Log from '../Log';
import Permissions from '../Permissions';

Expand Down Expand Up @@ -350,7 +351,9 @@ function createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, betas) {
*/
function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID, betas) {
const membersListKey = `${ONYXKEYS.COLLECTION.POLICY_MEMBERS}${policyID}`;
const logins = _.map(_.keys(invitedEmailsToAccountIDs), (memberLogin) => OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin));
const accountIDs = _.values(invitedEmailsToAccountIDs);
const newPersonalDetailsOnyxData = PersonalDetailsUtils.getNewPersonalDetailsOnyxData(logins, accountIDs);

// create onyx data for policy expense chats for each new member
const membersChats = createPolicyExpenseChats(policyID, invitedEmailsToAccountIDs, betas);
Expand All @@ -363,6 +366,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID,
// Convert to object with each key containing {pendingAction: ‘add’}
value: _.object(accountIDs, Array(accountIDs.length).fill({pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD})),
},
...newPersonalDetailsOnyxData.optimisticData,
...membersChats.onyxOptimisticData,
];

Expand All @@ -375,6 +379,7 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID,
// need to remove the members since that will be handled by onClose of OfflineWithFeedback.
value: _.object(accountIDs, Array(accountIDs.length).fill({pendingAction: null, errors: null})),
},
...newPersonalDetailsOnyxData.successData,
...membersChats.onyxSuccessData,
];

Expand All @@ -392,10 +397,10 @@ function addMembersToWorkspace(invitedEmailsToAccountIDs, welcomeNote, policyID,
}),
),
},
...newPersonalDetailsOnyxData.failureData,
...membersChats.onyxFailureData,
];

const logins = _.map(_.keys(invitedEmailsToAccountIDs), (memberLogin) => OptionsListUtils.addSMSDomainIfPhoneNumber(memberLogin));
const params = {
employees: JSON.stringify(_.map(logins, (login) => ({email: login}))),

Expand Down

0 comments on commit 555621c

Please sign in to comment.