import { JSONPath as jsonpath } from "jsonpath-plus";
import _ from "lodash";
import useTranslation from "next-translate/useTranslation";
import { useRouter } from "next/router";
import { useEffect, useState } from "react";
import { connect } from "react-redux";
import transLookup from "../../data/lookuptranslations.json";
import { getRepresentations } from "../../actions/services/portalService";
import { setRepresentations } from "../../store/searchOutput/action";
import { formatDates } from "../utils";
const RepresentationList = (props) => {
const { incidentid, representationsObj, setRepresentations } = props;
let { t } = useTranslation();
const router = useRouter();
const { locale } = router;
//const [showSpinnerState, setShowSpinnerState] = useState(false);
// let spinnerState = () => {
// showSpinnerState == true
// ? setShowSpinnerState(false)
// : setShowSpinnerState(true);
// };
var representationsArr = representationsObj || [];
const RepresentationsView = (representationsArr) => {
representationsArr = representationsArr.value;
return (
<>
{representationsObj["@odata.count"] > 1
? t("case:documents-count-label-1")
: t("case:documents-count-label-1a")}{" "}
{representationsObj["@odata.count"]}{" "}
{
representationsObj["@odata.count"] > 1
? "representations " //t("case:documents-count-label-2")
: "representations " //t("case:documents-count-label-2a")
}
-
{
"Type of representation" //t("case:documents-name-label")
}
-
{t("case:documents-doc-type-label")}
-
{t("case:documents-date-published-label")}
{representationsArr.map((item, key) => {
let detailsObj = jsonpath({
path:
"$..[?(@ && @.pinswg_representationsid=='" +
incidentid +
"')]",
json: item,
eval: true
});
detailsObj = item;
console.log(detailsObj);
return (
-
{t("case:documents-name-label")}:
{
detailsObj[
"pinswg_whatkind@OData.Community.Display.V1.FormattedValue"
]
}
-
{t("case:documents-doc-type-label")}:
{router.locale == "cy"
? jsonpath({
path:
'$..[?(@ && @.value=="' +
detailsObj[
"pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue"
] +
'")].value_cy',
json: transLookup,
sanbox: {}
})
: detailsObj[
"pinswg_isharedocumentlocations@OData.Community.Display.V1.FormattedValue"
] ||
t(
"case:summary-no-date-entered-label"
)}
-
{t(
"case:documents-date-published-label"
)}
:
{formatDates(
detailsObj[
"createdon@OData.Community.Display.V1.FormattedValue"
]
)}
{/*
-
{t("case:documents-size-label")}:
92
*/}
);
})}
>
);
};
useEffect(() => {
const representationDetailsObj = async () => {
let docObj = await getRepresentations(incidentid).then((data) => {
console.log(data);
setRepresentationSearchState(true);
setRepresentations(data);
return data;
});
return docObj;
};
const representationResultsObj = representationDetailsObj();
}, [incidentid, setRepresentations]);
const pagesCount = Math.ceil(representationsObj["@odata.count"] / 10);
let currentPage = 0;
currentPage = decodeURI(representationsObj["@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);
// let spinnerState = () => {
// showSpinnerState == true
// ? setShowSpinnerState(false)
// : setShowSpinnerState(true);
// };
const [showReperesentationSearchState, setRepresentationSearchState] =
useState(false);
let representationSearchState = () => {
showReperesentationSearchState == true
? setRepresentationSearchState(false)
: setRepresentationSearchState(true);
};
return (
<>
Representations
{_.isEmpty(representationsObj) ? (
{showReperesentationSearchState == false
? "Checking for representations "
: t(
"case:documents-non-available-label"
)}
) : (
RepresentationsView(representationsArr)
)}
>
);
};
const mapStateToProps = (state) => {
return {
...state
};
};
const mapDispatchToProps = (dispatch) => {
return {
setRepresentations: (representationObj) => {
dispatch(setRepresentations(representationObj));
}
};
};
export default connect(mapStateToProps, mapDispatchToProps)(RepresentationList);