src/applications/claims-status/containers/FilesPage.jsx
import React from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import { Toggler } from '~/platform/utilities/feature-toggles';
import { clearNotification } from '../actions';
import AskVAToDecide from '../components/AskVAToDecide';
import ClaimDetailLayout from '../components/ClaimDetailLayout';
import AdditionalEvidencePage from '../components/claim-files-tab/AdditionalEvidencePage';
import ClaimFileHeader from '../components/claim-files-tab/ClaimFileHeader';
import DocumentsFiled from '../components/claim-files-tab/DocumentsFiled';
import withRouter from '../utils/withRouter';
import {
claimAvailable,
isClaimOpen,
setPageFocus,
setTabDocumentTitle,
} from '../utils/helpers';
import { setUpPage, isTab } from '../utils/page';
// CONSTANTS
const NEED_ITEMS_STATUS = 'NEEDED_FROM_';
const FIRST_GATHERING_EVIDENCE_PHASE = 'GATHERING_OF_EVIDENCE';
class FilesPage extends React.Component {
componentDidMount() {
const { claim, location } = this.props;
// Only set the document title at mount-time if the claim is already available.
if (claimAvailable(claim)) setTabDocumentTitle(claim, 'Files');
if (location?.hash === '') {
setTimeout(() => {
const { lastPage, loading } = this.props;
setPageFocus(lastPage, loading);
}, 100);
}
}
componentDidUpdate(prevProps) {
const { claim, lastPage, loading } = this.props;
if (!loading && prevProps.loading && !isTab(lastPage)) {
setUpPage(false);
}
// Set the document title when loading completes.
// If loading was successful it will display a title specific to the claim.
// Otherwise it will display a default title of "Files for Your Claim".
if (loading !== prevProps.loading) {
setTabDocumentTitle(claim, 'Files');
}
}
componentWillUnmount() {
this.props.clearNotification();
}
getPageContent() {
const { claim } = this.props;
// Return null if the claim/ claim.attributes dont exist
if (!claimAvailable(claim)) {
return null;
}
const {
closeDate,
status,
supportingDocuments,
trackedItems,
evidenceWaiverSubmitted5103,
claimPhaseDates,
} = claim.attributes;
const isOpen = isClaimOpen(status, closeDate);
const waiverSubmitted = evidenceWaiverSubmitted5103;
const showDecision =
claimPhaseDates.latestPhaseType === FIRST_GATHERING_EVIDENCE_PHASE &&
!waiverSubmitted;
const documentsTurnedIn = trackedItems.filter(
item => !item.status.startsWith(NEED_ITEMS_STATUS),
);
documentsTurnedIn.push(...supportingDocuments);
documentsTurnedIn.sort((a, b) => {
if (a.date === b.date) return -1;
return a.date > b.date ? -1 : 1;
});
return (
<div className="claim-files">
<ClaimFileHeader isOpen={isOpen} />
<AdditionalEvidencePage />
<Toggler toggleName={Toggler.TOGGLE_NAMES.cst5103UpdateEnabled}>
<Toggler.Disabled>
{showDecision && <AskVAToDecide />}
</Toggler.Disabled>
</Toggler>
<DocumentsFiled claim={claim} />
</div>
);
}
render() {
const { claim, loading, message } = this.props;
let content = null;
if (!loading && claim) {
content = this.getPageContent();
}
return (
<ClaimDetailLayout
claim={claim}
loading={loading}
clearNotification={this.props.clearNotification}
currentTab="Files"
message={message}
>
{content}
</ClaimDetailLayout>
);
}
}
function mapStateToProps(state) {
const claimsState = state.disability.status;
return {
loading: claimsState.claimDetail.loading,
claim: claimsState.claimDetail.detail,
message: claimsState.notifications.message,
lastPage: claimsState.routing.lastPage,
};
}
const mapDispatchToProps = {
clearNotification,
};
FilesPage.propTypes = {
claim: PropTypes.object,
clearNotification: PropTypes.func,
lastPage: PropTypes.string,
loading: PropTypes.bool,
location: PropTypes.object,
message: PropTypes.shape({
body: PropTypes.oneOfType([PropTypes.object, PropTypes.string]),
title: PropTypes.string,
type: PropTypes.string,
}),
};
export default withRouter(
connect(
mapStateToProps,
mapDispatchToProps,
)(FilesPage),
);
export { FilesPage };