Skip to content

Commit

Permalink
Replace $.ajax() with fetch()
Browse files Browse the repository at this point in the history
  • Loading branch information
asdil12 committed Sep 13, 2024
1 parent 0967772 commit b4c6324
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) {
if (! 'headers' in options) options['headers'] = {};

Check failure on line 31 in assets/javascripts/openqa.js

View workflow job for this annotation

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

Replace `!·'headers'` with `(!'headers')`

Check failure on line 31 in assets/javascripts/openqa.js

View workflow job for this annotation

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

Unexpected negating the left operand of 'in' operator
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)

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

View workflow job for this annotation

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

Delete `⏎·······`
prioValueElement.text(newPrio);
else
addFlash('danger', '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 `'danger',·'Unable·to·set·the·priority·value·of·job·'·+·jobId·+·':·Server·returned·'·+·response.status·+·':·'·+·response.statusText` with `⏎··········'danger',⏎··········'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 b4c6324

Please sign in to comment.