Skip to content

Commit

Permalink
Refactoring the reject transaction to cancel money request
Browse files Browse the repository at this point in the history
  • Loading branch information
mountiny committed Oct 11, 2022
1 parent dfb5bca commit ff85464
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 35 deletions.
24 changes: 4 additions & 20 deletions src/components/ReportTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ import React, {Component} from 'react';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import ONYXKEYS from '../ONYXKEYS';
import styles from '../styles/styles';
import * as IOU from '../libs/actions/IOU';
import reportActionPropTypes from '../pages/home/report/reportActionPropTypes';
Expand All @@ -27,17 +25,8 @@ const propTypes = {
/** Can this transaction be rejected? */
canBeRejected: PropTypes.bool,

/** Text label for the reject transaction button */
rejectButtonLabelText: PropTypes.string.isRequired,

/* Onyx Props */

/** List of transactionIDs in process of rejection */
/* eslint-disable-next-line react/no-unused-prop-types, react/require-default-props */
transactionsBeingRejected: PropTypes.shape({
/** IOUTransactionID that's being rejected */
transactionID: PropTypes.bool,
}),
/** Type of the reject transaction button */
rejectButtonType: PropTypes.oneOf(['decline', 'cancel']).isRequired,
};

const defaultProps = {
Expand Down Expand Up @@ -88,10 +77,9 @@ class ReportTransaction extends Component {
<View style={[styles.flexRow, styles.justifyContentStart]}>
<Button
small
text={this.props.rejectButtonLabelText}
text={this.props.translate(`common.${this.props.rejectButtonType}`)}
style={[styles.mb3, styles.chatItemComposeSecondaryRowOffset]}
onPress={this.rejectTransaction}
isLoading={this.isBeingRejected()}
/>
</View>
)}
Expand All @@ -102,8 +90,4 @@ class ReportTransaction extends Component {

ReportTransaction.defaultProps = defaultProps;
ReportTransaction.propTypes = propTypes;
export default withOnyx({
transactionsBeingRejected: {
key: ONYXKEYS.TRANSACTIONS_BEING_REJECTED,
},
})(ReportTransaction);
export default ReportTransaction;
90 changes: 90 additions & 0 deletions src/libs/actions/IOU.js
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,95 @@ function rejectTransaction({
});
}

/**
* Cancels a transaction in iouReport. Declining and cancelling transactions are done via the same Auth command.
*
* @param {Object} params
* @param {Number} params.reportID
* @param {String} params.transactionID
*/
function cancelMoneyRequest(iouReportID, transactionID) {
const chatReport = lodashGet(report, 'reportID', null) ? report : ReportUtils.buildOptimisticChatReport(participants);
Onyx.merge(ONYXKEYS.TRANSACTIONS_BEING_REJECTED, {
[transactionID]: true,
});
let iouReport;
if (chatReport.hasOutstandingIOU) {
iouReport = iouReports[`${ONYXKEYS.COLLECTION.REPORT_IOUS}${chatReport.iouReportID}`];
iouReport.total += amount;
} else {
iouReport = ReportUtils.buildOptimisticIOUReport(recipientEmail, debtorEmail, amount, chatReport.reportID, currency, 'en');
}
const newSequenceNumber = Report.getMaxSequenceNumber(chatReport.reportID) + 1;
chatReport.maxSequenceNumber = newSequenceNumber;
const optimisticReportAction = ReportUtils.buildOptimisticIOUReportAction(
newSequenceNumber,
'create',
amount,
currency,
comment,
'',
'',
iouReport.reportID,
debtorEmail,
);

const optimisticData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
value: {
[optimisticReportAction.sequenceNumber]: {
...optimisticReportAction,
pendingAction: CONST.RED_BRICK_ROAD_PENDING_ACTION.ADD,
},
},
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT}${chatReport.reportID}`,
value: chatReport,
},
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_IOUS}${iouReport.reportID}`,
value: iouReport,
},
];
const successData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
value: {
[optimisticReportAction.sequenceNumber]: {
pendingAction: null,
},
},
},
];
const failureData = [
{
onyxMethod: CONST.ONYX.METHOD.MERGE,
key: `${ONYXKEYS.COLLECTION.REPORT_ACTIONS}${chatReport.reportID}`,
value: {
[optimisticReportAction.sequenceNumber]: {
...optimisticReportAction,
pendingAction: null,
error: Localize.translateLocal('iou.error.genericCreateFailureMessage'),
},
},
},
];
API.write('CancelMoneyRequest', {
transactionID,
iouReportID: iouReport.reportID,
comment: '',
clientID: optimisticReportAction.sequenceNumber,
cancelMoneyRequestReportActionID: optimisticReportAction.reportActionID,
}, {optimisticData, successData, failureData});
Navigation.navigate(ROUTES.getReportRoute(chatReport.reportID));
}

/**
* Sets IOU'S selected currency
*
Expand Down Expand Up @@ -321,6 +410,7 @@ function payIOUReport({
}

export {
cancelMoneyRequest,
createIOUTransaction,
createIOUSplit,
createIOUSplitGroup,
Expand Down
12 changes: 0 additions & 12 deletions src/libs/deprecatedAPI.js
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,6 @@ function PreferredLocale_Update(parameters) {
return Network.post(commandName, parameters);
}

/**
* @param {Object} parameters
* @param {Number} parameters.reportID
* @param {String} parameters.transactionID
* @returns {Promise}
*/
function RejectTransaction(parameters) {
const commandName = 'RejectTransaction';
requireParameters(['reportID', 'transactionID'], parameters, commandName);
return Network.post(commandName, parameters);
}

/**
* @param {Object} parameters
* @param {Number} parameters.reportID
Expand Down
4 changes: 1 addition & 3 deletions src/pages/iou/IOUTransactions.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,9 +90,7 @@ class IOUTransactions extends Component {
action={reportAction}
key={reportAction.sequenceNumber}
canBeRejected={canBeRejected}
rejectButtonLabelText={isCurrentUserTransactionCreator
? this.props.translate('common.cancel')
: this.props.translate('common.decline')}
rejectButtonType={isCurrentUserTransactionCreator ? 'cancel' : 'decline'}
/>
);
})}
Expand Down

0 comments on commit ff85464

Please sign in to comment.