Skip to content
This repository has been archived by the owner on Feb 8, 2018. It is now read-only.

Commit

Permalink
Clean up submission behavior
Browse files Browse the repository at this point in the history
- fix disabling of submit button
- send a bad nonce to trigger error handling vs. crash
  • Loading branch information
chadwhitacre committed Sep 12, 2017
1 parent 0c1a606 commit 72827bb
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 12 deletions.
17 changes: 11 additions & 6 deletions js/gratipay/homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@ Gratipay.homepage.initForm = function(clientAuthorization) {
self.$form = $('#homepage #content form');
self.$submit = self.$form.find('button[type=submit]');

function disable(e) {
e.preventDefault();
self.$submit.prop('disabled', true);
self.$submit.addClass('processing');
}

if (clientAuthorization === undefined) { // Offline mode

$('#braintree-container').addClass('offline').html(Gratipay.jsonml(['div',
Expand All @@ -16,7 +22,7 @@ Gratipay.homepage.initForm = function(clientAuthorization) {
]));

self.$submit.click(function(e) {
e.preventDefault();
disable(e);
nonce = $('#braintree-container input').val();
self.submitFormWithNonce(nonce);
});
Expand All @@ -28,9 +34,9 @@ Gratipay.homepage.initForm = function(clientAuthorization) {
$('#braintree-container').addClass('failed').text('Failed to load Braintree.');
} else {
self.$submit.click(function(e) {
e.preventDefault();
disable(e);
instance.requestPaymentMethod(function(requestPaymentMethodErr, payload) {
self.submitFormWithNonce(payload.nonce);
self.submitFormWithNonce(payload ? payload.nonce : '');
});
});
}
Expand All @@ -49,8 +55,6 @@ Gratipay.homepage.submitFormWithNonce = function(nonce) {
var data = new FormData(self.$form[0]);
data.set('payment_method_nonce', nonce);

self.$submit.prop('disable', true);

$.ajax({
url: self.$form.attr('action'),
type: 'POST',
Expand All @@ -61,7 +65,8 @@ Gratipay.homepage.submitFormWithNonce = function(nonce) {
success: function(data) {
// Due to Aspen limitations we use 200 for both success and failure. :/
if (data.errors.length > 0) {
self.$submit.prop('disable', false);
self.$submit.prop('disabled', false);
self.$submit.removeClass('processing');
Gratipay.notification(data.msg, 'error');
for (var i=0, fieldName; fieldName=data.errors[i]; i++) {
$('.'+fieldName, self.$form).addClass('error');
Expand Down
13 changes: 9 additions & 4 deletions scss/pages/homepage.scss
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,7 @@
}
}
}

.help {
display: none;
color: red;
Expand All @@ -213,10 +214,14 @@
}
}
}
&.optional {
button {
background: $light-gray;
color: $medium-gray;

button[type=submit] {
.processing {
display: none;
}
&.processing {
.active { display: none; }
.processing { display: inline; }
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion www/browse/payments/%uuid/invoice.spt
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,7 @@ if pfos is None:
<table>
<tbody>
<tr>
<th>open source software</th>
<th>open source software sponsorship</th>
<td>$</td>
<td>{{ pfos.amount }}.00</td>
</tr>
Expand Down
3 changes: 2 additions & 1 deletion www/index.spt
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,8 @@ $(document).ready(function() {

<div class="important-button">
<button type="submit" class="selected large">
{{ _("Pay for Open Source") }}
<span class="active">{{ _("Pay for Open Source") }}</span>
<span class="processing">{{ _("Processing ...") }}</span>
</button>
</div>
<p class="fine-print">
Expand Down

0 comments on commit 72827bb

Please sign in to comment.