import { JSONPath as jsonpath } from "jsonpath-plus"; import _ from "lodash"; import useTranslation from "next-translate/useTranslation"; import Link from "next/link"; import { useRouter } from "next/router"; import { useCallback, useState } from "react"; import { connect } from "react-redux"; import transLookup from "../../data/lookuptranslations.json"; import { setCurrentReference } from "../../store/currentView/action"; import { parseCookies, setCookie } from "nookies"; import { useEffect } from "react"; import { setLoggedInUserId } from "../../store/accountDetails/action"; import { setSearchDetails, setSearchResults, } from "../../store/searchOutput/action"; import { setWatchedCases, setWatchedCasesDetails, } from "../../store/watchedCases/action"; import { getDetailsProxy } from "../utils"; import PaginationControl from "./pagination"; import { signIn, useSession } from "next-auth/react"; import { createWatchedCases, deleteWatchedCases, getWatchedCasesProxy, } from "../../actions"; import RepsOnResults from "./repsonresults"; const SearchResults = (props) => { const { searchString, searchResultsObj, searchDetailsObj, setCurrentReference, setSearchResults, setSearchDetails, advancedSearch, myportal, watchedCases, setWatchedCases, setWatchedCasesDetails, isLinkedCase, showLoginCheck, } = props; let { t } = useTranslation(); const router = useRouter(); const { locale } = router; const { data: session } = useSession(); let [resultsArr, setResultsArr] = useState(searchResultsObj.value || []); const [currentPage, setCurrentPage] = useState(1); const [searchLoaded, setSearchLoaded] = useState(false); const [showSpinnerState, setShowSpinnerState] = useState(false); const [selectedOption, setSelectedOption] = useState(10); const [orderByState, setOrderbyState] = useState("createdon"); const [fieldSortState, setfieldSortState] = useState("asc"); const [loginToWatch, setLoginToWatch] = useState("true"); let spinnerState = () => { showSpinnerState == true ? setShowSpinnerState(false) : setShowSpinnerState(true); }; const cookies = parseCookies(); const selectWatchedCase = (loggedInUser, incidentID, appealType) => { let updateBody = { "pinswg_WatchedCase@odata.bind": "/incidents(" + incidentID + ")", "pinswg_Contact@odata.bind": "/contacts(" + loggedInUser + ")", "pinswg_appealcasetype": +appealType, }; createWatchedCases(updateBody) .then((data) => data) .then(() => { getWatchedCasesProxy(props.accountDetails.loggedinUserId).then( (data) => { setWatchedCases(data), getDetailsProxy(data, "myWatchedCases").then( (data) => { setWatchedCasesDetails(data); } ); } ); }); }; const isWatchedCase = (whichIncident) => { let isWatched = jsonpath( "$..value[?(@._pinswg_watchedcase_value=='" + whichIncident + "')]", watchedCases ); return isWatched; }; const deleteItem = (caseID, topThreeType) => { let cookies = parseCookies(); topThreeType == "watchedCases" && deleteWatchedCases(caseID) .then((data) => data) .then(() => { getWatchedCasesProxy( props.accountDetails.loggedinUserId ).then((data) => { setWatchedCases(data), getDetailsProxy(data, "myWatchedCases").then( (data) => { setWatchedCasesDetails(data); } ); }); }); }; const indexOfLastRecord = currentPage * selectedOption; const indexOfFirstRecord = indexOfLastRecord - selectedOption; const ResultsView = (resultsArr) => { var resultsArrPaged = resultsArr.slice( indexOfFirstRecord, indexOfLastRecord ); return ( <>
{t("search:searchresults-site-address-label")}
{/* {t("search:searchresults-status-label")} */}
{showSpinnerState == true ? (
{t("common:spinner-fetching-data")} {
) : ( resultsArrPaged.map((item, key) => { let detailsObj = jsonpath( '$..value[?(@.pinswg_name=="' + item.pinswg_name + '")]', searchDetailsObj ); detailsObj = detailsObj[0]; return (
{ // setCurrentReference({ // "ticketnumber": // item.ticketnumber, // "currentReference": // item.title, // "currentType": // "searchResultsObj", // "incidentid": // item.incidentid, // "appealType": // item.pinswg_appealcasetype, // "showLoginCheck": // props.showLoginCheck, // }); // }} > {t( "search:searchresults-case-reference-label" )} : {item.pinswg_name}
{t( "search:searchresults-site-address-label" )} : {_.has( detailsObj, "pinswg_projectlocation" ) ? detailsObj.pinswg_projectlocation != null ? detailsObj.pinswg_projectlocation.indexOf( ";" ) < 0 ? detailsObj.pinswg_projectlocation : router.locale == "cy" ? detailsObj.pinswg_projectlocation.split( ";" )[1] : detailsObj.pinswg_projectlocation.split( ";" )[0] : "" : ""} {_.has( detailsObj, "pinswg_siteaddressline1" ) ? detailsObj.pinswg_siteaddressline1 : ""} {_.has( detailsObj, "pinswg_addressline1" ) ? detailsObj.pinswg_addressline1 : ""} {/* {detailsObj.pinswg_siteaddressline1 != null &&
} */} {_.has( detailsObj, "pinswg_siteaddressline1" ) && detailsObj.pinswg_siteaddressline1 != null &&
} {_.has( detailsObj, "pinswg_addressline1" ) && detailsObj.pinswg_addressline1 != null &&
} {_.has( detailsObj, "pinswg_siteaddressline1" ) ? detailsObj.pinswg_siteaddressline2 : ""} {_.has( detailsObj, "pinswg_addressline1" ) ? detailsObj.pinswg_addressline2 : ""} {_.has( detailsObj, "pinswg_siteaddressline2" ) && detailsObj.pinswg_siteaddressline2 != null &&
} {_.has( detailsObj, "pinswg_addressline2" ) && detailsObj.pinswg_addressline2 != null &&
} {_.has( detailsObj, "pinswg_siteaddresstown" ) ? detailsObj.pinswg_siteaddresstown : ""} {_.has(detailsObj, "pinswg_addresstown") ? detailsObj.pinswg_addresstown : ""} {_.has( detailsObj, "pinswg_siteaddresstown" ) && detailsObj.pinswg_siteaddresstown != null &&
} {_.has( detailsObj, "pinswg_addresstown" ) && detailsObj.pinswg_addresstown != null &&
} {_.has( detailsObj, "pinswg_siteaddresscounty" ) ? detailsObj.pinswg_siteaddresscounty : ""} {_.has( detailsObj, "pinswg_addresscounty" ) ? detailsObj.pinswg_addresscounty : ""} {_.has( detailsObj, "pinswg_siteaddresscounty" ) && detailsObj.pinswg_siteaddresscounty != null &&
} {_.has( detailsObj, "pinswg_addresscounty" ) && detailsObj.pinswg_addresscounty != null &&
} {_.has( detailsObj, "pinswg_siteaddresspostcode" ) ? detailsObj.pinswg_siteaddresspostcode : ""} {_.has(detailsObj, "pinswg_postcode") ? detailsObj.pinswg_postcode : ""}
{t( "search:searchresults-applicant-label" )} : {_.has(detailsObj, [ "_pinswg_appellant_value@OData.Community.Display.V1.FormattedValue", ]) ? detailsObj[ "_pinswg_appellant_value@OData.Community.Display.V1.FormattedValue" ] : ""}
{t( "search:searchresults-authority-label" )} : {_.has(item, [ "_pinswg_associatedlpa_value@OData.Community.Display.V1.FormattedValue", ]) ? router.locale == "cy" ? jsonpath( '$..[?(@.value=="' + item[ "_pinswg_associatedlpa_value@OData.Community.Display.V1.FormattedValue" ] + '")].value_cy', transLookup ) : item[ "_pinswg_associatedlpa_value@OData.Community.Display.V1.FormattedValue" ] || "N/A" : ""}
{t( "search:searchresults-case-type-label" )} : {router.locale == "cy" ? jsonpath( '$..[?(@.value=="' + item[ "pinswg_appealType" ] + '")].value_cy', transLookup ) : item["pinswg_appealType"] || "N/A"}
{t( "search:searchresults-status-label" )} : {router.locale == "cy" ? jsonpath( '$..[?(@.value=="' + item[ "statuscode@OData.Community.Display.V1.FormattedValue" ] + '")].value_cy', transLookup ) : item[ "statuscode@OData.Community.Display.V1.FormattedValue" ] || "N/A"}
{myportal == true && (
{isWatchedCase(item.incidentid) .length > 0 == true ? ( ) : ( )}
)} {(showLoginCheck == "true" || showLoginCheck == true) && !session && (
{t( "case:watch-this-case-link" )}
)}
); }) )}
); }; const pagesCount = Math.ceil(searchResultsObj["@odata.count"] / 10); const getSearchPageResults = useCallback( (pageNumber, orderBy, fieldSort) => { // setSearchDetails(data); setCurrentPage(pageNumber); resultsArr = _.sortBy(resultsArr, [ function (o) { return o[orderBy]; }, ]); fieldSort == "desc" && resultsArr.reverse(); setShowSpinnerState(false); setResultsArr(resultsArr); console.log( "================================\n", resultsArr, "sort done", "\n=================================\n" ); }, [] ); const sortDataBy = (whichField) => { setOrderbyState(whichField); setfieldSortState("asc"); setShowSpinnerState(true); console.log( "================================\n", "sort start", "\n=================================\n" ); getSearchPageResults(1, whichField, fieldSortState); whichField == orderByState ? fieldSortState == "desc" ? setfieldSortState("asc") : setfieldSortState("desc") : setfieldSortState("asc"); setCurrentPage(1); }; useEffect(() => { const selectWatchedCase = (loggedInUser, incidentID, appealType) => { let updateBody = { "pinswg_WatchedCase@odata.bind": "/incidents(" + incidentID + ")", "pinswg_Contact@odata.bind": "/contacts(" + loggedInUser + ")", "pinswg_appealcasetype": +appealType, }; createWatchedCases(updateBody) .then((data) => data) .then(() => { getWatchedCasesProxy( props.accountDetails.loggedinUserId ).then((data) => { setWatchedCases(data), getDetailsProxy(data, "myWatchedCases").then( (data) => { setWatchedCasesDetails(data); } ); }); }); }; // if (searchLoaded) { // if (selectedOption) { // setShowSpinnerState(true); // getSearchPageResults(1, orderByState, fieldSortState); // } // } else { // setSearchLoaded(true); // } if (loginToWatch && router.query.hasOwnProperty("toWatch")) { console.log("am in here"); let now = new Date(); let expDate = new Date(now); expDate.setDate(now.getDate() + 1); setLoggedInUserId(props.accountDetails.loggedinUserId); setCookie(null, "pinsUser", props.accountDetails.loggedinUserId, { path: "/", expires: expDate, }); selectWatchedCase( props.accountDetails.loggedinUserId, router.query.toWatch.split("_")[0], router.query.toWatch.split("_")[1] ); getSearchPageResults(1, orderByState, fieldSortState); setLoginToWatch(false); } else { //console.log("or am in here"); setLoginToWatch(false); } }, [ selectedOption, fieldSortState, orderByState, searchLoaded, loginToWatch, setWatchedCases, getSearchPageResults, setWatchedCasesDetails, props.accountDetails.loggedinUserId, router.query, ]); return ( <>

{isLinkedCase ? t("search:searchresults-title-label-linked-case") : t("search:searchresults-title-label")}

{!isLinkedCase && (

{resultsArr.length > 200 ? t("search:searchresults-max-count-label") : t("search:searchresults-count-label", { count: searchResultsObj["@odata.count"], })} . {advancedSearch != true ? ( <> {t( "search:searchresults-case-count-label" )}{" "} {" "} "{searchString}" ) : ( "" )}

)} {searchResultsObj["@odata.count"] > 5 && (
)} {resultsArr.length > 0 ? ( ResultsView(resultsArr) ) : (

{t("search:searchresults-no-records-label")}

)}
); }; const mapDispatchToProps = (dispatch) => { return { setCurrentReference: (currentReference) => { dispatch(setCurrentReference(currentReference)); }, setSearchResults: (searchResultsObj) => { dispatch(setSearchResults(searchResultsObj)); }, setSearchDetails: (searchDetailsObj) => { dispatch(setSearchDetails(searchDetailsObj)); }, setWatchedCases: (watchedCases) => { dispatch(setWatchedCases(watchedCases)); }, setWatchedCasesDetails: (watchedCasesDetails) => { dispatch(setWatchedCasesDetails(watchedCasesDetails)); }, // setCurrentPage: (currentPage) => { // dispatch(setCurrentPage(currentPage)); // }, }; }; const mapStateToProps = (state) => { return { // currentView: state.currentView, accountDetails: state.accountDetails, // search: state.search, // searchResultsObj: state.searchResultsObj, }; }; export default connect(mapStateToProps, mapDispatchToProps)(SearchResults);