Skip to content

Commit

Permalink
Revert "Sreelekha | MOBN-2437 | Form Changes from lite branch"
Browse files Browse the repository at this point in the history
This reverts commit 11c7ccb.
  • Loading branch information
SreelekhaDarsi committed Jul 30, 2024
1 parent 11c7ccb commit 288a363
Showing 1 changed file with 69 additions and 6 deletions.
75 changes: 69 additions & 6 deletions ui/app/common/concept-set/directives/formControls.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ angular.module('bahmni.common.conceptSet')
unMountForm(document.getElementById(formUuid));
});
};

var controller = function ($scope) {
var formUuid = $scope.form.formUuid;
var formVersion = $scope.form.formVersion;
Expand All @@ -19,7 +18,6 @@ angular.module('bahmni.common.conceptSet')
var collapse = $scope.form.collapseInnerSections && $scope.form.collapseInnerSections.value;
var validateForm = $scope.validateForm || false;
var locale = $translate.use();

if (!loadedFormDetails[formUuid]) {
spinner.forPromise(formService.getFormDetail(formUuid, { v: "custom:(resources:(value))" })
.then(function (response) {
Expand Down Expand Up @@ -55,30 +53,95 @@ angular.module('bahmni.common.conceptSet')
unMountReactContainer($scope.form.formUuid);
}, 0, false);
}

$scope.$watch('form.collapseInnerSections', function () {
var collapse = $scope.form.collapseInnerSections && $scope.form.collapseInnerSections.value;
if (loadedFormDetails[formUuid]) {
$scope.form.component = renderWithControls(loadedFormDetails[formUuid], formObservations,
formUuid, collapse, $scope.patient, validateForm, locale, loadedFormTranslations[formUuid]);
}
});

$scope.$on('$destroy', function () {
if ($scope.$parent.consultation && $scope.$parent.consultation.observationForms) {
if ($scope.form.component) {
var formObservations = $scope.form.component.getValue();
$scope.form.observations = formObservations.observations;

var hasError = formObservations.errors;
if (!_.isEmpty(hasError)) {
$scope.form.isValid = false;
}
}
}
});
function checkGroupMembers (formObservation, consultationObservation) {
var isGroupMemberChanged = [];
if (formObservation.groupMembers && formObservation.groupMembers.length > 0 && consultationObservation.groupMembers && consultationObservation.groupMembers.length > 0) {
for (var formGroupIndex = 0; formGroupIndex < formObservation.groupMembers.length; formGroupIndex++) {
var formGroupMember = formObservation.groupMembers[formGroupIndex];
for (var consultationGroupIndex = 0; consultationGroupIndex < consultationObservation.groupMembers.length; consultationGroupIndex++) {
var consultationGroupMember = consultationObservation.groupMembers[consultationGroupIndex];
(formGroupMember.value && formGroupMember.value.uuid && consultationGroupMember.value && consultationGroupMember.value.uuid) ?
isGroupMemberChanged[formGroupIndex] = (consultationGroupMember.value.uuid === formGroupMember.value.uuid) ? false : true :
isGroupMemberChanged[formGroupIndex] = (consultationGroupMember.value === formGroupMember.value || (formGroupMember.value && consultationGroupMember.value === formGroupMember.value.toString())) ? false : true;
if (!isGroupMemberChanged[formGroupIndex]) {
break;
}
}
}
return isGroupMemberChanged.includes(true) ? true : false;
} else {
if (formObservation.value && formObservation.value.uuid && consultationObservation.value && consultationObservation.value.uuid) {
return (consultationObservation.value.uuid === formObservation.value.uuid) ? false : true;
} else {
return !((consultationObservation.value === formObservation.value || (formObservation.value && consultationObservation.value === formObservation.value.toString())));
}
}
}
function checkFormChanges ($scope) {
var isChanged = [];
$scope.dirtyForm = false;
if ($scope.form.observations.length > 0) {
if ($scope.$parent.consultation.observations.length === 0) {
return true;
}
for (var formIndex = 0; formIndex < $scope.form.observations.length; formIndex++) {
var formObservation = $scope.form.observations[formIndex];
for (var consultationIndex = 0; consultationIndex < $scope.$parent.consultation.observations.length; consultationIndex++) {
var consultationObservation = $scope.$parent.consultation.observations[consultationIndex];
isChanged[formIndex] = checkGroupMembers(formObservation, consultationObservation);
if (!isChanged[formIndex]) {
break;
}
}
}
return isChanged.includes(true);
}
}
$scope.$on('$stateChangeStart', function (event, next, current) {
var uuid = $state.params.patientUuid;
var currentUuid = current.patientUuid;
if ($scope.form.component) {
var formObservations = $scope.form.component.getValue();
$scope.form.observations = formObservations.observations;
}
if (!$scope.changesSaved) {
$scope.dirtyForm = checkFormChanges($scope);
}
$state.newPatientUuid = currentUuid;
next.url.includes("/patient/search") ? $state.isPatientSearch = true : $state.isPatientSearch = false;
var isNavigating = next.url.includes("/patient/search") || (uuid !== currentUuid);
$state.dirtyConsultationForm = $state.discardChanges ? false : $scope.dirtyForm;
if (isNavigating && $state.dirtyConsultationForm) {
messagingService.showMessage('alert', "{{'ALERT_MESSAGE_ON_EXIT' | translate }}");
$state.reviewButtonFocused = true;
event.preventDefault();
spinner.hide(next.spinnerToken);
}
});
$scope.$on("event:changes-saved", function () {
$scope.changesSaved = true;
$scope.dirtyForm = false;
});
};

return {
restrict: 'E',
scope: {
Expand Down

0 comments on commit 288a363

Please sign in to comment.