Skip to content

Commit 487a039

Browse files
committed
save responses as we go on first mark screens
1 parent 27d10a4 commit 487a039

File tree

3 files changed

+87
-2
lines changed

3 files changed

+87
-2
lines changed
+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
const form_regex = /form-\d-/i
2+
function get_name_from_input(input) {
3+
return input.name.replace(form_regex, "");
4+
}
5+
6+
function disable_submit_if_invalid() {
7+
if ($.find('.invalid-feedback').length > 0) {
8+
$('#save_all_answers').prop("disabled", true);
9+
} else {
10+
$('#save_all_answers').prop("disabled", false);
11+
}
12+
}
13+
14+
$(function(){
15+
$('.form-select, .form-control, .form-check-input').on('blur', function(e){
16+
var $d = $(this);
17+
var $fs = $d.parents('fieldset');
18+
19+
var csrf = $('input[name="csrfmiddlewaretoken"]').val()
20+
let data = {"csrfmiddlewaretoken": csrf};
21+
let has_values = false;
22+
$fs.find('.form-select, .form-control, input[type="hidden"]').each(function($i, $field) {
23+
let name = get_name_from_input($field);
24+
let $f = $( $field );
25+
let val = $( $field ).val();
26+
if (name != "question" && name != "authority" && val) {
27+
has_values = true;
28+
}
29+
data[name] = val;
30+
});
31+
if ( $fs.find('.form-check-input') ) {
32+
let $f = $fs.find('.form-check-input').get(0);
33+
let name = get_name_from_input($f);
34+
data[name] = $( $f.name ).val();
35+
}
36+
37+
if (!has_values) {
38+
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) {
39+
let $f = $($field);
40+
$f.removeClass("is-invalid").removeClass("is-valid");
41+
$f.next('.invalid-feedback').remove();
42+
});
43+
disable_submit_if_invalid()
44+
return;
45+
}
46+
47+
url = window.location + data["question"] + "/";
48+
49+
$.post(url, data, function(r_data) {
50+
if (r_data["success"] != 1) {
51+
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) {
52+
let name = get_name_from_input($field);
53+
let $f = $($field);
54+
$f.next('.invalid-feedback').remove();
55+
if (r_data["errors"].hasOwnProperty(name)) {
56+
$f.addClass("is-invalid").removeClass("is-valid");
57+
$f.after('<div class="invalid-feedback">' + r_data["errors"][name] + '</div>');
58+
} else {
59+
$f.addClass("is-valid").removeClass("is-invalid");
60+
}
61+
});
62+
$('#save_all_answers').prop("disabled", true);
63+
} else {
64+
$fs.find('.form-select, .form-control, .form-check-input, input[type="hidden"]').each(function($i, $field) {
65+
let name = get_name_from_input($field);
66+
$f = $($field);
67+
$f.next('.invalid-feedback').remove();
68+
$f.addClass("is-valid").removeClass("is-invalid");
69+
});
70+
71+
disable_submit_if_invalid()
72+
}
73+
});
74+
});
75+
});

crowdsourcer/templates/crowdsourcer/authority_questions.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{% extends 'crowdsourcer/base.html' %}
22

33
{% load django_bootstrap5 %}
4+
{% load static %}
45

56
{% block content %}
67
<h1 class="mb-3 mb-md-4">
@@ -82,8 +83,12 @@ <h3 class="mb-4 mb-md-5 text-success">
8283
{% endfor %}
8384

8485
<div class="sticky-bottom py-3 bg-white border-top" style="margin-top: -1px;">
85-
<input type="submit" class="btn btn-primary" value="Save answers">
86+
<input id="save_all_answers" type="submit" class="btn btn-primary" value="Save answers">
8687
</div>
8788
</form>
8889

8990
{% endblock %}
91+
92+
{% block script %}
93+
<script src={% static 'js/questions.esm.js' %}"></script>
94+
{% endblock %}

crowdsourcer/templates/crowdsourcer/authority_questions_with_previous.html

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
{% load django_bootstrap5 %}
44
{% load neighbourhood_filters %}
5+
{% load static %}
56

67
{% block content %}
78
<h1 class="mb-3 mb-md-4">
@@ -173,7 +174,7 @@ <h3 class="h5 text-muted mb-3 mb-lg-4">New response</h3>
173174
{% endfor %}
174175

175176
<div class="sticky-bottom py-3 bg-white border-top" style="margin-top: -1px;">
176-
<input type="submit" class="btn btn-primary" value="Save answers">
177+
<input id="save_all_answers" type="submit" class="btn btn-primary" value="Save answers">
177178
</div>
178179
</form>
179180

@@ -206,3 +207,7 @@ <h3 class="h5 text-muted mb-3 mb-lg-4">New response</h3>
206207
</script>
207208

208209
{% endblock %}
210+
211+
{% block script %}
212+
<script src="{% static 'js/questions.esm.js' %}"></script>
213+
{% endblock %}

0 commit comments

Comments
 (0)