diff --git a/components/case/representation/index.js b/components/case/representation/index.js index b86d4750..0eb50ad5 100644 --- a/components/case/representation/index.js +++ b/components/case/representation/index.js @@ -389,6 +389,8 @@ let MakeRepresentation = (props) => { caseReference={caseReference} setRepFileName={setRepFileName} updateRepresentation={updateRepresentation} + setSavingStatus={setSavingStatus} + savingStatus={savingStatus} /> ); } else { @@ -408,6 +410,8 @@ let MakeRepresentation = (props) => { caseReference={caseReference} setRepFileName={setRepFileName} updateRepresentation={updateRepresentation} + setSavingStatus={setSavingStatus} + savingStatus={savingStatus} />; break; case "appellant": @@ -431,6 +435,8 @@ let MakeRepresentation = (props) => { props={props} setRepFileName={setRepFileName} updateRepresentation={updateRepresentation} + setSavingStatus={setSavingStatus} + savingStatus={savingStatus} /> ); break; @@ -464,6 +470,8 @@ let MakeRepresentation = (props) => { questionnaireCount={questionnaireCount} setRepFileName={setRepFileName} updateRepresentation={updateRepresentation} + setSavingStatus={setSavingStatus} + savingStatus={savingStatus} /> ); break; @@ -488,6 +496,8 @@ let MakeRepresentation = (props) => { props={props} setRepFileName={setRepFileName} updateRepresentation={updateRepresentation} + setSavingStatus={setSavingStatus} + savingStatus={savingStatus} /> ); break; @@ -512,6 +522,8 @@ let MakeRepresentation = (props) => { props={props} setRepFileName={setRepFileName} updateRepresentation={updateRepresentation} + setSavingStatus={setSavingStatus} + savingStatus={savingStatus} /> ); break; @@ -615,6 +627,10 @@ let MakeRepresentation = (props) => { const sortFileObj = (filelistObj) => { let blobList = filelistObj; + function removeNullObjects(arr) { + return arr.filter((obj) => obj !== null); + } + function removeObjectsWithUndefinedKey(arr, key) { return arr.filter((obj) => obj[key] !== undefined); } @@ -623,6 +639,7 @@ let MakeRepresentation = (props) => { return arr.filter((obj) => Object.keys(obj).length > 0); } + blobList = removeNullObjects(blobList); blobList = removeEmptyObjects(blobList); blobList = removeObjectsWithUndefinedKey(blobList, "name"); @@ -819,6 +836,7 @@ let MakeRepresentation = (props) => { // Object.assign(values, { // "filesList": buildFileArray, // })), + uploadRepresentationFiles(values), generateRepPDF( values, @@ -931,54 +949,165 @@ let MakeRepresentation = (props) => { : values.representationCapacity, "locale": router.locale, })); - currentView.representationSubmit != true - ? _.isEmpty(currentView.representationCapacity) - ? isLPA - ? setRepresentationCapacity("lpa") - : setRepresentationCapacity( - values.representationCapacity - .replace(/(\band|[\s\-\+\(\)\,\&])/g, "") - .toLowerCase() - ) - : questionnaireCount != 0 && - showQuestionnaireSection < questionnaireCount - ? (setShowQuestionnaireSection(showQuestionnaireSection + 1), - updateRepresentation(values, false, false)) - : (updateRepresentation(values, false, false), - setRepresentationSubmit(true)) - : currentView.representationSubmit == true - ? (console.log("capacity", currentView.representationCapacity), - console.log("has errors:", props.invalid), - Object.assign(values, { "locale": router.locale }), - 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, - // })), - console.log( - "====================== rep lpa payload:\n", - JSON.stringify(values) - ), - generateRepPDF( - values, - props.props.accountDetails.containerID, - currentView.caseReference.currentReference - ), - uploadRepresentationFiles(values), - setRepresentationSubmitConfirmation(true)) - : console.log("nit updated"); + + if (currentView.representationSubmit != true) { + if (_.isEmpty(currentView.representationCapacity)) { + if (isLPA) { + setRepresentationCapacity( + values.representationCapacity + .replace(/(\band|[\s\-\+\(\)\,\&])/g, "") + .toLowerCase() + ); + } else { + setRepresentationCapacity( + values.representationCapacity + .replace(/(\band|[\s\-\+\(\)\,\&])/g, "") + .toLowerCase() + ); + } + } else { + if (values.representationType == "Questionnaire") { + if ( + questionnaireCount != 0 && + showQuestionnaireSection < questionnaireCount + ) { + setShowQuestionnaireSection( + showQuestionnaireSection + 1 + ); + updateRepresentation(values, false, false); + } else { + updateRepresentation(values, false, false); + setRepresentationSubmit(true); + } + } else { + updateRepresentation(values, false, false); + setRepresentationSubmit(true); + } + } + } else { + if (currentView.representationSubmit == true) { + console.log("capacity", currentView.representationCapacity); + console.log("has errors:", props.invalid); + Object.assign(values, { "locale": router.locale }); + 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, + // })), + console.log( + "====================== rep lpa payload:\n", + JSON.stringify(values) + ); + generateRepPDF( + values, + props.props.accountDetails.containerID, + currentView.caseReference.currentReference + ); + uploadRepresentationFiles(values); + setRepresentationSubmitConfirmation(true); + } else { + console.log("nit updated"); + } + } + + // currentView.representationSubmit != true + // ? _.isEmpty(currentView.representationCapacity) + // ? isLPA + // ? setRepresentationCapacity("lpa") + // : setRepresentationCapacity( + // values.representationCapacity + // .replace(/(\band|[\s\-\+\(\)\,\&])/g, "") + // .toLowerCase() + // ) + // : questionnaireCount != 0 && + // showQuestionnaireSection < questionnaireCount + // ? (setShowQuestionnaireSection(showQuestionnaireSection + 1), + // updateRepresentation(values, false, false)) + // : (updateRepresentation(values, false, false), + // setRepresentationSubmit(true)) + // : currentView.representationSubmit == true + // ? (console.log("capacity", currentView.representationCapacity), + // console.log("has errors:", props.invalid), + // Object.assign(values, { "locale": router.locale }), + // 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, + // // })), + // console.log( + // "====================== rep lpa payload:\n", + // JSON.stringify(values) + // ), + // 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 cleanFilesList = (blobList) => { + let newblobList = blobList.filesList; + function removeNullObjects(arr) { + if (arr.length === 0) { + return arr; + } + return arr.filter((obj) => obj !== null); + } + + function removeObjectsWithUndefinedKey(arr, key) { + if (arr.length === 0) { + return arr; + } + return arr.filter((obj) => obj[key] !== undefined); + } + + function removeEmptyObjects(arr) { + if (arr.length === 0) { + return arr; + } + return arr.filter( + (obj) => + obj && + typeof obj === "object" && + Object.keys(obj).length > 0 + ); + } + + newblobList = removeNullObjects(newblobList); + newblobList = removeEmptyObjects(newblobList); + newblobList = removeObjectsWithUndefinedKey(newblobList, "name"); + + Object.assign(blobList, { + "filesList": newblobList, + }); + + return blobList; + }; + const uploadRepresentationFiles = (values) => { + values = values.hasOwnProperty("filesList") + ? cleanFilesList(values) + : values; + // get filelists from values object let fileListObj = _.filter(values, function (v, key) { return _.includes(key, "representationDocuments"); @@ -1011,19 +1140,8 @@ let MakeRepresentation = (props) => { const repForm = props.props.form; - // try { - // let value = props.initialValues; - // setRepresentationCapacity( - // props.initialValues.representationCapacity.toLowerCase() - // ); - // //setRepresentationSubmit(false); - // } catch { - // console.log("not has props"); - // } - return (