Skip to content

Commit

Permalink
MW-1059: disable save btn when loading (#3774)
Browse files Browse the repository at this point in the history
Co-authored-by: Lajos Arpad <arpad@endpoint.com>
Co-authored-by: Kevin Foster <kevin.foster.uk@gmail.com>
  • Loading branch information
3 people committed Mar 6, 2024
1 parent 1f7a2af commit 0d96ffa
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 17 deletions.
2 changes: 2 additions & 0 deletions assets/packages/adminbasics/build/adminbasics.js
Expand Up @@ -19196,6 +19196,8 @@
if ($(el).data('form-id') == 'addnewsurvey') {
const loadingSpinner = '<i class="ri-settings-5-fill remix-spin lsLoadingStateIndicator"></i>';
$(el).prop('disabled', true).append(loadingSpinner);
} else if (el.id === 'save-button' || el.id === 'save-form-button' || el.id === 'save-and-close-button' || (el.id = 'save-and-close-button-create-question')) {
$('#ls-loading').show();
}
},
stopDisplayLoadingState = () => {
Expand Down
4 changes: 2 additions & 2 deletions assets/packages/adminbasics/build/adminbasics.min.js

Large diffs are not rendered by default.

14 changes: 7 additions & 7 deletions assets/packages/adminbasics/src/parts/globalMethods.js
Expand Up @@ -50,22 +50,22 @@ const globalWindowMethods = {
});
},
// finds any duplicate array elements using the fewest possible comparison
arrHasDupes: ( arrayToCheck ) => {
arrHasDupes: ( arrayToCheck ) => {
return (_.uniq(arrayToCheck).length !== arrayToCheck.length);
},
arrHasDupesWhich: ( arrayToCheck ) => {
arrHasDupesWhich: ( arrayToCheck ) => {
return (_.difference(_.uniq(arrayToCheck), arrayToCheck)).length > 0;
},
getkey : (e) => {
return (window.event) ? window.event.keyCode :(e ? e.which : null);
},
goodchars : (e, goods) => {
const key = getkey(e);
const key = globalWindowMethods.getkey(e);
if (key == null) return true;

// get character
const keychar = (String.fromCharCode(key)).toLowerCase();

goods = goods.toLowerCase();

return (goods.indexOf(keychar) != -1) || ( key==null || key==0 || key==8 || key==9 || key==27 );
Expand Down Expand Up @@ -100,11 +100,11 @@ const globalWindowMethods = {
contentObject = _.merge(contentObject, JSON.parse(content));
} catch(e) { console.error('JSON parse on sendPost failed!') }
}

_.each(contentObject, (value,key) => {
$("<input type='hidden'>").attr("name", key).attr("value", value).appendTo($form);
});

$("<input type='hidden'>").attr("name", LS.data.csrfTokenName).attr("value", LS.data.csrfToken).appendTo($form);
$form.appendTo("body");
$form.submit();
Expand Down
18 changes: 10 additions & 8 deletions assets/packages/adminbasics/src/parts/save.js
Expand Up @@ -3,14 +3,14 @@ import forEach from 'lodash/forEach';
import LOG from '../components/lslog';

const SaveController = () => {

let formSubmitting = false;

// Attach this <input> tag to form to check for closing after save
const closeAfterSaveInput = $("<input>")
.attr("type", "hidden")
.attr("name", "close-after-save");



/**
Expand Down Expand Up @@ -38,9 +38,11 @@ const SaveController = () => {
},
isSubmitting = () => formSubmitting,
displayLoadingState = (el) => {
if($(el).data('form-id') == 'addnewsurvey') {
if(($(el).data('form-id') == 'addnewsurvey')) {
const loadingSpinner = '<i class="ri-settings-5-fill remix-spin lsLoadingStateIndicator"></i>';
$(el).prop('disabled', true).append(loadingSpinner);
} else if ((el.id === 'save-button') || (el.id === 'save-form-button') || (el.id === 'save-and-close-button') || (el.id = 'save-and-close-button-create-question')) {
$('#ls-loading').show();
}
},
stopDisplayLoadingState = () => {
Expand All @@ -63,7 +65,7 @@ const SaveController = () => {
ev.preventDefault();
const $form = getForm(this);
formSubmitting = true;

if ($form.data('isvuecomponent') == true) {
LS.EventBus.$emit('componentFormSubmit', button)
} else {
Expand Down Expand Up @@ -122,7 +124,7 @@ const SaveController = () => {
} else {
$form.submit();
}

// check if there are any required inputs that are not filled
var cntInvalid = 0;
var requiredInputs = $form.find('input,select').filter("[required='required']");
Expand Down Expand Up @@ -192,15 +194,15 @@ const SaveController = () => {
name: 'saveandclose',
value: '1'
}).appendTo($form);

const submitButton = $form.find('[type="submit"]').first();
if (submitButton.length) {
submitButton.trigger('click');
} else {
$form.submit();
}
displayLoadingState(this);

return false;
},
on: 'click'
Expand Down Expand Up @@ -267,7 +269,7 @@ const SaveController = () => {

LS.EventBus.$off("saveButtonCalled");
LS.EventBus.$emit("saveButtonFlushed");

LS.EventBus.$on("saveButtonCalled", (button) => {
if(!isSubmitting()) {
forEach(checks(), (checkItem) => {
Expand Down
11 changes: 11 additions & 0 deletions assets/scripts/admin/questionEditor.js
Expand Up @@ -1818,30 +1818,35 @@ $(document).on('ready pjax:scriptcomplete', function () {
* @return {boolean}
*/
checkIfSaveIsValid: function(event /*: Event */, tabQuestionEditor = 'editor') {
$('#ls-loading').show();
event.preventDefault();
const qid = parseInt($('input[name="question[qid]"]').val());
const code = $('input[name="question[title]"]').val();
const target = event.currentTarget;
if (!(target instanceof HTMLElement)) {
alert('Internal error in checkIfSaveIsValid: target is not an HTMLElement, but ' + typeof target);
$('#ls-loading').hide();
return false;
}
const saveWithAjax = target.dataset.saveWithAjax === 'true';
const form = document.getElementById('edit-question-form');
if (!(form instanceof HTMLFormElement)) {
$('#ls-loading').hide();
throw 'form is not HTMLFormElement';
}
/* Check if input are HTML5 valid */
if (!form.checkValidity() ) {
// the form is invalid : show invalid part
form.reportValidity();
$('#ls-loading').hide();
return false;
}

const firstSubquestionRow = document.querySelector('.subquestions-table tr');
if (firstSubquestionRow) {
// This will show error message if subquestion code is not unique.
if (!LS.questionEditor.showSubquestionCodeUniqueError(firstSubquestionRow)) {
$('#ls-loading').hide();
return false;
}
}
Expand All @@ -1850,17 +1855,20 @@ $(document).on('ready pjax:scriptcomplete', function () {
if (firstAnsweroptionRow) {
// This will show error message if answer option code is not unique.
if (!LS.questionEditor.showAnswerOptionCodeUniqueError(firstAnsweroptionRow)) {
$('#ls-loading').hide();
return false;
}
}

const updateQuestionSummary = () => {
$('#ls-loading').show();
$.ajax({
url: form.dataset.summaryUrl,
method: 'GET',
data: {},
dataType: 'html',
success: (summaryHtml) => {
$('#ls-loading').hide();
const isVisible = $('#question-overview').is(':visible');
const newSummary = $(summaryHtml);
if (isVisible) {
Expand All @@ -1880,6 +1888,7 @@ $(document).on('ready pjax:scriptcomplete', function () {
});
},
error: (response) => {
$('#ls-loading').hide();
alert('Internal error in updateQuestionSummary: ' + response);
return false;
},
Expand Down Expand Up @@ -1914,6 +1923,7 @@ $(document).on('ready pjax:scriptcomplete', function () {
const data = {};
const form = document.getElementById('edit-question-form');
if (!(form instanceof HTMLFormElement)) {
$('#ls-loading').hide();
throw 'form is not HTMLFormElement';
}

Expand Down Expand Up @@ -2000,6 +2010,7 @@ $(document).on('ready pjax:scriptcomplete', function () {
saveFormWithAjax();
} else {
// Just submit form.
$('#ls-loading').show();
button.click();
}
return true;
Expand Down

0 comments on commit 0d96ffa

Please sign in to comment.