import _ from "lodash";
import useTranslation from "next-translate/useTranslation";
import { useRouter } from "next/router";
import React, { useEffect } from "react";
import "react-datepicker/dist/react-datepicker.css";
import { Field } from "redux-form";
import fieldLookup from "../../data/crmfieldlookuptranslations.json";
import "react-quill-new/dist/quill.snow.css";
import { validateField } from "./validationUtils"; // Import the validation function
import { getFieldTranslation } from "./helpers/translationHelpers";
import { RenderRichMultiline } from "./fields/renderRichMultiline";
import { RenderTextfield } from "./fields/renderTextfield";
import { RenderMultiline } from "./fields/renderMultiline";
import { RenderRadio } from "./fields/renderRadio";
import { PickList } from "./fields/pickListField";
import { CheckBoxfield } from "./fields/checkBoxField";
import { DateFieldPicker } from "./fields/dateFieldPicker";
import { YesNofield } from "./fields/yesNoField";
import { RenderDecimalField } from "./fields/renderDecimalField";
import { RenderFileUpload } from "./fields/renderFileUpload";
export { RenderSubFields } from "./fields/renderSubFields";
export { FieldArrayForm } from "./fields/fieldArrayForm";
export { PickList };
export { CheckBoxfield };
export { DateFieldPicker };
export { YesNofield };
const getValidationMessages = (t) => ({
requiredMessage: t("newappeal:is-required-label"),
emojiNotAllowedMessage: t("newappeal:emojis-not-allowed-label"),
invalidPostcodeMessage: t("newappeal:invalid-postcode-label")
});
const hasParentFieldValue = ({ parentField, form, formProps }) =>
parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField);
const isVisibleByEquality = ({
parentField,
form,
formProps,
parentFieldShowOnValue
}) =>
(hasParentFieldValue({ parentField, form, formProps }) &&
formProps[form].values[parentField]) == parentFieldShowOnValue;
const isVisibleByInclusion = ({
parentField,
form,
formProps,
parentFieldShowOnValue
}) =>
hasParentFieldValue({ parentField, form, formProps }) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
export function Textfield(props) {
const {
name,
label,
validation,
ticketnumber,
form,
formProps,
parentField,
parentFieldShowOnValue,
maxFieldLength,
hint
} = props;
let { t } = useTranslation();
if (name == "pinswg_name") {
return (
<>
{t("newappeal:new-appeal-introduction-paragraph")}
-
{t(
"newappeal:new-appeal-introduction-paragraph-bullet-one"
)}
-
{t(
"newappeal:new-appeal-introduction-paragraph-bullet-two"
)}
{t("newappeal:new-appeal-introduction-paragraph-two")}
{t(
"newappeal:new-appeal-introduction-warning-paragraph"
)}
-
{t(
"newappeal:new-appeal-introduction-warning-bullet-one"
)}
-
{t(
"newappeal:new-appeal-introduction-warning-bullet-two"
)}
>
);
} else {
var showIfHasParentShowValue = isVisibleByEquality({
parentField,
form,
formProps,
parentFieldShowOnValue
});
// console.log(parentField, showIfHasParentShowValue);
// console.log(parentField, formProps, form);
//console.log("validation props:", validation);
const {
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
} = getValidationMessages(t);
return (
{parentField != false ? (
showIfHasParentShowValue && (
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
label={props.label}
maxFieldLength={props.maxFieldLength}
/>
)
) : (
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
label={props.label}
errorMsg={t("newappeal:is-required-label")}
maxFieldLength={props.maxFieldLength}
/>
)}
);
}
}
export function MultiLinefield(props) {
const {
name,
label,
form,
formProps,
parentField,
parentFieldShowOnValue,
validation,
maxFieldLength,
hint
} = props;
var showIfHasParentShowValue = isVisibleByInclusion({
parentField,
form,
formProps,
parentFieldShowOnValue
});
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
//console.log(showIfHasParentShowValue, formProps[form].values[parentField]);
let { t } = useTranslation();
const translatedLabel = FieldsTranslations(props.label);
const { requiredMessage, emojiNotAllowedMessage, invalidPostcodeMessage } =
getValidationMessages(t);
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
{props.hasOwnProperty("hint") &&
props.hint != false &&
t(props.hint)}{" "}
{t("newappeal:new-appeal-multiline-pid")}
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
maxFieldLength={props.maxFieldLength}
/>
)
) : (
{props.hasOwnProperty("hint") &&
props.hint != false &&
t(props.hint)}{" "}
{t("newappeal:new-appeal-multiline-pid")}
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
maxFieldLength={props.maxFieldLength}
/>
)}
>
);
}
export function RichMultiLinefield(props) {
const {
name,
label,
form,
formProps,
parentField,
parentFieldShowOnValue,
validation,
maxFieldLength
} = props;
var showIfHasParentShowValue = isVisibleByInclusion({
parentField,
form,
formProps,
parentFieldShowOnValue
});
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
//console.log(showIfHasParentShowValue, formProps[form].values[parentField]);
let { t } = useTranslation();
const { requiredMessage, emojiNotAllowedMessage, invalidPostcodeMessage } =
getValidationMessages(t);
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
{props.hasOwnProperty("hint") &&
props.hint != false &&
t(props.hint)}{" "}
{t("newappeal:new-appeal-multiline-pid")}
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
maxFieldLength={props.maxFieldLength}
/>
)
) : (
{props.hasOwnProperty("hint") &&
props.hint != false &&
t(props.hint)}{" "}
{t("newappeal:new-appeal-multiline-pid")}
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
maxFieldLength={props.maxFieldLength}
/>
)}
>
);
}
export function DateField(props) {
const { name, label } = props;
return (
);
}
export function Radiofield(props) {
const {
name,
label,
datafieldname,
form,
formProps,
parentFieldShowOnValue,
parentField,
validation
} = props;
const router = useRouter();
// console.log(props.options);
let { t } = useTranslation();
const fieldOptions = props.options
.slice(1, props.options.length - 1)
.split(",");
//parentFieldShowOnValue
var showIfHasParentShowValue = isVisibleByInclusion({
parentField,
form,
formProps,
parentFieldShowOnValue
});
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
const { requiredMessage, emojiNotAllowedMessage, invalidPostcodeMessage } =
getValidationMessages(t);
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
requiredDocumentLabel={props.requiredDocumentLabel}
requiredDocumentValue={props.requiredDocumentValue}
setDocumentsList={props.setDocumentsList}
documentList={props.documentList}
/>
)
) : (
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
requiredDocumentLabel={props.requiredDocumentLabel}
requiredDocumentValue={props.requiredDocumentValue}
setDocumentsList={props.setDocumentsList}
documentList={props.documentList}
/>
)}
>
);
}
export function NumericField(props) {
const {
name,
label,
validation,
form,
formProps,
parentFieldShowOnValue,
parentField,
maxFieldLength
} = props;
let { t } = useTranslation();
const required = (value) => {
return value || value == 0
? undefined
: t("newappeal:is-required-label");
};
const isNumber = (value) => {
const regex = /^\d+$/;
return regex.test(value)
? undefined
: t("newappeal:invalid-number-label");
};
const maxLength = (max) => (value) =>
value && value.length > max
? `Must be ${max} characters or less`
: undefined;
//parentFieldShowOnValue
var showIfHasParentShowValue = isVisibleByInclusion({
parentField,
form,
formProps,
parentFieldShowOnValue
});
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
//console.log("parentField:", parentField, showIfHasParentShowValue);
const { requiredMessage, emojiNotAllowedMessage, invalidPostcodeMessage } =
getValidationMessages(t);
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
component={RenderTextfield}
label={label}
maxFieldLength={props.maxFieldLength}
/>
)
) : (
parseInt(value)}
// maxFieldLength={props.maxFieldLength}
name={props.name}
id={props.name}
type="text"
className="govuk-input govuk-input--width-10"
aria-describedby={name}
//pattern="^\d*\.?\d+$"
pattern="[0-9]*"
validate={[
required,
isNumber,
maxLength(props.maxFieldLength)
]}
component={RenderTextfield}
label={props.label}
maxFieldLength={props.maxFieldLength}
/>
)}
>
);
}
export function DecimalField(props) {
const {
name,
label,
validation,
form,
formProps,
parentFieldShowOnValue,
parentField,
maxFieldLength
} = props;
let { t } = useTranslation();
//parentFieldShowOnValue
var showIfHasParentShowValue = isVisibleByInclusion({
parentField,
form,
formProps,
parentFieldShowOnValue
});
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
//console.log("parentField:", parentField, showIfHasParentShowValue);
const validateDecimal = (value) => {
if (!value) return t("newappeal:is-required-label");
// Regex to match whole numbers or decimal numbers (up to 7 characters including decimal)
const regex = /^\d{1,6}(\.\d{1,2})?$/; // Up to 6 digits before the decimal, 2 after
// Check if the value matches the regex pattern
if (!regex.test(value)) {
return t("newappeal:invalid-decimal-label");
}
return undefined;
};
const { requiredMessage, emojiNotAllowedMessage, invalidPostcodeMessage } =
getValidationMessages(t);
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
validateField(
value,
validation,
requiredMessage,
emojiNotAllowedMessage,
invalidPostcodeMessage
)
} // Use the external validate function
component={RenderTextfield}
label={props.label}
maxFieldLength={props.maxFieldLength}
/>
)
) : (
)}
>
);
}
export function ReadOnlyfield(props) {
const { name, label, value } = props;
//console.log(props.value.refno);
return (
<>
>
);
}
export const FieldsTranslations = (label) => {
const router = useRouter();
const { locale } = router;
const { appealtypes } = router.query;
return getFieldTranslation({
label,
locale,
appealtypes,
fieldLookup
});
};
export function FileUploadField(props) {
return (
<>
{FieldsTranslations(props.label)}{" "}
>
);
}