import useTranslation from "next-translate/useTranslation";
import Head from "next/head";
import { useRouter } from "next/router";
import { connect } from "react-redux";
import { consoleLogger, getIP } from "../../actions/core/logger";
import { getPersonalAccount } from "../../actions/services/accountService";
import { getPortalModuleDetails } from "../../actions/services/caseService";
import {
getAddressSearch,
getAdvancedSearch
} from "../../actions/services/searchService";
import { getWatchedCases } from "../../actions/services/portalService";
import SearchResults from "../../components/addresssearchresults";
import Breadcrumbs from "../../components/breadcrumbs";
import CookieBanner from "../../components/cookieBanner";
import Footer from "../../components/footer";
import Header from "../../components/header";
import {
getFormCollectionByID,
getSearchDetails
} from "../../components/utils";
import {
setAccountDetails,
setContainerID,
setLoggedInUserId
} from "../../store/accountDetails/action";
import { setShowReps } from "../../store/currentView/action";
import { setSearch } from "../../store/search/action";
import {
setSearchDetails,
setSearchResults
} from "../../store/searchOutput/action";
import {
setWatchedCases,
setWatchedCasesDetails
} from "../../store/watchedCases/action";
import { wrapper } from "../../store/store";
import ServiceBanner from "../../components/myportal/servicebanner";
import TimeOut from "../../components/timeout";
import { resolveMyPortalAuthContext } from "../../lib/auth/resolveMyPortalAuthContext";
const Home = (props) => {
const { footerLinks, pages } = props;
let { t, lang } = useTranslation();
const router = useRouter();
const { locale } = router;
const { appealtypes } = router.query;
return (
{t("search:page-title")} - {t("common:service-name")}
<>
{/* If they have a Twitter Handle, include the meta details below */}
>
);
};
export const getServerSideProps = wrapper.getServerSideProps(
(store) => async (ctx) => {
const { query, req } = ctx;
getIP(req);
const showLoginCheck = process.env.SHOWLOGIN || false;
const showReps = process.env.SHOWREPRESENTATIONS || false;
store.dispatch(setShowReps(showReps, showLoginCheck));
store.dispatch(setSearch(Object.entries(query)));
const authContext = await resolveMyPortalAuthContext(ctx, {
loggerName: "MyPortalAddressSearchResultsAuthGuard",
noSessionMessage:
"myportal addresssearchresults loader missing session; redirecting to signin",
noSessionUserMessage:
"myportal addresssearchresults loader missing session user identity; redirecting to signin",
noPinsUserMessage:
"myportal addresssearchresults loader missing pinsUser cookie; redirecting to signin"
});
if (!authContext.ok) {
return { redirect: authContext.redirect };
}
const thisSession = authContext.session;
const loggedInUser = authContext.pinsUser;
thisSession != false &&
store.dispatch(setContainerID(thisSession.user.id));
let accountDetails;
let searchResultsObj;
let watchedCases;
let searchDetailsObj;
let watchedCasesDetails;
try {
[accountDetails, searchResultsObj, watchedCases] =
await Promise.all([
getPersonalAccount(loggedInUser),
getAdvancedSearch(query),
getWatchedCases(loggedInUser)
]);
[searchDetailsObj, watchedCasesDetails] = await Promise.all([
getSearchDetails(searchResultsObj),
getDetails(watchedCases, "myWatchedCases")
]);
} catch (error) {
consoleLogger({
name: "MyPortalAddressSearchResultsAuthGuard",
reasonCode: "UPSTREAM_FAILURE",
message:
"myportal addresssearchresults loader dependency call failed; redirecting to signin",
error: error?.message
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
if (!accountDetails || accountDetails?.errorCode) {
consoleLogger({
name: "MyPortalAddressSearchResultsAuthGuard",
reasonCode: "CONTACT_LOOKUP_FAILED",
message:
"myportal addresssearchresults loader account lookup failed; redirecting to signin",
hasAccountDetails: !!accountDetails,
hasErrorCode: !!accountDetails?.errorCode
});
return {
redirect: {
destination: "/auth/signin",
permanent: false
}
};
}
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setSearchResults(searchResultsObj));
store.dispatch(setSearchDetails(searchDetailsObj));
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
store.dispatch(setSearch(Object.entries(query)));
store.dispatch(setLoggedInUserId(loggedInUser));
return {
props: {
searchResultsObj: {
searchResultsObj: searchResultsObj,
searchDetailsObj: searchDetailsObj
},
currentType: "directResultsObj",
showLoginCheck: showLoginCheck
}
};
}
);
const getDetails = (resultsObj, detailsType) => {
let detailsArr = [];
resultsObj = resultsObj.value;
const detailsObj = resultsObj.map((searchDetail) => {
if (searchDetail.pinswg_appealcasetype == null) {
console.log(
detailsType != "myWatchedCases"
? searchDetail.ticketnumber
: searchDetail[
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
]
);
} else {
detailsArr.push(
getPortalModuleDetails(
getFormCollectionByID(searchDetail.pinswg_appealcasetype)
.LogicalCollectionName,
detailsType != "myWatchedCases"
? searchDetail.ticketnumber
: searchDetail[
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
]
)
);
}
});
return Promise.all(detailsArr);
};
const mapStateToProps = (state) => {
return {
currentView: state.currentView,
search: state.search,
//searchResultsObj: state.searchResultsObj,
formData: state.formData,
appealType: state.appealType,
form: state.form,
myCases: state.myCases,
watchedCases: state.watchedCases,
myRepresentations: state.myRepresentations,
awaitingSubmission: state.awaitingSubmission,
accountDetails: state.accountDetails
};
};
export default connect(mapStateToProps)(Home);