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 } from "../../utils";
import PaginationControl from "../../../components/case/pagination";
import DocumentLink from "../../utils/downloads";
import { useDownloadQueue } from "../../utils/downloadmanager";
import { sendGAEvent } from "@next/third-parties/google";
import {
getSearchDocumentDetails,
getSearchDocumentDetailsPaged,
getSearchDocumentTypes
} from "../../../actions/services/searchService";
import { getNewDocumentsPaged } from "../../../actions/services/adminService";
import { setCurrentPage } from "../../../store/currentView/action";
import { setDocumentDetails } from "../../../store/searchOutput/action";
const getDocumentTypes = [
{
"Value": 846040009,
"Label": "Main Party - Pre-Submission Docs"
},
{
"Value": 846040000,
"Label": "Main Party - Submission Document"
},
{
"Value": 846040001,
"Label": "Main Party - Questionnaire"
},
{
"Value": 846040002,
"Label": "Main Party - Statements"
},
{
"Value": 846040003,
"Label": "Main Party - Comments"
},
{
"Value": 846040004,
"Label": "Main Party - Written Statements of Evidence"
},
{
"Value": 846040010,
"Label": "Main Party - Environmental Statements"
},
{
"Value": 846040005,
"Label": "Interested Party Representations"
},
{
"Value": 846040006,
"Label": "Correspondence"
},
{
"Value": 846040008,
"Label": "Inspector Notes"
},
{
"Value": 846040007,
"Label": "Final Decision/Report"
},
{
"Value": 846040011,
"Label": "Post Decision Correspondence"
},
{
"Value": 846040012,
"Label": "High Court Challenge Correspondence"
},
{
"Value": 846040013,
"Label": "Event - Correspondence"
},
{
"Value": 846040014,
"Label": "Event - Documents"
},
{
"Value": 846040015,
"Label": "Pre-Application - General"
},
{
"Value": 846040016,
"Label": "Pre-Application - EIA Screening"
},
{
"Value": 846040017,
"Label": "Pre-Application - EIA Scoping"
},
{
"Value": 846040018,
"Label": "EIA Screening"
},
{
"Value": 846040019,
"Label": "EIA Scoping"
},
{
"Value": 846040020,
"Label": "Pre-Application Services Documents"
},
{
"Value": 846040021,
"Label": "Notification"
},
{
"Value": 846040022,
"Label":
"Submission - Environmental Statement - written statement and NTS"
},
{
"Value": 846040023,
"Label": "Submission - Environmental Statement - Appendices"
},
{
"Value": 846040024,
"Label": "Submission - Environmental Statement - Figures"
},
{
"Value": 846040025,
"Label": "Submission - Draft Infrastructure Consent Orders"
},
{
"Value": 846040026,
"Label": "Submission - Compulsory Acquisition Information"
},
{
"Value": 846040027,
"Label": "Submission - Application Documents"
},
{
"Value": 846040028,
"Label": "Examination Notices"
},
{
"Value": 846040029,
"Label": "Local/Marine Impact Reports"
},
{
"Value": 846040030,
"Label": "Submission - Further Information"
},
{
"Value": 846040031,
"Label": "Formal Variation requests"
},
{
"Value": 846040032,
"Label": "Further Information - Interested Party Representations"
},
{
"Value": 846040033,
"Label": "Hearing Statements"
},
{
"Value": 846040034,
"Label": "Events - Recordings"
},
{
"Value": 846040035,
"Label": "Internal Correspondence"
},
{
"Value": 846040036,
"Label": "Consultation Response"
}
];
const getOrigin = [
{
"Value": 846040000,
"Label": "MDU"
},
{
"Value": 846040001,
"Label": "Portal"
},
{
"Value": 846040002,
"Label": "Manual"
}
];
const DocumentsTab = (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;
var documentDetailsArr = documentDetailsObj || [];
const { startDownload, getStatus } = useDownloadQueue(3);
const [selectedOption, setSelectedOption] = useState(10);
const [selectedWeeks, setSelectedWeeks] = useState(1);
const [documentTypes, setDocumentTypes] = useState(getDocumentTypes);
const [documentOrigin, setDocumentOrigin] = useState(getOrigin);
const [selectedDocumentType, setSelectedDocumentType] = useState("all");
const [selectedDocumentOrigin, setSelectedDocumentOrigin] = useState("all");
const [selectAll, setSelectAll] = useState(false);
const [selectOriginAll, setSelectOriginAll] = useState(false);
const [checkedItems, setCheckedItems] = useState(
documentTypes.reduce((acc, item) => {
acc[item.pinswg_isharedocumentlocations] = false;
return acc;
}, {})
);
const [checkedOriginItems, setCheckedOriginItems] = useState(
documentOrigin.reduce((acc, item) => {
acc[item.pinswg_origin] = false;
return acc;
}, {})
);
const handleCheckboxChange = (e) => {
const { name, checked } = e.target;
setCheckedItems((prev) => ({
...prev,
[name]: checked
}));
setSelectAll(false);
};
const handleCheckboxOriginChange = (e) => {
const { name, checked } = e.target;
setCheckedOriginItems((prev) => ({
...prev,
[name]: checked
}));
setSelectOriginAll(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 handleSelectOriginAllChange = (e) => {
const { checked } = e.target;
setSelectOriginAll(checked); // Set the 'Select All' state
if (checked) {
// If 'Select All' is checked, check all the other checkboxes
setCheckedOriginItems(
documentOrigin.reduce((acc, item) => {
acc[item.pinswg_origin] = true;
return acc;
}, {})
);
} else {
clearCheckboxes();
}
};
const clearCheckboxes = () => {
setSelectAll(false);
setSelectOriginAll(false);
setCheckedItems(
documentTypes.reduce((acc, item) => {
acc[item.pinswg_isharedocumentlocations] = false;
return acc;
}, {}),
setSelectedDocumentType("all"),
setCurrentPage(1)
);
setCheckedOriginItems(
documentOrigin.reduce((acc, item) => {
acc["origin_" + item.pinswg_origin] = false;
return acc;
}, {}),
setSelectedDocumentOrigin("all")
);
};
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")}
Document Type
{" "}
Document Origin
{" "}
{documentDetailsObj["@odata.count"] > 5 && (
)}
{_.isEmpty(documentDetailsObj.value) ? (
{/* {showDocumentSearchState == false
? t("case:documents-checking-label")
: t(
"case:documents-non-available-label"
)} */}
No documents found
) : (
<>
-
-
{/* {t("case:documents-doc-type-label")} */}
-
{/* {t("case:documents-date-published-label")} */}
-
{/* {t("case:documents-date-published-label")} */}
Case
-
{/* {t("case:documents-date-published-label")} */}
Origin
-
{/* {t("case:documents-date-published-label")} */}
Published
{/* -
{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 ? (
detailsObj.pinswg_publishtoweb ? (
// {
// 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-name-label"
)}
:
{_.has(
detailsObj,
"pinswg_name"
)
? detailsObj.pinswg_name
: ""}
>
)}
-
{t(
"case:documents-doc-type-label"
)}
:
{router.locale == "cy"
? jsonpath({
path:
'$..[?(@ && @.value=="' +
detailsObj[
"pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue"
] +
'")].value_cy',
json: transLookup,
eval: true
})
: detailsObj[
"pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue"
] ||
t(
"case:summary-no-date-entered-label"
)}
-
{t(
"case:documents-date-published-label"
)}
:
{_.has(
detailsObj,
"createdon"
)
? new Date(
detailsObj.createdon
).toLocaleString()
: ""}
-
{detailsObj.publishtoweb ==
true ? (
{
detailsObj[
"_pinswg_documentids_value@OData.Community.Display.V1.FormattedValue"
]
}
) : (
detailsObj[
"_pinswg_documentids_value@OData.Community.Display.V1.FormattedValue"
]
)}
-
{
detailsObj[
"pinswg_origin@OData.Community.Display.V1.FormattedValue"
]
}
-
{
detailsObj[
"pinswg_publishtoweb@OData.Community.Display.V1.FormattedValue"
]
}
{/*
-
{t("case:documents-size-label")}:
92
*/}
);
})
)}
>
)}
>
);
};
const getDocumentResults = useCallback(
(pageNumber, orderBy, fieldSort) => {
getNewDocumentsPaged(
pageNumber,
orderBy,
fieldSort,
selectedOption,
selectedDocumentType,
selectedDocumentOrigin,
selectedWeeks
)
.then((data) => data)
.then((data) => {
setDocumentDetails(data);
setShowSpinnerState(false);
});
},
[
selectedOption,
selectedDocumentType,
selectedWeeks,
selectedDocumentOrigin,
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("desc");
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(() => {
if (selectedOption) {
setShowSpinnerState(true);
getDocumentResults(1, orderByState, fieldSortState);
}
if (selectedWeeks) {
setShowSpinnerState(true);
getDocumentResults(1, orderByState, fieldSortState);
}
if (selectedDocumentType) {
setShowSpinnerState(true);
getDocumentResults(
1,
orderByState,
fieldSortState,
selectedDocumentType
);
}
const docDetailsObj = async () => {
let docObj = await getNewDocumentsPaged(
pageNumber,
orderBy,
fieldSort,
selectedOption,
selectedDocumentType,
selectedWeeks
).then((data) => {
setDocumentDetails(data);
setDocumentSearchState(true);
return data;
});
return docObj;
};
//const searchDocumentResultsObj = docDetailsObj();
}, [
incidentid,
setDocumentDetails,
selectedOption,
selectedDocumentType,
fieldSortState,
orderByState,
getDocumentResults,
lang,
selectedWeeks
]);
function countFieldValue(obj, field, targetValue) {
let count = 0;
function traverse(node) {
if (Array.isArray(node)) {
node.forEach(traverse);
} else if (node !== null && typeof node === "object") {
// If this object has the field, check it
if (node.hasOwnProperty(field) && node[field] === targetValue) {
count++;
}
// Keep traversing children
Object.values(node).forEach(traverse);
}
}
traverse(obj);
return count;
}
return (
{t("case:documents-heading-label")}
{/*
MDU :{" "}
{countFieldValue(
documentDetailsObj,
"pinswg_origin",
846040000
)}{" "}
/ Portal :{" "}
{countFieldValue(
documentDetailsObj,
"pinswg_origin",
846040001
)}
*/}
{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)(DocumentsTab);