diff --git a/src/libs/ReportUtils.ts b/src/libs/ReportUtils.ts index e9c017db43ce..0c2f4f318ce5 100644 --- a/src/libs/ReportUtils.ts +++ b/src/libs/ReportUtils.ts @@ -1039,8 +1039,8 @@ function isSettled(reportOrID: OnyxInputOrEntry | SearchReport | string /** * Whether the current user is the submitter of the report */ -function isCurrentUserSubmitter(reportID: string): boolean { - if (!allReports) { +function isCurrentUserSubmitter(reportID: string | undefined): boolean { + if (!allReports || !reportID) { return false; } const report = allReports[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`]; @@ -7379,8 +7379,8 @@ function getWorkspaceChats(policyID: string, accountIDs: number[], reports: Onyx * * @param policyID - the workspace ID to get all associated reports */ -function getAllWorkspaceReports(policyID: string): Array> { - return Object.values(allReports ?? {}).filter((report) => (report?.policyID ?? '-1') === policyID); +function getAllWorkspaceReports(policyID: string | undefined): Array> { + return Object.values(allReports ?? {}).filter((report) => report?.policyID === policyID); } /** diff --git a/src/libs/TransactionUtils/index.ts b/src/libs/TransactionUtils/index.ts index 8bc1dd7356cb..240ab662f622 100644 --- a/src/libs/TransactionUtils/index.ts +++ b/src/libs/TransactionUtils/index.ts @@ -1210,11 +1210,10 @@ function compareDuplicateTransactionFields( return {keep, change}; } -function getTransactionID(threadReportID: string | undefined): string | undefined { +function getTransactionID(threadReportID?: string): string | undefined { if (!threadReportID) { return; } - const report = allReports?.[`${ONYXKEYS.COLLECTION.REPORT}${threadReportID}`]; const parentReportAction = ReportUtils.isThread(report) ? ReportActionsUtils.getReportAction(report.parentReportID, report.parentReportActionID) : undefined; const IOUTransactionID = ReportActionsUtils.isMoneyRequestAction(parentReportAction) ? ReportActionsUtils.getOriginalMessage(parentReportAction)?.IOUTransactionID : undefined; diff --git a/src/libs/actions/PersonalDetails.ts b/src/libs/actions/PersonalDetails.ts index 94a9dc95e846..e0e739b56e86 100644 --- a/src/libs/actions/PersonalDetails.ts +++ b/src/libs/actions/PersonalDetails.ts @@ -37,7 +37,7 @@ Onyx.connect({ key: ONYXKEYS.SESSION, callback: (val) => { currentUserEmail = val?.email ?? ''; - currentUserAccountID = val?.accountID ?? -1; + currentUserAccountID = val?.accountID ?? CONST.DEFAULT_NUMBER_ID; }, }); @@ -121,20 +121,36 @@ function updateDisplayName(firstName: string, lastName: string) { function updateLegalName(legalFirstName: string, legalLastName: string) { const parameters: UpdateLegalNameParams = {legalFirstName, legalLastName}; - - API.write(WRITE_COMMANDS.UPDATE_LEGAL_NAME, parameters, { - optimisticData: [ - { - onyxMethod: Onyx.METHOD.MERGE, - key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS, - value: { - legalFirstName, - legalLastName, + const optimisticData: OnyxUpdate[] = [ + { + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PRIVATE_PERSONAL_DETAILS, + value: { + legalFirstName, + legalLastName, + }, + }, + ]; + // In case the user does not have a display name, we will update the display name based on the legal name + if (!allPersonalDetails?.[currentUserAccountID]?.firstName && !allPersonalDetails?.[currentUserAccountID]?.lastName) { + optimisticData.push({ + onyxMethod: Onyx.METHOD.MERGE, + key: ONYXKEYS.PERSONAL_DETAILS_LIST, + value: { + [currentUserAccountID]: { + displayName: PersonalDetailsUtils.createDisplayName(currentUserEmail ?? '', { + firstName: legalFirstName, + lastName: legalLastName, + }), }, + firstName: legalFirstName, + lastName: legalLastName, }, - ], + }); + } + API.write(WRITE_COMMANDS.UPDATE_LEGAL_NAME, parameters, { + optimisticData, }); - Navigation.goBack(); } diff --git a/src/pages/home/HeaderView.tsx b/src/pages/home/HeaderView.tsx index cf7e2adfccd0..8f1933b076f0 100644 --- a/src/pages/home/HeaderView.tsx +++ b/src/pages/home/HeaderView.tsx @@ -70,9 +70,11 @@ function HeaderView({report, parentReportAction, reportID, onNavigationMenuButto const {isSmallScreenWidth} = useResponsiveLayout(); const route = useRoute(); const [isDeleteTaskConfirmModalVisible, setIsDeleteTaskConfirmModalVisible] = React.useState(false); - const [invoiceReceiverPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report?.invoiceReceiver && 'policyID' in report.invoiceReceiver ? report.invoiceReceiver.policyID : -1}`); + const [invoiceReceiverPolicy] = useOnyx( + `${ONYXKEYS.COLLECTION.POLICY}${report?.invoiceReceiver && 'policyID' in report.invoiceReceiver ? report.invoiceReceiver.policyID : CONST.DEFAULT_NUMBER_ID}`, + ); // eslint-disable-next-line @typescript-eslint/prefer-nullish-coalescing - const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID || report?.reportID || '-1'}`); + const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID || report?.reportID}`); const policy = usePolicy(report?.policyID); const [personalDetails] = useOnyx(ONYXKEYS.PERSONAL_DETAILS_LIST); @@ -100,7 +102,7 @@ function HeaderView({report, parentReportAction, reportID, onNavigationMenuButto const reportDescription = Parser.htmlToText(ReportUtils.getReportDescription(report)); const policyName = ReportUtils.getPolicyName(report, true); const policyDescription = ReportUtils.getPolicyDescriptionText(policy); - const isPersonalExpenseChat = isPolicyExpenseChat && ReportUtils.isCurrentUserSubmitter(report?.reportID ?? ''); + const isPersonalExpenseChat = isPolicyExpenseChat && ReportUtils.isCurrentUserSubmitter(report?.reportID); const shouldShowSubtitle = () => { if (!subtitle) { return false; @@ -258,7 +260,7 @@ function HeaderView({report, parentReportAction, reportID, onNavigationMenuButto { if (ReportUtils.canEditPolicyDescription(policy)) { - Navigation.navigate(ROUTES.WORKSPACE_PROFILE_DESCRIPTION.getRoute(report.policyID ?? '-1')); + Navigation.navigate(ROUTES.WORKSPACE_PROFILE_DESCRIPTION.getRoute(`${report.policyID ?? CONST.DEFAULT_NUMBER_ID}`)); return; } Navigation.navigate(ROUTES.REPORT_WITH_ID_DETAILS.getRoute(reportID, Navigation.getReportRHPActiveRoute()));