From a71ec86877c2e414bc28b4b19998f7860d90553f Mon Sep 17 00:00:00 2001 From: Vit Horacek <36083550+mountiny@users.noreply.github.com> Date: Mon, 21 Oct 2024 17:22:49 +0900 Subject: [PATCH] Merge pull request #51156 from Expensify/revert-51014-revert-47370-krishna2323/issue/47100 [CP Staging] Bring back fix: [P2P Distance] Split - Participants amount displayed 0.00 briefly on confirmation screen. (cherry picked from commit c1806c3526c2d3cf124aab750fabb38d4e6b6fe7) (CP triggered by mountiny) --- .../step/IOURequestStepConfirmation.tsx | 6 +-- .../request/step/IOURequestStepDistance.tsx | 40 ++++++++++++++++++- 2 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx index e8fc138f6a52..835e37ef877e 100644 --- a/src/pages/iou/request/step/IOURequestStepConfirmation.tsx +++ b/src/pages/iou/request/step/IOURequestStepConfirmation.tsx @@ -89,11 +89,11 @@ function IOURequestStepConfirmation({ const isSubmittingFromTrackExpense = action === CONST.IOU.ACTION.SUBMIT; const isMovingTransactionFromTrackExpense = IOUUtils.isMovingTransactionFromTrackExpense(action); const payeePersonalDetails = useMemo(() => { - if (personalDetails?.[transaction?.splitPayerAccountIDs?.[0] ?? -1]) { - return personalDetails?.[transaction?.splitPayerAccountIDs?.[0] ?? -1]; + if (personalDetails?.[transaction?.splitPayerAccountIDs?.at(0) ?? -1]) { + return personalDetails?.[transaction?.splitPayerAccountIDs?.at(0) ?? -1]; } - const participant = transaction?.participants?.find((val) => val.accountID === (transaction?.splitPayerAccountIDs?.[0] ?? -1)); + const participant = transaction?.participants?.find((val) => val.accountID === (transaction?.splitPayerAccountIDs?.at(0) ?? -1)); return { login: participant?.login ?? '', diff --git a/src/pages/iou/request/step/IOURequestStepDistance.tsx b/src/pages/iou/request/step/IOURequestStepDistance.tsx index 581f8a9176a9..899f024de4be 100644 --- a/src/pages/iou/request/step/IOURequestStepDistance.tsx +++ b/src/pages/iou/request/step/IOURequestStepDistance.tsx @@ -21,10 +21,12 @@ import usePolicy from '@hooks/usePolicy'; import usePrevious from '@hooks/usePrevious'; import useThemeStyles from '@hooks/useThemeStyles'; import DistanceRequestUtils from '@libs/DistanceRequestUtils'; +import type {MileageRate} from '@libs/DistanceRequestUtils'; import * as ErrorUtils from '@libs/ErrorUtils'; import * as IOUUtils from '@libs/IOUUtils'; import Navigation from '@libs/Navigation/Navigation'; import * as OptionsListUtils from '@libs/OptionsListUtils'; +import * as PolicyUtils from '@libs/PolicyUtils'; import * as ReportUtils from '@libs/ReportUtils'; import * as TransactionUtils from '@libs/TransactionUtils'; import * as IOU from '@userActions/IOU'; @@ -36,6 +38,7 @@ import ONYXKEYS from '@src/ONYXKEYS'; import ROUTES from '@src/ROUTES'; import type SCREENS from '@src/SCREENS'; import type * as OnyxTypes from '@src/types/onyx'; +import type {Participant} from '@src/types/onyx/IOU'; import type {Errors} from '@src/types/onyx/OnyxCommon'; import type {Waypoint, WaypointCollection} from '@src/types/onyx/Transaction'; import StepScreenWrapper from './StepScreenWrapper'; @@ -83,6 +86,7 @@ function IOURequestStepDistance({ const scrollViewRef = useRef(null); const isLoadingRoute = transaction?.comment?.isLoading ?? false; const isLoading = transaction?.isLoading ?? false; + const isSplitRequest = iouType === CONST.IOU.TYPE.SPLIT; const hasRouteError = !!transaction?.errorFields?.route; const [shouldShowAtLeastTwoDifferentWaypointsError, setShouldShowAtLeastTwoDifferentWaypointsError] = useState(false); const isWaypointEmpty = (waypoint?: Waypoint) => { @@ -104,7 +108,39 @@ function IOURequestStepDistance({ const isCreatingNewRequest = !(backTo || isEditing); const [recentWaypoints, {status: recentWaypointsStatus}] = useOnyx(ONYXKEYS.NVP_RECENT_WAYPOINTS); const iouRequestType = TransactionUtils.getRequestType(transaction); - const customUnitRateID = TransactionUtils.getRateID(transaction); + const customUnitRateID = TransactionUtils.getRateID(transaction) ?? '-1'; + + // Sets `amount` and `split` share data before moving to the next step to avoid briefly showing `0.00` as the split share for participants + const setDistanceRequestData = useCallback( + (participants: Participant[]) => { + // Get policy report based on transaction participants + const isPolicyExpenseChat = participants?.some((participant) => participant.isPolicyExpenseChat); + const selectedReportID = participants?.length === 1 ? participants.at(0)?.reportID ?? reportID : reportID; + const policyReport = participants.at(0) ? ReportUtils.getReport(selectedReportID) : report; + + const IOUpolicyID = IOU.getIOURequestPolicyID(transaction, policyReport); + const IOUpolicy = PolicyUtils.getPolicy(report?.policyID ?? IOUpolicyID); + const policyCurrency = policy?.outputCurrency ?? PolicyUtils.getPersonalPolicy()?.outputCurrency ?? CONST.CURRENCY.USD; + + const mileageRates = DistanceRequestUtils.getMileageRates(IOUpolicy); + const defaultMileageRate = DistanceRequestUtils.getDefaultMileageRate(IOUpolicy); + const mileageRate: MileageRate = TransactionUtils.isCustomUnitRateIDForP2P(transaction) + ? DistanceRequestUtils.getRateForP2P(policyCurrency, transaction) + : mileageRates?.[customUnitRateID] ?? defaultMileageRate; + + const {unit, rate} = mileageRate ?? {}; + const distance = TransactionUtils.getDistanceInMeters(transaction, unit); + const currency = mileageRate?.currency ?? policyCurrency; + const amount = DistanceRequestUtils.getDistanceRequestAmount(distance, unit ?? CONST.CUSTOM_UNITS.DISTANCE_UNIT_MILES, rate ?? 0); + IOU.setMoneyRequestAmount(transactionID, amount, currency); + + const participantAccountIDs: number[] | undefined = participants?.map((participant) => Number(participant.accountID ?? -1)); + if (isSplitRequest && amount && currency && !isPolicyExpenseChat) { + IOU.setSplitShares(transaction, amount, currency ?? '', participantAccountIDs ?? []); + } + }, + [report, transaction, transactionID, isSplitRequest, policy?.outputCurrency, reportID, customUnitRateID], + ); // For quick button actions, we'll skip the confirmation page unless the report is archived or this is a workspace // request and the workspace requires a category or a tag @@ -245,6 +281,7 @@ function IOURequestStepDistance({ const participantAccountID = participant?.accountID ?? -1; return participantAccountID ? OptionsListUtils.getParticipantsOption(participant, personalDetails) : OptionsListUtils.getReportOption(participant); }); + setDistanceRequestData(participants); if (shouldSkipConfirmation) { if (iouType === CONST.IOU.TYPE.SPLIT) { IOU.splitBill({ @@ -349,6 +386,7 @@ function IOURequestStepDistance({ iouRequestType, reportNameValuePairs, customUnitRateID, + setDistanceRequestData, ]); const getError = () => {