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 } from "../utils"; import PaginationControl from "./pagination"; import { sendGAEvent } from "@next/third-parties/google"; import { getSearchDocumentDetails, getSearchDocumentDetailsPaged, getSearchDocumentTypes, } from "../../actions"; import { setCurrentPage } from "../../store/currentView/action"; import { setDocumentDetails } from "../../store/searchOutput/action"; const DocumentDetails = (props) => { let { t } = useTranslation(); const firstRender = useRef(false); const { incidentid, caseReference, searchResultsObj, searchDetailsObj, documentDetailsObj, setDocumentDetails, setCurrentPage, docsOffline, showFilteredDocs, } = props; const router = useRouter(); const { locale } = router; var documentDetailsArr = documentDetailsObj || []; 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 = false; let checkShowDocLinks = docsOffline != false ? true : false; const getDocLink = (docsOffline) => { var startTime = docsOffline.split(",")[0]; var endTime = docsOffline.split(",")[1]; // console.log(startTime); // console.log(endTime); var currentDate = new Date(); var startDate = new Date(currentDate.getTime()); startDate.setHours(startTime.split(":")[0]); startDate.setMinutes(startTime.split(":")[1]); startDate.setSeconds(startTime.split(":")[2]); var endDate = new Date(currentDate.getTime()); endDate.setHours(endTime.split(":")[0]); endDate.setMinutes(endTime.split(":")[1]); endDate.setSeconds(endTime.split(":")[2]); // console.log(startDate); // console.log(endDate); // console.log(currentDate); var valid = startDate < currentDate && endDate > currentDate; ShowDocLinks = valid; }; checkShowDocLinks && getDocLink(docsOffline); const DocumentView = (documentDetailsArr) => { documentDetailsArr = documentDetailsArr.value; return ( <>

{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")}

{documentDetailsObj["@odata.count"] > 5 && (
)}
{ShowDocLinks && (
Notice {t("case:documents-not-available-label")}
)}
{/* {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( "$..[?(@._pinswg_documentids_value=='" + incidentid + "')]", item ); detailsObj = item; return (
{showDownloadingState === key && (
{/* { */}
)} {!ShowDocLinks ? ( { toggleDownLoadLink(key), sendGAEvent( "event", "DownloadedFile", { caseReference: props.caseReference, filename: detailsObj.pinswg_name, } ); }} > {t( "case:documents-name-label" )} : {_.has( detailsObj, "pinswg_name" ) ? detailsObj.pinswg_name : ""} ) : ( <> {t( "case:documents-name-label" )} : {_.has( detailsObj, "pinswg_name" ) ? detailsObj.pinswg_name : ""} )}
{t("case:documents-doc-type-label")} : {router.locale == "cy" ? jsonpath( '$..[?(@.value=="' + detailsObj[ "pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue" ] + '")].value_cy', transLookup ) : detailsObj[ "pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue" ] || t( "case:summary-no-date-entered-label" )}
{t( "case:documents-date-published-label" )} : {_.has( detailsObj, "pinswg_latestpublisheddate" ) ? formatDates( detailsObj.pinswg_latestpublisheddate ) : ""}
{/*
{t("case:documents-size-label")}: 92
*/}
); }) )}
); }; 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( "$..[?(@._pinswg_documentids_value=='" + incidentid + "')]", item ); detailsObj = item; return (
{showDownloadingState === key && (
{/* { */}
)} {!ShowDocLinks ? ( { toggleDownLoadLink( key ), sendGAEvent( "event", "DownloadedFile", { caseReference: props.caseReference, filename: detailsObj.pinswg_name, } ); }} > {t( "case:documents-name-label" )} : {_.has( detailsObj, "pinswg_name" ) ? detailsObj.pinswg_name : ""} ) : ( <> {t( "case:documents-name-label" )} : {_.has( detailsObj, "pinswg_name" ) ? detailsObj.pinswg_name : ""} )}
{t( "case:documents-doc-type-label" )} : {router.locale == "cy" ? jsonpath( '$..[?(@.value=="' + detailsObj[ "pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue" ] + '")].value_cy', transLookup ) : detailsObj[ "pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue" ] || t( "case:summary-no-date-entered-label" )}
{t( "case:documents-date-published-label" )} : {_.has( 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, ]); return (

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

{_.isEmpty(documentDetailsObj.value) ? (

{showDocumentSearchState == false ? t("case:documents-checking-label") : t("case:documents-non-available-label")}

) : showFilteredDocs == "true" ? ( FilteredDocumentView(documentDetailsArr) ) : ( DocumentView(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);