import _ from "lodash"; import Link from "next/link"; import React, { useEffect, useState } from "react"; import { useRouter } from "next/router"; import useTranslation from "next-translate/useTranslation"; import jsonpath from "jsonpath"; import { Field, reduxForm, formValueSelector, getFormSubmitErrors, } from "redux-form"; import { useDispatch, useSelector, shallowEqual, connect, setRegisterFormComplete, setAccountCreatedComplete, } from "react-redux"; import { setAccCr, setLogout } from "../../store/accountDetails/action"; import { getEmailAccountCheck, createAccount } from "../../actions"; const RegisterComplete = (props) => { const { footerLinks, pages, handleSubmit, value, setRegisterFormComplete, setAccountCreatedComplete, setAccCr, } = props; let { t, lang } = useTranslation(); const router = useRouter(); const { locale } = router; const [accountCreated, setAccountCreated] = useState(false); useEffect(() => { let accountBody = props.props.props.form.accountRegisterForm.values; let emailAddress = props.props.props.form.accountRegisterForm.values.emailaddress1; Object.assign(accountBody, { "pinswg_typeofinvolvement": 846040001, }); accountBody = _.omit(accountBody, ["custom_password_check"]); let checkEmailObj = {}; switch (props.accountDetails.accCr) { case "created": case "exists": break; default: let emailExists = getEmailAccountCheck( accountBody.emailaddress1 ).then((data) => { data["@odata.count"] > 0 ? setAccCr("exists") : (checkEmailObj = createAccount(accountBody).then( (data) => { //console.log(data); setAccCr("created"); } )); }); break; } }); var registerBody = ""; switch (props.accountDetails.accCr) { case "created": return (registerBody = ( <>

{t("account:register-new-account-confirmation-email")}

{t( "account:register-new-account-sub-heading-what-next" )}

{t("account:register-new-account-paragraph-what-next")}

{ setLogout(), window.localStorage.clear(); }} > {t( "account:register-new-account-my-portal-link" )}

)); break; case "exists": return (registerBody = ( <>

{t("account:register-new-account-account-failed")}

{ setRegisterFormComplete(false), setAccountCreatedComplete(false), setAccCr(false); }} className="govuk-link" > Try registering again

)); default: return (registerBody = ""); break; } return registerBody; }; const mapStateToProps = (state) => { return { search: state.search, searchResultsObj: state.searchResultsObj, formData: state.formData, appealType: state.appealType, accountDetails: state.accountDetails, //form: state.form, }; }; const mapDispatchToProps = (dispatch) => { return { setAccCr: (accountCreated) => { dispatch(setAccCr(accountCreated)); }, setLogout: () => { dispatch(setLogout()); }, }; }; export default connect(mapStateToProps, mapDispatchToProps)(RegisterComplete);