|
1 | | -jQuery(function () { |
2 | | - const $submissions = $('.submissions'); |
3 | | - configureCsrf($("[name=csrfmiddlewaretoken]").val()); |
| 1 | +document.addEventListener('DOMContentLoaded', function () { |
| 2 | + const submissionsContainers = document.querySelectorAll('.submissions'); |
4 | 3 |
|
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 | + }); |
8 | 19 |
|
9 | 20 | 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 | + }); |
14 | 26 |
|
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, { |
16 | 33 | 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({ |
19 | 39 | 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 | + }); |
23 | 48 | } |
24 | 49 |
|
25 | | -}) |
| 50 | + function deduplicate(action) { |
| 51 | + let actionInProgress = false; |
| 52 | + let actionPending = false; |
26 | 53 |
|
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; |
34 | 59 | } |
35 | | - } |
36 | | - }); |
37 | | -} |
38 | 60 |
|
39 | | -function deduplicate(action) { |
40 | | - let actionInProgress = false, actionPending = false; |
| 61 | + actionInProgress = true; |
| 62 | + console.log('apply action'); |
41 | 63 |
|
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; |
48 | 68 |
|
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); |
57 | 76 | actionPending = false; |
| 77 | + actionInProgress = false; |
58 | 78 | } |
59 | | - }).catch(function () { |
60 | | - console.log('action failed') |
61 | | - actionPending = false; |
62 | | - actionInProgress = false; |
63 | | - }) |
| 79 | + }; |
64 | 80 | } |
65 | | -} |
| 81 | +}); |
0 commit comments