Skip to content

Commit

Permalink
Fix invoice room and report display after receiver policy is added
Browse files Browse the repository at this point in the history
  • Loading branch information
VickyStash committed Aug 1, 2024
1 parent fbcaef9 commit 5123bb5
Show file tree
Hide file tree
Showing 10 changed files with 78 additions and 49 deletions.
10 changes: 7 additions & 3 deletions src/components/AvatarWithDisplayName.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {useCallback, useEffect, useRef} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import type {ValueOf} from 'type-fest';
import useStyleUtils from '@hooks/useStyleUtils';
import useTheme from '@hooks/useTheme';
Expand Down Expand Up @@ -58,12 +58,16 @@ function AvatarWithDisplayName({
const theme = useTheme();
const styles = useThemeStyles();
const StyleUtils = useStyleUtils();
const title = ReportUtils.getReportName(report);
const [parentReport] = useOnyx(`${ONYXKEYS.COLLECTION.REPORT}${report?.parentReportID}`);
const [invoiceReceiverPolicy] = useOnyx(
`${ONYXKEYS.COLLECTION.POLICY}${parentReport?.invoiceReceiver && 'policyID' in parentReport.invoiceReceiver ? parentReport.invoiceReceiver.policyID : -1}`,
);
const title = ReportUtils.getReportName(report, undefined, undefined, invoiceReceiverPolicy);
const subtitle = ReportUtils.getChatRoomSubtitle(report);
const parentNavigationSubtitleData = ReportUtils.getParentNavigationSubtitle(report);
const isMoneyRequestOrReport =
ReportUtils.isMoneyRequestReport(report) || ReportUtils.isMoneyRequest(report) || ReportUtils.isTrackExpenseReport(report) || ReportUtils.isInvoiceReport(report);
const icons = ReportUtils.getIcons(report, personalDetails, null, '', -1, policy);
const icons = ReportUtils.getIcons(report, personalDetails, null, '', -1, policy, invoiceReceiverPolicy);
const ownerPersonalDetails = OptionsListUtils.getPersonalDetailsForAccountIDs(report?.ownerAccountID ? [report.ownerAccountID] : [], personalDetails);
const displayNamesWithTooltips = ReportUtils.getDisplayNamesWithTooltips(Object.values(ownerPersonalDetails) as PersonalDetails[], false);
const shouldShowSubscriptAvatar = ReportUtils.shouldReportShowSubscript(report);
Expand Down
11 changes: 11 additions & 0 deletions src/components/LHNOptionsList/LHNOptionsList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -116,10 +116,20 @@ function LHNOptionsList({style, contentContainerStyles, data, onSelectRow, optio
const renderItem = useCallback(
({item: reportID}: RenderItemProps): ReactElement => {
const itemFullReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${reportID}`];
const itemParentReport = reports?.[`${ONYXKEYS.COLLECTION.REPORT}${itemFullReport?.parentReportID ?? '-1'}`];
const itemReportActions = reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${reportID}`];
const itemParentReportActions = reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${itemFullReport?.parentReportID}`];
const itemParentReportAction = itemParentReportActions?.[itemFullReport?.parentReportActionID ?? '-1'];

let invoiceReceiverPolicyID = '-1';
if (itemFullReport?.invoiceReceiver && 'policyID' in itemFullReport.invoiceReceiver) {
invoiceReceiverPolicyID = itemFullReport.invoiceReceiver.policyID;
}
if (itemParentReport?.invoiceReceiver && 'policyID' in itemParentReport.invoiceReceiver) {
invoiceReceiverPolicyID = itemParentReport.invoiceReceiver.policyID;
}
const itemInvoiceReceiverPolicy = policy?.[`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiverPolicyID}`];

const iouReportIDOfLastAction = OptionsListUtils.getIOUReportIDOfLastAction(itemFullReport);
const itemIouReportReportActions = iouReportIDOfLastAction ? reportActions?.[`${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportIDOfLastAction}`] : undefined;

Expand Down Expand Up @@ -148,6 +158,7 @@ function LHNOptionsList({style, contentContainerStyles, data, onSelectRow, optio
parentReportAction={itemParentReportAction}
iouReportReportActions={itemIouReportReportActions}
policy={itemPolicy}
invoiceReceiverPolicy={itemInvoiceReceiverPolicy}
personalDetails={personalDetails ?? {}}
transaction={itemTransaction}
lastReportActionTransaction={lastReportActionTransaction}
Expand Down
3 changes: 3 additions & 0 deletions src/components/LHNOptionsList/OptionRowLHNData.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function OptionRowLHNData({
personalDetails = {},
preferredLocale = CONST.LOCALES.DEFAULT,
policy,
invoiceReceiverPolicy,
receiptTransactions,
parentReportAction,
iouReportReportActions,
Expand Down Expand Up @@ -50,6 +51,7 @@ function OptionRowLHNData({
parentReportAction,
hasViolations: !!shouldDisplayViolations || shouldDisplayReportViolations,
transactionViolations,
invoiceReceiverPolicy,
});
if (deepEqual(item, optionItemRef.current)) {
return optionItemRef.current;
Expand All @@ -74,6 +76,7 @@ function OptionRowLHNData({
transactionViolations,
canUseViolations,
receiptTransactions,
invoiceReceiverPolicy,
shouldDisplayReportViolations,
]);

Expand Down
3 changes: 3 additions & 0 deletions src/components/LHNOptionsList/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,9 @@ type OptionRowLHNDataProps = {
/** The policy which the user has access to and which the report could be tied to */
policy?: OnyxEntry<Policy>;

/** Invoice receiver policy */
invoiceReceiverPolicy?: OnyxEntry<Policy>;

/** The action from the parent report */
parentReportAction?: OnyxEntry<ReportAction>;

Expand Down
7 changes: 5 additions & 2 deletions src/components/ReportActionItem/ReportPreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import React, {useMemo, useState} from 'react';
import type {StyleProp, ViewStyle} from 'react-native';
import {View} from 'react-native';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import Button from '@components/Button';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
Expand Down Expand Up @@ -140,6 +140,9 @@ function ReportPreview({
const [nonHeldAmount, fullAmount] = ReportUtils.getNonHeldAndFullAmount(iouReport, policy);
const hasOnlyHeldExpenses = ReportUtils.hasOnlyHeldExpenses(iouReport?.reportID ?? '');
const [paymentType, setPaymentType] = useState<PaymentMethodType>();
const [invoiceReceiverPolicy] = useOnyx(
`${ONYXKEYS.COLLECTION.POLICY}${chatReport?.invoiceReceiver && 'policyID' in chatReport.invoiceReceiver ? chatReport.invoiceReceiver.policyID : -1}`,
);

const managerID = iouReport?.managerID ?? action.childManagerAccountID ?? 0;
const {totalDisplaySpend, reimbursableSpend} = ReportUtils.getMoneyRequestSpendBreakdown(iouReport);
Expand Down Expand Up @@ -275,7 +278,7 @@ function ReportPreview({
if (isPolicyExpenseChat) {
payerOrApproverName = ReportUtils.getPolicyName(chatReport);
} else if (isInvoiceRoom) {
payerOrApproverName = ReportUtils.getInvoicePayerName(chatReport);
payerOrApproverName = ReportUtils.getInvoicePayerName(chatReport, invoiceReceiverPolicy);
} else {
payerOrApproverName = ReportUtils.getDisplayNameForParticipant(managerID, true);
}
Expand Down
40 changes: 20 additions & 20 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2052,6 +2052,7 @@ function getIcons(
defaultName = '',
defaultAccountID = -1,
policy?: OnyxInputOrEntry<Policy>,
invoiceReceiverPolicy?: OnyxInputOrEntry<Policy>,
): Icon[] {
if (isEmptyObject(report)) {
const fallbackIcon: Icon = {
Expand Down Expand Up @@ -2130,7 +2131,7 @@ function getIcons(
icons.push(...getIconsForParticipants([report?.invoiceReceiver.accountID], personalDetails));
} else {
const receiverPolicyID = report?.invoiceReceiver?.policyID;
const receiverPolicy = getPolicy(receiverPolicyID);
const receiverPolicy = invoiceReceiverPolicy ?? getPolicy(receiverPolicyID);
if (!isEmptyObject(receiverPolicy)) {
icons.push({
source: receiverPolicy?.avatarURL ?? getDefaultWorkspaceAvatar(receiverPolicy.name),
Expand Down Expand Up @@ -2210,7 +2211,7 @@ function getIcons(
}

const receiverPolicyID = invoiceRoomReport?.invoiceReceiver?.policyID;
const receiverPolicy = getPolicy(receiverPolicyID);
const receiverPolicy = invoiceReceiverPolicy ?? getPolicy(receiverPolicyID);

if (!isEmptyObject(receiverPolicy)) {
icons.push({
Expand Down Expand Up @@ -2664,7 +2665,7 @@ function getAvailableReportFields(report: Report, policyReportFields: PolicyRepo
/**
* Get the title for an IOU or expense chat which will be showing the payer and the amount
*/
function getMoneyRequestReportName(report: OnyxEntry<Report>, policy?: OnyxEntry<Policy>): string {
function getMoneyRequestReportName(report: OnyxEntry<Report>, policy?: OnyxEntry<Policy>, invoiceReceiverPolicy?: OnyxEntry<Policy>): string {
const isReportSettled = isSettled(report?.reportID ?? '-1');
const reportFields = isReportSettled ? report?.fieldList : getReportFieldsByPolicyID(report?.policyID ?? '-1');
const titleReportField = getFormulaTypeReportField(reportFields ?? {});
Expand All @@ -2681,7 +2682,7 @@ function getMoneyRequestReportName(report: OnyxEntry<Report>, policy?: OnyxEntry
payerOrApproverName = getPolicyName(report, false, policy);
} else if (isInvoiceReport(report)) {
const chatReport = getReportOrDraftReport(report?.chatReportID);
payerOrApproverName = getInvoicePayerName(chatReport);
payerOrApproverName = getInvoicePayerName(chatReport, invoiceReceiverPolicy);
} else {
payerOrApproverName = getDisplayNameForParticipant(report?.managerID) ?? '';
}
Expand Down Expand Up @@ -3359,15 +3360,15 @@ function getAdminRoomInvitedParticipants(parentReportAction: OnyxEntry<ReportAct
* - Individual - a receiver display name.
* - Policy - a receiver policy name.
*/
function getInvoicePayerName(report: OnyxEntry<Report>): string {
function getInvoicePayerName(report: OnyxEntry<Report>, invoiceReceiverPolicy?: OnyxEntry<Policy>): string {
const invoiceReceiver = report?.invoiceReceiver;
const isIndividual = invoiceReceiver?.type === CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL;

if (isIndividual) {
return PersonalDetailsUtils.getDisplayNameOrDefault(allPersonalDetails?.[invoiceReceiver.accountID]);
}

return getPolicyName(report, false, allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiver?.policyID}`]);
return getPolicyName(report, false, invoiceReceiverPolicy ?? allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiver?.policyID}`]);
}

/**
Expand Down Expand Up @@ -3443,13 +3444,13 @@ function getReportActionMessage(reportAction: OnyxEntry<ReportAction>, reportID?
/**
* Get the title for an invoice room.
*/
function getInvoicesChatName(report: OnyxEntry<Report>): string {
function getInvoicesChatName(report: OnyxEntry<Report>, receiverPolicy: OnyxEntry<Policy>): string {
const invoiceReceiver = report?.invoiceReceiver;
const isIndividual = invoiceReceiver?.type === CONST.REPORT.INVOICE_RECEIVER_TYPE.INDIVIDUAL;
const invoiceReceiverAccountID = isIndividual ? invoiceReceiver.accountID : -1;
const invoiceReceiverPolicyID = isIndividual ? '' : invoiceReceiver?.policyID ?? '-1';
const isCurrentUserReceiver =
(isIndividual && invoiceReceiverAccountID === currentUserAccountID) || (!isIndividual && PolicyUtils.isPolicyEmployee(invoiceReceiverPolicyID, allPolicies));
const invoiceReceiverPolicy = receiverPolicy ?? getPolicy(invoiceReceiverPolicyID);
const isCurrentUserReceiver = (isIndividual && invoiceReceiverAccountID === currentUserAccountID) || (!isIndividual && PolicyUtils.isPolicyAdmin(invoiceReceiverPolicy));

if (isCurrentUserReceiver) {
return getPolicyName(report);
Expand All @@ -3459,14 +3460,13 @@ function getInvoicesChatName(report: OnyxEntry<Report>): string {
return PersonalDetailsUtils.getDisplayNameOrDefault(allPersonalDetails?.[invoiceReceiverAccountID]);
}

// TODO: Check this flow in a scope of the Invoice V0.3
return getPolicyName(report, false, allPolicies?.[`${ONYXKEYS.COLLECTION.POLICY}${invoiceReceiverPolicyID}`]);
return getPolicyName(report, false, invoiceReceiverPolicy);
}

/**
* Get the title for a report.
*/
function getReportName(report: OnyxEntry<Report>, policy?: OnyxEntry<Policy>, parentReportActionParam?: OnyxInputOrEntry<ReportAction>): string {
function getReportName(report: OnyxEntry<Report>, policy?: OnyxEntry<Policy>, parentReportActionParam?: OnyxInputOrEntry<ReportAction>, invoiceReceiverPolicy?: OnyxEntry<Policy>): string {
let formattedName: string | undefined;
const parentReportAction = parentReportActionParam ?? ReportActionsUtils.getParentReportAction(report);
const parentReportActionMessage = ReportActionsUtils.getReportActionMessage(parentReportAction);
Expand Down Expand Up @@ -3537,12 +3537,16 @@ function getReportName(report: OnyxEntry<Report>, policy?: OnyxEntry<Policy>, pa
formattedName = getPolicyExpenseChatName(report, policy);
}

if (isMoneyRequestReport(report) || isInvoiceReport(report)) {
if (isMoneyRequestReport(report)) {
formattedName = getMoneyRequestReportName(report, policy);
}

if (isInvoiceReport(report)) {
formattedName = getMoneyRequestReportName(report, policy, invoiceReceiverPolicy);
}

if (isInvoiceRoom(report)) {
formattedName = getInvoicesChatName(report);
formattedName = getInvoicesChatName(report, invoiceReceiverPolicy);
}

if (isArchivedRoom(report, getReportNameValuePairs(report?.reportID))) {
Expand All @@ -3553,10 +3557,6 @@ function getReportName(report: OnyxEntry<Report>, policy?: OnyxEntry<Policy>, pa
formattedName = getDisplayNameForParticipant(currentUserAccountID, undefined, undefined, true);
}

if (isInvoiceRoom(report)) {
formattedName = getInvoicesChatName(report);
}

if (formattedName) {
return formatReportLastMessageText(formattedName);
}
Expand Down Expand Up @@ -3631,14 +3631,14 @@ function getPendingChatMembers(accountIDs: number[], previousPendingChatMembers:
/**
* Gets the parent navigation subtitle for the report
*/
function getParentNavigationSubtitle(report: OnyxEntry<Report>): ParentNavigationSummaryParams {
function getParentNavigationSubtitle(report: OnyxEntry<Report>, invoiceReceiverPolicy?: OnyxEntry<Policy>): ParentNavigationSummaryParams {
const parentReport = getParentReport(report);
if (isEmptyObject(parentReport)) {
return {};
}

if (isInvoiceReport(report) || isInvoiceRoom(parentReport)) {
let reportName = `${getPolicyName(parentReport)} & ${getInvoicePayerName(parentReport)}`;
let reportName = `${getPolicyName(parentReport)} & ${getInvoicePayerName(parentReport, invoiceReceiverPolicy)}`;

if (isArchivedRoom(parentReport, getReportNameValuePairs(parentReport?.reportID))) {
reportName += ` (${Localize.translateLocal('common.archived')})`;
Expand Down
6 changes: 4 additions & 2 deletions src/libs/SidebarUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,7 @@ function getOptionData({
parentReportAction,
hasViolations,
transactionViolations,
invoiceReceiverPolicy,
}: {
report: OnyxEntry<Report>;
reportActions: OnyxEntry<ReportActions>;
Expand All @@ -252,6 +253,7 @@ function getOptionData({
policy: OnyxEntry<Policy> | undefined;
parentReportAction: OnyxEntry<ReportAction> | undefined;
hasViolations: boolean;
invoiceReceiverPolicy?: OnyxEntry<Policy>;
transactionViolations?: OnyxCollection<TransactionViolation[]>;
}): ReportUtils.OptionData | undefined {
// When a user signs out, Onyx is cleared. Due to the lazy rendering with a virtual list, it's possible for
Expand Down Expand Up @@ -462,13 +464,13 @@ function getOptionData({
result.phoneNumber = personalDetail?.phoneNumber;
}

const reportName = ReportUtils.getReportName(report, policy);
const reportName = ReportUtils.getReportName(report, policy, undefined, invoiceReceiverPolicy);

result.text = reportName;
result.subtitle = subtitle;
result.participantsList = participantPersonalDetailList;

result.icons = ReportUtils.getIcons(report, personalDetails, personalDetail?.avatar, personalDetail?.login, personalDetail?.accountID, policy);
result.icons = ReportUtils.getIcons(report, personalDetails, personalDetail?.avatar, personalDetail?.login, personalDetail?.accountID, policy, invoiceReceiverPolicy);
result.searchText = OptionsListUtils.getSearchText(report, reportName, participantPersonalDetailList, result.isChatRoom || result.isPolicyExpenseChat, result.isThread);
result.displayNamesWithTooltips = displayNamesWithTooltips;

Expand Down
8 changes: 5 additions & 3 deletions src/pages/home/HeaderView.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, {memo} from 'react';
import {View} from 'react-native';
import type {OnyxEntry} from 'react-native-onyx';
import {withOnyx} from 'react-native-onyx';
import {useOnyx, withOnyx} from 'react-native-onyx';
import Badge from '@components/Badge';
import Button from '@components/Button';
import CaretWrapper from '@components/CaretWrapper';
Expand Down Expand Up @@ -64,6 +64,8 @@ type HeaderViewProps = HeaderViewOnyxProps & {

function HeaderView({report, personalDetails, parentReport, parentReportAction, policy, reportID, onNavigationMenuButtonClicked, shouldUseNarrowLayout = false}: HeaderViewProps) {
const [isDeleteTaskConfirmModalVisible, setIsDeleteTaskConfirmModalVisible] = React.useState(false);
const [invoiceReceiverPolicy] = useOnyx(`${ONYXKEYS.COLLECTION.POLICY}${report.invoiceReceiver && 'policyID' in report.invoiceReceiver ? report.invoiceReceiver.policyID : -1}`);

const {translate} = useLocalize();
const theme = useTheme();
const styles = useThemeStyles();
Expand All @@ -82,7 +84,7 @@ function HeaderView({report, personalDetails, parentReport, parentReportAction,
const isTaskReport = ReportUtils.isTaskReport(report);
const reportHeaderData = !isTaskReport && !isChatThread && report.parentReportID ? parentReport : report;
// Use sorted display names for the title for group chats on native small screen widths
const title = ReportUtils.getReportName(reportHeaderData, undefined, parentReportAction);
const title = ReportUtils.getReportName(reportHeaderData, undefined, parentReportAction, invoiceReceiverPolicy);
const subtitle = ReportUtils.getChatRoomSubtitle(reportHeaderData);
const parentNavigationSubtitleData = ReportUtils.getParentNavigationSubtitle(reportHeaderData);
const reportDescription = ReportUtils.getReportDescriptionText(report);
Expand Down Expand Up @@ -129,7 +131,7 @@ function HeaderView({report, personalDetails, parentReport, parentReportAction,

const shouldShowSubscript = ReportUtils.shouldReportShowSubscript(report);
const defaultSubscriptSize = ReportUtils.isExpenseRequest(report) ? CONST.AVATAR_SIZE.SMALL_NORMAL : CONST.AVATAR_SIZE.DEFAULT;
const icons = ReportUtils.getIcons(reportHeaderData, personalDetails);
const icons = ReportUtils.getIcons(reportHeaderData, personalDetails, null, '', -1, undefined, invoiceReceiverPolicy);
const brickRoadIndicator = ReportUtils.hasReportNameError(report) ? CONST.BRICK_ROAD_INDICATOR_STATUS.ERROR : '';
const shouldShowBorderBottom = !isTaskReport || !shouldUseNarrowLayout;
const shouldDisableDetailPage = ReportUtils.shouldDisableDetailPage(report);
Expand Down
Loading

0 comments on commit 5123bb5

Please sign in to comment.