Skip to content

Commit

Permalink
Fix type error
Browse files Browse the repository at this point in the history
  • Loading branch information
blazejkustra committed Sep 26, 2023
1 parent a6f5d24 commit faf29b7
Show file tree
Hide file tree
Showing 3 changed files with 144 additions and 156 deletions.
155 changes: 0 additions & 155 deletions src/libs/PersonalDetailsUtils.js

This file was deleted.

142 changes: 142 additions & 0 deletions src/libs/PersonalDetailsUtils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,142 @@
import Onyx, {OnyxEntry, OnyxUpdate} from 'react-native-onyx';
import {NullishDeep} from 'react-native-onyx/lib/types';
import ONYXKEYS from '../ONYXKEYS';
import * as Localize from './Localize';
import * as UserUtils from './UserUtils';
import * as LocalePhoneNumber from './LocalePhoneNumber';
import * as OnyxTypes from '../types/onyx';

type PersonalDetailsList = Record<string, OnyxTypes.PersonalDetails>;

let personalDetails: OnyxTypes.PersonalDetails[] = [];
let allPersonalDetails: OnyxEntry<Record<string, OnyxTypes.PersonalDetails>> = {};
Onyx.connect({
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
callback: (val) => {
personalDetails = Object.values(val ?? {});
allPersonalDetails = val;
},
});

/**
* @param [defaultValue] optional default display name value
*/
function getDisplayNameOrDefault(displayName: string, defaultValue?: string): string {
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 accountIDs - Array of accountIDs
* @param shouldChangeUserDisplayName - It will replace the current user's personal detail object's displayName with 'You'.
* @returns Array of personal detail objects
*/
function getPersonalDetailsByIDs(accountIDs: number[], currentUserAccountID: number, shouldChangeUserDisplayName = false): OnyxTypes.PersonalDetails[] {
const result: OnyxTypes.PersonalDetails[] = [];
personalDetails
.filter((detail) => accountIDs.includes(detail.accountID))
.forEach((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 logins Array of user logins
* @returns Array of accountIDs according to passed logins
*/
function getAccountIDsByLogins(logins: string[]): number[] {
return logins.reduce((foundAccountIDs: number[], login) => {
const currentDetail = personalDetails.find((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 accountIDs Array of user accountIDs
* @returns Array of logins according to passed accountIDs
*/
function getLoginsByAccountIDs(accountIDs: number[]): string[] {
return accountIDs.reduce((foundLogins: string[], accountID) => {
const currentDetail: Partial<OnyxTypes.PersonalDetails> = personalDetails.find((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 logins Array of user logins
* @param accountIDs Array of user accountIDs
* @returns Object with optimisticData, successData and failureData (object of personal details objects)
*/
function getNewPersonalDetailsOnyxData(logins: string[], accountIDs: number[]): Record<string, OnyxUpdate[]> {
const optimisticData: NullishDeep<PersonalDetailsList> = {};
const successData: NullishDeep<PersonalDetailsList> = {};
const failureData: NullishDeep<PersonalDetailsList> = {};

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

if (allPersonalDetails && Object.keys(allPersonalDetails[accountID]).length === 0) {
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};
3 changes: 2 additions & 1 deletion src/libs/UserUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import hashCode from './hashCode';
import {ConciergeAvatar, FallbackAvatar} from '../components/Icon/Expensicons';
import * as defaultAvatars from '../components/Icon/DefaultAvatars';
import Login from '../types/onyx/Login';
import {ErrorFields} from '../types/onyx/OnyxCommon';

type AvatarRange = 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24;

Expand Down Expand Up @@ -32,7 +33,7 @@ type LoginListIndicator = ValueOf<typeof CONST.BRICK_ROAD_INDICATOR_STATUS> | ''
* }}
*/
function hasLoginListError(loginList: Login): boolean {
const errorFields = loginList?.errorFields ?? {};
const errorFields: ErrorFields = loginList?.errorFields ?? {};
return Object.values(errorFields).some((field) => Object.keys(field).length > 0);
}

Expand Down

0 comments on commit faf29b7

Please sign in to comment.