Files
pedwfrontend/components/myportal/topthree.js
T
2024-06-05 19:51:43 +01:00

487 lines
22 KiB
JavaScript

import Link from "next/link";
import _ from "lodash";
import { useRouter } from "next/router";
import useTranslation from "next-translate/useTranslation";
import { setCurrentReference } from "../../store/currentView/action";
import { connect } from "react-redux";
import { parseCookies } from "nookies";
import {
getWatchedCasesProxy,
getPortalModuleDetailsProxy,
deleteWatchedCases,
deleteAwaitingSubmissions,
getAwaitingSubmissionProxy,
} from "../../actions";
import { JSONPath as jsonpath } from "jsonpath-plus";
import transLookup from "../../data/lookuptranslations.json";
import {
setWatchedCases,
setWatchedCasesDetails,
} from "../../store/watchedCases/action";
import {
setAwaitingSubmission,
setAwaitingSubmissionDetails,
} from "../../store/awaitingSubmission/action";
import { getFormCollectionByID } from "../../components/utils";
import {
setSearchDetails,
setSearchResults,
} from "../../store/searchOutput/action";
const TopThree = (props) => {
let { t } = useTranslation();
const {
currentView,
showTopThree,
showDetails,
setCurrentReference,
topThreeType,
setWatchedCases,
setWatchedCasesDetails,
setAwaitingSubmission,
setAwaitingSubmissionDetails,
setSearchResults,
setSearchDetails,
showLoginCheck,
} = props;
const router = useRouter();
const { locale } = router;
let topthreeRow = [];
let showTopThreeArr;
showTopThreeArr = props.myReps ? showTopThree : showTopThree.value;
Object.keys(showTopThreeArr).map((key, index) => {
let createdDate = parseInt(showTopThreeArr[key].createdon);
showTopThreeArr[key].createdDate = createdDate;
});
showTopThreeArr.sort(function compare(a, b) {
var dateA = new Date(a.createdDate);
var dateB = new Date(b.createdDate);
return dateB - dateA;
});
const raisedDate = (datePart) => {
var dateObj = new Date(datePart);
return dateObj.toLocaleString();
};
const deleteItem = (caseID, topThreeType) => {
let cookies = parseCookies();
//console.log("sssss", caseID, topThreeType);
topThreeType == "watchedCases" &&
deleteWatchedCases(caseID)
.then((data) => data)
.then(() => {
getWatchedCasesProxy(cookies.pinsUser).then((data) => {
setWatchedCases(data),
getDetails(data, "myWatchedCases").then((data) => {
setWatchedCasesDetails(data);
});
});
});
topThreeType == "awaitingSubmission" &&
deleteAwaitingSubmissions(caseID)
.then((data) => {
return data;
})
.then(() => {
getAwaitingSubmissionProxy(cookies.pinsUser).then(
(data) => {
setAwaitingSubmission(data),
getDetails(data, "awaitingSubmission").then(
(data) => {
//console.log(
// "submission get details:",
// data
// );
setAwaitingSubmissionDetails(data);
}
);
}
);
});
};
const getDetails = (resultsObj, detailsType) => {
//console.log(resultsObj);
let detailsArr = [];
resultsObj = resultsObj.value;
const detailsObj = resultsObj.map((searchDetail, index) => {
if (searchDetail.pinswg_appealcasetype == null) {
console.log(
detailsType != "myWatchedCases"
? searchDetail.ticketnumber
: searchDetail[
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
]
);
} else {
detailsArr.push(
getPortalModuleDetailsProxy(
getFormCollectionByID(
searchDetail.pinswg_appealcasetype
).LogicalCollectionName,
detailsType != "myWatchedCases"
? searchDetail.ticketnumber
: searchDetail[
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
]
)
);
}
});
//console.log(detailsArr);
return Promise.all(detailsArr);
};
const getIncidentId = (topThreeType, objArr) => {
switch (topThreeType) {
case "myCases":
return objArr.incidentid;
break;
case "watchedCases":
return objArr._pinswg_watchedcase_value;
break;
case "myRepresentations":
return objArr.ticketnumber;
break;
case "awaitingSubmission":
return objArr.ticketnumber;
break;
default:
// code block
}
};
let topThreeOnlyArr = showTopThreeArr.slice(0, 3);
Object.keys(topThreeOnlyArr).map((key, index) => {
topthreeRow.push(
<div className="cardModuleItem" key={index}>
<div className="cardModuleDetails">
<div className="cardModuleReference">
<b>{t("myportal:case-reference")}:</b>
{topThreeType == "myCases" &&
showTopThreeArr[key].pinswg_publishtoweb === false ? (
<span>
{showTopThreeArr[key].ticketnumber}{" "}
<b> - {t("myportal:appeal-pending-label")}</b>
</span>
) : (
<Link
href={
topThreeType == "awaitingSubmission" ||
topThreeType == "awaitingSubmissionFromBlob"
? router.locale == "cy"
? "/fymhorth/parhauigyflwyno"
: "/myportal" +
"/" +
getFormCollectionByID(
showTopThreeArr[key]
.pinswg_appealcasetype
).UrlName +
"?lpa=" +
showTopThreeArr[key][
"_pinswg_associatedlpa_value"
] +
"&apt=" +
showTopThreeArr[key]
.pinswg_appealcasetype +
"&casereference=" +
showTopThreeArr[key]
.pinswg_title +
"&inid=" +
showTopThreeArr[key].incidentid
: router.locale == "cy"
? "/fymhorth/achos/" +
showTopThreeArr[key].pinswg_title +
"?key=" +
topThreeType
: "/myportal/case/" +
showTopThreeArr[key].pinswg_title +
"?key=" +
topThreeType
}
className="govuk-link--no-underline"
data-id={index}
onClick={() => {
setSearchResults(
props.props[topThreeType][topThreeType]
),
setSearchDetails(
props.props[topThreeType][
topThreeType + "Details"
]
),
setCurrentReference({
"ticketnumber":
showTopThreeArr[key]
.ticketnumber,
"currentReference":
topThreeType != "watchedCases"
? topThreeType !=
"myRepresentations"
? showTopThreeArr[key]
.ticketnumber
: showTopThreeArr[key][
"_pinswg_case_value@OData.Community.Display.V1.FormattedValue"
]
: showTopThreeArr[key][
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
],
"currentType": topThreeType,
"incidentid": getIncidentId(
topThreeType,
showTopThreeArr[key]
),
"appealType":
showTopThreeArr[key]
.pinswg_appealcasetype,
});
}}
>
{topThreeType != "watchedCases"
? topThreeType != "myRepresentations"
? showTopThreeArr[key].ticketnumber
: showTopThreeArr[key][
"_pinswg_case_value@OData.Community.Display.V1.FormattedValue"
]
: showTopThreeArr[key][
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
]}{" "}
</Link>
)}
</div>
<div>
<b>{t("myportal:appeal-type-label")}:</b>{" "}
{router.locale == "cy"
? jsonpath(
'$..[?(@.value=="' +
showTopThreeArr[key][
"pinswg_appealcasetype@OData.Community.Display.V1.FormattedValue"
] +
'")].value_cy',
transLookup
)
: showTopThreeArr[key][
"pinswg_appealcasetype@OData.Community.Display.V1.FormattedValue"
] || ""}
</div>
{/* )} */}
{/* {topThreeType != "awaitingSubmission" ? ( */}
<div className="carModuleAddress">
<b>{t("myportal:case-address")}:</b>{" "}
{_.isEmpty(showDetails)
? t("myportal:case-address-not-entered")
: _.isEmpty(showDetails[index])
? t("myportal:case-address-not-entered")
: _.isEmpty(showDetails[index].value[0])
? t("myportal:case-address-not-entered")
: _.isEmpty(
showDetails[index].value[0]
.pinswg_siteaddressline1
)
? t("myportal:case-address-not-entered")
: showDetails[index].value[0]
.pinswg_siteaddressline1 +
(!_.isEmpty(
showDetails[index].value[0]
.pinswg_siteaddressline2
)
? ", " +
showDetails[index].value[0]
.pinswg_siteaddressline2
: "") +
(!_.isEmpty(
showDetails[index].value[0]
.pinswg_siteaddresstown
)
? ", " +
showDetails[index].value[0]
.pinswg_siteaddresstown
: "")}
</div>
{showTopThreeArr[key].pinswg_appellantagent != null && (
<div>
{showTopThreeArr[key].pinswg_appellantagent !=
846040000 && (
<b>{t("myportal:raised-by-label")}</b>
)}
{showTopThreeArr[key].pinswg_appellantagent !=
846040000 &&
(showTopThreeArr[key].pinswg_agentcompanyname !=
null &&
showTopThreeArr[key]
.pinswg_agentcompanyname + ", ") +
showTopThreeArr[key].pinswg_agentfirstname +
" " +
showTopThreeArr[key].pinswg_agentlastname}
</div>
)}
{showTopThreeArr[key].pinswg_appellantfirstname != null && (
<div>
<b>{t("myportal:on-behalf-of-label")}: </b>
{showTopThreeArr[key].pinswg_appellantfirstname +
" " +
showTopThreeArr[key].pinswg_appellantlastname}
</div>
)}
{router.locale == "cy"
? jsonpath(
'$..[?(@.value=="' +
showTopThreeArr[key][
"_pinswg_associatedlpa_value@OData.Community.Display.V1.FormattedValue"
] +
'")].value_cy',
transLookup
)
: showTopThreeArr[key][
"_pinswg_associatedlpa_value@OData.Community.Display.V1.FormattedValue"
] || ""}
{topThreeType == "awaitingSubmission" ? (
<div className="carModuleAddress">
Case Incomplete, not submitted
</div>
) : (
""
)}
<div className="cardModuleReference">
<b>{t("myportal:date-raised")}:</b>
{" "}
{raisedDate(showTopThreeArr[key].createdon)}
</div>{" "}
{topThreeType == "watchedCases" &&
props.myReps &&
showTopThreeArr[key].pinswg_representationsubmitted !=
null && (
<div className="cardModuleReference">
<b>{t("myportal:representation-submitted")}:</b>
{" "}
{raisedDate(
showTopThreeArr[key]
.pinswg_representationsubmitted
)}
</div>
)}
{topThreeType == "watchedCases" &&
props.myReps &&
showTopThreeArr[key].pinswg_representationtype !=
null && (
<div className="cardModuleReference">
<b>{t("myportal:representation-type")}:</b>
{" "}
{showTopThreeArr[key].pinswg_representationtype}
</div>
)}
</div>
{topThreeType == "watchedCases" && !props.myReps && (
<div className="">
<button
title={t("case:you-have-stopped-watching-label")}
className=" cardModuleRemoveCase govuk-link govuk-link--no-underline govuk-link--inverse "
onClick={() => {
confirm(
t("case:you-have-stopped-watching-label") +
showTopThreeArr[key][
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
]
) &&
deleteItem(
showTopThreeArr[key].pinswg_watchlistid,
"watchedCases"
);
}}
>
&mdash;
</button>
</div>
)}
{topThreeType == "awaitingSubmission" && (
<div className="">
<button
title={t("case:do-you-want-to-delete-case-label")}
className=" cardModuleRemoveCase govuk-link govuk-link--no-underline govuk-link--inverse "
onClick={() => {
confirm(
t("case:do-you-want-to-delete-case-label") +
showTopThreeArr[key].ticketnumber +
"?\n" +
t(
"case:do-you-want-to-delete-case-disclaimer-label"
)
) &&
deleteItem(
showTopThreeArr[key].incidentid,
"awaitingSubmission"
);
}}
>
&mdash;
</button>
</div>
)}
</div>
);
});
let noRecordsLabel = "";
switch (topThreeType) {
case "watchedCases":
noRecordsLabel = t("myportal:norecords-watched-cases");
break;
case "myRepresentations":
noRecordsLabel = t("myportal:norecords-representations");
break;
case "myCases":
noRecordsLabel = t("myportal:norecords-cases");
break;
case "awaitingSubmission":
noRecordsLabel = t("myportal:norecords-awaiting-submissions");
break;
}
return (
<>
{topthreeRow.length == 0 ? (
<div>{noRecordsLabel}</div>
) : (
topthreeRow
)}
</>
);
};
const mapDispatchToProps = (dispatch) => {
return {
setCurrentReference: (currentReference) => {
dispatch(setCurrentReference(currentReference));
},
setWatchedCases: (watchedCases) => {
dispatch(setWatchedCases(watchedCases));
},
setWatchedCasesDetails: (watchedCasesDetails) => {
dispatch(setWatchedCasesDetails(watchedCasesDetails));
},
setAwaitingSubmission: (awaitingSubmission) => {
dispatch(setAwaitingSubmission(awaitingSubmission));
},
setAwaitingSubmissionDetails: (awaitingSubmissionDetails) => {
dispatch(setAwaitingSubmissionDetails(awaitingSubmissionDetails));
},
setSearchResults: (searchResults) => {
dispatch(setSearchResults(searchResults));
},
setSearchDetails: (searchDetails) => {
dispatch(setSearchDetails(searchDetails));
},
};
};
export default connect(null, mapDispatchToProps)(TopThree);