Skip to content

Commit

Permalink
#1178 | fix: saga for multi file
Browse files Browse the repository at this point in the history
  • Loading branch information
vedfordev committed Dec 11, 2024
1 parent 12cafb8 commit 28d9962
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 33 deletions.
35 changes: 8 additions & 27 deletions src/dataEntryApp/components/MediaUploader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Expand All @@ -142,8 +133,7 @@ function uploadMediaAndUpdateObservationValue(
isMultiSelect,
localObsValue,
update,
onDelete,
obsValue
onDelete
) {
etFiles.forEach(file => {
const fileReader = new FileReader();
Expand All @@ -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"}`;
Expand Down Expand Up @@ -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);
};

Expand Down
10 changes: 4 additions & 6 deletions src/dataEntryApp/services/FormElementService.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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();
Expand All @@ -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;
Expand Down

0 comments on commit 28d9962

Please sign in to comment.