Skip to content

Commit

Permalink
Merge pull request #22179 from kidroca/kidroca/feat/attachment-modal-url
Browse files Browse the repository at this point in the history
Improve Attachment Carousel State Management in AttachmentModal and ReportAttachments
  • Loading branch information
Julesssss authored Jul 7, 2023
2 parents 5723571 + 806734f commit 776ea5d
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 5 deletions.
21 changes: 16 additions & 5 deletions src/components/AttachmentModal.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ const propTypes = {
/** Optional callback to fire when we want to do something after modal hide. */
onModalHide: PropTypes.func,

/** Optional callback to fire when we want to do something after attachment carousel changes. */
onCarouselAttachmentChange: PropTypes.func,

/** Optional original filename when uploading */
originalFileName: PropTypes.string,

Expand Down Expand Up @@ -81,13 +84,14 @@ const defaultProps = {
report: {},
onModalShow: () => {},
onModalHide: () => {},
onCarouselAttachmentChange: () => {},
};

function AttachmentModal(props) {
const [isModalOpen, setIsModalOpen] = useState(props.defaultOpen);
const [shouldLoadAttachment, setShouldLoadAttachment] = useState(false);
const [isAttachmentInvalid, setIsAttachmentInvalid] = useState(false);
const [isAuthTokenRequired] = useState(props.isAuthTokenRequired);
const [isAuthTokenRequired, setIsAuthTokenRequired] = useState(props.isAuthTokenRequired);
const [attachmentInvalidReasonTitle, setAttachmentInvalidReasonTitle] = useState(null);
const [attachmentInvalidReason, setAttachmentInvalidReason] = useState(null);
const [source, setSource] = useState(props.source);
Expand All @@ -102,14 +106,21 @@ function AttachmentModal(props) {
: undefined,
);

const onCarouselAttachmentChange = props.onCarouselAttachmentChange;

/**
* Keeps the attachment source in sync with the attachment displayed currently in the carousel.
* @param {{ source: String, isAuthTokenRequired: Boolean, file: { name: string } }} attachment
*/
const onNavigate = useCallback((attachment) => {
setSource(attachment.source);
setFile(attachment.file);
}, []);
const onNavigate = useCallback(
(attachment) => {
setSource(attachment.source);
setFile(attachment.file);
setIsAuthTokenRequired(attachment.isAuthTokenRequired);
onCarouselAttachmentChange(attachment);
},
[onCarouselAttachmentChange],
);

/**
* If our attachment is a PDF, return the unswipeable Modal type.
Expand Down
5 changes: 5 additions & 0 deletions src/pages/home/report/ReportAttachments.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import PropTypes from 'prop-types';
import AttachmentModal from '../../../components/AttachmentModal';
import Navigation from '../../../libs/Navigation/Navigation';
import * as ReportUtils from '../../../libs/ReportUtils';
import ROUTES from '../../../ROUTES';

const propTypes = {
/** Navigation route context info provided by react navigation */
Expand All @@ -30,6 +31,10 @@ function ReportAttachments(props) {
report={report}
source={source}
onModalHide={() => Navigation.dismissModal(reportID)}
onCarouselAttachmentChange={(attachment) => {
const route = ROUTES.getReportAttachmentRoute(reportID, attachment.source);
Navigation.navigate(route);
}}
/>
);
}
Expand Down

0 comments on commit 776ea5d

Please sign in to comment.