Skip to content
Closed
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion src/components/ReportActionItem/MoneyRequestAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import _ from 'underscore';
import React from 'react';
import PropTypes from 'prop-types';
import {withOnyx} from 'react-native-onyx';
import lodashGet from 'lodash/get';
import ONYXKEYS from '../../ONYXKEYS';
import CONST from '../../CONST';
import {withNetwork} from '../OnyxProvider';
Expand Down Expand Up @@ -69,7 +70,7 @@ const defaultProps = {
};

const MoneyRequestAction = (props) => {
const hasMultipleParticipants = props.chatReport.participants.length > 1;
const hasMultipleParticipants = lodashGet(props.chatReport, 'participants.length', 0) > 1;
const onIOUPreviewPressed = () => {
if (hasMultipleParticipants) {
Navigation.navigate(ROUTES.getReportParticipantsRoute(props.chatReportID));
Expand Down
125 changes: 65 additions & 60 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment
};

// Note: The created action must be optimistically generated before the IOU action so there's no chance that the created action appears after the IOU action in the chat
const optimisticCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(payeeEmail);
const optimisticReportAction = ReportUtils.buildOptimisticIOUReportAction(
const optimisticCreatedActionForChat = ReportUtils.buildOptimisticCreatedReportAction(payeeEmail);
const optimisticCreatedActionForIOU = ReportUtils.buildOptimisticCreatedReportAction(payeeEmail);
const optimisticIOUAction = ReportUtils.buildOptimisticIOUReportAction(
CONST.IOU.REPORT_ACTION_TYPE.CREATE,
amount,
currency,
Expand All @@ -131,8 +132,6 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment
value: {
...chatReport,
lastReadTime: DateUtils.getDBTime(),
lastMessageText: optimisticReportAction.message[0].text,
lastMessageHtml: optimisticReportAction.message[0].html,
hasOutstandingIOU: moneyRequestReport.total !== 0,
iouReportID: moneyRequestReport.reportID,
},
Expand All @@ -141,23 +140,28 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment
const optimisticIOUReportData = {
onyxMethod: chatReport.hasOutstandingIOU ? Onyx.METHOD.MERGE : Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${moneyRequestReport.reportID}`,
value: moneyRequestReport,
value: {
...moneyRequestReport,
lastMessageText: optimisticIOUAction.message[0].text,
lastMessageHtml: optimisticIOUAction.message[0].html,
},
};

const optimisticReportActionsData = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${moneyRequestReport.reportID}`,
value: {
[optimisticReportAction.reportActionID]: optimisticReportAction,
...(chatReport.iouReportID ? {} : {[optimisticCreatedActionForIOU.reportActionID]: optimisticCreatedActionForIOU}),
[optimisticIOUAction.reportActionID]: optimisticIOUAction,
},
};

let chatReportSuccessData = {};
const reportActionsSuccessData = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${moneyRequestReport.reportID}`,
value: {
[optimisticReportAction.reportActionID]: {
[optimisticIOUAction.reportActionID]: {
pendingAction: null,
},
},
Expand All @@ -173,9 +177,9 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment

const reportActionsFailureData = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${moneyRequestReport.reportID}`,
value: {
[optimisticReportAction.reportActionID]: {
[optimisticIOUAction.reportActionID]: {
errors: {
[DateUtils.getMicroseconds()]: Localize.translateLocal('iou.error.genericCreateFailureMessage'),
},
Expand Down Expand Up @@ -210,12 +214,12 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment
};

// Then add an optimistic created action
optimisticReportActionsData.value[optimisticCreatedAction.reportActionID] = optimisticCreatedAction;
reportActionsSuccessData.value[optimisticCreatedAction.reportActionID] = {pendingAction: null};
optimisticReportActionsData.value[optimisticCreatedActionForChat.reportActionID] = optimisticCreatedActionForChat;
reportActionsSuccessData.value[optimisticCreatedActionForChat.reportActionID] = {pendingAction: null};

// Failure data should feature red brick road
reportActionsFailureData.value[optimisticCreatedAction.reportActionID] = {pendingAction: null};
reportActionsFailureData.value[optimisticReportAction.reportActionID] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD};
reportActionsFailureData.value[optimisticCreatedActionForChat.reportActionID] = {pendingAction: null};
reportActionsFailureData.value[optimisticIOUAction.reportActionID] = {pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD};
}

const optimisticData = [optimisticChatReportData, optimisticIOUReportData, optimisticReportActionsData, optimisticTransactionData];
Expand All @@ -238,8 +242,8 @@ function requestMoney(report, amount, currency, payeeEmail, participant, comment
iouReportID: moneyRequestReport.reportID,
chatReportID: chatReport.reportID,
transactionID: optimisticTransaction.transactionID,
reportActionID: optimisticReportAction.reportActionID,
createdReportActionID: isNewChat ? optimisticCreatedAction.reportActionID : 0,
reportActionID: optimisticIOUAction.reportActionID,
createdReportActionID: isNewChat ? optimisticCreatedActionForChat.reportActionID : 0,
},
{optimisticData, successData, failureData},
);
Expand Down Expand Up @@ -649,7 +653,7 @@ function deleteMoneyRequest(chatReportID, iouReportID, moneyRequestAction, shoul

// Get the amount we are deleting
const amount = moneyRequestAction.originalMessage.amount;
const optimisticReportAction = ReportUtils.buildOptimisticIOUReportAction(
const optimisticIOUAction = ReportUtils.buildOptimisticIOUReportAction(
CONST.IOU.REPORT_ACTION_TYPE.DELETE,
amount,
moneyRequestAction.originalMessage.currency,
Expand All @@ -660,19 +664,19 @@ function deleteMoneyRequest(chatReportID, iouReportID, moneyRequestAction, shoul
iouReportID,
);

const currentUserEmail = optimisticReportAction.actorEmail;
const currentUserEmail = optimisticIOUAction.actorEmail;
const updatedIOUReport = IOUUtils.updateIOUOwnerAndTotal(iouReport, currentUserEmail, amount, moneyRequestAction.originalMessage.currency, CONST.IOU.REPORT_ACTION_TYPE.DELETE);
chatReport.lastMessageText = optimisticReportAction.message[0].text;
chatReport.lastMessageHtml = optimisticReportAction.message[0].html;
iouReport.lastMessageText = optimisticIOUAction.message[0].text;
iouReport.lastMessageHtml = optimisticIOUAction.message[0].html;
chatReport.hasOutstandingIOU = updatedIOUReport.total !== 0;

const optimisticData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`,
value: {
[optimisticReportAction.reportActionID]: {
...optimisticReportAction,
[optimisticIOUAction.reportActionID]: {
...optimisticIOUAction,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
},
Expand All @@ -696,9 +700,9 @@ function deleteMoneyRequest(chatReportID, iouReportID, moneyRequestAction, shoul
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`,
value: {
[optimisticReportAction.reportActionID]: {
[optimisticIOUAction.reportActionID]: {
pendingAction: null,
},
},
Expand All @@ -707,9 +711,9 @@ function deleteMoneyRequest(chatReportID, iouReportID, moneyRequestAction, shoul
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReportID}`,
value: {
[optimisticReportAction.reportActionID]: {
[optimisticIOUAction.reportActionID]: {
errors: {
[DateUtils.getMicroseconds()]: Localize.translateLocal('iou.error.genericDeleteFailureMessage'),
},
Expand All @@ -720,15 +724,17 @@ function deleteMoneyRequest(chatReportID, iouReportID, moneyRequestAction, shoul
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`,
value: {
lastMessageText: chatReports[`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`].lastMessageText,
lastMessageHtml: chatReports[`${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`].lastMessageHtml,
hasOutstandingIOU: iouReport.total !== 0,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReportID}`,
value: iouReport,
value: {
...iouReport,
lastMessageText: iouReport.lastMessageText,
lastMessageHtml: iouReport.lastMessageHtml,
},
},
{
onyxMethod: Onyx.METHOD.SET,
Expand All @@ -742,7 +748,7 @@ function deleteMoneyRequest(chatReportID, iouReportID, moneyRequestAction, shoul
{
transactionID,
chatReportID,
reportActionID: optimisticReportAction.reportActionID,
reportActionID: optimisticIOUAction.reportActionID,
iouReportID: updatedIOUReport.reportID,
},
{optimisticData, successData, failureData},
Expand Down Expand Up @@ -822,7 +828,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType

// Note: The created action must be optimistically generated before the IOU action so there's no chance that the created action appears after the IOU action in the chat
const optimisticCreatedAction = ReportUtils.buildOptimisticCreatedReportAction(recipientEmail);
const optimisticIOUReportAction = ReportUtils.buildOptimisticIOUReportAction(
const optimisticIOUAction = ReportUtils.buildOptimisticIOUReportAction(
CONST.IOU.REPORT_ACTION_TYPE.PAY,
amount,
currency,
Expand All @@ -840,33 +846,32 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
value: {
...chatReport,
lastReadTime: DateUtils.getDBTime(),
lastVisibleActionCreated: optimisticIOUReportAction.created,
lastMessageText: optimisticIOUReportAction.message[0].text,
lastMessageHtml: optimisticIOUReportAction.message[0].html,
},
};
const optimisticIOUReportData = {
onyxMethod: Onyx.METHOD.SET,
key: `${ONYXKEYS.COLLECTION.REPORT}${optimisticIOUReport.reportID}`,
value: optimisticIOUReport,
value: {
...optimisticIOUReport,
lastMessageText: optimisticIOUAction.message[0].text,
lastMessageHtml: optimisticIOUAction.message[0].html,
lastVisibleActionCreated: optimisticIOUAction.created,
},
};
const optimisticReportActionsData = {
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticIOUReport.reportID}`,
value: {
[optimisticIOUReportAction.reportActionID]: {
...optimisticIOUReportAction,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
[optimisticIOUAction.reportActionID]: optimisticIOUAction,
},
};

const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticIOUReport.reportID}`,
value: {
[optimisticIOUReportAction.reportActionID]: {
[optimisticIOUAction.reportActionID]: {
pendingAction: null,
},
},
Expand All @@ -881,9 +886,9 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${optimisticIOUReport.reportID}`,
value: {
[optimisticIOUReportAction.reportActionID]: {
[optimisticIOUAction.reportActionID]: {
errors: {
[DateUtils.getMicroseconds()]: Localize.translateLocal('iou.error.other'),
},
Expand Down Expand Up @@ -933,7 +938,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
optimisticReportActionsData.value[optimisticCreatedAction.reportActionID] = optimisticCreatedAction;

// If we're going to fail to create the report itself, let's not have redundant error messages for the IOU
failureData[0].value[optimisticIOUReportAction.reportActionID] = {pendingAction: null};
failureData[0].value[optimisticIOUAction.reportActionID] = {pendingAction: null};
}

const optimisticData = [optimisticChatReportData, optimisticIOUReportData, optimisticReportActionsData, optimisticTransactionData];
Expand All @@ -942,7 +947,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
params: {
iouReportID: optimisticIOUReport.reportID,
chatReportID: chatReport.reportID,
reportActionID: optimisticIOUReportAction.reportActionID,
reportActionID: optimisticIOUAction.reportActionID,
paymentMethodType,
transactionID: optimisticTransaction.transactionID,
newIOUReportDetails,
Expand All @@ -963,7 +968,7 @@ function getSendMoneyParams(report, amount, currency, comment, paymentMethodType
*/
function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMethodType) {
const optimisticTransaction = TransactionUtils.buildOptimisticTransaction(iouReport.total, iouReport.currency, iouReport.reportID);
const optimisticIOUReportAction = ReportUtils.buildOptimisticIOUReportAction(
const optimisticIOUAction = ReportUtils.buildOptimisticIOUReportAction(
CONST.IOU.REPORT_ACTION_TYPE.PAY,
iouReport.total,
iouReport.currency,
Expand All @@ -982,19 +987,16 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho
value: {
...chatReport,
lastReadTime: DateUtils.getDBTime(),
lastVisibleActionCreated: optimisticIOUReportAction.created,
lastMessageText: optimisticIOUReportAction.message[0].text,
lastMessageHtml: optimisticIOUReportAction.message[0].html,
hasOutstandingIOU: false,
iouReportID: null,
},
},
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`,
value: {
[optimisticIOUReportAction.reportActionID]: {
...optimisticIOUReportAction,
[optimisticIOUAction.reportActionID]: {
...optimisticIOUAction,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
},
Expand All @@ -1004,6 +1006,9 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho
key: `${ONYXKEYS.COLLECTION.REPORT}${iouReport.reportID}`,
value: {
...iouReport,
lastVisibleActionCreated: optimisticIOUAction.created,
lastMessageText: optimisticIOUAction.message[0].text,
lastMessageHtml: optimisticIOUAction.message[0].html,
hasOutstandingIOU: false,
stateNum: CONST.REPORT.STATE_NUM.SUBMITTED,
},
Expand All @@ -1018,9 +1023,9 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho
const successData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`,
value: {
[optimisticIOUReportAction.reportActionID]: {
[optimisticIOUAction.reportActionID]: {
pendingAction: null,
},
},
Expand All @@ -1037,9 +1042,9 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho
const failureData = [
{
onyxMethod: Onyx.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${iouReport.reportID}`,
value: {
[optimisticIOUReportAction.reportActionID]: {
[optimisticIOUAction.reportActionID]: {
pendingAction: null,
errors: {
[DateUtils.getMicroseconds()]: Localize.translateLocal('iou.error.other'),
Expand All @@ -1063,7 +1068,7 @@ function getPayMoneyRequestParams(chatReport, iouReport, recipient, paymentMetho
params: {
iouReportID: iouReport.reportID,
chatReportID: chatReport.reportID,
reportActionID: optimisticIOUReportAction.reportActionID,
reportActionID: optimisticIOUAction.reportActionID,
paymentMethodType,
},
optimisticData,
Expand Down
4 changes: 2 additions & 2 deletions src/pages/iou/IOUDetailsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ const propTypes = {
const defaultProps = {
iou: {},
reportActions: {},
iouReport: undefined,
iouReport: {},
session: {
email: null,
},
Expand Down Expand Up @@ -186,7 +186,7 @@ class IOUDetailsModal extends Component {
<IOUPreview
chatReportID={this.props.route.params.chatReportID}
iouReportID={this.props.route.params.iouReportID}
isBillSplit={this.props.chatReport.participants.length > 1}
isBillSplit={lodashGet(this.props.chatReport, 'participants.length', 0) > 1}
isIOUAction={false}
pendingAction={pendingAction}
/>
Expand Down
Loading