import Link from "next/link";
import { useState } from "react";
import { useRouter } from "next/router";
import useTranslation from "next-translate/useTranslation";
import { connect } from "react-redux";
import { Field, reduxForm } from "redux-form";
import LoadingOverlay from "react-loading-overlay";
import { getLogin } from "../../actions";
import { setLoggedInUserId } from "../../store/accountDetails/action";
const required = (errorMsg) => (value) =>
value || typeof value === "number" ? undefined : errorMsg;
export const minLength = (errorMsg) => (value) =>
value && value.length < 4
? errorMsg //`Must be ${min} characters or more`
: undefined;
const RenderTextfield = ({
id,
className,
rows,
datafieldname,
name,
label,
input,
hint1,
hint2,
meta: { touched, error },
...custom
}) => {
return (
<>
{hint1}
{hint2}
{touched && error && (
Error:{" "}
{error}
)}
>
);
};
const RenderPasswordfield = ({
id,
className,
rows,
datafieldname,
name,
label,
input,
hint1,
hint2,
meta: { touched, error },
...custom
}) => {
return (
<>
{hint1}
{touched && error && (
Error:{" "}
{error}
)}
>
);
};
let Login = (props) => {
let { t } = useTranslation();
const { handleSubmit, pristine, reset, submitting, setLoggedInUserId } =
props;
const router = useRouter();
const { locale } = router;
const [showSpinnerState, setShowSpinnerState] = useState(false);
const [validLoginID, setValidLoginID] = useState(null);
let spinnerState = () => {
showSpinnerState == true
? setShowSpinnerState(false)
: setShowSpinnerState(true);
};
const onHandleSubmit = (values) => {
spinnerState();
getLogin(values.loginUserID, values.loginUserPassword).then((data) => {
data.value.length != 0
? (setValidLoginID(true),
setLoggedInUserId(data.value[0].contactid),
spinnerState(),
router.replace({
pathname:
router.locale == "cy" ? "/fymhorth" : "/myportal",
query: { q: data.value[0].contactid },
shallow: true,
}))
: (setShowSpinnerState(false), setValidLoginID(false));
});
};
return (
);
};
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,
};
};
const mapDispatchToProps = (dispatch) => {
return {
setLoggedInUserId: (userID) => {
dispatch(setLoggedInUserId(userID));
},
};
};
export default connect(
mapStateToProps,
mapDispatchToProps
)(
reduxForm({
form: "loginForm",
destroyOnUnmount: false,
})(Login)
);