Skip to content

Commit

Permalink
Replace $.ajax() with fetch()
Browse files Browse the repository at this point in the history
  • Loading branch information
asdil12 and Martchus committed Sep 16, 2024
1 parent 0967772 commit 9f07f1a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
10 changes: 10 additions & 0 deletions assets/javascripts/openqa.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ function setupForAll() {
});
}

function getCSRFToken() {
return document.querySelector('meta[name="csrf-token"]').content;
}

function fetchWithCSRF(resource, options) {
options.headers ??= {};
options.headers['X-CSRF-TOKEN'] ??= getCSRFToken();
return fetch(resource, options);
}

function makeFlashElement(text) {
return typeof text === 'string' ? '<span>' + text + '</span>' : text;
}
Expand Down
21 changes: 11 additions & 10 deletions assets/javascripts/tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -191,16 +191,17 @@ function changeJobPrio(jobId, delta, linkElement) {
}

var newPrio = currentPrio + delta;
$.ajax({
url: urlWithBase('/api/v1/jobs/' + jobId + '/prio?prio=' + newPrio),
method: 'POST',
success: function (result) {
prioValueElement.text(newPrio);
},
error: function (xhr, ajaxOptions, thrownError) {
addFlash('danger', 'Unable to set the priority value of job ' + jobId + '.');
}
});

fetchWithCSRF(urlWithBase(`/api/v1/jobs/${jobId}/prio?prio=${newPrio}`), {method: 'POST'})
.then(response => {
if (response.status == 200) prioValueElement.text(newPrio);
else
addFlash('danger',

Check failure on line 199 in assets/javascripts/tests.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Insert `⏎··········`
`Unable to set the priority value of job ${jobId}: Server returned ${response.status}: ${response.statusText}`);

Check failure on line 200 in assets/javascripts/tests.js

View workflow job for this annotation

GitHub Actions / Lint JavaScript code (using Node version 20)

Replace `·················`Unable·to·set·the·priority·value·of·job·${jobId}:·Server·returned·${response.status}:·${response.statusText}`` with `··········`Unable·to·set·the·priority·value·of·job·${jobId}:·Server·returned·${response.status}:·${response.statusText}`⏎········`
})
.catch(error => {
addFlash('danger', `Unable to set the priority value of job ${jobId}: ${error}`);
});
}

function renderTestSummary(data) {
Expand Down

0 comments on commit 9f07f1a

Please sign in to comment.