From 28d9962be8858fea891c12c91c4966134eb8658f Mon Sep 17 00:00:00 2001 From: vedfordev Date: Wed, 11 Dec 2024 14:51:11 +0530 Subject: [PATCH] avniproject/avni-webapp#1178 | fix: saga for multi file --- src/dataEntryApp/components/MediaUploader.js | 35 +++++-------------- .../services/FormElementService.js | 10 +++--- 2 files changed, 12 insertions(+), 33 deletions(-) diff --git a/src/dataEntryApp/components/MediaUploader.js b/src/dataEntryApp/components/MediaUploader.js index 4fc837156..901ce7ac9 100644 --- a/src/dataEntryApp/components/MediaUploader.js +++ b/src/dataEntryApp/components/MediaUploader.js @@ -89,33 +89,24 @@ function invokeUpdate(update, mediaUrl) { update(mediaUrl); } -function addMediaUrlToLocalObsValue(update, fileName, isMultiSelect, localObsValue, setLocalObsValue, obsValue) { +function addMediaUrlToLocalObsValue(update, fileName, isMultiSelect, localObsValue, setLocalObsValue) { + invokeUpdate(update, fileName); if (isMultiSelect) { setLocalObsValue(locObsValue => locObsValue && !isArrayLikeObject(locObsValue) ? [locObsValue, fileName].filter(ele => !isEmpty(ele)) : [...(locObsValue || []), fileName].filter(ele => !isEmpty(ele)) ); - const files = - obsValue && !isArrayLikeObject(obsValue) - ? [obsValue, fileName].filter(ele => !isEmpty(ele)) - : [...(obsValue || []), fileName].filter(ele => !isEmpty(ele)); - invokeUpdate(update, files); } else { setLocalObsValue(fileName); - invokeUpdate(update, fileName); } } -function removeMediaUrlFromLocalObsValue(update, fileName, isMultiSelect, localObsValue, setLocalObsValue, obsValue) { - if (isMultiSelect && isArrayLikeObject(obsValue)) { - const files = obsValue.filter(item => item !== fileName); - invokeUpdate(update, files); - } +function removeMediaUrlFromLocalObsValue(update, fileName, isMultiSelect, localObsValue, setLocalObsValue) { + invokeUpdate(update, fileName); if (isMultiSelect && isArrayLikeObject(localObsValue)) { setLocalObsValue(locObsValue => locObsValue.filter(item => item !== fileName)); } else { - invokeUpdate(update, fileName); if (localObsValue === fileName) setLocalObsValue(); //Remove previous value } } @@ -142,8 +133,7 @@ function uploadMediaAndUpdateObservationValue( isMultiSelect, localObsValue, update, - onDelete, - obsValue + onDelete ) { etFiles.forEach(file => { const fileReader = new FileReader(); @@ -154,7 +144,7 @@ function uploadMediaAndUpdateObservationValue( .uploadFile("/web/uploadMedia", file) .then(r => { setUploadButtonClicked(oldValue => oldValue - 1); - addMediaUrlToLocalObsValue(update, r.data, isMultiSelect, localObsValue, setLocalObsValue, obsValue); + addMediaUrlToLocalObsValue(update, r.data, isMultiSelect, localObsValue, setLocalObsValue); }) .catch(r => { const error = `${get(r, "response.data") || get(r, "message") || "Unknown error occurred while uploadButtonClicked media"}`; @@ -202,20 +192,11 @@ export const MediaUploader = ({ label, obsValue, mediaType, update, formElement alert(alerts); return; } - uploadMediaAndUpdateObservationValue( - etFiles, - setUploadButtonClicked, - setLocalObsValue, - isMultiSelect, - localObsValue, - update, - onDelete, - obsValue - ); + uploadMediaAndUpdateObservationValue(etFiles, setUploadButtonClicked, setLocalObsValue, isMultiSelect, localObsValue, update, onDelete); }; const onDelete = fileName => { - removeMediaUrlFromLocalObsValue(update, fileName, isMultiSelect, localObsValue, setLocalObsValue, obsValue); + removeMediaUrlFromLocalObsValue(update, fileName, isMultiSelect, localObsValue, setLocalObsValue); removeFileFromPreview(fileName, preview, setPreview); }; diff --git a/src/dataEntryApp/services/FormElementService.js b/src/dataEntryApp/services/FormElementService.js index d84466b13..4e12ef219 100644 --- a/src/dataEntryApp/services/FormElementService.js +++ b/src/dataEntryApp/services/FormElementService.js @@ -32,7 +32,8 @@ export default { (formElement.concept.datatype === Concept.dataType.Coded || formElement.concept.datatype === Concept.dataType.Subject || formElement.concept.datatype === Concept.dataType.Image || - formElement.concept.datatype === Concept.dataType.Video) + formElement.concept.datatype === Concept.dataType.Video || + formElement.concept.datatype === Concept.dataType.File) ) { const observation = observationsHolder.toggleMultiSelectAnswer(formElement.concept, value); return observation && observation.getValueWrapper(); @@ -41,7 +42,8 @@ export default { (formElement.concept.datatype === Concept.dataType.Coded || formElement.concept.datatype === Concept.dataType.Subject || formElement.concept.datatype === Concept.dataType.Image || - formElement.concept.datatype === Concept.dataType.Video) + formElement.concept.datatype === Concept.dataType.Video || + formElement.concept.datatype === Concept.dataType.File) ) { const observation = observationsHolder.toggleSingleSelectAnswer(formElement.concept, value); return observation && observation.getValueWrapper(); @@ -55,10 +57,6 @@ export default { const { phoneNumber, isVerified } = value; observationsHolder.updatePhoneNumberValue(formElement.concept, phoneNumber, isVerified); return phoneNumber; - } else if (formElement.concept.datatype === Concept.dataType.File) { - //TODO Handle File same as Image or Video later when its fixed in MediaUploader as well - const observation = observationsHolder.toggleSingleSelectAnswer(formElement.concept, value); - return observation && observation.getValueWrapper(); } else { observationsHolder.addOrUpdatePrimitiveObs(formElement.concept, value); return value;