Skip to content

Commit

Permalink
Merge pull request #27426 from tienifr/fix/26828
Browse files Browse the repository at this point in the history
fix: 26828 Receipt modal closes when API response updates image url
  • Loading branch information
luacmartins authored Sep 18, 2023
2 parents 88d36fd + 726f9fa commit ef82772
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ function extractAttachmentsFromReport(report, reportActions) {
isAuthTokenRequired: true,
file: {name: transaction.filename},
isReceipt: true,
transactionID,
});
return;
}
Expand Down
19 changes: 16 additions & 3 deletions src/components/Attachments/AttachmentCarousel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import BlockingView from '../../BlockingViews/BlockingView';
import * as Illustrations from '../../Icon/Illustrations';
import variables from '../../../styles/variables';
import * as DeviceCapabilities from '../../../libs/DeviceCapabilities';
import * as ReportActionsUtils from '../../../libs/ReportActionsUtils';

const viewabilityConfig = {
// To facilitate paging through the attachments, we want to consider an item "viewable" when it is
Expand All @@ -38,13 +39,25 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
const [activeSource, setActiveSource] = useState(source);
const [shouldShowArrows, setShouldShowArrows, autoHideArrows, cancelAutoHideArrows] = useCarouselArrows();

const compareImage = useCallback(
(attachment) => {
if (attachment.isReceipt) {
const action = ReportActionsUtils.getParentReportAction(report);
const transactionID = _.get(action, ['originalMessage', 'IOUTransactionID']);
return attachment.transactionID === transactionID;
}
return attachment.source === source;
},
[source, report],
);

useEffect(() => {
const attachmentsFromReport = extractAttachmentsFromReport(report, reportActions);

const initialPage = _.findIndex(attachmentsFromReport, (a) => a.source === source);
const initialPage = _.findIndex(attachmentsFromReport, compareImage);

// Dismiss the modal when deleting an attachment during its display in preview.
if (initialPage === -1 && _.find(attachments, (a) => a.source === source)) {
if (initialPage === -1 && _.find(attachments, compareImage)) {
Navigation.dismissModal();
} else {
setPage(initialPage);
Expand All @@ -57,7 +70,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, setDownl
if (!_.isUndefined(attachmentsFromReport[initialPage])) onNavigate(attachmentsFromReport[initialPage]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [report, reportActions, source]);
}, [reportActions, compareImage]);

/**
* Updates the page state when the user navigates between attachments
Expand Down
19 changes: 16 additions & 3 deletions src/components/Attachments/AttachmentCarousel/index.native.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import * as Illustrations from '../../Icon/Illustrations';
import variables from '../../../styles/variables';
import compose from '../../../libs/compose';
import withLocalize from '../../withLocalize';
import * as ReportActionsUtils from '../../../libs/ReportActionsUtils';

function AttachmentCarousel({report, reportActions, source, onNavigate, onClose, setDownloadButtonVisibility, translate}) {
const pagerRef = useRef(null);
Expand All @@ -27,13 +28,25 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, onClose,
const [isPinchGestureRunning, setIsPinchGestureRunning] = useState(true);
const [shouldShowArrows, setShouldShowArrows, autoHideArrows, cancelAutoHideArrows] = useCarouselArrows();

const compareImage = useCallback(
(attachment) => {
if (attachment.isReceipt) {
const action = ReportActionsUtils.getParentReportAction(report);
const transactionID = _.get(action, ['originalMessage', 'IOUTransactionID']);
return attachment.transactionID === transactionID;
}
return attachment.source === source;
},
[source, report],
);

useEffect(() => {
const attachmentsFromReport = extractAttachmentsFromReport(report, reportActions);

const initialPage = _.findIndex(attachmentsFromReport, (a) => a.source === source);
const initialPage = _.findIndex(attachmentsFromReport, compareImage);

// Dismiss the modal when deleting an attachment during its display in preview.
if (initialPage === -1 && _.find(attachments, (a) => a.source === source)) {
if (initialPage === -1 && _.find(attachments, compareImage)) {
Navigation.dismissModal();
} else {
setPage(initialPage);
Expand All @@ -46,7 +59,7 @@ function AttachmentCarousel({report, reportActions, source, onNavigate, onClose,
if (!_.isUndefined(attachmentsFromReport[initialPage])) onNavigate(attachmentsFromReport[initialPage]);
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [report, reportActions, source]);
}, [reportActions, compareImage]);

/**
* Updates the page state when the user navigates between attachments
Expand Down

0 comments on commit ef82772

Please sign in to comment.