nopaque/app/templates/test/index.html.j2
2020-01-23 09:43:51 +01:00

85 lines
2.7 KiB
Django/Jinja

{% extends "limited_width.html.j2" %}
{% block page_content %}
<div class="col s12">
<h3>Submit a Test</h3>
</div>
<div class="col s12">
<div class="card">
<form id="test-form">
{{ test_form.hidden_tag() }}
<div class="card-content">
<div class="row">
<div class="col s12 m4">
<div class="input-field">
<i class="material-icons prefix">title</i>
{{ test_form.title(class='validate', data_length='32') }}
{{ test_form.title.label }}
</div>
</div>
<div class="col s12 m8">
<div class="input-field">
<i class="material-icons prefix">description</i>
{{ test_form.description(class='validate', data_length='255') }}
{{ test_form.description.label }}
</div>
</div>
<div class="col s12">
<div class="file-field input-field">
<div class="btn">
<span>{{ test_form.file.label.text }}</span>
{{ test_form.file() }}
</div>
<div class="file-path-wrapper">
<input class="file-path validate" type="text">
</div>
</div>
</div>
</div>
</div>
<div class="card-action right-align">
<button class="btn waves-effect waves-light" name="submit" type="submit">Submit<i class="material-icons right">send</i></button>
</div>
</form>
</div>
</div>
<script>
var testFormElement = document.forms.namedItem("test-form");
testFormElement.addEventListener("submit", function(event) {
event.preventDefault();
let data = {};
for (let input of event.target.querySelectorAll("input")) {
if (input.type === "file") {
file = input.files[0];
data[input.name] = {"bytes": file, "name": file.name};
} else {
data[input.name] = input.value;
}
}
nopaque.socket.emit("submit-test-form", data);
});
nopaque.socket.on("submit-test-form", function(response) {
console.log(response);
if (response.status === "success") {
testFormElement.reset();
nopaque.toast("Success!");
for (let helperText of testFormElement.querySelectorAll(".helper-text")) {
helperText.remove();
}
} else if (response.status === "error") {
let errorElement;
for (let [field, error] of Object.entries(response.msg)) {
errorElement = document.createElement("span");
errorElement.classList.add("helper-text", "red-text");
errorElement.innerText = error;
testFormElement.querySelector(`input[name="${field}"]`).closest(".input-field").appendChild(errorElement);
}
}
});
</script>
{% endblock %}