Files
pedwfrontend/pages/index.js
T

249 lines
8.4 KiB
JavaScript

import _ from "lodash";
import useTranslation from "next-translate/useTranslation";
import Head from "next/head";
import { useRouter } from "next/router";
import { setCookie } from "nookies";
import { connect } from "react-redux";
import { getToken, hashString } from "../actions";
import {
getGGToken,
getPortalLogin,
getProviderConfig,
getUserInfo,
} from "../actions/govgateway";
import CookieBanner from "../components/cookieBanner";
import Footer from "../components/footer";
import Header from "../components/header";
import Main from "../components/main";
import ServiceBanner from "../components/servicebanner";
import { setAccountDetails } from "../store/accountDetails/action";
import { getGovGatewayConfig } from "../store/govgateway/action";
import { wrapper } from "../store/store";
import { getSession, useSession, signIn, signOut } from "next-auth/react";
const Home = (props) => {
const {
footerLinks,
pages,
showLogin,
showMap,
loggedInUserId,
hasGGCode,
loginEmailStr,
loggedInUserEmail,
ggLocale,
} = props;
let { t, lang } = useTranslation();
const router = useRouter();
const { locale } = router;
let now = new Date();
let expDate = new Date(now);
expDate.setDate(now.getDate() + 1);
//console.log("hashed email str", loginEmailStr);
_.isEmpty(loggedInUserId) != false && !hasGGCode
? console.log("no session")
: _.has(loggedInUserId, "value")
? _.isEmpty(loggedInUserId.value)
? router.replace({
pathname:
router.locale == "cy"
? "/account/register"
: "/account/register",
query: {
id: encodeURI(loggedInUserEmail),
},
})
: (setCookie(null, "pinsUser", loggedInUserId.value[0].contactid, {
path: "/",
expires: expDate,
}),
router.replace({
pathname:
router.locale == "cy" || ggLocale == "cy"
? "/cy/fymhorth"
: "/myportal",
}))
: console.log("no value in user id");
return (
<div>
<Head>
<title>{t("common:service-name")}</title>
{/* Language by default is EN, if multilingual site this will need updated */}
<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: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.pathname}
/>
<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="Appeals Casework Portal" />
<div className="govuk-width-container">
{hasGGCode ? (
<>
<h1>
{ggLocale == "cy"
? "Mewngofnodi"
: "Logging in"}
</h1>
<h2>
{ggLocale == "cy"
? "Arhoswch os gwelwch yn dda..."
: "Please wait..."}
</h2>
</>
) : (
<>
<ServiceBanner />
<Main
ggLocale={ggLocale}
pages={pages}
showLogin={showLogin}
showMap={showMap}
loggedInUserId={loggedInUserId}
GG_REDIRECT_URI={props.GG_REDIRECT_URI}
GG_CLIENT_ID={props.GG_CLIENT_ID}
/>
</>
)}
</div>
<Footer footerLinks={footerLinks} />
</div>
</div>
);
};
export const getServerSideProps = wrapper.getServerSideProps(
(store) => async (ctx) => {
const { query, req, res } = ctx;
let thisSession = await getSession(ctx);
var relayToken = await getToken();
relayToken = relayToken.access_token;
console.log("auth sess:", thisSession);
const showLoginCheck = process.env.SHOWLOGIN || false;
const showMapCheck = process.env.SHOWMAPS || false;
const hasLoginCode = thisSession != null;
let providerConfig = await getProviderConfig();
let ggLocale =
typeof query.ui_locales != "undefined" && query.ui_locales;
console.log(
"endpoint: ",
providerConfig.token_endpoint,
"\n",
"hasloginCode:",
hasLoginCode,
"\n",
"redirect:",
process.env.GG_REDIRECT_URI
);
store.dispatch(getGovGatewayConfig(providerConfig));
let ggToken = {};
let ggUserInfo = {};
let portalUserObj = {};
let loginEmailStr = "";
if (hasLoginCode == true) {
(loginEmailStr = hashString(thisSession.user.email)),
(portalUserObj = await getPortalLogin(
thisSession.user.email,
relayToken
)),
console.log("loggin user stuff: ", portalUserObj),
store.dispatch(setAccountDetails(portalUserObj));
} else {
console.log("no session");
}
if (ggToken.status == 400) {
return {
redirect: {
destination: "/myportal/error",
permanent: false,
},
};
} else {
return {
props: {
ggLocale: ggLocale,
showLogin: showLoginCheck,
showMap: showMapCheck,
hasGGCode: hasLoginCode,
loggedInUserId: portalUserObj,
loggedInUserEmail: loginEmailStr,
GG_REDIRECT_URI: process.env.GG_REDIRECT_URI,
GG_CLIENT_ID: process.env.GG_CLIENT_ID,
},
};
}
}
);
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,
govGateway: state.govGateway,
};
};
export default connect(mapStateToProps)(Home);