Skip to content

Commit

Permalink
Merge pull request #38882 from FitseTLT/fix-update-getIOUReportAction…
Browse files Browse the repository at this point in the history
…DisplayMessage-to-take-transaction-param

Fix - Update getIOUReportActionDisplayMessage() to accept the transaction as a parameter
  • Loading branch information
tgolen authored Apr 3, 2024
2 parents ebc8de8 + 338aeff commit ff492c4
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 26 deletions.
6 changes: 3 additions & 3 deletions src/libs/ReportActionsUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -675,12 +675,12 @@ function getLatestReportActionFromOnyxData(onyxData: OnyxUpdate[] | null): OnyxE
/**
* Find the transaction associated with this reportAction, if one exists.
*/
function getLinkedTransactionID(reportID: string, reportActionID: string): string | null {
const reportAction = allReportActions?.[reportID]?.[reportActionID];
function getLinkedTransactionID(reportActionOrID: string | OnyxEntry<ReportAction>, reportID?: string): string | null {
const reportAction = typeof reportActionOrID === 'string' ? allReportActions?.[reportID ?? '']?.[reportActionOrID] : reportActionOrID;
if (!reportAction || reportAction.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU) {
return null;
}
return reportAction.originalMessage.IOUTransactionID ?? null;
return reportAction.originalMessage?.IOUTransactionID ?? null;
}

function getReportAction(reportID: string, reportActionID: string): OnyxEntry<ReportAction> {
Expand Down
22 changes: 11 additions & 11 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ import isReportMessageAttachment from './isReportMessageAttachment';
import localeCompare from './LocaleCompare';
import * as LocalePhoneNumber from './LocalePhoneNumber';
import * as Localize from './Localize';
import Log from './Log';
import {isEmailPublicDomain} from './LoginUtils';
import ModifiedExpenseMessage from './ModifiedExpenseMessage';
import linkingConfig from './Navigation/linkingConfig';
Expand Down Expand Up @@ -2512,15 +2513,6 @@ function getLinkedTransaction(reportAction: OnyxEntry<ReportAction | OptimisticI
return allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {};
}

/**
* Retrieve the particular transaction object given its ID.
*
* NOTE: This method is only meant to be used inside this action file. Do not export and use it elsewhere. Use withOnyx or Onyx.connect() instead.
*/
function getTransaction(transactionID: string): OnyxEntry<Transaction> | EmptyObject {
return allTransactions?.[`${ONYXKEYS.COLLECTION.TRANSACTION}${transactionID}`] ?? {};
}

/**
* Given a parent IOU report action get report name for the LHN.
*/
Expand Down Expand Up @@ -5236,7 +5228,7 @@ function getVisibleMemberIDs(report: OnyxEntry<Report>): number[] {
/**
* Return iou report action display message
*/
function getIOUReportActionDisplayMessage(reportAction: OnyxEntry<ReportAction>): string {
function getIOUReportActionDisplayMessage(reportAction: OnyxEntry<ReportAction>, transaction?: OnyxEntry<Transaction>, shouldLog = false): string {
if (reportAction?.actionName !== CONST.REPORT.ACTIONS.TYPE.IOU) {
return '';
}
Expand Down Expand Up @@ -5266,7 +5258,15 @@ function getIOUReportActionDisplayMessage(reportAction: OnyxEntry<ReportAction>)
return Localize.translateLocal(translationKey, {amount: formattedAmount, payer: ''});
}

const transaction = getTransaction(originalMessage.IOUTransactionID ?? '');
// This log to server is temporary and needed to determine if there is a case we need the transaction param
// when we call getIOUReportActionDisplayMessage from ReportActionItemMessage
if (shouldLog) {
Log.alert('Transaction Param Used when getIOUReportActionDisplayMessage was called from ReportActionItemMessage', {
reportActionID: reportAction.reportActionID,
originalMessageType: originalMessage.type,
});
}

const transactionDetails = getTransactionDetails(!isEmptyObject(transaction) ? transaction : null);
const formattedAmount = CurrencyUtils.convertToDisplayString(transactionDetails?.amount ?? 0, transactionDetails?.currency);
const isRequestSettled = isSettled(originalMessage.IOUReportID);
Expand Down
2 changes: 1 addition & 1 deletion src/libs/actions/ReportActions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ function clearReportActionErrors(reportID: string, reportAction: ReportAction, k
});

// If there's a linked transaction, delete that too
const linkedTransactionID = ReportActionUtils.getLinkedTransactionID(originalReportID ?? '', reportAction.reportActionID);
const linkedTransactionID = ReportActionUtils.getLinkedTransactionID(reportAction.reportActionID, originalReportID ?? '');
if (linkedTransactionID) {
Onyx.set(`${ONYXKEYS.COLLECTION.TRANSACTION}${linkedTransactionID}`, null);
}
Expand Down
16 changes: 14 additions & 2 deletions src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,12 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useStyleUtils from '@hooks/useStyleUtils';
import useWindowDimensions from '@hooks/useWindowDimensions';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as Session from '@userActions/Session';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import type {Beta, ReportAction, ReportActions} from '@src/types/onyx';
import type {Beta, ReportAction, ReportActions, Transaction} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
import type {ContextMenuAction, ContextMenuActionPayload} from './ContextMenuActions';
import ContextMenuActions from './ContextMenuActions';
Expand All @@ -31,6 +32,9 @@ type BaseReportActionContextMenuOnyxProps = {

/** All of the actions of the report */
reportActions: OnyxEntry<ReportActions>;

/** The transaction linked to the report action this context menu is attached to. */
transaction: OnyxEntry<Transaction>;
};

type BaseReportActionContextMenuProps = BaseReportActionContextMenuOnyxProps & {
Expand Down Expand Up @@ -106,6 +110,7 @@ function BaseReportActionContextMenu({
selection = '',
draftMessage = '',
reportActionID,
transaction,
reportID,
betas,
reportActions,
Expand Down Expand Up @@ -252,6 +257,7 @@ function BaseReportActionContextMenu({
textTranslateKey === 'reportActionContextMenu.deleteAction' ||
textTranslateKey === 'reportActionContextMenu.deleteConfirmation';
const text = textTranslateKey && (isKeyInActionUpdateKeys ? translate(textTranslateKey, {action: reportAction}) : translate(textTranslateKey));
const transactionPayload = textTranslateKey === 'reportActionContextMenu.copyToClipboard' && transaction && {transaction};
const isMenuAction = textTranslateKey === 'reportActionContextMenu.menu';

return (
Expand All @@ -268,7 +274,7 @@ function BaseReportActionContextMenu({
key={contextAction.textTranslateKey}
onPress={(event) =>
interceptAnonymousUser(
() => contextAction.onPress?.(closePopup, {...payload, event, ...(isMenuAction ? {anchorRef: threedotRef} : {})}),
() => contextAction.onPress?.(closePopup, {...payload, ...transactionPayload, event, ...(isMenuAction ? {anchorRef: threedotRef} : {})}),
contextAction.isAnonymousAction,
)
}
Expand All @@ -292,6 +298,12 @@ export default withOnyx<BaseReportActionContextMenuProps, BaseReportActionContex
key: ({originalReportID}) => `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${originalReportID}`,
canEvict: false,
},
transaction: {
key: ({reportActions, reportActionID}) => {
const reportAction = reportActions?.[reportActionID];
return `${ONYXKEYS.COLLECTION.TRANSACTION}${(reportAction && ReportActionsUtils.getLinkedTransactionID(reportAction)) ?? 0}`;
},
},
})(
memo(BaseReportActionContextMenu, (prevProps, nextProps) => {
const {reportActions: prevReportActions, ...prevPropsWithoutReportActions} = prevProps;
Expand Down
7 changes: 4 additions & 3 deletions src/pages/home/report/ContextMenu/ContextMenuActions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import type {TranslationPaths} from '@src/languages/types';
import ROUTES from '@src/ROUTES';
import type {Beta, ReportAction, ReportActionReactions, Report as ReportType} from '@src/types/onyx';
import type {Beta, ReportAction, ReportActionReactions, Report as ReportType, Transaction} from '@src/types/onyx';
import type IconAsset from '@src/types/utils/IconAsset';
import type {ContextMenuAnchor} from './ReportActionContextMenu';
import {hideContextMenu, showDeleteModal} from './ReportActionContextMenu';
Expand Down Expand Up @@ -64,6 +64,7 @@ type ShouldShow = (

type ContextMenuActionPayload = {
reportAction: ReportAction;
transaction?: OnyxEntry<Transaction>;
reportID: string;
draftMessage: string;
selection: string;
Expand Down Expand Up @@ -340,7 +341,7 @@ const ContextMenuActions: ContextMenuAction[] = [
// If return value is true, we switch the `text` and `icon` on
// `ContextMenuItem` with `successText` and `successIcon` which will fall back to
// the `text` and `icon`
onPress: (closePopover, {reportAction, selection, reportID}) => {
onPress: (closePopover, {reportAction, transaction, selection, reportID}) => {
const isReportPreviewAction = ReportActionsUtils.isReportPreviewAction(reportAction);
const messageHtml = getActionHtml(reportAction);
const messageText = ReportActionsUtils.getReportActionMessageText(reportAction);
Expand All @@ -364,7 +365,7 @@ const ContextMenuActions: ContextMenuAction[] = [
const displayMessage = ReportUtils.getReimbursementDeQueuedActionMessage(reportAction, expenseReport);
Clipboard.setString(displayMessage);
} else if (ReportActionsUtils.isMoneyRequestAction(reportAction)) {
const displayMessage = ReportUtils.getIOUReportActionDisplayMessage(reportAction);
const displayMessage = ReportUtils.getIOUReportActionDisplayMessage(reportAction, transaction);
Clipboard.setString(displayMessage);
} else if (ReportActionsUtils.isCreatedTaskReportAction(reportAction)) {
const taskPreviewMessage = TaskUtils.getTaskCreatedMessage(reportAction);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {BaseReportActionContextMenuProps} from '@pages/home/report/ContextMenu/BaseReportActionContextMenu';

type MiniReportActionContextMenuProps = Omit<BaseReportActionContextMenuProps, 'isMini' | 'betas' | 'reportActions'> & {
type MiniReportActionContextMenuProps = Omit<BaseReportActionContextMenuProps, 'isMini' | 'betas' | 'reportActions' | 'transaction'> & {
/** Should the reportAction this menu is attached to have the appearance of being grouped with the previous reportAction? */
displayAsGroup?: boolean;
};
Expand Down
22 changes: 17 additions & 5 deletions src/pages/home/report/ReportActionItemMessage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,26 @@ import type {ReactElement} from 'react';
import React from 'react';
import type {StyleProp, TextStyle, ViewStyle} from 'react-native';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import Text from '@components/Text';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ReportActionsUtils from '@libs/ReportActionsUtils';
import * as ReportUtils from '@libs/ReportUtils';
import CONST from '@src/CONST';
import type {ReportAction} from '@src/types/onyx';
import ONYXKEYS from '@src/ONYXKEYS';
import type {ReportAction, Transaction} from '@src/types/onyx';
import type {OriginalMessageAddComment} from '@src/types/onyx/OriginalMessage';
import TextCommentFragment from './comment/TextCommentFragment';
import ReportActionItemFragment from './ReportActionItemFragment';

type ReportActionItemMessageProps = {
type ReportActionItemMessageOnyxProps = {
/** The transaction linked to the report action. */
transaction: OnyxEntry<Transaction>;
};

type ReportActionItemMessageProps = ReportActionItemMessageOnyxProps & {
/** The report action */
action: ReportAction;

Expand All @@ -30,7 +38,7 @@ type ReportActionItemMessageProps = {
reportID: string;
};

function ReportActionItemMessage({action, displayAsGroup, reportID, style, isHidden = false}: ReportActionItemMessageProps) {
function ReportActionItemMessage({action, transaction, displayAsGroup, reportID, style, isHidden = false}: ReportActionItemMessageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();

Expand Down Expand Up @@ -58,7 +66,7 @@ function ReportActionItemMessage({action, displayAsGroup, reportID, style, isHid
const originalMessage = action.actionName === CONST.REPORT.ACTIONS.TYPE.IOU ? action.originalMessage : null;
const iouReportID = originalMessage?.IOUReportID;
if (iouReportID) {
iouMessage = ReportUtils.getIOUReportActionDisplayMessage(action);
iouMessage = ReportUtils.getIOUReportActionDisplayMessage(action, transaction, true);
}
}

Expand Down Expand Up @@ -115,4 +123,8 @@ function ReportActionItemMessage({action, displayAsGroup, reportID, style, isHid

ReportActionItemMessage.displayName = 'ReportActionItemMessage';

export default ReportActionItemMessage;
export default withOnyx<ReportActionItemMessageProps, ReportActionItemMessageOnyxProps>({
transaction: {
key: ({action}) => `${ONYXKEYS.COLLECTION.TRANSACTION}${ReportActionsUtils.getLinkedTransactionID(action) ?? 0}`,
},
})(ReportActionItemMessage);

0 comments on commit ff492c4

Please sign in to comment.