From cd2e075b1e3e3cc2e4b492cf7e70da568865c653 Mon Sep 17 00:00:00 2001 From: Ishan Masdekar Date: Sat, 13 Jul 2024 23:34:21 +0530 Subject: [PATCH] fix: correctly handles the enabling of submit button on reset - 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 --- xmodule/js/src/capa/display.js | 59 +++++++++++++++++++++++++++++----- 1 file changed, 51 insertions(+), 8 deletions(-) diff --git a/xmodule/js/src/capa/display.js b/xmodule/js/src/capa/display.js index f4254b43f8ff..26e4fc81a608 100644 --- a/xmodule/js/src/capa/display.js +++ b/xmodule/js/src/capa/display.js @@ -7,6 +7,10 @@ (function() { 'use strict'; + // constants for denoting the click of submit/save/reset button + var submitClick = "submit"; + var saveClick = "save"; + var resetClick = "reset"; var indexOfHelper = [].indexOf || function(item) { @@ -136,6 +140,7 @@ this.url = this.el.data('url'); this.content = this.el.data('content'); + // 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 @@ -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, submitClick); } }; @@ -696,7 +701,7 @@ }; Problem.prototype.reset = function() { - return this.disableAllButtonsWhileRunning(this.reset_internal, false); + return this.disableAllButtonsWhileRunning(this.reset_internal, false, resetClick); }; Problem.prototype.reset_internal = function() { @@ -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, saveClick); } }; @@ -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 === submitClick) || (isWhichOperation === saveClick)) { + // Add the submit button to the initially enabled buttons if it was initially enabled + if (submitInitiallyEnabled) { + initiallyEnabledButtons.push(this.submitButton); + } + } else if (isWhichOperation === 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 === 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); }); + }; /**