Files
pedwfrontend/pages/myportal/index.js
T

430 lines
14 KiB
JavaScript

import _ from "lodash";
import { getSession, useSession } from "next-auth/react";
import useTranslation from "next-translate/useTranslation";
import Head from "next/head";
import { useRouter } from "next/router";
import { connect } from "react-redux";
import {
getRepsFromBlob,
getAwaitingSubmissionFromBlob,
getAwaitingSubmission,
getMyCases,
getMyLPACases,
getMyRepresentations,
getPersonalAccount,
getPortalModuleDetails,
getWatchedCases,
consoleLogger,
getCase,
getPortalLogin,
createContainerProxy,
getIP,
} from "../../actions";
import Breadcrumbs from "../../components/breadcrumbs";
import CookieBanner from "../../components/cookieBanner";
import Footer from "../../components/footer";
import Header from "../../components/header";
import MyPortal from "../../components/myportal";
import { getFormCollectionByID } from "../../components/utils";
import {
setAccountDetails,
setContainerID,
setLoggedInUserId,
} from "../../store/accountDetails/action";
import {
setAwaitingSubmission,
setAwaitingSubmissionFromBlob,
setAwaitingSubmissionDetails,
} from "../../store/awaitingSubmission/action";
import { setMyCases, setMyCasesDetails } from "../../store/myCases/action";
import {
setMyRepresentations,
setMyRepresentationsDetails,
} from "../../store/myRepresentations/action";
import { setShowReps } from "../../store/currentView/action";
import { wrapper } from "../../store/store";
import {
setWatchedCases,
setWatchedCasesDetails,
} from "../../store/watchedCases/action";
const Home = (props) => {
const {
footerLinks,
pages,
myCases,
myRepresentations,
watchedCases,
awaitingSubmission,
accountDetails,
showFileUpload,
showLoginCheck,
currentView,
} = props;
let { t, lang } = useTranslation();
const router = useRouter();
const { locale } = router;
const { appealtypes } = router.query;
const { data: session } = useSession();
return (
<div>
<Head>
<title>
{t("myportal:page-title")} - {t("common:service-name")}
</title>
<>
<link
rel="canonical"
href={t("common:gov-wales-link") + router.asPath}
/>
<meta
key="og:locale"
property="og:locale"
content={router.locale}
/>
{/* If they have a Twitter Handle, include the meta details below */}
<meta
key="og:site_name"
property="og:site_name"
content={t("common:service-name")}
/>
<meta
key="twitter:site"
property="twitter:site"
content="@UKGovWales"
/>
<meta
name="description"
content={t("common:service-description")}
/>
<meta
property="og:site_name"
content={t("common:gov-wales-label")}
/>
<meta
property="og:title"
content={t("myportal:page-title")}
/>
<meta
property="og:description"
content={t("common:service-description")}
/>
<meta property="og:type" content="website" />
<meta
property="og:url"
content={t("common:gov-wales-link")}
/>
<meta
property="og:image"
content="https://gov.wales/themes/custom/govwales/images/content/og-global-1200.png"
/>
<meta name="twitter:card" content="summary" />
<meta
name="twitter:title"
content={t("common:gov-wales-label")}
/>
<meta
name="twitter:url"
content={t("common:gov-wales-link") + router.asPath}
/>
<meta
name="twitter:image"
content="https://gov.wales/themes/custom/govwales/images/content/og-global-120.png"
/>
</>
</Head>
<div id="page_wrapper">
<CookieBanner />
<Header courseName={t("common:service-name")} />
<div className="govuk-width-container">
<Breadcrumbs slug={appealtypes} />
<MyPortal
myCases={myCases}
myRepresentations={myRepresentations}
watchedCases={watchedCases}
awaitingSubmission={awaitingSubmission}
accountDetails={accountDetails}
showFileUpload={showFileUpload}
containerID={accountDetails.containerID}
showLoginCheck={showLoginCheck}
currentView={currentView}
/>
</div>
<Footer footerLinks={footerLinks} ticketnumber={props} />
</div>
</div>
);
};
export const getServerSideProps = wrapper.getServerSideProps(
(store) => async (ctx) => {
const { query, req, res } = ctx;
getIP(req);
console.log("The query", query);
const { cookies } = req;
let loggedInUserCookie = cookies.pinsUser;
let thisSession = await getSession(ctx);
let loggedInUser = {};
if (!thisSession) {
console.log("not has sesssion.......");
return {
redirect: {
destination: "/auth/signin",
permanent: false,
},
};
} else {
console.log(" has sesssion.......");
[loggedInUser] = await Promise.all([
await getPortalLogin(thisSession.user.email),
]);
loggedInUser = loggedInUser.value[0].contactid;
//console.log("nextAuth Session:", thisSession);
}
//Create Storage container for logged in user
await createContainerProxy(thisSession.user.id);
const accountDetails = await getPersonalAccount(loggedInUser);
//console.log("----------", accountDetails);
const lpaid =
accountDetails[
"pinswg_contact_associatedlpa@OData.Community.Display.V1.FormattedValue"
];
const [
myCases,
myRepresentations,
watchedCases,
awaitingSubmission,
awaitingSubmissionFromBlob,
] = await Promise.all([
accountDetails.pinswg_typeofinvolvement == 846040012
? await getMyLPACases(lpaid)
: await getMyCases(loggedInUser),
await getRepsFromBlob(thisSession.user.id),
await getWatchedCases(loggedInUser),
await getAwaitingSubmission(loggedInUser),
await getAwaitingSubmissionFromBlob(thisSession.user.id),
]);
// console.log(
// "/////////////////////////\n ",
// awaitingSubmissionFromBlob.value,
// awaitingSubmissionFromBlob.case
// );
console.log(
"preferred language:",
accountDetails.pinswg_preferredlanguage
);
console.log(
"locale :",
accountDetails.pinswg_preferredlanguage == "846040000" ? "cy" : "en"
);
console.log("req locale :", ctx.locale);
const preferredLocale =
accountDetails.pinswg_preferredlanguage == "846040000"
? "cy"
: "en";
const showLoginCheck = process.env.SHOWLOGIN || false;
const showMapCheck = process.env.SHOWMAPS || false;
const showReps = process.env.SHOWREPRESENTATIONS || false;
store.dispatch(setShowReps(showReps, showLoginCheck));
// if (preferredLocale != ctx.locale) {
// return {
// redirect: {
// destination: "/cy/myportal",
// permanent: false,
// },
// };
// } else {
if (_.has(accountDetails, "errorCode") != false) {
return {
redirect: {
destination: "/myportal/error",
permanent: false,
},
};
} else {
const [
myCasesDetails,
watchedCasesDetails,
//awaitingSubmissionDetails,
myRepresentationsDetails,
] = await Promise.all([
await getDetails(myCases, "myCases"),
await getDetails(watchedCases, "myWatchedCases"),
//await getDetails(awaitingSubmission, "awaitingSubmission"),
await getDetails(myRepresentations, "myRepresentations"),
]);
store.dispatch(setAccountDetails(accountDetails));
store.dispatch(setLoggedInUserId(loggedInUser));
store.dispatch(setMyCases(myCases));
store.dispatch(setMyCasesDetails(myCasesDetails));
store.dispatch(setMyRepresentations(myRepresentations));
store.dispatch(
setMyRepresentationsDetails(myRepresentationsDetails)
);
store.dispatch(setWatchedCases(watchedCases));
store.dispatch(setWatchedCasesDetails(watchedCasesDetails));
store.dispatch(setAwaitingSubmission(awaitingSubmission));
store.dispatch(
setAwaitingSubmissionFromBlob(awaitingSubmissionFromBlob)
);
store.dispatch(
setAwaitingSubmissionDetails(awaitingSubmissionFromBlob)
);
store.dispatch(setContainerID(thisSession.user.id));
return {
props: {
showFileUpload: process.env.SHOWFILEUPLOAD,
containerID: thisSession.user.id,
showLoginCheck: process.env.SHOWLOGIN,
},
};
}
// }
}
);
const getDetails = (resultsObj, detailsType) => {
let detailsArr = [];
// console.log(
// "//////////////////////////////////\n",
// resultsObj,
// //"\n",
// detailsType,
// "\n//////////////////////////////////\n"
// );
resultsObj = resultsObj.value;
// console.log(
// "////////////////////////////////// resultsobj\n",
// detailsType,
// "\n",
// resultsObj.length + "\n//////////////////////////////////\n"
// );
if (detailsType == "myRepresentations") {
const repsObj = resultsObj.map((searchDetail, index) => {
//console.log(".......... ", searchDetail);
detailsArr.push(
getCase(searchDetail["incidentID"]).then((data) => {
let caseID = "";
switch (detailsType) {
case "myCases":
caseID = data.pinswg_title;
break;
case "myWatchedCases":
caseID =
data[
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
];
break;
case "awaitingSubmission":
caseID = data.ticketnumber;
break;
case "myRepresentations":
caseID = data.ticketnumber;
break;
default:
caseID = data.pinswg_title;
}
return getPortalModuleDetails(
getFormCollectionByID(data.pinswg_appealcasetype)
.LogicalCollectionName,
caseID
);
})
);
});
}
const detailsObj = resultsObj.map((searchDetail, index) => {
let caseID = "";
switch (detailsType) {
case "myCases":
caseID = searchDetail.pinswg_title;
break;
case "myWatchedCases":
caseID =
searchDetail[
"_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
];
break;
case "awaitingSubmission":
caseID = searchDetail.ticketnumber;
break;
case "myRepresentations":
caseID = searchDetail.casereference;
break;
default:
caseID = searchDetail.pinswg_title;
}
//console.log(detailsType, " case id : ", caseID);
searchDetail.pinswg_appealcasetype != null &&
detailsArr.push(
getPortalModuleDetails(
getFormCollectionByID(searchDetail.pinswg_appealcasetype)
.LogicalCollectionName,
caseID
// detailsType != "myWatchedCases"
// ? searchDetail.ticketnumber
// : searchDetail[
// "_pinswg_watchedcase_value@OData.Community.Display.V1.FormattedValue"
// ]
)
);
});
let detArr = Promise.all(detailsArr);
console.log(detArr);
return detArr;
};
const mapStateToProps = (state) => {
//console.log(state);
return {
accountDetails: state.accountDetails,
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,
};
};
export default connect(mapStateToProps)(Home);