|
1 | 1 | const manage_submissions_endpoints = { |
2 | 2 | 'regrade-selected': 'regradeBatch', |
| 3 | + 'delete-selected': 'submissions/destroy_batch', |
| 4 | + 'download-selected': 'submissions/download_batch', |
| 5 | + 'excuse-selected': 'submissions/excuse_batch', |
3 | 6 | 'score_details': 'submissions/score_details', |
4 | 7 | }; |
5 | 8 |
|
@@ -327,20 +330,73 @@ $(document).ready(function() { |
327 | 330 | } |
328 | 331 |
|
329 | 332 | function changeButtonStates(state) { |
330 | | - state ? buttonIDs.forEach((id) => $(id).addClass('disabled')) : buttonIDs.forEach((id) => $(id).removeClass('disabled')); |
331 | | - |
332 | | - // prop each selected button with selected submissions |
333 | | - if (!state) { |
334 | | - var urlParam = $.param({'submission_ids': selectedSubmissions}); |
335 | | - buttonIDs.forEach(function(id) { |
336 | | - var newHref = baseURLs[id] + '?' + urlParam; |
337 | | - $(id).prop('href', newHref); |
338 | | - }); |
339 | | - } else { |
340 | | - buttonIDs.forEach(function(id) { |
341 | | - $(id).prop('href', baseURLs[id]); |
342 | | - }); |
343 | | - } |
| 333 | + buttonIDs.forEach((id) => { |
| 334 | + const button = $(id); |
| 335 | + if (state) { |
| 336 | + if (id === "#download-selected") { |
| 337 | + $(id).prop('href', baseURLs[id]); |
| 338 | + } |
| 339 | + button.addClass("disabled"); |
| 340 | + button.off("click").prop("disabled", true); |
| 341 | + } else { |
| 342 | + button.removeClass("disabled").prop("disabled", false); |
| 343 | + if (id == "#download-selected") { |
| 344 | + var urlParam = $.param({'submission_ids': selectedSubmissions}); |
| 345 | + buttonIDs.forEach(function(id) { |
| 346 | + var newHref = baseURLs[id] + '?' + urlParam; |
| 347 | + $(id).prop('href', newHref); |
| 348 | + }); |
| 349 | + return; |
| 350 | + } |
| 351 | + $(document).off("click", id).on("click", id, function (event) { |
| 352 | + console.log(`${id} button clicked`); |
| 353 | + event.preventDefault(); |
| 354 | + if (selectedSubmissions.length === 0) { |
| 355 | + alert("No submissions selected."); |
| 356 | + return; |
| 357 | + } |
| 358 | + const endpoint = manage_submissions_endpoints[id.replace("#", "")]; |
| 359 | + const requestData = { submission_ids: selectedSubmissions }; |
| 360 | + if (id === "#delete-selected") { |
| 361 | + if (!confirm("Deleting will delete all checked submissions and cannot be undone. Are you sure you want to delete these submissions?")) { |
| 362 | + return; |
| 363 | + } |
| 364 | + } |
| 365 | + let refreshInterval = setInterval(() => { |
| 366 | + location.reload(); |
| 367 | + }, 5000); |
| 368 | + $.ajax({ |
| 369 | + url: endpoint, |
| 370 | + type: "POST", |
| 371 | + contentType: "application/json", |
| 372 | + data: JSON.stringify(requestData), |
| 373 | + dataType: "json", |
| 374 | + headers: { |
| 375 | + "X-CSRF-Token": $('meta[name="csrf-token"]').attr("content"), |
| 376 | + }, |
| 377 | + success: function (response) { |
| 378 | + clearInterval(refreshInterval); |
| 379 | + if (response.redirect) { |
| 380 | + window.location.href = response.redirect; |
| 381 | + return; |
| 382 | + } |
| 383 | + if (response.error) { |
| 384 | + alert(response.error); |
| 385 | + } |
| 386 | + if (response.success) { |
| 387 | + alert(response.success); |
| 388 | + } |
| 389 | + selectedSubmissions = []; |
| 390 | + changeButtonStates(true); |
| 391 | + }, |
| 392 | + error: function (error) { |
| 393 | + clearInterval(refreshInterval); |
| 394 | + alert("An error occurred while processing the request."); |
| 395 | + }, |
| 396 | + }); |
| 397 | + }); |
| 398 | + } |
| 399 | + }); |
344 | 400 | } |
345 | 401 |
|
346 | 402 | changeButtonStates(true); // disable all buttons by default |
|
0 commit comments