import Link from "next/link"; import { useState, useEffect } from "react"; import { useRouter } from "next/router"; import useTranslation from "next-translate/useTranslation"; import jsonpath, { value } from "jsonpath"; import _, { property } from "lodash"; import { Field, FieldArray, reduxForm, formValueSelector, reset, } from "redux-form"; import { connect } from "react-redux"; import { useDropzone } from "react-dropzone"; import { setRepresentationCapacity, setRepresentationSubmit, setSubmitBack, setRepresentationSubmitConfirmation, setRepresentationMessageSent, setRepresentationReset, } from "../../../store/currentView/action"; import RepCapacitySelection from "./representationCapacitySelection"; import RepLPACapacitySelection from "./representationLPACapacitySelection"; import RepDetails from "./representationDetails"; import RepAppellant from "./representationAppellant"; import RepAgent from "./representationAgent"; import RepInterestedPartyPerson from "./representationInterestedPartyPerson"; import RepLandOwner from "./representationLandowner"; import RepLPA from "./representationLPA"; import RepCompleteSubmit from "./representationCompleteSubmit"; import { useSession, signIn, signOut } from "next-auth/react"; import { updateCase, patchCase, uploadRepFiles, sendEmail, generateRepPDF, } from "../../../actions"; import { formatDates } from "../../../components/utils"; import RepresentationProgress_s78 from "./representationProgress/representationProgress_s78"; import RepresentationProgress_enforcement from "./representationProgress/representationProgress_enforcement"; let MakeRepresentation = (props) => { let { t } = useTranslation(); const { acceptedFiles, getRootProps, getInputProps } = useDropzone(); const [clearRepForm, setClearRepForm] = useState(false); const { data: session } = useSession(); const router = useRouter(); const { locale } = router; const { caseReference, myCases, myRepresentations, watchedCases, awaitingSubmission, searchResultsObj, representationSubmit, currentType, currentView, setRepresentationCapacity, setRepresentationSubmit, setSubmitBack, handleSubmit, setRepresentationSubmitConfirmation, setRepresentationMessageSent, initialValues, } = props; const formObj = props.props.form; let setCaseQueryObj = props[currentType] || {}; var casesObj = {}; const [showQuestionnaireSection, setShowQuestionnaireSection] = useState(1); const isLPA = props.props.accountDetails.accountDetails[ "pinswg_typeofinvolvement@OData.Community.Display.V1.FormattedValue" ] == "LPA" ? true : false; currentType == "searchResultsObj" ? (casesObj = jsonpath.query( setCaseQueryObj, '$..[?(@.title=="' + caseReference + '")]' )) : (casesObj = jsonpath.query( setCaseQueryObj, '$..[?(@.reference=="' + caseReference + '")]' )); casesObj = Object.assign({}, ...casesObj); var detailsObj = {}; const capacityOptionsArr = [ t("myrepresentations:capacity-options-arr-appellant"), t("myrepresentations:capacity-options-arr-agent"), t("myrepresentations:capacity-options-arr-interested"), ]; let setCaseDetailsObj = router.query.hasOwnProperty("state") ? router.query.state == "edit" ? props.props.myRepresentations.myRepresentations : props.props.searchResultsObj.searchDetailsObj : props.props.searchResultsObj.searchDetailsObj; var detailsObj = {}; detailsObj = jsonpath.query( setCaseDetailsObj, '$..[?(@.pinswg_name=="' + currentView.caseReference.currentReference + '")]' ); detailsObj = detailsObj[0]; const buildRepsArr = (detailsObj) => { let buildArr = []; let todaysDate = new Date(); let startDate = new Date(detailsObj.pinswg_startdate); let questionnaireDate = new Date( detailsObj.pinswg_questionnaireduedate ); let finalCommentsDate = new Date( detailsObj.pinswg_finalcommentsduedate ); let statementDueDate = new Date(detailsObj.pinswg_statementduedate); isLPA && todaysDate >= startDate && todaysDate <= questionnaireDate && buildArr.push("Questionnaire"); todaysDate >= startDate && todaysDate <= statementDueDate && buildArr.push("Statement"); todaysDate >= statementDueDate && todaysDate <= finalCommentsDate && buildArr.push("Final comments"); return buildArr; }; const repCapacityType = () => { return _.isEmpty(formObj["representationForm"]) ? false : _.isEmpty(formObj["representationForm"].values) ? false : _.isEmpty( formObj["representationForm"].values.representationCapacity ) ? false : formObj["representationForm"].values.representationCapacity .replace(/(\band|[\s\-\+\(\)\,\&])/g, "") .toLowerCase(); }; const whichControl = () => { // console.log(isLPA); // console.log( // currentView.representationCapacity, // _.isEmpty(currentView.representationCapacity) // ); if (isLPA && _.isEmpty(currentView.representationCapacity)) { return ( ); } else { switch (currentView.representationCapacity) { case false: ; break; case t( "myrepresentations:capacity-options-arr-appellant" ).toLocaleLowerCase(): //setRepresentationCapacity("appellant"); return ( ); break; case "lpa": //setRepresentationCapacity("appellant"); return ( ); break; case t( "myrepresentations:capacity-options-arr-agent" ).toLowerCase(): //setRepresentationCapacity("agent"); return ( ); break; case t( "myrepresentations:capacity-options-arr-interested" ).toLowerCase(): // setRepresentationCapacity("interestedpartyperson"); return ( ); break; case "landowner": //setRepresentationCapacity("landowner"); return ( ); break; default: return ( ); } } }; const whichProgress = () => { { console.log("curr"); switch (currentView.caseReference.appealType) { case 846040000: case 846040001: case 846040006: case 846040025: case 846040004: case 846040018: case 846040003: case 846040009: case 846040008: return ( ); break; case 846040005: case 846040006: case 846040007: return ( ); break; default: return <>hello; break; } } }; const updateRepresentation = async ( values, sendSavedEmail, useSaveStatus ) => { let incidentId = props.appealType.caseReference.incidentid; let updateBody = values || {}; // "pinswg_name": "2022-11-29-IP-REP_BOND_1", const repDate = new Date(); let day = repDate.getDate(); let month = repDate.getMonth() + 1; let year = repDate.getFullYear(); let time = repDate.getTime(); let repType = ""; switch (values.representationCapacity) { case t("myrepresentations:capacity-options-arr-appellant"): repType = "APPELLANT"; break; case t("myrepresentations:capacity-options-arr-agent"): repType = "AGENT"; break; case t("myrepresentations:capacity-options-arr-interested"): repType = "IP"; break; case "Land Owner": repType = "LO"; break; case "lpa": repType = "LPA"; break; default: // code block } Object.assign(updateBody, { "repfile_name": updateBody.hasOwnProperty("repfile_name") ? updateBody.repfile_name : year + "-" + month + "-" + day + "-" + time + "-" + repType + "-REP" + "_" + props.props.accountDetails.accountDetails.lastname, "pinswg_name": props.props.currentView.caseReference.currentReference, "containerID": props.props.accountDetails.containerID, "appealType": props.props.currentView.caseReference.appealType, "incidentID": props.props.currentView.caseReference.incidentid, "caseRef": props.props.currentView.caseReference.currentReference, }); updateBody = JSON.stringify(updateBody); updateBody = updateBody.replace(/:"Yes"/gm, `:true`); updateBody = updateBody.replace(/:"No"/gm, `:false`); updateBody = JSON.parse(updateBody); Object.keys(updateBody).forEach((key) => { if (updateBody[key] === null) { delete updateBody[key]; } if (key.indexOf("_") == 0) { delete updateBody[key]; } }); //console.log(updateBody); //window.alert(`form is :\n\n${JSON.stringify(updateBody, null, 2)}`); const reference = "PEDW-PARTIAL-REP"; const templateId = "021b0a7b-df00-41f1-b94e-c269bee98c75"; const emailAddress = props.props.accountDetails.loggedinUserEmail; const personalisation = { "caseReference": props.caseReference, "emailAddress": props.props.accountDetails.loggedinUserEmail, "returnLink": "", "linkExpiry": 10 * 60, }; props.currentView.representationSubmit == "true" && (sendSavedEmail == true && sendEmail(templateId, emailAddress, personalisation, reference), useSaveStatus && router.replace( router.locale != "en" ? router.locale + "/myportal" : "/myportal" )); }; const onHandleSubmit = (values) => { console.log("form values - ", JSON.stringify(values)); const buildFileArray = []; console.log("attached docs: - ", buildFileArray); let setCaseDetailsObj = router.query.hasOwnProperty("state") ? router.query.state == "edit" ? props.props.myRepresentations.myRepresentations : props.props.searchResultsObj.searchDetailsObj : props.props.searchResultsObj.searchDetailsObj; router.query.hasOwnProperty("state") && router.query.state == "edit" && setRepresentationSubmit(true); var detailsObj = {}; detailsObj = jsonpath.query( setCaseDetailsObj, '$..[?(@.pinswg_name=="' + caseReference + '")]' ); detailsObj = detailsObj[0]; console.log( "Has data from form:", formObj.hasOwnProperty("representationForm") && formObj.representationForm.hasOwnProperty("values") ); formObj.hasOwnProperty("representationForm") && !router.query.hasOwnProperty("state") && formObj.representationForm.hasOwnProperty("values") && (Object.assign(values, formObj.representationForm.values), Object.assign(values, { "containerID": props.props.accountDetails.containerID, "appealType": props.props.currentView.caseReference.appealType, "incidentID": props.props.currentView.caseReference.incidentid, "caseRef": props.props.currentView.caseReference.currentReference, "pinswg_name": props.props.currentView.caseReference.currentReference, "firstname": props.props.accountDetails.accountDetails.firstname, "lastname": props.props.accountDetails.accountDetails.lastname, "emailAddress": props.props.accountDetails.accountDetails.emailaddress1, "siteAddress": detailsObj.pinswg_siteaddressline1 + " " + detailsObj.pinswg_siteaddressline2 + " " + detailsObj.pinswg_siteaddresstown + " " + detailsObj.pinswg_siteaddresspostcode, "pinswg_siteaddressline1": detailsObj.pinswg_siteaddressline1, "pinswg_siteaddressline2": detailsObj.pinswg_siteaddressline2, "pinswg_siteaddresstown": detailsObj.pinswg_siteaddresstown, "pinswg_siteaddresspostcode": detailsObj.pinswg_siteaddresspostcode, "_pinswg_appellant_value": detailsObj["_pinswg_appellant_value"], "_pinswg_appellant_value@OData.Community.Display.V1.FormattedValue": detailsObj[ "_pinswg_appellant_value@OData.Community.Display.V1.FormattedValue" ], "_pinswg_localplanningauthority_value": detailsObj["_pinswg_localplanningauthority_value"], "_pinswg_localplanningauthority_value@OData.Community.Display.V1.FormattedValue": detailsObj[ "_pinswg_localplanningauthority_value@OData.Community.Display.V1.FormattedValue" ], "representationCapacity": router.query.hasOwnProperty("state") ? router.query.state == "edit" ? props.props.currentView.representationCapacity : values.representationCapacity : isLPA ? "lpa" : values.representationCapacity, "locale": locale, })); console.log(values); currentView.representationSubmit != true ? _.isEmpty(currentView.representationCapacity) ? isLPA ? setRepresentationCapacity("lpa") : setRepresentationCapacity( values.representationCapacity .replace(/(\band|[\s\-\+\(\)\,\&])/g, "") .toLowerCase() ) : setRepresentationSubmit(true) : currentView.representationSubmit == true ? (console.log("capacity", currentView.representationCapacity), console.log("has errors:", props.invalid), props.invalid == false && updateRepresentation(values, false, false), values.hasOwnProperty("representationDocuments") && (values.representationDocuments.map((Blob) => buildFileArray.push({ "name": Blob.name, "size": Blob.size, }) ), Object.assign(values, { "filesList": buildFileArray })), generateRepPDF( values, props.props.accountDetails.containerID, currentView.caseReference.currentReference ), uploadRepresentationFiles(values), setRepresentationSubmitConfirmation(true)) : console.log("nit updated"); //window.alert(`You submitted:\n\n${JSON.stringify(values, null, 2)}`); }; const uploadRepresentationFiles = (values) => { // get filelists from values object let fileListObj = _.filter(values, function (v, key) { return _.includes(key, "representationDocuments"); }); console.log(fileListObj); console.log( "\n////////////////////////\n upload files:", values.containerID ); var dataObj = values; for (let key in dataObj) { _.includes(key, "representationDocuments") == true && delete dataObj[key]; } uploadRepFiles( dataObj, fileListObj, values.containerID, props.caseReference + "/" + dataObj.repfile_name ).then((data) => { //console.log("hello", data); return data; }); }; const repForm = props.props.form; try { let value = props.initialValues; //console.log("has props"), value; setRepresentationCapacity( props.initialValues.representationCapacity.toLowerCase() ); //setRepresentationSubmit(false); } catch { console.log("not has props"); } return (
{currentView.representationSubmit == true ? ( ) : ( <> {!session ? ( <>

Some text in here to explain about raising a representation and why logging in

) : ( <>

{t( "myrepresentations:make-a-representation-on-label" )}{" "} {currentView.caseReference.currentReference}

{whichControl()}
{" "}

{t( "myrepresentations:timetable-title" )}

{t( "myrepresentations:questionnaire-due-label" )} : {" "} {formatDates( detailsObj.pinswg_questionnaireduedate )}
{t( "myrepresentations:statements-due-label" )} : {" "} {formatDates( detailsObj.pinswg_statementduedate )}
{t( "myrepresentations:final-comments-due-label" )} : {" "} {formatDates( detailsObj.pinswg_finalcommentsduedate )}
{ formObj.hasOwnProperty( "representationForm" ) && formObj[ "representationForm" ].hasOwnProperty("values") && formObj[ "representationForm" ].values.hasOwnProperty( "representationType" ) && formObj["representationForm"] .values .representationType == "Questionnaire" && whichProgress() // }
)} )}
); }; const mapStateToProps = (state) => { //console.log("\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \n in map state", state); return { search: state.search, searchResultsObj: state.searchResultsObj, formData: state.formData, appealType: state.appealType, currentView: state.currentView, myRepresentations: state.myRepresentations, initialValues: state.currentView.caseReference.repDetails, //form: state.form, }; }; const mapDispatchToProps = (dispatch) => { return { setRepresentationCapacity: (representationCapacity) => { //dispatch(reset("representationForm")); dispatch(setRepresentationCapacity(representationCapacity)); }, setRepresentationSubmit: (representationSubmit) => { dispatch(setRepresentationSubmit(representationSubmit)); }, setSubmitBack: () => { dispatch(setRepresentationSubmit(false)); }, setRepresentationSubmitConfirmation: () => { dispatch(setRepresentationSubmitConfirmation(true)); }, setRepresentationMessageSent: (messageSent) => { dispatch(setRepresentationMessageSent(messageSent)); }, setRepresentationReset: () => { dispatch(setRepresentationReset()); }, }; }; const selector = formValueSelector("representationForm"); export default connect( mapStateToProps, mapDispatchToProps )( reduxForm({ form: "representationForm", destroyOnUnmount: false, enableReinitialize: true, })(MakeRepresentation) );