import useTranslation from "next-translate/useTranslation";
import { useRouter } from "next/router";
import { connect } from "react-redux";
import { useEffect, useState } from "react";
import { Field, formValueSelector, reduxForm, change } from "redux-form";
import { tr } from "date-fns/locale";
const RenderTextfield = ({
id,
className,
rows,
datafieldname,
name,
label,
input,
type,
errorMsg,
disabled,
meta: { touched, error },
...custom
}) => {
//console.log(custom);
return (
<>
{touched && error && (
Error:{" "}
{touched &&
((error && (
{errorMsg ? errorMsg : error}
)) ||
(warning && {warning}))}
)}
>
);
};
const RenderMultiline = ({
id,
className,
rows,
datafieldname,
name,
label,
input,
errorMsg,
meta: { touched, error },
...custom
}) => {
return (
<>
{touched && error && (
Error:{" "}
{touched &&
((error && (
{errorMsg ? errorMsg : error}
)) ||
(warning && {warning}))}
)}
>
);
};
const RenderYesNo = ({
datafieldname,
name,
label,
id,
errorMsg,
options,
input: { onChange, value },
meta: { touched, error },
...custom
}) => {
return (
<>
>
);
};
const RenderRadio = ({
datafieldname,
name,
label,
id,
errorMsg,
options,
input: { onChange, value },
meta: { touched, error },
...custom
}) => {
let { t } = useTranslation();
const required = (value) => (value ? undefined : "Required");
return (
<>
>
);
};
function Radiofield(props) {
const {
name,
label,
datafieldname,
parentField,
form,
formProps,
parentFieldShowOnValue,
validation,
} = props;
const router = useRouter();
const required = (value) => (value ? undefined : "Required");
// console.log(props.options);
const fieldOptions = props.options
.slice(1, props.options.length - 1)
.split(",");
//parentFieldShowOnValue
var showIfHasParentShowValue = parentField != false;
//debugger;
// parentField != false &&
// !_.isEmpty(formProps[form]) &&
// _.has(formProps[form].values, parentField) &&
// parentFieldShowOnValue.indexOf(
// formProps[form].values[parentField].toString()
// ) > -1;
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
// console.log(
// datafieldname,
// showIfHasParentShowValue,
// formProps[form].values[parentField]
// );
let { t } = useTranslation();
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
)
) : (
)}
>
);
}
let AboutYou = (props) => {
let { t } = useTranslation();
const router = useRouter();
const { locale } = router;
const {
onSubmit,
handleSubmit,
appellantAgentValue,
setFormStep,
updateField,
} = props;
const [isAgent, setIsAgent] = useState(false);
const yesNo = router.locale == "cy" ? ["Ydw", "Na"] : ["Yes", "No"];
const appellantAgent =
router.locale == "cy" ? ["Apelydd", "Asiant"] : ["Appellant", "Agent"];
const contactPref =
router.locale == "cy" ? ["Ebost", "Post"] : ["Email", "Post"];
const required = (value) => (value ? undefined : "Is required");
const emailRegex =
/(?:[a-z0-9!#$%&'*+\/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+\/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])/gi;
const email = (value) =>
value && !emailRegex.test(value) ? "Invalid email address" : undefined;
const phoneNumber = (value) =>
value &&
!/^(?:(?:\(?(?:0(?:0|11)\)?[\s-]?\(?|\+)44\)?[\s-]?(?:\(?0\)?[\s-]?)?)|(?:\(?0))(?:(?:\d{5}\)?[\s-]?\d{4,5})|(?:\d{4}\)?[\s-]?(?:\d{5}|\d{3}[\s-]?\d{3}))|(?:\d{3}\)?[\s-]?\d{3}[\s-]?\d{3,4})|(?:\d{2}\)?[\s-]?\d{4}[\s-]?\d{4}))(?:[\s-]?(?:x|ext\.?|\#)\d{3,4})?$/i.test(
value
)
? "Invalid phone number"
: undefined;
const onHandleSubmit = (values) => {
event.preventDefault();
console.log(JSON.stringify(values));
setFormStep(1);
};
const handlePartialUpdate = () => {
if (appellantAgentValue == "846040001" && isAgent == false) {
updateField("caseForm", "pinswg_appellantfirstname", "");
updateField("caseForm", "pinswg_appellantlastname", "");
updateField("caseForm", "pinswg_appellantemailaddress", "");
updateField("caseForm", "pinswg_appellantaddress", "");
updateField("caseForm", "pinswg_appellantphonenumber", "");
setIsAgent(true);
} //else {
// updateField(
// "caseForm",
// "pinswg_appellantfirstname",
// props.initialValues.pinswg_appellantfirstname
// );
// updateField(
// "caseForm",
// "pinswg_appellantlastname",
// props.initialValues.pinswg_appellantlastname
// );
// updateField(
// "caseForm",
// "pinswg_appellantemailaddress",
// props.initialValues.pinswg_appellantemailaddress
// );
// updateField(
// "caseForm",
// "pinswg_appellantaddress",
// props.initialValues.pinswg_appellantaddress
// );
// updateField(
// "caseForm",
// "pinswg_appellantphonenumber",
// props.initialValues.pinswg_appellantphonenumber
// );
};
useEffect(() => {
handlePartialUpdate();
});
return (
);
};
const mapStateToProps = (state) => {
return {
search: state.search,
searchResultsObj: state.searchResultsObj,
formData: state.formData,
appealType: state.appealType,
//form: state.form,
initialValues: {
pinswg_appellantfirstname:
state.accountDetails.accountDetails.firstname,
pinswg_appellantlastname:
state.accountDetails.accountDetails.lastname,
pinswg_appellantemailaddress:
state.accountDetails.accountDetails.emailaddress1,
pinswg_appellantaddress:
state.accountDetails.accountDetails.address1_composite,
pinswg_appellantphonenumber:
state.accountDetails.accountDetails.telephone1,
},
};
};
const mapDispatchToProps = (dispatch) => {
return {
updateField: (form, field, newValue) =>
dispatch(change(form, field, newValue)),
};
};
// AboutYou = reduxForm({
// form: "caseForm", // a unique identifier for this form
// destroyOnUnmount: false,
// })(AboutYou);
const selector = formValueSelector("caseForm"); // <-- same as form name
AboutYou = connect((state) => {
// can select values individually
const appellantAgentValue = selector(state, "pinswg_appellantagent");
return {
appellantAgentValue,
};
})(AboutYou);
//export default AboutYou;
export default connect(
mapStateToProps,
mapDispatchToProps
)(
reduxForm({
form: "caseForm",
enableReinitialize: true, // this is needed!!
destroyOnUnmount: false,
})(AboutYou)
);