Skip to content

Commit 5d3c2a3

Browse files
committed
Introduce custom success forms
1 parent 3945893 commit 5d3c2a3

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

evap/evaluation/templates/base.html

+3-1
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,11 @@
210210

211211
</script>
212212
<script type="module">
213-
import { NotebookLogic } from "{% static 'js/notebook.js' %}"
213+
import { NotebookLogic } from "{% static 'js/notebook.js' %}";
214+
import { setupForms } from "{% static 'js/custom-success-form.js' %}";
214215

215216
new NotebookLogic("#notebook").attach();
217+
setupForms();
216218
</script>
217219

218220
{% block additional_javascript %}{% endblock %}

evap/staff/templates/staff_semester_view.html

+6-18
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ <h3 class="d-inline">{{ semester.name }}</h3>
9292
<div class="collapse{% if semester.participations_are_archived or semester.grade_documents_are_deleted or semester.results_are_archived %} show{% endif %}" id="archivingCard">
9393
<div class="card-body">
9494
{% if semester.participations_can_be_archived %}
95-
<form method="POST" action="{% url 'staff:semester_archive_participations' %}">
95+
<form reload-on-success method="POST" action="{% url 'staff:semester_archive_participations' %}">
9696
{% csrf_token %}
9797
<confirmation-modal type="submit" name="semester_id" value="{{ semester.id }}" confirm-button-class="btn-danger">
9898
<span slot="title">{% trans 'Archive participations' %}</span>
@@ -115,7 +115,7 @@ <h3 class="d-inline">{{ semester.name }}</h3>
115115
{% trans 'Archive participations' %}</button>
116116
{% endif %}
117117
{% if semester.grade_documents_can_be_deleted %}
118-
<form method="POST" action="{% url 'staff:semester_delete_grade_documents' %}">
118+
<form reload-on-success method="POST" action="{% url 'staff:semester_delete_grade_documents' %}">
119119
{% csrf_token %}
120120
<confirmation-modal type="submit" name="semester_id" value="{{ semester.id }}" confirm-button-class="btn-danger">
121121
<span slot="title">{% trans 'Delete grade documents' %}</span>
@@ -135,7 +135,7 @@ <h3 class="d-inline">{{ semester.name }}</h3>
135135
<button type="button" class="btn btn-sm btn-success disabled">{% trans 'Grade documents have been deleted' %}</button>
136136
{% endif %}
137137
{% if semester.results_can_be_archived %}
138-
<form method="POST" action="{% url 'staff:semester_archive_results' %}">
138+
<form reload-on-success method="POST" action="{% url 'staff:semester_archive_results' %}">
139139
{% csrf_token %}
140140
<confirmation-modal type="submit" name="semester_id" value="{{ semester.id }}" confirm-button-class="btn-danger">
141141
<span slot="title">{% trans 'Archive results' %}</span>
@@ -484,24 +484,12 @@ <h3 class="d-inline">{{ semester.name }}</h3>
484484
</div>
485485
</div>
486486
{% if courses %}
487-
<form id="course-deletion-form" method="POST" action="{% url 'staff:course_delete' %}">
487+
<form id="course-deletion-form" custom-success method="POST" action="{% url 'staff:course_delete' %}">
488488
{% csrf_token %}
489489
</form>
490490
<script async>
491-
const form = document.getElementById("course-deletion-form");
492-
form.addEventListener("submit", event => {
493-
event.preventDefault();
494-
const body = new FormData(form);
495-
const request = fetch(
496-
form.action,
497-
{ method: form.method, body },
498-
);
499-
request.then(response => {
500-
assert(response.ok);
501-
fadeOutThenRemove(document.querySelector(`#course-row-${body.get("course_id")}`));
502-
}).catch(error => {
503-
window.alert("{% trans 'The server is not responding.' %}");
504-
});
491+
document.getElementById("course-deletion-form").addEventListener("submit-success", event => {
492+
fadeOutThenRemove(document.querySelector(`#course-row-${event.detail.body.get("course_id")}`));
505493
});
506494
</script>
507495

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import "./translation.js";
2+
import { assert } from "./utils.js";
3+
4+
const overrideSuccessfulSubmit = (
5+
form: HTMLFormElement,
6+
onSuccess: (context: { body: FormData; response: Response }) => void
7+
) => {
8+
form.addEventListener("submit", event => {
9+
event.preventDefault();
10+
11+
const body = new FormData(form);
12+
13+
fetch(form.action, { method: form.method, body })
14+
.then(response => {
15+
assert(response.ok);
16+
onSuccess({ body, response });
17+
})
18+
.catch(error => {
19+
console.error(error);
20+
window.alert(window.gettext("The server is not responding."));
21+
});
22+
});
23+
};
24+
25+
const makeCustomSuccessForm = (form: HTMLFormElement) => {
26+
overrideSuccessfulSubmit(form, ({ body }) => {
27+
form.dispatchEvent(new CustomEvent("submit-success", { detail: { body } }));
28+
});
29+
};
30+
31+
const makeReloadOnSuccessForm = (form: HTMLFormElement) => {
32+
overrideSuccessfulSubmit(form, () => window.location.reload());
33+
};
34+
35+
export const setupForms = () => {
36+
document.querySelectorAll("form[custom-success]").forEach(form => {
37+
makeCustomSuccessForm(form as HTMLFormElement);
38+
});
39+
document.querySelectorAll("form[reload-on-success]").forEach(form => {
40+
makeReloadOnSuccessForm(form as HTMLFormElement);
41+
});
42+
};

0 commit comments

Comments
 (0)