549 lines
24 KiB
JavaScript
549 lines
24 KiB
JavaScript
import Link from "next/link";
|
|
import { useRouter } from "next/router";
|
|
import useTranslation from "next-translate/useTranslation";
|
|
import jsonpath from "jsonpath";
|
|
import { useState, useEffect, useRef } from "react";
|
|
import { useDispatch, useSelector, shallowEqual, connect } from "react-redux";
|
|
import transLookup from "../../data/lookuptranslations.json";
|
|
import PaginationControl from "./pagination";
|
|
import LoadingOverlay from "react-loading-overlay";
|
|
import CryptoJS from "crypto-js";
|
|
|
|
import { setCurrentPage } from "../../store/currentView/action";
|
|
import {
|
|
getBasicSearch,
|
|
getBasicSearchDetails,
|
|
getSearchDocumentDetails,
|
|
getSearchDocumentHistory,
|
|
getSearchDocumentHistoryPaged,
|
|
getSearchDocumentDetailsPaged,
|
|
} from "../../actions";
|
|
|
|
import {
|
|
setSearchResults,
|
|
setSearchDetails,
|
|
setDocumentDetails,
|
|
setDocumentHistory,
|
|
getSearchResultsObj,
|
|
} from "../../store/searchOutput/action";
|
|
|
|
const DocumentDetails = (props) => {
|
|
let { t } = useTranslation();
|
|
const firstRender = useRef(false);
|
|
const {
|
|
incidentid,
|
|
caseReference,
|
|
searchResultsObj,
|
|
searchDetailsObj,
|
|
documentDetailsObj,
|
|
documentHistoryObj,
|
|
setDocumentDetails,
|
|
setDocumentHistory,
|
|
setCurrentPage,
|
|
} = props;
|
|
|
|
const router = useRouter();
|
|
const { locale } = router;
|
|
|
|
var documentDetailsArr = documentDetailsObj || [];
|
|
|
|
const [selectedOption, setSelectedOption] = useState(10);
|
|
|
|
const DocumentView = (documentDetailsArr) => {
|
|
documentDetailsArr = documentDetailsArr.value;
|
|
|
|
return (
|
|
<>
|
|
<p>
|
|
{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")}
|
|
</p>
|
|
{documentDetailsObj["@odata.count"] > 5 && (
|
|
<div id="recordCountSelect" className="govuk-grid-row">
|
|
<div className="govuk-form-group">
|
|
<label className="govuk-label-s " htmlFor="sort">
|
|
{t("search:searchresults-show-records-label-a")}
|
|
<select
|
|
onChange={(e) => {
|
|
setSelectedOption(e.target.value);
|
|
setCurrentPage(1);
|
|
}}
|
|
value={selectedOption}
|
|
className="govuk-select"
|
|
id="showNumberOfRecords"
|
|
name="showNumberOfRecords"
|
|
>
|
|
<option value="5">5</option>
|
|
<option value="10">10</option>
|
|
<option value="20">20</option>
|
|
<option value="30">30</option>
|
|
<option value="50">50</option>
|
|
</select>{" "}
|
|
{t("search:searchresults-show-records-label-b")}
|
|
</label>
|
|
</div>
|
|
</div>
|
|
)}
|
|
|
|
<dl className="documentList govuk-summary-list govuk-!-margin-bottom-9 results">
|
|
<div className="govuk-summary-list__row results-headings">
|
|
<dd className="govuk-summary-list__key govuk-!-font-size-16 ">
|
|
{t("case:documents-name-label")}
|
|
</dd>
|
|
<dd className="govuk-summary-list__key govuk-!-font-size-16">
|
|
{/* {t("case:documents-doc-type-label")} */}
|
|
<button
|
|
type="button"
|
|
onClick={() =>
|
|
sortDataBy("pinswg_isharedocumentlocations")
|
|
}
|
|
className={
|
|
orderByState ==
|
|
"pinswg_isharedocumentlocations"
|
|
? fieldSortState == "asc"
|
|
? `govuk-link govuk-link--no-underline govuk-!-font-weight-bold govuk-!-font-size-16 sortAsc`
|
|
: `govuk-link govuk-link--no-underline govuk-!-font-weight-bold govuk-!-font-size-16 sortDesc`
|
|
: `govuk-link govuk-link--no-underline govuk-!-font-weight-bold govuk-!-font-size-16`
|
|
}
|
|
>
|
|
{t("case:documents-doc-type-label")}
|
|
</button>
|
|
</dd>
|
|
<dd className="govuk-summary-list__key govuk-!-font-size-16">
|
|
{/* {t("case:documents-date-published-label")} */}
|
|
<button
|
|
type="button"
|
|
onClick={() => sortDataBy("createdon")}
|
|
className={
|
|
orderByState == "createdon"
|
|
? fieldSortState == "asc"
|
|
? `govuk-link govuk-link--no-underline govuk-!-font-weight-bold govuk-!-font-size-16 sortAsc`
|
|
: `govuk-link govuk-link--no-underline govuk-!-font-weight-bold govuk-!-font-size-16 sortDesc`
|
|
: `govuk-link govuk-link--no-underline govuk-!-font-weight-bold govuk-!-font-size-16`
|
|
}
|
|
>
|
|
{t("case:documents-date-published-label")}
|
|
</button>
|
|
</dd>
|
|
{/* <dd className="govuk-summary-list__key govuk-!-font-size-16">
|
|
{t("case:documents-size-label")}
|
|
</dd> */}
|
|
</div>
|
|
{showSpinnerState == true ? (
|
|
<div className="govuk-summary-list__row">
|
|
<div className="govuk-!-margin-top-4 govuk-!-margin-bottom-4 govuk-!-font-weight-bold govuk-!-font-size-20 ">
|
|
{t("common:spinner-fetching-data")}
|
|
<img
|
|
className="data-loading-icon"
|
|
src="/assets/images/loading.gif"
|
|
alt={
|
|
router.locale == "cy"
|
|
? "Nôl data"
|
|
: "Fetching data"
|
|
}
|
|
/>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
documentDetailsArr.map((item) => {
|
|
let detailsObj = jsonpath.query(
|
|
item,
|
|
"$..[?(@._pinswg_documentids_value=='" +
|
|
incidentid +
|
|
"')]"
|
|
);
|
|
detailsObj = item;
|
|
|
|
if (typeof detailsObj != "undefined") {
|
|
var documentHistoryArr =
|
|
documentHistoryObj || [];
|
|
return documentHistoryArr.map(
|
|
(historyItem, key) => {
|
|
let historyObj = jsonpath.query(
|
|
documentHistoryObj[key],
|
|
"$..value[?(@._pinswg_documentid_value=='" +
|
|
detailsObj.pinswg_documentid +
|
|
"')]"
|
|
);
|
|
|
|
historyObj = historyObj[0];
|
|
return !_.isEmpty(historyObj) ? (
|
|
<div
|
|
className="govuk-summary-list__row"
|
|
key={key}
|
|
>
|
|
<dd className="govuk-summary-list__value govuk-!-font-size-16 ">
|
|
{showDownloadingState ===
|
|
key && (
|
|
<div className="coverLoadingLink downloading">
|
|
{/* <img
|
|
className="data-loading-icon"
|
|
src="/assets/images/loading.gif"
|
|
alt={
|
|
router.locale ==
|
|
"cy"
|
|
? "Nôl data"
|
|
: "Fetching data"
|
|
}
|
|
/> */}
|
|
</div>
|
|
)}
|
|
|
|
<Link
|
|
scroll={false}
|
|
href={
|
|
detailsObj.pinswg_hashlink
|
|
}
|
|
>
|
|
<a
|
|
key={key}
|
|
onClick={() => {
|
|
toggleDownLoadLink(
|
|
key
|
|
);
|
|
}}
|
|
>
|
|
<span className="results-visually-hidden">
|
|
{t(
|
|
"case:documents-name-label"
|
|
)}
|
|
:
|
|
</span>
|
|
{_.has(historyObj, [
|
|
"_pinswg_documentid_value@OData.Community.Display.V1.FormattedValue",
|
|
])
|
|
? historyObj[
|
|
"_pinswg_documentid_value@OData.Community.Display.V1.FormattedValue"
|
|
]
|
|
: ""}
|
|
</a>
|
|
</Link>
|
|
</dd>
|
|
<dd className="govuk-summary-list__value govuk-!-font-size-16">
|
|
<span className="results-visually-hidden">
|
|
{t(
|
|
"case:documents-doc-type-label"
|
|
)}
|
|
:
|
|
</span>
|
|
|
|
{router.locale == "cy"
|
|
? jsonpath.query(
|
|
transLookup,
|
|
'$..[?(@.value=="' +
|
|
detailsObj[
|
|
"pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue"
|
|
] +
|
|
'")].value_cy'
|
|
)
|
|
: detailsObj[
|
|
"pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue"
|
|
] ||
|
|
t(
|
|
"case:summary-no-date-entered-label"
|
|
)}
|
|
</dd>
|
|
<dd className="govuk-summary-list__value govuk-!-font-size-16">
|
|
<span className="results-visually-hidden">
|
|
{t(
|
|
"case:documents-date-published-label"
|
|
)}
|
|
:
|
|
</span>
|
|
|
|
{_.has(historyObj, [
|
|
"pinswg_publisheddate@OData.Community.Display.V1.FormattedValue",
|
|
])
|
|
? formatDates(
|
|
historyObj.pinswg_publisheddate
|
|
)
|
|
: ""}
|
|
</dd>
|
|
{/* <dd className="govuk-summary-list__value govuk-!-font-size-16">
|
|
<span className="results-visually-hidden">
|
|
{t("case:documents-size-label")}:
|
|
</span>
|
|
92
|
|
</dd> */}
|
|
</div>
|
|
) : (
|
|
""
|
|
);
|
|
}
|
|
);
|
|
}
|
|
})
|
|
)}
|
|
</dl>
|
|
<PaginationControl
|
|
showNoOfRecords={selectedOption}
|
|
currentPage={currentPage}
|
|
searchResultsObj={documentDetailsObj}
|
|
spinnerState={spinnerState}
|
|
orderBy={orderByState}
|
|
fieldSort={fieldSortState}
|
|
getDocumentResults={getDocumentResults}
|
|
/>
|
|
</>
|
|
);
|
|
};
|
|
|
|
useEffect(() => {
|
|
if (selectedOption) {
|
|
setShowSpinnerState(true);
|
|
getDocumentResults(1, orderByState, fieldSortState);
|
|
}
|
|
|
|
const docDetailsObj = async () => {
|
|
let docObj = await getSearchDocumentDetails(incidentid)
|
|
.then((data) => {
|
|
//console.log(data);
|
|
setDocumentDetails(data);
|
|
return data;
|
|
})
|
|
.then((data) => {
|
|
let historyObj = getDocumentHistory(data.value).then(
|
|
(result) => {
|
|
setDocumentHistory(result);
|
|
setDocumentSearchState(true);
|
|
return result;
|
|
}
|
|
);
|
|
|
|
return data;
|
|
});
|
|
|
|
return docObj;
|
|
};
|
|
|
|
const getDocumentHistory = async (searchDocumentDetailsObj) => {
|
|
let historyArr = [];
|
|
searchDocumentDetailsObj = searchDocumentDetailsObj || [];
|
|
|
|
const detailsObj = searchDocumentDetailsObj.map(
|
|
(historyDetail, index) => {
|
|
if (historyDetail.pinswg_publishtoweb != true) {
|
|
console.log(historyDetail.pinswg_documentid);
|
|
} else {
|
|
historyArr.push(
|
|
getSearchDocumentHistory(
|
|
historyDetail.pinswg_documentid
|
|
)
|
|
);
|
|
}
|
|
}
|
|
);
|
|
return Promise.all(historyArr);
|
|
};
|
|
const searchDocumentResultsObj = docDetailsObj();
|
|
}, [
|
|
incidentid,
|
|
setDocumentHistory,
|
|
setDocumentDetails,
|
|
selectedOption,
|
|
fieldSortState,
|
|
orderByState,
|
|
]);
|
|
|
|
const getDocumentResults = (pageNumber, orderBy, fieldSort) => {
|
|
getSearchDocumentDetailsPaged(
|
|
props.incidentid,
|
|
pageNumber,
|
|
orderBy,
|
|
fieldSort,
|
|
selectedOption
|
|
)
|
|
.then((data) => data)
|
|
.then((data) => {
|
|
//console.log("getting details", data);
|
|
setDocumentDetails(data);
|
|
return getDocumentHistory(data.value).then((data) => {
|
|
setDocumentHistory(data);
|
|
setShowSpinnerState(false);
|
|
});
|
|
});
|
|
};
|
|
|
|
const getDocumentDetails = async () => {
|
|
let docObj = await getSearchDocumentDetailsPaged(
|
|
incidentid,
|
|
pageNumber,
|
|
orderBy,
|
|
fieldSort
|
|
)
|
|
.then((data) => {
|
|
//console.log(data);
|
|
setDocumentDetails(data);
|
|
return data;
|
|
})
|
|
.then((data) => {
|
|
let historyObj = getDocumentHistory(
|
|
data.value,
|
|
pageNumber
|
|
).then((result) => {
|
|
setDocumentHistory(result);
|
|
return result;
|
|
});
|
|
|
|
return data;
|
|
});
|
|
|
|
return docObj;
|
|
};
|
|
|
|
const getDocumentHistory = async (searchDocumentDetailsObj, pageNumber) => {
|
|
let historyArr = [];
|
|
searchDocumentDetailsObj = searchDocumentDetailsObj || [];
|
|
|
|
const detailsObj = searchDocumentDetailsObj.map(
|
|
(historyDetail, index) => {
|
|
if (historyDetail.pinswg_publishtoweb != true) {
|
|
console.log(historyDetail.pinswg_documentid);
|
|
} else {
|
|
historyArr.push(
|
|
getSearchDocumentHistoryPaged(
|
|
historyDetail.pinswg_documentid,
|
|
pageNumber
|
|
)
|
|
);
|
|
}
|
|
}
|
|
);
|
|
return Promise.all(historyArr);
|
|
};
|
|
|
|
function formatDates(dateObj) {
|
|
var dateObj = new Date(dateObj);
|
|
var dd = String(dateObj.getDate()).padStart(2, "0");
|
|
var mm = String(dateObj.getMonth() + 1).padStart(2, "0"); //January is 0!
|
|
var yyyy = dateObj.getFullYear();
|
|
|
|
return dd + "/" + mm + "/" + yyyy;
|
|
}
|
|
|
|
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);
|
|
};
|
|
|
|
return (
|
|
<>
|
|
<div>
|
|
<div className="govuk-grid-row">
|
|
<div className="govuk-grid-column-full">
|
|
<h2>{t("case:documents-heading-label")}</h2>
|
|
{_.isEmpty(documentHistoryObj) ? (
|
|
<div className="govuk-grid-row">
|
|
<div className="govuk-grid-column-two-thirds">
|
|
<h3>
|
|
{showDocumentSearchState == false
|
|
? t("case:documents-checking-label")
|
|
: t(
|
|
"case:documents-non-available-label"
|
|
)}
|
|
</h3>
|
|
</div>
|
|
</div>
|
|
) : (
|
|
DocumentView(documentDetailsArr)
|
|
)}
|
|
</div>
|
|
</div>
|
|
</div>
|
|
{/* <LoadingOverlay
|
|
active={showSpinnerState}
|
|
spinner
|
|
text={t("common:spinner-searching")}
|
|
/> */}
|
|
</>
|
|
);
|
|
};
|
|
|
|
const mapStateToProps = (state) => {
|
|
return {
|
|
...state,
|
|
};
|
|
};
|
|
|
|
const mapDispatchToProps = (dispatch) => {
|
|
return {
|
|
setDocumentDetails: (detailsObj) => {
|
|
dispatch(setDocumentDetails(detailsObj));
|
|
},
|
|
setDocumentHistory: (historyObj) => {
|
|
dispatch(setDocumentHistory(historyObj));
|
|
},
|
|
setCurrentPage: (currentPage) => {
|
|
dispatch(setCurrentPage(currentPage));
|
|
},
|
|
};
|
|
};
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(DocumentDetails);
|