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, getPortalLogin, getIP } from "../actions";
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,
setContainerID,
} from "../store/accountDetails/action";
import { setShowReps } from "../store/currentView/action";
import { wrapper } from "../store/store";
import { getSession, useSession, signIn, signOut } from "next-auth/react";
const Home = (props) => {
const {
footerLinks,
pages,
showLogin,
showMap,
loggedInUserId,
loginEmailStr,
loggedInUserEmail,
hasLoginCode,
siteurl,
} = 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);
//const { data: session } = useSession();
//console.log("has session:", session);
//console.log("has LoginCode", hasLoginCode);
hasLoginCode == false
? "" //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: "/",
}),
router.replace({
pathname:
router.locale == "cy" ? "/cy/fymhorth" : "/myportal",
}))
: console.log("no value in user id");
return (
{t("common:service-name")}
{/* Language by default is EN, if multilingual site this will need updated */}
{/* If they have a Twitter Handle, include the meta details below */}
{hasLoginCode ? (
{t("home:logging-in-heading")}
{t("home:logging-in-paragraph")}
) : (
<>
>
)}
);
};
export const getServerSideProps = wrapper.getServerSideProps(
(store) => async (ctx) => {
const { query, req, res } = ctx;
getIP(req);
let thisSession = await getSession(ctx);
var relayToken = await getToken();
relayToken = relayToken.access_token;
console.log("\n////////////////////\nauth sess:", thisSession);
const showLoginCheck = process.env.SHOWLOGIN || false;
const showMapCheck = process.env.SHOWMAPS || false;
const showReps = process.env.SHOWREPRESENTATIONS || false;
store.dispatch(setShowReps(showReps, showLoginCheck));
console.log("server checks :", showReps, showLoginCheck);
let hasLoginCode = thisSession != null;
console.log("server side has session:", thisSession != null);
let ggLocale =
typeof query.ui_locales != "undefined" && query.ui_locales;
console.log("hasloginCode:", hasLoginCode + "\n////////////////////\n");
let ggToken = {};
let ggUserInfo = {};
let portalUserObj = {};
let loginEmailStr = "";
if (hasLoginCode == true) {
loginEmailStr = hashString(thisSession.user.email);
store.dispatch(setContainerID(thisSession.user.id));
portalUserObj = await getPortalLogin(thisSession.user.email);
} else {
console.log("///////////////\nempty session \n///////////////\n");
}
return {
props: {
showLogin: showLoginCheck,
showMap: showMapCheck,
hasSession: hasLoginCode,
loggedInUserId: portalUserObj,
loggedInUserEmail: loginEmailStr,
hasLoginCode: hasLoginCode,
siteurl: process.env.API_ROOT,
},
};
}
);
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,
};
};
export default connect(mapStateToProps)(Home);