Skip to content
131 changes: 61 additions & 70 deletions src/components/ReportActionItem/IOUPreview.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React from 'react';
import {
View,
ActivityIndicator,
TouchableWithoutFeedback,
} from 'react-native';
import PropTypes from 'prop-types';
Expand Down Expand Up @@ -34,6 +33,7 @@ const propTypes = {
onPayButtonPressed: PropTypes.func,

/** The active IOUReport, used for Onyx subscription */
// eslint-disable-next-line react/no-unused-prop-types
iouReportID: PropTypes.string.isRequired,
Comment thread
Julesssss marked this conversation as resolved.
Resolved

/** The associated chatReport */
Expand Down Expand Up @@ -117,11 +117,6 @@ const IOUPreview = (props) => {

// Pay button should only be visible to the manager of the report.
const isCurrentUserManager = managerEmail === sessionEmail;
const reportIsLoading = _.isEmpty(props.iouReport);

if (reportIsLoading) {
Report.fetchIOUReportByID(props.iouReportID, props.chatReportID);
}

const managerName = lodashGet(props.personalDetails, [managerEmail, 'firstName'], '')
|| Str.removeSMSDomain(managerEmail);
Expand All @@ -137,72 +132,68 @@ const IOUPreview = (props) => {
return (
<TouchableWithoutFeedback onPress={props.onPreviewPressed}>
<View style={[styles.iouPreviewBox, ...props.containerStyles]}>
{reportIsLoading
? <ActivityIndicator style={styles.iouPreviewBoxLoading} color={themeColors.text} />
: (
<OfflineWithFeedback
pendingAction={props.pendingAction}
errors={props.walletTerms.errors}
onClose={() => {
PaymentMethods.clearWalletTermsError();
Report.clearIOUError(props.chatReportID);
}}
errorRowStyles={[styles.mbn1]}
>
<View>
<View style={[styles.flexRow]}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={styles.h1}>
{cachedTotal}
</Text>
{!props.iouReport.hasOutstandingIOU && (
<View style={styles.iouPreviewBoxCheckmark}>
<Icon src={Expensicons.Checkmark} fill={themeColors.iconSuccessFill} />
</View>
)}
<OfflineWithFeedback
pendingAction={props.pendingAction}
errors={props.walletTerms.errors}
onClose={() => {
PaymentMethods.clearWalletTermsError();
Report.clearIOUError(props.chatReportID);
}}
errorRowStyles={[styles.mbn1]}
>
<View>
<View style={[styles.flexRow]}>
<View style={[styles.flex1, styles.flexRow, styles.alignItemsCenter]}>
<Text style={styles.h1}>
{cachedTotal}
</Text>
{!props.iouReport.hasOutstandingIOU && (
<View style={styles.iouPreviewBoxCheckmark}>
<Icon src={Expensicons.Checkmark} fill={themeColors.iconSuccessFill} />
</View>
<View style={styles.iouPreviewBoxAvatar}>
<MultipleAvatars
icons={[managerAvatar, ownerAvatar]}
secondAvatarStyle={[
styles.secondAvatarInline,
props.isHovered
? styles.iouPreviewBoxAvatarHover
: undefined,
]}
avatarTooltips={avatarTooltip}
/>
</View>
</View>
{isCurrentUserManager
? (
<Text>
{props.iouReport.hasOutstandingIOU
? props.translate('iou.youowe', {owner: ownerName})
: props.translate('iou.youpaid', {owner: ownerName})}
</Text>
)
: (
<Text>
{props.iouReport.hasOutstandingIOU
? props.translate('iou.owesyou', {manager: managerName})
: props.translate('iou.paidyou', {manager: managerName})}
</Text>
)}
{(isCurrentUserManager
&& !props.shouldHidePayButton
&& props.iouReport.stateNum === CONST.REPORT.STATE_NUM.PROCESSING && (
<Button
style={styles.mt4}
onPress={props.onPayButtonPressed}
text={props.translate('iou.pay')}
success
medium
/>
))}
)}
</View>
<View style={styles.iouPreviewBoxAvatar}>
<MultipleAvatars
icons={[managerAvatar, ownerAvatar]}
secondAvatarStyle={[
styles.secondAvatarInline,
props.isHovered
? styles.iouPreviewBoxAvatarHover
: undefined,
]}
avatarTooltips={avatarTooltip}
/>
</View>
</OfflineWithFeedback>
)}
</View>
{isCurrentUserManager
? (
<Text>
{props.iouReport.hasOutstandingIOU
? props.translate('iou.youowe', {owner: ownerName})
: props.translate('iou.youpaid', {owner: ownerName})}
</Text>
)
: (
<Text>
{props.iouReport.hasOutstandingIOU
? props.translate('iou.owesyou', {manager: managerName})
: props.translate('iou.paidyou', {manager: managerName})}
</Text>
)}
{(isCurrentUserManager
&& !props.shouldHidePayButton
&& props.iouReport.stateNum === CONST.REPORT.STATE_NUM.PROCESSING && (
<Button
style={styles.mt4}
onPress={props.onPayButtonPressed}
text={props.translate('iou.pay')}
success
medium
/>
))}
</View>
</OfflineWithFeedback>
</View>
</TouchableWithoutFeedback>
);
Expand Down
75 changes: 0 additions & 75 deletions src/libs/actions/Report.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,12 @@ import Navigation from '../Navigation/Navigation';
import * as ActiveClientManager from '../ActiveClientManager';
import Visibility from '../Visibility';
import ROUTES from '../../ROUTES';
import * as DeprecatedAPI from '../deprecatedAPI';
import * as API from '../API';
import CONFIG from '../../CONFIG';
import CONST from '../../CONST';
import Log from '../Log';
import * as LoginUtils from '../LoginUtils';
import * as ReportUtils from '../ReportUtils';
import Growl from '../Growl';
import * as Localize from '../Localize';
import DateUtils from '../DateUtils';
import * as ReportActionsUtils from '../ReportActionsUtils';
import * as OptionsListUtils from '../OptionsListUtils';
Expand Down Expand Up @@ -181,76 +178,6 @@ function getSimplifiedIOUReport(reportData, chatReportID) {
};
}

/**
* Given IOU and chat report ID fetches most recent IOU data from DeprecatedAPI.
*
* @param {Number} iouReportID
* @param {Number} chatReportID
* @returns {Promise}
*/
function fetchIOUReport(iouReportID, chatReportID) {
return DeprecatedAPI.Get({
returnValueList: 'reportStuff',
reportIDList: iouReportID,
shouldLoadOptionalKeys: true,
includePinnedReports: true,
}).then((response) => {
if (!response) {
return;
}
if (response.jsonCode !== 200) {
console.error(response.message);
return;
}
const iouReportData = response.reports[iouReportID];
if (!iouReportData) {
// IOU data for a report will be missing when the IOU report has already been paid.
// This is expected and we return early as no further processing can be done.
return;
}
return getSimplifiedIOUReport(iouReportData, chatReportID);

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is still used in syncChatAndIOUReports. We can remove it once PayWithWallet is refactored

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🎉

}).catch((error) => {
Log.hmmm('[Report] Failed to populate IOU Collection:', error.message);
});
}

/**
* Given IOU object, save the data to Onyx.
*
* @param {Object} iouReportObject
* @param {Number} iouReportObject.stateNum
* @param {Number} iouReportObject.total
* @param {Number} iouReportObject.reportID
*/
function setLocalIOUReportData(iouReportObject) {
const iouReportKey = `${ONYXKEYS.COLLECTION.REPORT}${iouReportObject.reportID}`;
Onyx.merge(iouReportKey, iouReportObject);
}

/**
* Fetch the iouReport and persist the data to Onyx.
*
* @param {Number} iouReportID - ID of the report we are fetching
* @param {Number} chatReportID - associated chatReportID, set as an iouReport field
* @param {Boolean} [shouldRedirectIfEmpty=false] - Whether to redirect to Active Report Screen if IOUReport is empty
* @returns {Promise}
*/
function fetchIOUReportByID(iouReportID, chatReportID, shouldRedirectIfEmpty = false) {
return fetchIOUReport(iouReportID, chatReportID)
.then((iouReportObject) => {
if (!iouReportObject && shouldRedirectIfEmpty) {
Growl.error(Localize.translateLocal('notFound.iouReportNotFound'));
Navigation.navigate(ROUTES.REPORT);
return;
}
if (!iouReportObject) {
return;
}
setLocalIOUReportData(iouReportObject);
return iouReportObject;
});
}

/**
* Get the private pusher channel name for a Report.
*
Expand Down Expand Up @@ -1416,7 +1343,6 @@ Onyx.connect({
});

export {
fetchIOUReportByID,
addComment,
addAttachment,
reconnect,
Expand All @@ -1431,7 +1357,6 @@ export {
handleUserDeletedLinks,
saveReportActionDraft,
deleteReportComment,
getSimplifiedIOUReport,
syncChatAndIOUReports,
navigateToConciergeChat,
setReportWithDraft,
Expand Down
1 change: 0 additions & 1 deletion src/pages/iou/IOUDetailsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,6 @@ class IOUDetailsModal extends Component {
<View style={[styles.flex1, styles.justifyContentBetween]}>
<ScrollView contentContainerStyle={styles.iouDetailsContainer}>
<IOUPreview
iou={this.props.iouReport}
chatReportID={this.props.route.params.chatReportID}
iouReportID={this.props.route.params.iouReportID}
shouldHidePayButton
Expand Down