Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unify TBD-related logic for Distance requests #34135

Merged
merged 41 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
41 commits
Select commit Hold shift + click to select a range
f4aff81
Use transaction.isLoading to identify pending Distance requests
paultsimura Jan 9, 2024
152b569
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 9, 2024
b30a5e7
Use transaction.isLoading to identify pending Distance requests
paultsimura Jan 9, 2024
f68e06e
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 12, 2024
27e9415
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 16, 2024
46dec59
Rename isLoadingDistanceRequest -> isDistanceBeingCalculated
paultsimura Jan 16, 2024
c05c602
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 19, 2024
b87642a
Use "Route pending" instead of "TBD"
paultsimura Jan 19, 2024
5a24138
Fix the pending route logic in Distance e-Receipt
paultsimura Jan 19, 2024
dd94a01
Show the "Pending route..." as merchant for requests with manual amount
paultsimura Jan 20, 2024
cda5367
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 20, 2024
373c670
Update hasPendingRoute logic
paultsimura Jan 20, 2024
634b5fa
Update "Route pending" display based on amount
paultsimura Jan 20, 2024
f2a7a06
Revert some changes
paultsimura Jan 20, 2024
98ec065
Update IOU Report total on waypoints change
paultsimura Jan 21, 2024
5443b83
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 23, 2024
c6ed187
Use TransactionUtils.hasPendingRoute to check pending route for receipt
paultsimura Jan 23, 2024
6cdea23
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 24, 2024
0d72dde
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 26, 2024
3f3564c
Show "Route pending" when the rate is not loaded yet
paultsimura Jan 26, 2024
98c7b9b
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 27, 2024
9452819
Cosmetic change for readability
paultsimura Jan 27, 2024
db6e2a7
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 29, 2024
26da439
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Jan 30, 2024
16f6b54
Change hasPendingRoute to isFetchingWaypointsFromServer
paultsimura Jan 30, 2024
646c97f
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 1, 2024
19d97aa
Update comment for readability
paultsimura Feb 1, 2024
42757dc
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 2, 2024
2cd2c52
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 2, 2024
3ff6f9a
Refactor checks for readability
paultsimura Feb 3, 2024
e63553c
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 5, 2024
e88d6d5
TS migration cleanup
paultsimura Feb 6, 2024
516ae47
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 6, 2024
d22cfe5
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 6, 2024
a020a23
Remove hasOnlyPendingDistanceRequests vars
paultsimura Feb 6, 2024
1c849c3
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 6, 2024
534b2c1
Explain the setMoneyRequestPendingFields call
paultsimura Feb 6, 2024
82bb06e
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 6, 2024
749a395
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 6, 2024
8d493e2
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 7, 2024
2e4c40f
Merge branch 'main' into fix/33362-is-loading-tbd
paultsimura Feb 7, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/components/DistanceEReceipt.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import useLocalize from '@hooks/useLocalize';
import useNetwork from '@hooks/useNetwork';
import useTheme from '@hooks/useTheme';
import useThemeStyles from '@hooks/useThemeStyles';
import * as CurrencyUtils from '@libs/CurrencyUtils';
import * as ReceiptUtils from '@libs/ReceiptUtils';
import * as ReportUtils from '@libs/ReportUtils';
import * as TransactionUtils from '@libs/TransactionUtils';
Expand Down Expand Up @@ -35,8 +34,7 @@ function DistanceEReceipt({transaction}) {
const {translate} = useLocalize();
const {isOffline} = useNetwork();
const {thumbnail} = TransactionUtils.hasReceipt(transaction) ? ReceiptUtils.getThumbnailAndImageURIs(transaction) : {};
const {amount: transactionAmount, currency: transactionCurrency, merchant: transactionMerchant, created: transactionDate} = ReportUtils.getTransactionDetails(transaction);
const formattedTransactionAmount = transactionAmount ? CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency) : translate('common.tbd');
const {formattedAmount: formattedTransactionAmount, merchant: transactionMerchant, created: transactionDate} = ReportUtils.getTransactionDetails(transaction);
const thumbnailSource = tryResolveUrlFromApiRoot(thumbnail || '');
const waypoints = lodashGet(transaction, 'comment.waypoints', {});
const sortedWaypoints = useMemo(
Expand Down
17 changes: 8 additions & 9 deletions src/components/ReportActionItem/MoneyRequestPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,13 @@ function MoneyRequestPreview(props) {
// Pay button should only be visible to the manager of the report.
const isCurrentUserManager = managerID === sessionAccountID;

const {amount: requestAmount, currency: requestCurrency, comment: requestComment, merchant} = ReportUtils.getTransactionDetails(props.transaction);
const {
amount: requestAmount,
formattedAmount: formattedRequestAmount,
currency: requestCurrency,
comment: requestComment,
merchant,
} = ReportUtils.getTransactionDetails(props.transaction);
const description = truncate(requestComment, {length: CONST.REQUEST_PREVIEW.MAX_LENGTH});
const requestMerchant = truncate(merchant, {length: CONST.REQUEST_PREVIEW.MAX_LENGTH});
const hasReceipt = TransactionUtils.hasReceipt(props.transaction);
Expand All @@ -166,13 +172,10 @@ function MoneyRequestPreview(props) {
// Show the merchant for IOUs and expenses only if they are custom or not related to scanning smartscan
paultsimura marked this conversation as resolved.
Show resolved Hide resolved
const shouldShowMerchant = !_.isEmpty(requestMerchant) && requestMerchant !== CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT && requestMerchant !== CONST.TRANSACTION.DEFAULT_MERCHANT;
const shouldShowDescription = !_.isEmpty(description) && !shouldShowMerchant && !isScanning;
const hasPendingWaypoints = lodashGet(props.transaction, 'pendingFields.waypoints', null);

let merchantOrDescription = requestMerchant;
if (!shouldShowMerchant) {
merchantOrDescription = description || '';
} else if (hasPendingWaypoints) {
merchantOrDescription = requestMerchant.replace(CONST.REGEX.FIRST_SPACE, translate('common.tbd'));
}

const receiptImages = hasReceipt ? [ReceiptUtils.getThumbnailAndImageURIs(props.transaction)] : [];
Expand Down Expand Up @@ -221,10 +224,6 @@ function MoneyRequestPreview(props) {
};

const getDisplayAmountText = () => {
if (isDistanceRequest) {
return requestAmount && !hasPendingWaypoints ? CurrencyUtils.convertToDisplayString(requestAmount, requestCurrency) : translate('common.tbd');
}

if (isScanning) {
return translate('iou.receiptScanning');
}
Expand All @@ -233,7 +232,7 @@ function MoneyRequestPreview(props) {
return Localize.translateLocal('iou.receiptMissingDetails');
}

return CurrencyUtils.convertToDisplayString(requestAmount, requestCurrency);
return formattedRequestAmount;
};

const getDisplayDeleteAmountText = () => {
Expand Down
9 changes: 2 additions & 7 deletions src/components/ReportActionItem/MoneyRequestView.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate
const {
created: transactionDate,
amount: transactionAmount,
currency: transactionCurrency,
formattedAmount: formattedTransactionAmount,
comment: transactionDescription,
merchant: transactionMerchant,
billable: transactionBillable,
Expand All @@ -140,11 +140,6 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate
} = ReportUtils.getTransactionDetails(transaction);
const isEmptyMerchant = transactionMerchant === '' || transactionMerchant === CONST.TRANSACTION.PARTIAL_TRANSACTION_MERCHANT;
const isDistanceRequest = TransactionUtils.isDistanceRequest(transaction);
let formattedTransactionAmount = transactionAmount ? CurrencyUtils.convertToDisplayString(transactionAmount, transactionCurrency) : '';
const hasPendingWaypoints = lodashGet(transaction, 'pendingFields.waypoints', null);
if (isDistanceRequest && (!formattedTransactionAmount || hasPendingWaypoints)) {
formattedTransactionAmount = translate('common.tbd');
}
const formattedOriginalAmount = transactionOriginalAmount && transactionOriginalCurrency && CurrencyUtils.convertToDisplayString(transactionOriginalAmount, transactionOriginalCurrency);
const isCardTransaction = TransactionUtils.isCardTransaction(transaction);
const cardProgramName = isCardTransaction ? CardUtils.getCardDescription(transactionCardID) : '';
Expand Down Expand Up @@ -297,7 +292,7 @@ function MoneyRequestView({report, parentReport, parentReportActions, policyCate
<OfflineWithFeedback pendingAction={lodashGet(transaction, 'pendingFields.waypoints') || lodashGet(transaction, 'pendingAction')}>
<MenuItemWithTopDescription
description={translate('common.distance')}
title={hasPendingWaypoints ? transactionMerchant.replace(CONST.REGEX.FIRST_SPACE, translate('common.tbd')) : transactionMerchant}
title={transactionMerchant}
interactive={canEditDistance}
shouldShowRightIcon={canEditDistance}
titleStyle={styles.flex1}
Expand Down
12 changes: 3 additions & 9 deletions src/components/ReportActionItem/ReportPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,11 +154,8 @@ function ReportPreview(props) {
const hasErrors = hasReceipts && hasMissingSmartscanFields;
const lastThreeTransactionsWithReceipts = transactionsWithReceipts.slice(-3);
const lastThreeReceipts = _.map(lastThreeTransactionsWithReceipts, (transaction) => ReceiptUtils.getThumbnailAndImageURIs(transaction));
let formattedMerchant = numberOfRequests === 1 && hasReceipts ? TransactionUtils.getMerchant(transactionsWithReceipts[0]) : null;
const hasPendingWaypoints = formattedMerchant && hasOnlyDistanceRequests && _.every(transactionsWithReceipts, (transaction) => lodashGet(transaction, 'pendingFields.waypoints', null));
if (hasPendingWaypoints) {
formattedMerchant = formattedMerchant.replace(CONST.REGEX.FIRST_SPACE, props.translate('common.tbd'));
}
const formattedMerchant = numberOfRequests === 1 && hasReceipts ? TransactionUtils.getMerchant(transactionsWithReceipts[0]) : null;
const hasOnlyLoadingDistanceRequests = hasOnlyDistanceRequests && _.every(transactionsWithReceipts, (transaction) => TransactionUtils.isLoadingDistanceRequest(transaction));
const previewSubtitle =
formattedMerchant ||
props.translate('iou.requestCount', {
Expand All @@ -175,7 +172,7 @@ function ReportPreview(props) {
);

const getDisplayAmount = () => {
if (hasPendingWaypoints) {
if (hasOnlyLoadingDistanceRequests) {
return props.translate('common.tbd');
}
if (totalDisplaySpend) {
Expand All @@ -184,9 +181,6 @@ function ReportPreview(props) {
if (isScanning) {
return props.translate('iou.receiptScanning');
}
if (hasOnlyDistanceRequests) {
return props.translate('common.tbd');
}
paultsimura marked this conversation as resolved.
Show resolved Hide resolved

// If iouReport is not available, get amount from the action message (Ex: "Domain20821's Workspace owes $33.00" or "paid ₫60" or "paid -₫60 elsewhere")
let displayAmount = '';
Expand Down
17 changes: 15 additions & 2 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ type TransactionDetails =
cardID: number;
originalAmount: number;
originalCurrency: string;
formattedAmount: string;
paultsimura marked this conversation as resolved.
Show resolved Hide resolved
}
| undefined;

Expand Down Expand Up @@ -1829,11 +1830,23 @@ function getTransactionDetails(transaction: OnyxEntry<Transaction>, createdDateF
if (!transaction) {
return;
}

const report = getReport(transaction?.reportID);
const amount = TransactionUtils.getAmount(transaction, isNotEmptyObject(report) && isExpenseReport(report));
const currency = TransactionUtils.getCurrency(transaction);

let formattedAmount;
if (TransactionUtils.isLoadingDistanceRequest(transaction)) {
formattedAmount = Localize.translateLocal('common.tbd');
} else {
formattedAmount = amount ? CurrencyUtils.convertToDisplayString(amount, currency) : '';
}

return {
created: TransactionUtils.getCreated(transaction, createdDateFormat),
amount: TransactionUtils.getAmount(transaction, isNotEmptyObject(report) && isExpenseReport(report)),
currency: TransactionUtils.getCurrency(transaction),
amount,
currency,
formattedAmount,
comment: TransactionUtils.getDescription(transaction),
merchant: TransactionUtils.getMerchant(transaction),
waypoints: TransactionUtils.getWaypoints(transaction),
Expand Down
23 changes: 22 additions & 1 deletion src/libs/TransactionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import type {Comment, Receipt, Waypoint, WaypointCollection} from '@src/types/on
import type {EmptyObject} from '@src/types/utils/EmptyObject';
import {isCorporateCard, isExpensifyCard} from './CardUtils';
import DateUtils from './DateUtils';
import * as Localize from './Localize';
import * as NumberUtils from './NumberUtils';

type AdditionalTransactionChanges = {comment?: string; waypoints?: WaypointCollection};
Expand Down Expand Up @@ -307,11 +308,30 @@ function getOriginalAmount(transaction: Transaction): number {
return Math.abs(amount);
}

/**
* Verify if the transaction is of Distance request and is not fully ready:
* - it has a zero amount, which means the request was created offline and expects the distance calculation from the server
paultsimura marked this conversation as resolved.
Show resolved Hide resolved
* - it is in `isLoading` state, which means the waypoints were updated offline and the distance requires re-calculation
*/
function isLoadingDistanceRequest(transaction: OnyxEntry<Transaction>): boolean {
paultsimura marked this conversation as resolved.
Show resolved Hide resolved
if (!transaction) {
return false;
}

const amount = getAmount(transaction, false);
return isDistanceRequest(transaction) && (!!transaction?.isLoading || amount === 0);
}

/**
* Return the merchant field from the transaction, return the modifiedMerchant if present.
*/
function getMerchant(transaction: OnyxEntry<Transaction>): string {
return transaction?.modifiedMerchant ? transaction.modifiedMerchant : transaction?.merchant ?? '';
if (!transaction) {
return '';
}

const merchant = transaction.modifiedMerchant ? transaction.modifiedMerchant : transaction.merchant ?? '';
return isLoadingDistanceRequest(transaction) ? merchant.replace(CONST.REGEX.FIRST_SPACE, Localize.translateLocal('common.tbd')) : merchant;
paultsimura marked this conversation as resolved.
Show resolved Hide resolved
}

function getDistance(transaction: Transaction): number {
Expand Down Expand Up @@ -566,6 +586,7 @@ export {
isReceiptBeingScanned,
getValidWaypoints,
isDistanceRequest,
isLoadingDistanceRequest,
isExpensifyCardTransaction,
isCardTransaction,
isPending,
Expand Down
2 changes: 2 additions & 0 deletions src/types/onyx/Transaction.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,8 @@ type Transaction = {
/** If the transaction was made in a foreign currency, we send the original amount and currency */
originalAmount?: number;
originalCurrency?: string;

isLoading?: boolean;
};

export default Transaction;
Expand Down
Loading