import { JSONPath as jsonpath } from "jsonpath-plus"; import useTranslation from "next-translate/useTranslation"; import Link from "next/link"; import { useRouter } from "next/router"; import { useCallback, useEffect, useRef, useState } from "react"; import { connect } from "react-redux"; import transLookup from "../../data/lookuptranslations.json"; import { formatDates, getDocLink, hasOwn } from "../utils"; import PaginationControl from "./pagination"; import { sendGAEvent } from "@next/third-parties/google"; import { getSearchDocumentDetails, getSearchDocumentDetailsPaged, getSearchDocumentTypes } from "../../actions/services/searchService"; import { setCurrentPage } from "../../store/currentView/action"; import { setDocumentDetails } from "../../store/searchOutput/action"; import DocumentLink from "../utils/downloads"; import { useDownloadQueue } from "../utils/downloadmanager"; import resolveCrmDisplayValue from "../../lib/i18n/crmDisplay/resolveCrmDisplayValue"; const DocumentDetails = (props) => { let { t, lang } = useTranslation(); const firstRender = useRef(false); const { incidentid, caseReference, searchResultsObj, searchDetailsObj, documentDetailsObj, setDocumentDetails, setCurrentPage, docsOffline, showFilteredDocs } = props; const router = useRouter(); const { locale } = router; const appealType = props.currentView?.caseReference?.appealType; var documentDetailsArr = documentDetailsObj || []; const { startDownload, getStatus } = useDownloadQueue(3); const [selectedOption, setSelectedOption] = useState(10); const [documentTypes, setDocumentTypes] = useState([{}]); const [selectedDocumentType, setSelectedDocumentType] = useState("all"); const [selectAll, setSelectAll] = useState(false); const [checkedItems, setCheckedItems] = useState( documentTypes.reduce((acc, item) => { acc[item.pinswg_isharedocumentlocations] = false; return acc; }, {}) ); const handleCheckboxChange = (e) => { const { name, checked } = e.target; setCheckedItems((prev) => ({ ...prev, [name]: checked })); setSelectAll(false); }; const handleSelectAllChange = (e) => { const { checked } = e.target; setSelectAll(checked); // Set the 'Select All' state if (checked) { // If 'Select All' is checked, check all the other checkboxes setCheckedItems( documentTypes.reduce((acc, item) => { acc[item.pinswg_isharedocumentlocations] = true; return acc; }, {}) ); } else { clearCheckboxes(); } }; const clearCheckboxes = () => { setSelectAll(false); setCheckedItems( documentTypes.reduce((acc, item) => { acc[item.pinswg_isharedocumentlocations] = false; return acc; }, {}), setSelectedDocumentType("all"), setCurrentPage(1) ); }; let ShowDocLinks = getDocLink(docsOffline); const FilteredDocumentView = (documentDetailsArr) => { documentDetailsArr = documentDetailsArr.value; return ( <> {ShowDocLinks && (
Notice {t("case:documents-not-available-label")}
)}
{" "}

{documentDetailsObj["@odata.count"] > 1 ? t("case:documents-count-label-1") : t("case:documents-count-label-1a")}{" "} {documentDetailsObj["@odata.count"]}{" "} {documentDetailsObj["@odata.count"] > 1 ? t("case:documents-count-label-2") : t("case:documents-count-label-2a")}

{" "} <>
{documentTypes.map((item, key) => { return (
); })}
{" "}
{documentDetailsObj["@odata.count"] > 5 && (
)}
{" "}
{/* {t("case:documents-doc-type-label")} */}
{/* {t("case:documents-date-published-label")} */}
{/*
{t("case:documents-size-label")}
*/}
{showSpinnerState == true ? (
{t("common:spinner-fetching-data")} {
) : ( documentDetailsArr.map((item, key) => { let detailsObj = jsonpath({ path: "$..[?(@ && @._pinswg_documentids_value=='" + incidentid + "')]", json: item, eval: true }); detailsObj = item; return (
{showDownloadingState === key && (
{/* { */}
)} {!ShowDocLinks ? ( <> {/* { toggleDownLoadLink( key ), sendGAEvent( "event", "DownloadedFile", { caseReference: props.caseReference, filename: detailsObj.pinswg_name, } ); }} > {t( "case:documents-name-label" )} : {hasOwn( detailsObj, "pinswg_name" ) ? detailsObj.pinswg_name : ""} */} ) : ( <> {t( "case:documents-name-label" )} : {hasOwn( detailsObj, "pinswg_name" ) ? detailsObj.pinswg_name : ""} )}
{t( "case:documents-doc-type-label" )} : {resolveCrmDisplayValue({ value: detailsObj[ "pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue" ], locale, translations: transLookup })}
{t( "case:documents-date-published-label" )} : {hasOwn( detailsObj, "pinswg_latestpublisheddate" ) ? formatDates( detailsObj.pinswg_latestpublisheddate ) : ""}
{/*
{t("case:documents-size-label")}: 92
*/}
); }) )}
); }; const getDocumentTypes = useCallback(() => { getSearchDocumentTypes(props.incidentid).then((data) => { setDocumentTypes(data); }); }, [props.incidentid]); const getDocumentResults = useCallback( (pageNumber, orderBy, fieldSort) => { getSearchDocumentDetailsPaged( props.incidentid, pageNumber, orderBy, fieldSort, selectedOption, selectedDocumentType ) .then((data) => data) .then((data) => { setDocumentDetails(data); setShowSpinnerState(false); }); }, [ props.incidentid, selectedOption, selectedDocumentType, setDocumentDetails ] ); const getDocumentDetails = async () => { let docObj = await getSearchDocumentDetailsPaged( incidentid, pageNumber, orderBy, fieldSort ).then((data) => { setDocumentDetails(data); return data; }); return docObj; }; const pagesCount = Math.ceil(documentDetailsObj["@odata.count"] / 10); let currentPage = 0; currentPage = decodeURI(documentDetailsObj["@odata.nextLink"]).split( "skiptoken=" )[1]; currentPage = typeof currentPage != "undefined" ? parseInt( decodeURI(currentPage) .split('pagenumber="')[1] .slice( 0, decodeURI(currentPage) .split('pagenumber="')[1] .indexOf('"') ) - 1 ) : pagesCount; const [showSpinnerState, setShowSpinnerState] = useState(false); const [showDownloadingState, setShowDownloadingState] = useState(null); const toggleDownLoadLink = (i) => { setShowDownloadingState(i); const downloadTime = setTimeout(hideDownloadCover, 5000); downloadTime; }; function hideDownloadCover() { setShowDownloadingState(null); } let spinnerState = () => { showSpinnerState == true ? setShowSpinnerState(false) : setShowSpinnerState(true); }; const [showDocumentSearchState, setDocumentSearchState] = useState(false); const [orderByState, setOrderbyState] = useState("createdon"); const [fieldSortState, setfieldSortState] = useState("asc"); let documentSearchState = () => { showDocumentSearchState == true ? setDocumentSearchState(false) : setDocumentSearchState(true); }; const sortDataBy = (whichField) => { setOrderbyState(whichField); setfieldSortState("asc"); getDocumentResults(1, whichField, fieldSortState); whichField == orderByState ? fieldSortState == "desc" ? setfieldSortState("asc") : setfieldSortState("desc") : setfieldSortState("asc"); setCurrentPage(1); setShowSpinnerState(true); }; useEffect(() => { getDocumentTypes(); if (selectedOption) { setShowSpinnerState(true); getDocumentResults(1, orderByState, fieldSortState); } if (selectedDocumentType) { setShowSpinnerState(true); getDocumentResults( 1, orderByState, fieldSortState, selectedDocumentType ); } const docDetailsObj = async () => { let docObj = await getSearchDocumentDetails(incidentid).then( (data) => { setDocumentDetails(data); setDocumentSearchState(true); return data; } ); return docObj; }; const searchDocumentResultsObj = docDetailsObj(); }, [ incidentid, setDocumentDetails, selectedOption, selectedDocumentType, fieldSortState, orderByState, getDocumentTypes, getDocumentResults, lang ]); return (

{t("case:documents-heading-label")}

{_.isEmpty(documentDetailsObj.value) ? (

{showDocumentSearchState == false ? t("case:documents-checking-label") : appealType === 846040002 || appealType === 846040011 ? t( "case:documents-non-available-app-label" ) : t("case:documents-non-available-label")}

) : ( FilteredDocumentView(documentDetailsArr) )}
); }; const mapStateToProps = (state) => { return { ...state }; }; const mapDispatchToProps = (dispatch) => { return { setDocumentDetails: (detailsObj) => { dispatch(setDocumentDetails(detailsObj)); }, setCurrentPage: (currentPage) => { dispatch(setCurrentPage(currentPage)); } }; }; export default connect(mapStateToProps, mapDispatchToProps)(DocumentDetails);