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

Workspace currency optimistic actions update #33171

Merged
merged 8 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
165 changes: 84 additions & 81 deletions src/components/ReportActionItem/ReportPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import _ from 'underscore';
import Button from '@components/Button';
import Icon from '@components/Icon';
import * as Expensicons from '@components/Icon/Expensicons';
import OfflineWithFeedback from '@components/OfflineWithFeedback';
import PressableWithoutFeedback from '@components/Pressable/PressableWithoutFeedback';
import refPropTypes from '@components/refPropTypes';
import SettlementButton from '@components/SettlementButton';
Expand Down Expand Up @@ -219,94 +220,96 @@ function ReportPreview(props) {
}, [isGroupPolicy, isCurrentUserManager, isDraftExpenseReport, isApproved, iouSettled]);
const shouldShowSettlementButton = shouldShowPayButton || shouldShowApproveButton;
return (
<View style={[styles.chatItemMessage, ...props.containerStyles]}>
<PressableWithoutFeedback
onPress={() => {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(props.iouReportID));
}}
onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
onPressOut={() => ControlSelection.unblock()}
onLongPress={(event) => showContextMenuForReport(event, props.contextMenuAnchor, props.chatReportID, props.action, props.checkIfContextMenuActive)}
style={[styles.flexRow, styles.justifyContentBetween, styles.reportPreviewBox]}
role="button"
accessibilityLabel={props.translate('iou.viewDetails')}
>
<View style={[styles.reportPreviewBox, props.isHovered || isScanning || props.isWhisper ? styles.reportPreviewBoxHoverBorder : undefined]}>
{hasReceipts && (
<ReportActionItemImages
images={lastThreeReceipts}
total={transactionsWithReceipts.length}
isHovered={props.isHovered || isScanning}
size={CONST.RECEIPT.MAX_REPORT_PREVIEW_RECEIPTS}
/>
)}
<View style={styles.reportPreviewBoxBody}>
<View style={styles.flexRow}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={[styles.textLabelSupporting, styles.mb1, styles.lh20]}>{getPreviewMessage()}</Text>
</View>
{!iouSettled && hasErrors && (
<Icon
src={Expensicons.DotIndicator}
fill={theme.danger}
/>
)}
</View>
<View style={styles.flexRow}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={styles.textHeadline}>{getDisplayAmount()}</Text>
{ReportUtils.isSettled(props.iouReportID) && (
<View style={styles.defaultCheckmarkWrapper}>
<Icon
src={Expensicons.Checkmark}
fill={theme.iconSuccessFill}
/>
</View>
<OfflineWithFeedback pendingAction={lodashGet(props, 'iouReport.pendingFields.preview')}>
<View style={[styles.chatItemMessage, ...props.containerStyles]}>
<PressableWithoutFeedback
onPress={() => {
Navigation.navigate(ROUTES.REPORT_WITH_ID.getRoute(props.iouReportID));
}}
onPressIn={() => DeviceCapabilities.canUseTouchScreen() && ControlSelection.block()}
onPressOut={() => ControlSelection.unblock()}
onLongPress={(event) => showContextMenuForReport(event, props.contextMenuAnchor, props.chatReportID, props.action, props.checkIfContextMenuActive)}
style={[styles.flexRow, styles.justifyContentBetween, styles.reportPreviewBox]}
role="button"
accessibilityLabel={props.translate('iou.viewDetails')}
>
<View style={[styles.reportPreviewBox, props.isHovered || isScanning || props.isWhisper ? styles.reportPreviewBoxHoverBorder : undefined]}>
{hasReceipts && (
<ReportActionItemImages
images={lastThreeReceipts}
total={transactionsWithReceipts.length}
isHovered={props.isHovered || isScanning}
size={CONST.RECEIPT.MAX_REPORT_PREVIEW_RECEIPTS}
/>
)}
<View style={styles.reportPreviewBoxBody}>
<View style={styles.flexRow}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={[styles.textLabelSupporting, styles.mb1, styles.lh20]}>{getPreviewMessage()}</Text>
</View>
{!iouSettled && hasErrors && (
<Icon
src={Expensicons.DotIndicator}
fill={theme.danger}
/>
)}
</View>
</View>
{!isScanning && (numberOfRequests > 1 || hasReceipts) && (
<View style={styles.flexRow}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={[styles.textLabelSupporting, styles.mb1, styles.lh20]}>{previewSubtitle || moneyRequestComment}</Text>
<Text style={styles.textHeadline}>{getDisplayAmount()}</Text>
{ReportUtils.isSettled(props.iouReportID) && (
<View style={styles.defaultCheckmarkWrapper}>
<Icon
src={Expensicons.Checkmark}
fill={theme.iconSuccessFill}
/>
</View>
)}
</View>
</View>
)}
{shouldShowSettlementButton && (
<SettlementButton
currency={props.iouReport.currency}
policyID={props.policyID}
chatReportID={props.chatReportID}
iouReport={props.iouReport}
onPress={(paymentType) => IOU.payMoneyRequest(paymentType, props.chatReport, props.iouReport)}
enablePaymentsRoute={ROUTES.ENABLE_PAYMENTS}
addBankAccountRoute={bankAccountRoute}
shouldHidePaymentOptions={!shouldShowPayButton}
shouldShowApproveButton={shouldShowApproveButton}
style={[styles.mt3]}
kycWallAnchorAlignment={{
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
}}
paymentMethodDropdownAnchorAlignment={{
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
}}
/>
)}
{shouldShowSubmitButton && (
<Button
medium
success={props.chatReport.isOwnPolicyExpenseChat}
text={translate('common.submit')}
style={styles.mt3}
onPress={() => IOU.submitReport(props.iouReport)}
/>
)}
{!isScanning && (numberOfRequests > 1 || hasReceipts) && (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from #33874

We should have checked for a partial merchant. i.e. (none)

<View style={styles.flexRow}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={[styles.textLabelSupporting, styles.mb1, styles.lh20]}>{previewSubtitle || moneyRequestComment}</Text>
</View>
</View>
)}
{shouldShowSettlementButton && (
<SettlementButton
currency={props.iouReport.currency}
policyID={props.policyID}
chatReportID={props.chatReportID}
iouReport={props.iouReport}
onPress={(paymentType) => IOU.payMoneyRequest(paymentType, props.chatReport, props.iouReport)}
enablePaymentsRoute={ROUTES.ENABLE_PAYMENTS}
addBankAccountRoute={bankAccountRoute}
shouldHidePaymentOptions={!shouldShowPayButton}
shouldShowApproveButton={shouldShowApproveButton}
style={[styles.mt3]}
kycWallAnchorAlignment={{
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.LEFT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
}}
paymentMethodDropdownAnchorAlignment={{
horizontal: CONST.MODAL.ANCHOR_ORIGIN_HORIZONTAL.RIGHT,
vertical: CONST.MODAL.ANCHOR_ORIGIN_VERTICAL.BOTTOM,
}}
/>
)}
{shouldShowSubmitButton && (
<Button
medium
success={props.chatReport.isOwnPolicyExpenseChat}
text={translate('common.submit')}
style={styles.mt3}
onPress={() => IOU.submitReport(props.iouReport)}
/>
)}
</View>
</View>
</View>
</PressableWithoutFeedback>
</View>
</PressableWithoutFeedback>
</View>
</OfflineWithFeedback>
);
}

Expand Down
4 changes: 1 addition & 3 deletions src/libs/ReportUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2629,8 +2629,6 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa
const formattedTotal = CurrencyUtils.convertToDisplayString(storedTotal, currency);
const policy = getPolicy(policyID);

// The expense report is always created with the policy's output currency
const outputCurrency = policy?.outputCurrency ?? CONST.CURRENCY.USD;
const isFree = policy?.type === CONST.POLICY.TYPE.FREE;

// Define the state and status of the report based on whether the policy is free or paid
Expand All @@ -2644,7 +2642,7 @@ function buildOptimisticExpenseReport(chatReportID: string, policyID: string, pa
policyID,
type: CONST.REPORT.TYPE.EXPENSE,
ownerAccountID: payeeAccountID,
currency: outputCurrency,
currency,

// We don't translate reportName because the server response is always in English
reportName: `${policyName} owes ${formattedTotal}`,
Expand Down
53 changes: 36 additions & 17 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ function buildOnyxDataForMoneyRequest(
...iouReport,
lastMessageText: iouAction.message[0].text,
lastMessageHtml: iouAction.message[0].html,
...(isNewIOUReport ? {pendingFields: {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD}} : {}),
pendingFields: {
...(isNewIOUReport ? {createChat: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD} : {preview: CONST.RED_BRICK_ROAD_PENDING_ACTION.UPDATE}),
},
},
},
{
Expand Down Expand Up @@ -384,18 +386,14 @@ function buildOnyxDataForMoneyRequest(
},
]
: []),
...(isNewIOUReport
? [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
pendingFields: null,
errorFields: null,
},
},
]
: []),
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
pendingFields: null,
errorFields: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.TRANSACTION}${transaction.transactionID}`,
Expand Down Expand Up @@ -554,6 +552,7 @@ function buildOnyxDataForMoneyRequest(
* @param {String} [category]
* @param {String} [tag]
* @param {Boolean} [billable]
* @param {Object} [policy]
* @returns {Object} data
* @returns {String} data.payerEmail
* @returns {Object} data.iouReport
Expand Down Expand Up @@ -583,6 +582,7 @@ function getMoneyRequestInformation(
category = undefined,
tag = undefined,
billable = undefined,
policy = undefined,
) {
const payerEmail = OptionsListUtils.addSMSDomainIfPhoneNumber(participant.login);
const payerAccountID = Number(participant.accountID);
Expand Down Expand Up @@ -624,9 +624,10 @@ function getMoneyRequestInformation(
if (iouReport) {
if (isPolicyExpenseChat) {
iouReport = {...iouReport};

// Because of the Expense reports are stored as negative values, we substract the total from the amount
iouReport.total -= amount;
if (policy.outputCurrency === currency) {
Copy link
Contributor

@c3024 c3024 Dec 17, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we better use lodash here so that it does not crash when we make a distance request in the workspace chat because we are not passing any policy here and accessing outputCurrency on undefined will crash the app.

// Because of the Expense reports are stored as negative values, we substract the total from the amount
iouReport.total -= amount;
}
} else {
iouReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, payeeAccountID, amount, currency);
}
Expand Down Expand Up @@ -1054,6 +1055,7 @@ function updateDistanceRequest(transactionID, transactionThreadReportID, transac
* @param {String} [category]
* @param {String} [tag]
* @param {Boolean} [billable]
* @param {Object} [policy]
*/
function requestMoney(
report,
Expand All @@ -1069,12 +1071,29 @@ function requestMoney(
category = undefined,
tag = undefined,
billable = undefined,
policy = undefined,
) {
// If the report is iou or expense report, we should get the linked chat report to be passed to the getMoneyRequestInformation function
const isMoneyRequestReport = ReportUtils.isMoneyRequestReport(report);
const currentChatReport = isMoneyRequestReport ? ReportUtils.getReport(report.chatReportID) : report;
const {payerAccountID, payerEmail, iouReport, chatReport, transaction, iouAction, createdChatReportActionID, createdIOUReportActionID, reportPreviewAction, onyxData} =
getMoneyRequestInformation(currentChatReport, participant, comment, amount, currency, created, merchant, payeeAccountID, payeeEmail, receipt, undefined, category, tag, billable);
getMoneyRequestInformation(
currentChatReport,
participant,
comment,
amount,
currency,
created,
merchant,
payeeAccountID,
payeeEmail,
receipt,
undefined,
category,
tag,
billable,
policy,
);
const activeReportID = isMoneyRequestReport ? report.reportID : chatReport.reportID;

API.write(
Expand Down
3 changes: 2 additions & 1 deletion src/pages/iou/request/step/IOURequestStepConfirmation.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,10 @@ function IOURequestStepConfirmation({
transaction.category,
transaction.tag,
transaction.billable,
policy,
);
},
[report, transaction, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID],
[report, transaction, currentUserPersonalDetails.login, currentUserPersonalDetails.accountID, policy],
);

/**
Expand Down
2 changes: 2 additions & 0 deletions src/pages/iou/steps/MoneyRequestConfirmPage.js
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ function MoneyRequestConfirmPage(props) {
props.iou.category,
props.iou.tag,
props.iou.billable,
props.policy,
);
},
[
Expand All @@ -176,6 +177,7 @@ function MoneyRequestConfirmPage(props) {
props.iou.category,
props.iou.tag,
props.iou.billable,
props.policy,
],
);

Expand Down
Loading