Skip to content

Commit 4883d28

Browse files
committed
replace jquery code with plain javascript
1 parent e6feb61 commit 4883d28

2 files changed

Lines changed: 65 additions & 50 deletions

File tree

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,65 +1,81 @@
1-
jQuery(function () {
2-
const $submissions = $('.submissions');
3-
configureCsrf($("[name=csrfmiddlewaretoken]").val());
1+
document.addEventListener('DOMContentLoaded', function () {
2+
const submissionsContainers = document.querySelectorAll('.submissions');
43

5-
$submissions.on('click', '.submission', function () {
6-
$(this).toggleClass('preferred');
7-
}).on('click', '.submission', deduplicate(savePreferredTalks));
4+
const csrfInput = document.querySelector("[name=csrfmiddlewaretoken]");
5+
const csrfToken = csrfInput ? csrfInput.value : '';
6+
7+
const saveHandler = deduplicate(savePreferredTalks);
8+
9+
submissionsContainers.forEach(container => {
10+
container.addEventListener('click', function (e) {
11+
const submissionEl = e.target.closest('.submission');
12+
13+
if (submissionEl && container.contains(submissionEl)) {
14+
submissionEl.classList.toggle('preferred');
15+
saveHandler();
16+
}
17+
});
18+
});
819

920
function savePreferredTalks() {
10-
const preferredTalkIds = $submissions.find('.submission.preferred').map(function () {
11-
return $(this).data('id')
12-
}).get();
13-
console.log('start save', 'preferredTalkIds=', preferredTalkIds)
21+
const allSubmissions = document.querySelectorAll('.submissions .submission.preferred');
22+
23+
const preferredTalkIds = Array.from(allSubmissions).map(function (el) {
24+
return parseInt(el.dataset.id, 10);
25+
});
1426

15-
return jQuery.ajax(window.location.href + 'my-preferences/', {
27+
console.log('start save', 'preferredTalkIds=', preferredTalkIds);
28+
29+
const cleanPath = window.location.pathname.replace(/\/?$/, '/');
30+
const url = window.location.origin + cleanPath + 'my-preferences/';
31+
32+
return fetch(url, {
1633
method: 'POST',
17-
dataType: 'json',
18-
data: JSON.stringify({
34+
headers: {
35+
'Content-Type': 'application/json',
36+
'X-CSRFToken': csrfToken
37+
},
38+
body: JSON.stringify({
1939
preferred_submissions: preferredTalkIds
20-
}),
21-
contentType: 'application/json',
22-
})
40+
})
41+
}).then(async response => {
42+
if (!response.ok) {
43+
const text = await response.text();
44+
throw new Error(`Server returned ${response.status}: ${text}`);
45+
}
46+
return response.json();
47+
});
2348
}
2449

25-
})
50+
function deduplicate(action) {
51+
let actionInProgress = false;
52+
let actionPending = false;
2653

27-
function configureCsrf(csrfToken) {
28-
const csrfSafeMethods = ['GET', 'HEAD', 'OPTIONS', 'TRACE'];
29-
30-
$.ajaxSetup({
31-
beforeSend: function (xhr, settings) {
32-
if (!csrfSafeMethods.includes(settings.type) && !this.crossDomain) {
33-
xhr.setRequestHeader("X-CSRFToken", csrfToken);
54+
return async function applyActionOrSetFlag() {
55+
if (actionInProgress) {
56+
console.log('action still running, setting pending flag');
57+
actionPending = true;
58+
return;
3459
}
35-
}
36-
});
37-
}
3860

39-
function deduplicate(action) {
40-
let actionInProgress = false, actionPending = false;
61+
actionInProgress = true;
62+
console.log('apply action');
4163

42-
return function applyActionOrSetFlag() {
43-
if (actionInProgress) {
44-
console.log('action still running, setting pending flag')
45-
actionPending = true;
46-
return;
47-
}
64+
try {
65+
await action();
66+
console.log('action done');
67+
actionInProgress = false;
4868

49-
actionInProgress = true;
50-
console.log('apply action')
51-
action().then(function () {
52-
console.log('action done')
53-
actionInProgress = false;
54-
if (actionPending) {
55-
console.log('pending flag set, re-run action')
56-
applyActionOrSetFlag();
69+
if (actionPending) {
70+
console.log('pending flag set, re-run action');
71+
actionPending = false;
72+
applyActionOrSetFlag();
73+
}
74+
} catch (error) {
75+
console.error('Save failed:', error);
5776
actionPending = false;
77+
actionInProgress = false;
5878
}
59-
}).catch(function () {
60-
console.log('action failed')
61-
actionPending = false;
62-
actionInProgress = false;
63-
})
79+
};
6480
}
65-
}
81+
});

pretalx_halfnarp/templates/pretalx_halfnarp/frontend.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
{% block custom_header %}
88
<link rel="stylesheet" type="text/css" href="{% static "pretalx_halfnarp/frontend.css" %}"/>
9-
<script defer src="{% static "js/jquery.js" %}"></script>
109
<script defer src="{% static "pretalx_halfnarp/frontend.js" %}"></script>
1110
{% endblock %}
1211

0 commit comments

Comments
 (0)