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 { getNotice, 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 NoticeBanner from "../components/noticebanner";
import {
setAccountDetails,
setContainerID,
} 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,
hasLoginCode,
noticeContent,
} = 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: "/",
expires: expDate,
}),
router.replace({
pathname:
router.locale == "cy" || ggLocale == "cy"
? "/cy/fymhorth"
: "/myportal",
}))
: console.log("no value in user id");
var noticeDateFrom = new Date(
noticeContent.value[0].dateFrom.split("/")[2],
noticeContent.value[0].dateFrom.split("/")[1] - 1,
noticeContent.value[0].dateFrom.split("/")[0]
);
var noticeDateTo = new Date(
noticeContent.value[0].dateTo.split("/")[2],
noticeContent.value[0].dateTo.split("/")[1] - 1,
noticeContent.value[0].dateTo.split("/")[0]
);
var today = new Date();
var day = today.getDate();
var month = today.getMonth();
var year = today.getFullYear();
today = new Date(year, month, day);
const displayNotice = today >= noticeDateFrom && today <= noticeDateTo;
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 ? (
<>
{ggLocale == "cy"
? "Mewngofnodi"
: "Logging in"}
{ggLocale == "cy"
? "Arhoswch os gwelwch yn dda..."
: "Please wait..."}
>
) : (
<>
{displayNotice && (
)}
>
)}
);
};
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("\n///////////////\nauth sess:", thisSession);
const showLoginCheck = process.env.SHOWLOGIN || false;
const showMapCheck = process.env.SHOWMAPS || false;
let hasLoginCode = thisSession != null;
console.log("server side has session:", thisSession != null);
// let providerConfig = await getProviderConfig();
let ggLocale =
typeof query.ui_locales != "undefined" && query.ui_locales;
console.log("hasloginCode:", hasLoginCode);
// store.dispatch(getGovGatewayConfig(providerConfig));
const noticeContent = await getNotice();
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,
relayToken
);
if (_.has(portalUserObj, "status") != false) {
if (portalUserObj.status == 400) {
return {
redirect: {
//destination: "/dns-not-found",
destination: "/error",
permanent: false,
},
};
} else {
console.log("loggin user stuff: ", portalUserObj);
store.dispatch(setAccountDetails(portalUserObj));
}
}
} else {
console.log("empty session");
}
console.log("///////////////\n");
if (ggToken.status == 400) {
return {
redirect: {
destination: "/myportal/error",
permanent: false,
},
};
} else {
return {
props: {
ggLocale: ggLocale,
showLogin: showLoginCheck,
showMap: showMapCheck,
hasSession: hasLoginCode,
loggedInUserId: portalUserObj,
loggedInUserEmail: loginEmailStr,
hasLoginCode: hasLoginCode,
noticeContent: noticeContent,
},
};
}
}
);
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);