Skip to content
Merged
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
30 changes: 6 additions & 24 deletions src/components/ReportActionItemIOUAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import ReportActionItemIOUQuote from './ReportActionItemIOUQuote';
import ReportActionPropTypes from '../pages/home/report/ReportActionPropTypes';
import ReportActionItemIOUPreview from './ReportActionItemIOUPreview';
import Navigation from '../libs/Navigation/Navigation';
import compose from '../libs/compose';
import ROUTES from '../ROUTES';

const propTypes = {
Expand All @@ -25,25 +24,17 @@ const propTypes = {
/** The participants of this report */
participants: PropTypes.arrayOf(PropTypes.string),
}),

/** iouReport associated with this iouAction */
iouReport: PropTypes.shape({
/** Does the iouReport have an outstanding IOU? */
hasOutstandingIOU: PropTypes.bool,
}),
};

const defaultProps = {
chatReport: {
participants: [],
},
iouReport: {},
};

const ReportActionItemIOUAction = ({
action,
chatReportID,
iouReport,
isMostRecentIOUReportAction,
}) => {
const launchDetailsModal = () => {
Expand All @@ -56,9 +47,7 @@ const ReportActionItemIOUAction = ({
shouldShowViewDetailsLink={Boolean(action.originalMessage.IOUReportID)}
onViewDetailsPressed={launchDetailsModal}
/>
{isMostRecentIOUReportAction

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.

I don't get why this is not needed anymore, wouldn't this mean that we would show the preview for a paid IOU? I thought we did not want that?

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.

Oh, is it because of this?

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.

Yeah exactly. That logic had to move to within the Preview, as once the iouReport is paid we cannot guarantee it will be stored locally -- and the iouReport will be null

&& iouReport.hasOutstandingIOU
&& Boolean(action.originalMessage.IOUReportID) && (
{isMostRecentIOUReportAction && Boolean(action.originalMessage.IOUReportID) && (
<ReportActionItemIOUPreview
iouReportID={action.originalMessage.IOUReportID}
chatReportID={chatReportID}
Expand All @@ -73,15 +62,8 @@ ReportActionItemIOUAction.propTypes = propTypes;
ReportActionItemIOUAction.defaultProps = defaultProps;
ReportActionItemIOUAction.displayName = 'ReportActionItemIOUAction';

export default compose(
withOnyx({
chatReport: {
key: ({chatReportID}) => `${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`,
},
}),
withOnyx({
iouReport: {
key: ({chatReport}) => `${ONYXKEYS.COLLECTION.REPORT_IOUS}${chatReport.iouReportID}`,
},
}),
)(ReportActionItemIOUAction);
export default withOnyx({
chatReport: {
key: ({chatReportID}) => `${ONYXKEYS.COLLECTION.REPORT}${chatReportID}`,
},
})(ReportActionItemIOUAction);
13 changes: 12 additions & 1 deletion src/components/ReportActionItemIOUPreview.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import withLocalize, {withLocalizePropTypes} from './withLocalize';
import {fetchIOUReportByID} from '../libs/actions/Report';
import themeColors from '../styles/themes/default';
import Icon from './Icon';
import CONST from '../CONST';
import {Checkmark} from './Icon/Expensicons';

const propTypes = {
Expand Down Expand Up @@ -82,6 +83,14 @@ const ReportActionItemIOUPreview = ({
onPayButtonPressed,
translate,
}) => {
// Usually the parent determines whether the IOU Preview is displayed. But as the iouReport total cannot be known
// until it is stored locally, we need to make this check within the Component after retrieving it. This allows us
// to handle the loading UI from within this Component instead of needing to declare it within each parent, which
// would duplicate and complicate the logic

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.

Tbh, I don't really follow what logic would be complicated by this. It seems like the logic is already kind of weird and confusing and this comment doesn't explain much about why it was set up that way.

e.g. this component perhaps should not be responsible for fetching the iou at all...

this comment made me look into how we are doing it and... I think it's a pretty bad practice to be making an API call like this when the component renders... (instead of e.g. using a lifecycle method)

https://github.com/Expensify/Expensify.cash/blob/ec0070aafa0374c2d02bdf9db757e98f10b59aaf/src/components/ReportActionItemIOUPreview.js#L102-L104

Not sure if anything needs to be done right now and I won't block on it (since the issue seems important to address), but feels like a code smell to me.

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.

Yeah, when making these changes it became apparent that the parent should perhaps be handling the loading state. Also, only one use of the comp has any requirement for displaying the preview now, so it might be possible to further simplify the shouldDisplay logic.

I didn't want this to turn into a large refactor and risk longer delays, so I've created an issue to refactor this component that I'll make external.

if (iouReport.total === 0) {
return null;
}

const sessionEmail = lodashGet(session, 'email', null);
const managerEmail = iouReport.managerEmail || '';
const ownerEmail = iouReport.ownerEmail || '';
Expand Down Expand Up @@ -131,7 +140,9 @@ const ReportActionItemIOUPreview = ({
? translate('iou.owes', {manager: managerName, owner: ownerName})
: translate('iou.paid', {manager: managerName, owner: ownerName})}
</Text>
{(isCurrentUserManager && !shouldHidePayButton && (
{(isCurrentUserManager
&& !shouldHidePayButton
&& iouReport.stateNum === CONST.REPORT.STATE_NUM.PROCESSING && (
<TouchableOpacity
style={[styles.buttonSmall, styles.buttonSuccess, styles.mt4]}
onPress={onPayButtonPressed}
Expand Down
14 changes: 6 additions & 8 deletions src/pages/iou/IOUDetailsModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -211,14 +211,12 @@ class IOUDetailsModal extends Component {
{reportIsLoading ? <ActivityIndicator color={themeColors.text} /> : (
<View style={[styles.flex1, styles.justifyContentBetween]}>
<ScrollView contentContainerStyle={styles.iouDetailsContainer}>
{(this.props.iouReport.hasOutstandingIOU) && (
<ReportActionItemIOUPreview
iou={this.props.iouReport}
chatReportID={Number(this.props.route.params.chatReportID)}
iouReportID={Number(this.props.route.params.iouReportID)}
shouldHidePayButton
/>
)}
<ReportActionItemIOUPreview
iou={this.props.iouReport}
chatReportID={Number(this.props.route.params.chatReportID)}
iouReportID={Number(this.props.route.params.iouReportID)}
shouldHidePayButton
/>
<IOUTransactions
chatReportID={Number(this.props.route.params.chatReportID)}
iouReportID={Number(this.props.route.params.iouReportID)}
Expand Down