import useSWR from "swr";
import { Field, reduxForm } from "redux-form";
import { useRouter } from "next/router";
import DatePicker, { registerLocale, setDefaultLocale } from "react-datepicker";
import cy from "date-fns/locale/cy";
import React, { useCallback, useEffect, useMemo, useState } from "react";
import "react-datepicker/dist/react-datepicker.css";
import moment from "moment";
import jsonpath from "jsonpath";
import fieldLookup from "../../data/crmfieldlookuptranslations.json";
import pickListLookup from "../../data/picklistLookups.json";
import useTranslation from "next-translate/useTranslation";
import FileUpload from "../newappeal/fileupload";
import Dropzone, { useDropzone } from "react-dropzone";
import _ from "lodash";
import { connect, useDispatch } from "react-redux";
import {
addYears,
addMonths,
addDays,
subYears,
subMonths,
subDays,
} from "date-fns";
const RenderTextfield = ({
id,
className,
rows,
datafieldname,
name,
label,
input,
errorMsg,
meta: { touched, error },
...custom
}) => {
return (
<>
{touched && error && (
Error:{" "}
{errorMsg}
)}
>
);
};
export function Textfield(props) {
const {
name,
label,
validation,
ticketnumber,
form,
formProps,
parentField,
parentFieldShowOnValue,
hint,
} = props;
const required = (value) => (value ? undefined : "Required");
if (name == "pinswg_name") {
let { t } = useTranslation();
return (
<>
{t(hint)}
>
);
} 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,
parentField,
form,
formProps,
parentFieldShowOnValue,
validation,
} = props;
const required = (value) => (value ? undefined : "Is required");
var showIfHasParentShowValue =
(parentField != false &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
formProps[form].values[parentField]) == parentFieldShowOnValue;
// console.log(parentField, showIfHasParentShowValue);
return (
<>
{parentField != false ? (
showIfHasParentShowValue && (
Do not include personal or financial information.
)
) : (
Do not include personal or financial information.
)}
>
);
}
const RenderDatePicker = ({
datafieldname,
name,
label,
id,
errorMsg,
input: { onChange, value },
meta: { touched, error },
...custom
}) => {
const router = useRouter();
registerLocale("cy", cy);
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();
const { name, label, inline, validation } = props;
const yesNo = router.locale == "cy" ? ["Ydw", "Na"] : ["Yes", "No"];
const required = (value) => (value ? undefined : "Required");
return (
);
}
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,
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 &&
!_.isEmpty(formProps[form]) &&
_.has(formProps[form].values, parentField) &&
parentFieldShowOnValue.indexOf(
formProps[form].values[parentField].toString()
) > -1;
// console.log(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,
name,
label,
input,
meta: { touched, errorStr },
}) => {
let { t } = useTranslation();
var dropdownObj = jsonpath.query(
picklistData,
"$..value[?(@.LogicalName=='" + datafieldname + "')]..Options"
);
dropdownObj = dropdownObj[0];
return (
{touched && errorStr && {errorStr}}
);
};
export function PickList(props) {
const { name, label, datafieldname } = props;
return (
);
}
export function NumericField(props) {
const { name, label, validation } = props;
const required = (value) => (value ? undefined : "Required");
return (
);
}
export function DecimalField(props) {
const { name, label, validation } = props;
const required = (value) => (value ? undefined : "Required");
return (
);
}
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.query(fieldLookup, "$['" + appealtypes + "']");
let labelTrans =
router.locale == "cy"
? jsonpath.query(
formObj,
"$..[?(@.value=='" + label + "')].value_cy"
)
: 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.query(
pickListLookup,
'$..[?(@.value=="' + optionValue + '")].value_cy'
)
: optionValue;
//console.log(optionTrans, optionValue);
return optionTrans;
};
export function FileUploadField(props) {
return (
);
}
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 RenderFileUpload2 = ({
// id,
// className,
// rows,
// datafieldname,
// name,
// label,
// input,
// errorMsg,
// meta: { touched, errorStr },
// ...custom
// }) => {
// const [files, setFiles] = useState([]);
// const onDrop = useCallback((acceptedFiles, fileRejections, event) => {
// setFiles(
// acceptedFiles.map((file) =>
// Object.assign(file, {
// preview: URL.createObjectURL(file),
// })
// )
// );
// console.log(acceptedFiles);
// event.target[acceptedFiles];
// console.log(event.target.files);
// }, []);
// const onChange = useCallback((acceptedFiles, fileRejections, event) => {
// setFiles(
// acceptedFiles.map((file) =>
// Object.assign(file, {
// preview: URL.createObjectURL(file),
// })
// )
// );
// event.target.files[acceptedFiles];
// console.log(event.target.files);
// }, []);
// const {
// getRootProps,
// getInputProps,
// isDragActive,
// isDragAccept,
// isDragReject,
// } = useDropzone({
// onDrop,
// onChange,
// });
// const style = useMemo(
// () => ({
// ...baseStyle,
// ...(isDragActive ? activeStyle : {}),
// ...(isDragAccept ? acceptStyle : {}),
// ...(isDragReject ? rejectStyle : {}),
// }),
// [isDragActive, isDragReject, isDragAccept]
// );
// const thumbs = files.map((file) => (
//
//

-{" "}
//
// {file.name} ({file.size / 1024}kb)
//
//
// ));
// // clean up
// useEffect(
// () => () => {
// files.forEach((file) => URL.revokeObjectURL(file.preview));
// },
// [files]
// );
// const onChangeFile = (e) => {
// const {
// input: { onChange },
// } = props;
// onChange(e.target.files[0]);
// };
// return (
//
//
//
//
//
//
Drag and drop your images here.
//
// {" "}
//
// {touched && errorStr &&
{errorStr}}
//
// );
// };
const RenderFileUpload = (field) => {
const files = field.input.value;
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)
));
function bytesToSize(bytes) {
var sizes = ["Bytes", "KB", "MB", "GB", "TB"];
if (bytes == 0) return "0 Byte";
var i = parseInt(Math.floor(Math.log(bytes) / Math.log(1024)));
return Math.round(bytes / Math.pow(1024, i), 2) + " " + sizes[i];
}
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;
}
};
return (
field.input.onChange(filesToUpload)
}
>
{({ getRootProps, getInputProps }) => (
)}
{field.meta.touched && field.meta.error && (
{field.meta.error}
)}
{files && Array.isArray(files) && (
{files.map((file, i) => (

{" "}
{" "}
{file.name} ({bytesToSize(file.size)})
))}
)}
);
};