Skip to content

Commit

Permalink
fixup! save responses as we go on first mark screens
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Jul 15, 2024
1 parent 74d8a25 commit 8692831
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions crowdsourcer/static/js/questions.esm.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
const form_regex = /form-\d-/i
const form_regex = /form-\d+-/i
function get_name_from_input(input) {
return input.name.replace(form_regex, "");
}

function disable_submit_if_invalid() {
if ($.find('.invalid-feedback').length > 0) {
if ($('.invalid-feedback').length > 0) {
$('#save_all_answers').prop("disabled", true);
} else {
$('#save_all_answers').prop("disabled", false);
Expand All @@ -16,41 +16,41 @@ $(function(){
var $d = $(this);
var $fs = $d.parents('fieldset');

var csrf = $('input[name="csrfmiddlewaretoken"]').val()
var csrf = $d.parents('form').find('input[name="csrfmiddlewaretoken"]').val()
let data = {"csrfmiddlewaretoken": csrf};
let has_values = false;
$fs.find('.form-select, .form-control, input[type="hidden"]').each(function($i, $field) {
let name = get_name_from_input($field);
let $f = $( $field );
let val = $( $field ).val();
$fs.find('.form-select, .form-control, input[type="hidden"]').each(function() {
let $f = $(this);
let name = get_name_from_input($f[0]);
let val = $f.val();
if (name != "question" && name != "authority" && val) {
has_values = true;
}
data[name] = val;
});
if ( $fs.find('.form-check-input') ) {
if ( $fs.find('.form-check-input').length ) {
let $f = $fs.find('.form-check-input').get(0);
let name = get_name_from_input($f);
data[name] = $( $f.name ).val();
}

if (!has_values) {
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) {
let $f = $($field);
$f.removeClass("is-invalid").removeClass("is-valid");
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function() {
let $f = $(this);
$f.removeClass("is-invalid is-valid");
$f.next('.invalid-feedback').remove();
});
disable_submit_if_invalid()
return;
}

url = window.location + data["question"] + "/";
let url = window.location + data["question"] + "/";

$.post(url, data, function(r_data) {
if (r_data["success"] != 1) {
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) {
let name = get_name_from_input($field);
let $f = $($field);
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function() {
let $f = $(this);
let name = get_name_from_input($f[0]);
$f.next('.invalid-feedback').remove();
if (r_data["errors"].hasOwnProperty(name)) {
$f.addClass("is-invalid").removeClass("is-valid");
Expand All @@ -61,9 +61,9 @@ $(function(){
});
$('#save_all_answers').prop("disabled", true);
} else {
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) {
let name = get_name_from_input($field);
$f = $($field);
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function() {
$f = $(this);
let name = get_name_from_input($f[0]);
$f.next('.invalid-feedback').remove();
$f.addClass("is-valid").removeClass("is-invalid");
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,5 +90,5 @@ <h3 class="mb-4 mb-md-5 text-success">
{% endblock %}

{% block script %}
<script src={% static 'js/questions.esm.js' %}"></script>
<script src="{% static 'js/questions.esm.js' %}"></script>
{% endblock %}

0 comments on commit 8692831

Please sign in to comment.