Files
pedwfrontend/pages/myportal/index.js
T
2021-11-15 14:49:44 +00:00

256 lines
8.8 KiB
JavaScript

import _ from "lodash";
import jsonpath from "jsonpath";
import Head from "next/head";
import Header from "../../components/header";
import Banner from "../../components/banner";
import CookieBanner from "../../components/cookieBanner";
import Breadcrumbs from "../../components/breadcrumbs";
import MyPortal from "../../components/myportal";
import Footer from "../../components/footer";
import Errorpage from "../../components/errorpage";
import styles from "../../styles/Home.module.css";
import { useSelector, shallowEqual, connect } from "react-redux";
import { wrapper } from "../../store/store";
import { useRouter } from "next/router";
import useTranslation from "next-translate/useTranslation";
import data from "../../data/collections.json";
import nookies from "nookies";
import { getFormCollectionByID } from "../../components/utils";
import {
setLoggedInUserId,
setAccountDetails,
} from "../../store/accountDetails/action";
import { setMyCases, setMyCasesDetails } from "../../store/myCases/action";
import {
setMyRepresentations,
setMyRepresentationsDetails,
} from "../../store/myRepresentations/action";
import {
setWatchedCases,
setWatchedCasesDetails,
} from "../../store/watchedCases/action";
import {
setAwaitingSubmission,
setAwaitingSubmissionDetails,
} from "../../store/awaitingSubmission/action";
import {
getToken,
getPersonalAccount,
getMyCases,
getMyRepresentations,
getWatchedCases,
getAwaitingSubmission,
getPortalModuleDetails,
} from "../../actions";
const Home = (props) => {
const {
footerLinks,
pages,
myCases,
myRepresentations,
watchedCases,
awaitingSubmission,
} = props;
let { t, lang } = useTranslation();
const router = useRouter();
const { locale } = router;
const { appealtypes } = router.query;
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}
/>
</div>
<Footer footerLinks={footerLinks} />
</div>
</div>
);
};
export const getServerSideProps = wrapper.getServerSideProps(
(store) => async (ctx) => {
const { query, req, res } = ctx;
console.log("the query", query);
const { cookies } = req;
console.log("the cookoies", cookies);
let token = await getToken();
token = token.access_token;
let loggedInUser = cookies.pinsUser;
const [
accountDetails,
myCases,
myRepresentations,
watchedCases,
awaitingSubmission,
] = await Promise.all([
await getPersonalAccount(token, loggedInUser),
await getMyCases(token, loggedInUser),
await getMyRepresentations(token, loggedInUser),
await getWatchedCases(token, loggedInUser),
await getAwaitingSubmission(token),
]);
//const cookiesMaker = nookies.get(ctx)
console.log(accountDetails);
const [
myCasesDetails,
myRepresentationsDetails,
watchedCasesDetails,
// awaitingSubmissionDetails,
] = await Promise.all([
await getDetails(token, myCases, "myCases"),
await getDetails(token, myRepresentations, "myRepresentations"),
await getDetails(token, watchedCases, "myWatchedCases"),
// await getDetails(awaitingSubmission),
]);
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(
// setAwaitingSubmissionDetails(awaitingSubmissionDetails)
// );
}
);
const getDetails = (token, 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(
getPortalModuleDetails(
token,
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 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);