import { JSONPath as jsonpath } from "jsonpath-plus";
import _ from "lodash";
import useTranslation from "next-translate/useTranslation";
import Link from "next/link";
import { useRouter } from "next/router";
import React, { useMemo, useState } from "react";
import DatePicker from "react-datepicker";
import "react-datepicker/dist/react-datepicker.css";
import Dropzone from "react-dropzone";
import { Field, FieldArray } from "redux-form";
import { deleteBlob, getFilesFromBlobHashed } from "../../actions";
import fieldLookup from "../../data/crmfieldlookuptranslations.json";
import pickListLookup from "../../data/picklistLookups.json";
import dynamic from "next/dynamic";
import { bytesToSize, getThumbnailIconByExtension } from "../utils";
import {
addDays,
addMonths,
addYears,
parseISO,
subDays,
subMonths,
subYears,
} from "date-fns";
import "react-quill-new/dist/quill.snow.css";
const ReactQuill = dynamic(() => import("react-quill-new"), { ssr: false });
const RenderTextfield = ({
id,
className,
rows,
datafieldname,
name,
label,
input,
errorMsg,
meta: { touched, error },
...custom
}) => {
return (
<>
{touched && error && (
Error:{" "}
{error}
)}
>
);
};
export function Textfield(props) {
const {
name,
label,
validation,
ticketnumber,
form,
formProps,
parentField,
parentFieldShowOnValue,
maxFieldLength,
hint,
} = props;
const required = (value) =>
value ? undefined : t("newappeal:is-required-label");
const postcode = (value) =>
value &&
!/^([A-Z][A-HJ-Y]?[0-9][A-Z0-9]? ?[0-9][A-Z]{2}|GIR ?0A{2})$/i.test(
value
)
? t("newappeal:invalid-postcode-label")
: undefined;
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 =
(parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
formProps[form].values[parentField]) == parentFieldShowOnValue;
// console.log(parentField, showIfHasParentShowValue);
// console.log(parentField, formProps, form);
//console.log("validation props:", validation);
return (
{parentField != false ? (
showIfHasParentShowValue && (
)
) : (
)}
);
}
}
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}))}
)}
>
);
};
export function MultiLinefield(props) {
const {
name,
label,
form,
formProps,
parentField,
parentFieldShowOnValue,
validation,
maxFieldLength,
hint,
} = props;
const required = (value) => (value ? undefined : "Is required");
var showIfHasParentShowValue =
parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
//console.log(showIfHasParentShowValue, formProps[form].values[parentField]);
let { t } = useTranslation();
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
{props.hasOwnProperty("hint") &&
props.hint != false &&
t(props.hint)}{" "}
{t("newappeal:new-appeal-multiline-pid")}
)
) : (
{props.hasOwnProperty("hint") &&
props.hint != false &&
t(props.hint)}{" "}
{t("newappeal:new-appeal-multiline-pid")}
)}
>
);
}
export const RenderRichMultiline = ({
id,
className,
rows,
datafieldname,
name,
label,
input,
meta: { touched, error },
...custom
}) => {
const [editValue, setEditValue] = useState(
input.value != null ? input.value : ""
);
const changeEdit = (valStr) => {
setEditValue(valStr);
input.onChange(valStr);
};
var modules = {
toolbar: [
[{ "header": [1, 2, false] }],
["bold", "italic", "underline", "blockquote"],
[{ "list": "ordered" }, { "list": "bullet" }],
["clean"],
],
};
return (
<>
>
);
};
export function RichMultiLinefield(props) {
const {
name,
label,
form,
formProps,
parentField,
parentFieldShowOnValue,
validation,
maxFieldLength,
} = props;
const required = (value) => (value ? undefined : "Is required");
var showIfHasParentShowValue =
parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
//console.log(showIfHasParentShowValue, formProps[form].values[parentField]);
let { t } = useTranslation();
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
{props.hasOwnProperty("hint") &&
props.hint != false &&
t(props.hint)}{" "}
{t("newappeal:new-appeal-multiline-pid")}
)
) : (
{props.hasOwnProperty("hint") &&
props.hint != false &&
t(props.hint)}{" "}
{t("newappeal:new-appeal-multiline-pid")}
)}
>
);
}
const RenderDatePicker = ({
datafieldname,
name,
label,
id,
errorMsg,
input: { onChange, value },
meta: { touched, error },
...custom
}) => {
const router = useRouter();
var startDateArr =
custom.dateStart != "0" ? custom.dateStart.split("") : custom.dateStart;
var endDateArr =
custom.dateEnd != "0" ? custom.dateEnd.split("") : custom.dateEnd;
var minDate =
startDateArr == 0
? new Date()
: startDateArr[0] == "-"
? startDateArr[2] == "y"
? subYears(new Date(), startDateArr[1])
: startDateArr[2] == "m"
? subMonths(new Date(), startDateArr[1])
: subDays(new Date(), startDateArr[1])
: startDateArr[2] == "y"
? addYears(new Date(), startDateArr[1])
: startDateArr[2] == "m"
? addMonths(new Date(), startDateArr[1])
: addDays(new Date(), startDateArr[1]);
var maxDate =
endDateArr == 0
? new Date()
: startDateArr[0] == "-"
? startDateArr[2] == "y"
? subYears(new Date(), startDateArr[1])
: startDateArr[2] == "m"
? subMonths(new Date(), startDateArr[1])
: subDays(new Date(), startDateArr[1])
: startDateArr[2] == "y"
? addYears(new Date(), startDateArr[1])
: startDateArr[2] == "m"
? addMonths(new Date(), startDateArr[1])
: addDays(new Date(), startDateArr[1]);
return (
<>
>
);
};
export function DateFieldPicker(props) {
const {
name,
label,
parentField,
form,
formProps,
parentFieldShowOnValue,
validation,
dateStart,
dateEnd,
} = props;
const required = (value) => (value ? undefined : "Required");
var showIfHasParentShowValue =
(parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
formProps[form].values[parentField]) == parentFieldShowOnValue;
return (
{parentField != false ? (
showIfHasParentShowValue && (
)
) : (
)}
);
}
export function DateField(props) {
const { name, label } = props;
return (
);
}
const RenderYesNo = ({
datafieldname,
name,
label,
id,
errorMsg,
options,
input: { onChange, value },
meta: { touched, error },
...custom
}) => {
return (
<>
>
);
};
export function YesNofield(props) {
const router = useRouter();
let { t } = useTranslation();
const {
name,
label,
inline,
form,
formProps,
validation,
parentFieldShowOnValue,
parentField,
} = props;
const yesNo = router.locale == "cy" ? ["Ydw", "Na"] : ["Yes", "No"];
const required = (value) => (value ? undefined : "Required");
var showIfHasParentShowValue =
parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
)
) : (
)}
>
);
}
const RenderRadio = ({
datafieldname,
name,
label,
id,
errorMsg,
options,
input: { onChange, value },
meta: { touched, error },
...custom
}) => {
let { t } = useTranslation();
return (
<>
>
);
};
export function Radiofield(props) {
const {
name,
label,
datafieldname,
form,
formProps,
parentFieldShowOnValue,
parentField,
validation,
} = props;
const router = useRouter();
const required = (value) => (value ? undefined : "Required");
// console.log(props.options);
let { t } = useTranslation();
const fieldOptions = props.options
.slice(1, props.options.length - 1)
.split(",");
//parentFieldShowOnValue
var showIfHasParentShowValue =
parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
)
) : (
)}
>
);
}
const RenderCheckBox = ({
datafieldname,
name,
label,
id,
errorMsg,
options,
input: { onChange, value },
meta: { touched, error },
...custom
}) => {
return (
<>
>
);
};
export function CheckBoxfield(props) {
const router = useRouter();
console.log(props);
const required = (value) => (value ? undefined : "Required");
const fieldOptions =
router.locale == "cy" ? ["Ydw", "Na", "2323"] : ["Yes", "No", "23232"];
return (
);
}
const RenderPickList = ({
datafieldname,
picklistData,
id,
name,
label,
input,
errorMsg,
meta: { touched, error },
}) => {
let { t } = useTranslation();
var dropdownObj = jsonpath(
"$..value[?(@.LogicalName=='" + datafieldname + "')]..Options",
picklistData
);
dropdownObj = dropdownObj[0];
const required = (value) => {
return value || value == 0 ? undefined : "Requirewwwd";
};
return (
<>
{touched && error && (
Error:{" "}
{error}
)}
>
);
};
export function PickList(props) {
const { name, label, datafieldname, hint } = props;
let { t } = useTranslation();
const required = (value) => {
return value || value == 0 ? undefined : "Is required";
};
return (
);
}
export function NumericField(props) {
const {
name,
label,
validation,
form,
formProps,
parentFieldShowOnValue,
parentField,
maxFieldLength,
} = props;
const required = (value) => {
return value || value == 0 ? undefined : "Is required";
};
const isNumber = (value) => {
const regex = /^\d*\.?\d{0,1}$/;
return regex.test(value) ? undefined : "Should be a number or decimal";
};
const maxLength = (max) => (value) =>
value && value.length > max
? `Must be ${max} characters or less`
: undefined;
//parentFieldShowOnValue
var showIfHasParentShowValue =
parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
//console.log("parentField:", parentField, showIfHasParentShowValue);
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
)
) : (
parseInt(value)}
// name={props.name}
// id={props.name}
// // component="input"
// type="text"
// className="govuk-input govuk-input--width-20 wwww"
// aria-describedby={name}
// pattern="^\d*(\.\d{0,2})?$"
// 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;
const required = (value) => (value ? undefined : "Is required");
const isNumber = (value) => {
const regex = /^\d*\.?\d{0,1}$/;
return regex.test(value) ? undefined : "Should be a number or decimal";
};
const maxLength = (max) => (value) =>
value && value.length > max
? `Must be ${max} characters or less`
: undefined;
//parentFieldShowOnValue
var showIfHasParentShowValue =
parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
//console.log("parentField:", parentField, showIfHasParentShowValue);
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
)
) : (
)}
>
);
}
const RenderCaseID = {};
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;
let formObj = jsonpath("$['" + appealtypes + "']", fieldLookup);
let labelTrans =
router.locale == "cy"
? jsonpath('$..[?(@.value=="' + label + '")].value_cy', formObj)
: label;
return labelTrans;
};
const PickListTranslations = (optionValue) => {
const router = useRouter();
const { locale } = router;
const { appealtypes } = router.query;
//console.log(optionValue);
let optionTrans =
router.locale == "cy"
? jsonpath(
'$..[?(@.value=="' + optionValue + '")].value_cy',
pickListLookup
)
: optionValue;
//console.log(optionTrans, optionValue);
return optionTrans;
};
export function FileUploadField(props) {
return (
<>
{FieldsTranslations(props.label)}{" "}
>
);
}
const baseStyle = {
display: "flex",
flexDirection: "column",
alignItems: "center",
padding: "20px",
borderWidth: 2,
borderRadius: 2,
borderColor: "#eeeeee",
borderStyle: "dashed",
backgroundColor: "#fafafa",
color: "#bdbdbd",
transition: "border .3s ease-in-out",
};
const activeStyle = {
borderColor: "#2196f3",
};
const acceptStyle = {
borderColor: "#00e676",
};
const rejectStyle = {
borderColor: "#ff1744",
};
const RenderFileUpload = (field) => {
const files = field.input.value;
let { t } = useTranslation();
const style = useMemo(
() => ({
...baseStyle,
// ...(isDragActive ? activeStyle : {}),
// ...(isDragAccept ? acceptStyle : {}),
// ...(isDragReject ? rejectStyle : {}),
}),
[]
// [isDragActive, isDragReject, isDragAccept]
);
const [filesList, setFilesList] = useState([]);
const thumbs = filesList.map((file) => (

-{" "}
{file.name} ({file.size / 1024}kb)
));
const getThumbnailIcon = (fileObj, fileType) => {
switch (fileType) {
case "text/html":
return "/assets/images/documenttypes/html.png";
break;
case "text/plain":
return "/assets/images/documenttypes/txt.png";
break;
case "application/msword":
return "/assets/images/documenttypes/doc.png";
break;
case "application/pdf":
return "/assets/images/documenttypes/pdf.png";
break;
case "application/vnd.openxmlformats-officedocument.wordprocessingml.document":
return "/assets/images/documenttypes/docx.png";
break;
case "text/csv":
return "/assets/images/documenttypes/csv.png";
break;
case "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet":
return "/assets/images/documenttypes/xlsx.png";
break;
case "application/zip":
return "/assets/images/documenttypes/zip.png";
break;
case "image/jpeg":
case "image/png":
return URL.createObjectURL(fileObj);
break;
default:
return "/assets/images/documenttypes/txt.png";
break;
}
};
const getDocumentType = (docCode) => {
const repDate = new Date();
let day = repDate.getDate();
let month = repDate.getMonth() + 1;
let year = repDate.getFullYear();
const pdfFileNameDateStamp = year + "-" + month + "-" + day;
switch (docCode) {
case "000000":
return pdfFileNameDateStamp + "_-_Statement_of_Case";
case "000001":
return pdfFileNameDateStamp + "_-_Application_Form";
break;
case "000002":
return pdfFileNameDateStamp + "_-_Site_Ownership_Certificate";
break;
case "000003":
return pdfFileNameDateStamp + "_-_Decision_Notice";
break;
case "000004":
return pdfFileNameDateStamp + "_-_Site_Location_Plan";
break;
case "000005":
return pdfFileNameDateStamp + "_-_Plans_Drawing_Documents";
break;
case "000006":
return (
pdfFileNameDateStamp +
"_-_Additional_Plans_Drawings_Documents"
);
break;
case "000007":
return pdfFileNameDateStamp + "_-_Design_and_Access_Statement";
break;
case "000008":
return (
pdfFileNameDateStamp + "_-_Green_Infrastructure_Statement"
);
break;
case "000009":
return pdfFileNameDateStamp + "_-_LPA_Correspondence";
break;
case "000010":
return pdfFileNameDateStamp + "_-_LPA_Original_Permission";
break;
case "000011":
return pdfFileNameDateStamp + "_-_LPA's_Registration_Letter";
break;
case "000012":
return pdfFileNameDateStamp + +"_-_Environmental_Statement";
break;
case "000013":
return pdfFileNameDateStamp + "_-_Cost_of_Application";
break;
case "000014":
return pdfFileNameDateStamp + "_-_Other_Relevant_Material";
break;
case "000015":
return (
pdfFileNameDateStamp +
"_-_S106_Agreement_or_Unilateral_Undertaking"
);
break;
default:
return pdfFileNameDateStamp + "_-_000000_-_";
}
};
const filelistObj = field.fileList || {};
const blobList = jsonpath(
"$..[?(@.documentType=='" + field.documentTypeCode + "')]",
filelistObj
);
//const blobList = filelistObj;
const deleteThisBlob = async (
containerName,
blobName,
deleteblobhash,
getblobshash,
casefolderID
) => {
//console.log(containerName, blobName, deleteblobhash);
deleteBlob(containerName, blobName, deleteblobhash, casefolderID)
.then((data) => data)
.then(() => {
getFilesFromBlobHashed(
containerName,
getblobshash,
casefolderID
).then((newfilelist) => field.setFilesForAppeal(newfilelist));
});
};
return (
<>
{
const renamedAcceptedFiles = filesToUpload.map(
(file) =>
new File(
[file],
`${getDocumentType(field.documentTypeCode)}_-_${
file.name
}`,
{
type: file.type,
}
)
);
field.input.onChange(renamedAcceptedFiles);
}}
>
{({ getRootProps, getInputProps }) => (
<>
{/* {field.hint != false && (
ss{t(field.hint)}
)}
*/}
>
)}
{field.meta.touched && field.meta.error && (
{field.meta.error}
)}
{files && Array.isArray(files) && (
{files.map(
(file, i) =>
typeof file.name != "undefined" && (
{" "}
{}
{/* {file.name.slice(file.name.indexOf("_") + 1)} ( */}{" "}
{file.name}( ({bytesToSize(file.size)})
)
)}
)}
{blobList.map(
(blob, i) =>
blob.documentType == field.documentTypeCode && (
{
deleteThisBlob(
field.containerID,
blob.name,
blob.hasheddeletepath,
blob.hashgetblobs,
field.ticketnumber
);
}}
title="Remove this file "
>
−
{blob.name} ({bytesToSize(blob.contentLength)})
)
)}{" "}
>
);
};
const renderField = ({ input, label, type, meta: { touched, error } }) => (
);
const RenderSubFields = ({ fields }) => {
let { t } = useTranslation();
let subTitle = fields.name.split("pinswg_")[1];
return (
);
};
export const FieldArrayForm = (props) => {
const {
name,
label,
validation,
form,
formProps,
parentFieldShowOnValue,
parentField,
maxFieldLength,
} = props;
var showIfHasParentShowValue =
parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
(showIfHasParentShowValue == parentField) != false &&
showIfHasParentShowValue;
parentFieldShowOnValue;
const { handleSubmit, pristine, reset, submitting } = props;
return (
<>
{" "}
{showIfHasParentShowValue && (
)}
>
);
};