Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve Attachment Carousel State Management in AttachmentModal and ReportAttachments #22179

Merged
merged 3 commits into from
Jul 7, 2023
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
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],
);
Julesssss marked this conversation as resolved.
Show resolved Hide resolved

/**
* 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
Loading