Skip to content

Commit

Permalink
fix: correctly handles the enabling of submit button on reset
Browse files Browse the repository at this point in the history
- disables the submit button on the click of reset button except for the
cases when there is a gentle alert notification.
- adds an argument to the disableAllButtonsWhileRunning function to
identify the caller was reset/submit/save.

closes openedx/frontend-app-learning#1406

Signed-off by: Ishan Masdekar <imasdekar@disroot.org>
  • Loading branch information
imasdekar committed Jul 17, 2024
1 parent c370028 commit 7deaedf
Showing 1 changed file with 51 additions and 8 deletions.
59 changes: 51 additions & 8 deletions xmodule/js/src/capa/display.js
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@
this.url = this.el.data('url');
this.content = this.el.data('content');

// constants for denoting the click of submit/save/reset button
this.submitClick = "submit"
this.saveClick = "save"
this.resetClick = "reset"

// has_timed_out and has_response are used to ensure that
// we wait a minimum of ~ 1s before transitioning the submit
// button from disabled to enabled
Expand Down Expand Up @@ -630,7 +635,7 @@

Problem.prototype.submit = function() {
if (!this.submit_save_waitfor(this.submit_internal)) {
this.disableAllButtonsWhileRunning(this.submit_internal, true);
this.disableAllButtonsWhileRunning(this.submit_internal, true, false, this.submitClick);
}
};

Expand Down Expand Up @@ -696,7 +701,7 @@
};

Problem.prototype.reset = function() {
return this.disableAllButtonsWhileRunning(this.reset_internal, false);
return this.disableAllButtonsWhileRunning(this.reset_internal, false, true, this.resetClick);
};

Problem.prototype.reset_internal = function() {
Expand Down Expand Up @@ -802,7 +807,7 @@

Problem.prototype.save = function() {
if (!this.submit_save_waitfor(this.save_internal)) {
this.disableAllButtonsWhileRunning(this.save_internal, false);
this.disableAllButtonsWhileRunning(this.save_internal, false, this.saveClick);
}
};

Expand Down Expand Up @@ -1217,20 +1222,58 @@
*
* params:
* 'operationCallback' is an operation to be run.
* isFromCheckOperation' is a boolean to keep track if 'operationCallback' was
* isFromCheckOperation' is an boolean to keep track if 'operationCallback' was
* from submit, if so then text of submit button will be changed as well.
* isWhichOperation' is an str to keep track if 'operationCallback' was
* from submit/reset/save, if so then handle the submit button enabling/disabling accordingly.
*
*/
Problem.prototype.disableAllButtonsWhileRunning = function(operationCallback, isFromCheckOperation) {
Problem.prototype.disableAllButtonsWhileRunning = function(operationCallback, isFromCheckOperation, isWhichOperation) {
var that = this;
var allButtons = [this.resetButton, this.saveButton, this.showButton, this.hintButton, this.submitButton];
var initiallyEnabledButtons = allButtons.filter(function(button) {

// Array of all buttons except the submit button
var buttonsExceptSubmit = [this.resetButton, this.saveButton, this.showButton, this.hintButton];
// Array containing only the submit button
var submitButtonArr = [this.submitButton];

// Check if the submit button is initially enabled
const submitInitiallyEnabled = !this.submitButton.attr('disabled');

// Filter out buttons that are initially enabled
var initiallyEnabledButtons = buttonsExceptSubmit.filter(function(button) {
return !button.attr('disabled');
});

if ((isWhichOperation === this.submitClick) || (isWhichOperation === this.saveClick)) {
// Add the submit button to the initially enabled buttons if it was initially enabled
if (submitInitiallyEnabled) {
initiallyEnabledButtons.push(this.submitButton);
}
} else if (isWhichOperation === this.resetClick) {
// If reset is clicked, disable the submit button if it was initially enabled
if (submitInitiallyEnabled) {
this.enableButtons(submitButtonArr, false, isFromCheckOperation);
}
}
this.enableButtons(initiallyEnabledButtons, false, isFromCheckOperation);

return operationCallback().always(function() {
return that.enableButtons(initiallyEnabledButtons, true, isFromCheckOperation);
if (isWhichOperation === this.resetClick) {
// Enable the submit button if there is a gentle alert visible
if (that.el.find(".notification-gentle-alert .notification-message:visible").length > 0) {
// Ensure the submit button is added to the initially enabled buttons if it was initially enabled
if (submitInitiallyEnabled) {
initiallyEnabledButtons.push(that.submitButton);
}
} else {
// Otherwise, disable the submit button
that.enableButtons(submitButtonArr, false, isFromCheckOperation);
}
}
// Re-enable all initially enabled buttons
return that.enableButtons(initiallyEnabledButtons, true, isFromCheckOperation);
});

};

/**
Expand Down

0 comments on commit 7deaedf

Please sign in to comment.