-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
PersonalDetailsUtils.js
155 lines (143 loc) · 5.3 KB
/
PersonalDetailsUtils.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import lodashGet from 'lodash/get';
import Onyx from 'react-native-onyx';
import _ from 'underscore';
import ONYXKEYS from '../ONYXKEYS';
import * as Localize from './Localize';
import * as UserUtils from './UserUtils';
import * as LocalePhoneNumber from './LocalePhoneNumber';
let personalDetails = [];
let allPersonalDetails = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => {
personalDetails = _.values(val);
allPersonalDetails = val;
},
});
/**
* @param {Object} passedPersonalDetails
* @param {Array} pathToDisplayName
* @param {String} [defaultValue] optional default display name value
* @returns {String}
*/
function getDisplayNameOrDefault(passedPersonalDetails, pathToDisplayName, defaultValue) {
const displayName = lodashGet(passedPersonalDetails, pathToDisplayName);
return displayName || defaultValue || Localize.translateLocal('common.hidden');
}
/**
* Given a list of account IDs (as number) it will return an array of personal details objects.
* @param {Array<number>} accountIDs - Array of accountIDs
* @param {Number} currentUserAccountID
* @param {Boolean} shouldChangeUserDisplayName - It will replace the current user's personal detail object's displayName with 'You'.
* @returns {Array} - Array of personal detail objects
*/
function getPersonalDetailsByIDs(accountIDs, currentUserAccountID, shouldChangeUserDisplayName = false) {
const result = [];
_.each(
_.filter(personalDetails, (detail) => accountIDs.includes(detail.accountID)),
(detail) => {
if (shouldChangeUserDisplayName && currentUserAccountID === detail.accountID) {
result.push({
...detail,
displayName: Localize.translateLocal('common.you'),
});
} else {
result.push(detail);
}
},
);
return result;
}
/**
* Given a list of logins, find the associated personal detail and return related accountIDs.
*
* @param {Array<string>} logins Array of user logins
* @returns {Array} - Array of accountIDs according to passed logins
*/
function getAccountIDsByLogins(logins) {
return _.reduce(
logins,
(foundAccountIDs, login) => {
const currentDetail = _.find(personalDetails, (detail) => detail.login === login);
if (!currentDetail) {
// generate an account ID because in this case the detail is probably new, so we don't have a real accountID yet
foundAccountIDs.push(UserUtils.generateAccountID(login));
} else {
foundAccountIDs.push(Number(currentDetail.accountID));
}
return foundAccountIDs;
},
[],
);
}
/**
* Given a list of accountIDs, find the associated personal detail and return related logins.
*
* @param {Array<number>} accountIDs Array of user accountIDs
* @returns {Array} - Array of logins according to passed accountIDs
*/
function getLoginsByAccountIDs(accountIDs) {
return _.reduce(
accountIDs,
(foundLogins, accountID) => {
const currentDetail = _.find(personalDetails, (detail) => Number(detail.accountID) === Number(accountID)) || {};
if (currentDetail.login) {
foundLogins.push(currentDetail.login);
}
return foundLogins;
},
[],
);
}
/**
* 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: LocalePhoneNumber.formatPhoneNumber(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;
}
});
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};