Skip to content

Commit

Permalink
Merge pull request #3779 from klikstermkd/3773-fix-multiple-resources
Browse files Browse the repository at this point in the history
Prevent multiple consecutive form submissions
  • Loading branch information
smotornyuk committed Aug 24, 2017
2 parents a9e9e86 + a20b2e8 commit ff99fbd
Show file tree
Hide file tree
Showing 4 changed files with 62 additions and 2 deletions.
16 changes: 16 additions & 0 deletions ckan/public-bs2/base/javascript/modules/basic-form.js
Expand Up @@ -2,7 +2,23 @@ this.ckan.module('basic-form', function (jQuery) {
return {
initialize: function () {
var message = this._('There are unsaved modifications to this form');

$.proxyAll(this, /_on/);

this.el.incompleteFormWarning(message);

// Disable the submit button on form submit, to prevent multiple
// consecutive form submissions.
this.el.on('submit', this._onSubmit);
},
_onSubmit() {

// The button is not disabled immediately so that its value can be sent
// the first time the form is submitted, because the "save" field is
// used in the backend.
setTimeout(function() {
this.el.find('button[name="save"]').attr('disabled', true);
}.bind(this), 0);
}
};
});
16 changes: 15 additions & 1 deletion ckan/public-bs2/base/test/spec/modules/basic-form.spec.js
Expand Up @@ -5,9 +5,11 @@ describe('ckan.module.BasicFormModule()', function () {
beforeEach(function () {
sinon.stub(jQuery.fn, 'incompleteFormWarning');

this.el = document.createElement('button');
this.el = document.createElement('form');
this.el.innerHTML = '<button name="save" type="submit">Save</button>'
this.sandbox = ckan.sandbox();
this.sandbox.body = this.fixture;
this.sandbox.body.append(this.el)
this.module = new BasicFormModule(this.el, {}, this.sandbox);
});

Expand All @@ -21,5 +23,17 @@ describe('ckan.module.BasicFormModule()', function () {
this.module.initialize();
assert.called(jQuery.fn.incompleteFormWarning);
});

it('should disable the submit button on form submit', function(done) {
this.module.initialize();
this.module._onSubmit();

setTimeout(function() {
var buttonAttrDisabled = this.el.querySelector('button').getAttribute('disabled');

assert.ok(buttonAttrDisabled === 'disabled')
done();
}.bind(this), 0);
});
});
});
16 changes: 16 additions & 0 deletions ckan/public/base/javascript/modules/basic-form.js
Expand Up @@ -2,7 +2,23 @@ this.ckan.module('basic-form', function (jQuery) {
return {
initialize: function () {
var message = this._('There are unsaved modifications to this form');

$.proxyAll(this, /_on/);

this.el.incompleteFormWarning(message);

// Disable the submit button on form submit, to prevent multiple
// consecutive form submissions.
this.el.on('submit', this._onSubmit);
},
_onSubmit() {

// The button is not disabled immediately so that its value can be sent
// the first time the form is submitted, because the "save" field is
// used in the backend.
setTimeout(function() {
this.el.find('button[name="save"]').attr('disabled', true);
}.bind(this), 0);
}
};
});
16 changes: 15 additions & 1 deletion ckan/public/base/test/spec/modules/basic-form.spec.js
Expand Up @@ -5,9 +5,11 @@ describe('ckan.module.BasicFormModule()', function () {
beforeEach(function () {
sinon.stub(jQuery.fn, 'incompleteFormWarning');

this.el = document.createElement('button');
this.el = document.createElement('form');
this.el.innerHTML = '<button name="save" type="submit">Save</button>'
this.sandbox = ckan.sandbox();
this.sandbox.body = this.fixture;
this.sandbox.body.append(this.el)
this.module = new BasicFormModule(this.el, {}, this.sandbox);
});

Expand All @@ -21,5 +23,17 @@ describe('ckan.module.BasicFormModule()', function () {
this.module.initialize();
assert.called(jQuery.fn.incompleteFormWarning);
});

it('should disable the submit button on form submit', function(done) {
this.module.initialize();
this.module._onSubmit();

setTimeout(function() {
var buttonAttrDisabled = this.el.querySelector('button').getAttribute('disabled');

assert.ok(buttonAttrDisabled === 'disabled')
done();
}.bind(this), 0);
});
});
});

0 comments on commit ff99fbd

Please sign in to comment.