Skip to content

Commit 33e16da

Browse files
committed
Fix auto-filling inputs in acceptPost exercises
1 parent 4320e4b commit 33e16da

1 file changed

Lines changed: 42 additions & 6 deletions

File tree

exercise/static/exercise/chapter.js

Lines changed: 42 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,12 @@
413413
exercise.element.find(exercise.settings.exercise_info_selector).remove();
414414
}
415415

416+
// Restore form inputs if we have saved data from a recent submission
417+
if (exercise.savedFormData) {
418+
exercise.fillFormInputs(exercise.savedFormData, exercise.element);
419+
delete exercise.savedFormData;
420+
}
421+
416422
content.show();
417423

418424
// Active element can have height settings in the A+ exercise div that need to be
@@ -656,6 +662,14 @@
656662
}
657663
var url = $(form_element).attr("action");
658664
var formData = new FormData(form_element);
665+
666+
const hasTextarea = exercise.element.find('textarea').length > 0;
667+
const hasFieldset = exercise.element.find('fieldset').length > 0;
668+
if (hasTextarea && !hasFieldset) { // Identify acceptPost exercises
669+
// Save form data to restore after update
670+
exercise.savedFormData = Array.from(formData.entries());
671+
}
672+
659673
const aplusJsonString = formData.get('__aplus__');
660674
if (aplusJsonString) {
661675
const aplusDict = JSON.parse(aplusJsonString);
@@ -853,6 +867,31 @@
853867
}
854868
},
855869

870+
fillFormInputs: function(submissionData, container) {
871+
submissionData.forEach(function([fieldName, fieldValue]) {
872+
const field = container.find('[name="' + fieldName + '"]');
873+
if (field.length === 0) return;
874+
875+
const fieldType = field.attr('type');
876+
const tagName = field.prop('tagName').toLowerCase();
877+
878+
if (tagName === 'textarea') {
879+
field.val(fieldValue);
880+
} else if (tagName === 'select') {
881+
field.val(fieldValue);
882+
} else if (fieldType === 'checkbox') {
883+
// For checkboxes, check if this value matches
884+
field.filter('[value="' + fieldValue + '"]').prop('checked', true);
885+
} else if (fieldType === 'radio') {
886+
// For radio buttons, select the one with matching value
887+
field.filter('[value="' + fieldValue + '"]').prop('checked', true);
888+
} else {
889+
// For text, hidden, and other input types
890+
field.val(fieldValue);
891+
}
892+
});
893+
},
894+
856895
loadLastSubmission: function(input, fillInputs = false) {
857896
var link = input.find(this.settings.last_submission_selector);
858897
var exercise = this;
@@ -895,12 +934,9 @@
895934
new CustomEvent("aplus:exercise-ready",
896935
{bubbles: true, detail: {type: exercise.exercise_type}}));
897936
} else {
898-
// Fill textareas with last submission inputs
899-
const lastInputs = data.submission_data.map((x) => x[1]);
900-
const textareas = responseElement.find('textarea');
901-
textareas.each(function(index) {
902-
$(this).val(lastInputs[index]);
903-
});
937+
// Fill form inputs with last submission data
938+
const submissionData = data.submission_data;
939+
exercise.fillFormInputs(submissionData, exercise.element);
904940
}
905941

906942
} else {

0 commit comments

Comments
 (0)