From a97b3fce8a6c3950127b452a4346485e7664b376 Mon Sep 17 00:00:00 2001 From: SaintPeter Date: Fri, 12 Feb 2016 19:20:35 -0800 Subject: [PATCH] Fix issue with duplicate submits --- client/commonFramework/show-completion.js | 127 +++++++++++----------- 1 file changed, 64 insertions(+), 63 deletions(-) diff --git a/client/commonFramework/show-completion.js b/client/commonFramework/show-completion.js index 86d852a660..f38f1281c2 100644 --- a/client/commonFramework/show-completion.js +++ b/client/commonFramework/show-completion.js @@ -6,6 +6,68 @@ window.common = (function(global) { common = { init: [] } } = global; + function submitChallengeHandler(e) { + e.preventDefault(); + + var solution = common.editor.getValue(); + + $('#submit-challenge') + .attr('disabled', 'true') + .removeClass('btn-primary') + .addClass('btn-warning disabled'); + + var $checkmarkContainer = $('#checkmark-container'); + $checkmarkContainer.css({ height: $checkmarkContainer.innerHeight() }); + + $('#challenge-checkmark') + .addClass('zoomOutUp') + // .removeClass('zoomInDown') + .delay(1000) + .queue(function(next) { + $(this).replaceWith( + '
' + + 'submitting...
' + ); + next(); + }); + + let timezone = 'UTC'; + try { + timezone = moment.tz.guess(); + } catch (err) { + err.message = ` + known bug, see: https://github.com/moment/moment-timezone/issues/294: + ${err.message} + `; + console.error(err); + } + const data = JSON.stringify({ + id: common.challengeId, + name: common.challengeName, + challengeType: +common.challengeType, + solution, + timezone + }); + + $.ajax({ + url: '/completed-challenge/', + type: 'POST', + data, + contentType: 'application/json', + dataType: 'json' + }) + .success(function(res) { + if (res) { + window.location = + '/challenges/next-challenge?id=' + common.challengeId; + } + }) + .fail(function() { + window.location.replace(window.location.href); + }); + } + common.showCompletion = function showCompletion() { ga( @@ -17,72 +79,11 @@ window.common = (function(global) { true ); - var solution = common.editor.getValue(); - var didCompleteWith = $('#completed-with').val() || null; - $('#complete-courseware-dialog').modal('show'); $('#complete-courseware-dialog .modal-header').click(); - $('#submit-challenge').click(function(e) { - e.preventDefault(); - - $('#submit-challenge') - .attr('disabled', 'true') - .removeClass('btn-primary') - .addClass('btn-warning disabled'); - - var $checkmarkContainer = $('#checkmark-container'); - $checkmarkContainer.css({ height: $checkmarkContainer.innerHeight() }); - - $('#challenge-checkmark') - .addClass('zoomOutUp') - // .removeClass('zoomInDown') - .delay(1000) - .queue(function(next) { - $(this).replaceWith( - '
' + - 'submitting...
' - ); - next(); - }); - - let timezone = 'UTC'; - try { - timezone = moment.tz.guess(); - } catch (err) { - err.message = ` - known bug, see: https://github.com/moment/moment-timezone/issues/294: - ${err.message} - `; - console.error(err); - } - const data = JSON.stringify({ - id: common.challengeId, - name: common.challengeName, - completedWith: didCompleteWith, - challengeType: +common.challengeType, - solution, - timezone - }); - - $.ajax({ - url: '/completed-challenge/', - type: 'POST', - data, - contentType: 'application/json', - dataType: 'json' - }) - .success(function(res) { - if (res) { - window.location = - '/challenges/next-challenge?id=' + common.challengeId; - } - }) - .fail(function() { - window.location.replace(window.location.href); - }); - }); + $('#submit-challenge').off('click'); + $('#submit-challenge').on('click', submitChallengeHandler); }; return common;