docbrown/app/javascript/utilities/http/csrfToken.js
Ridhwana 3f9b7c073c
Add a MultiSelect Autocomplete component to the Display Ads Page (#18560)
* feat: Add a pack file that pulls in the MultiSelect Component

* feat: move the tags to its own component

* save tags

* refactor: create a getCSRFToken function in the packs files so that it can be used in the admin

* feat: import the new module in request.js

* feat: remove unnecessary id

* feat: first pass of csrf token test

* chore: update the test

* fix: csrf token

* feat: hide the enw functionality behinda  feature flag

* fix: loading form twice

* refactor: import for csrftoken
2022-10-26 16:24:31 +02:00

30 lines
860 B
JavaScript

const MAX_RETRIES = 30;
const RETRY_INTERVAL = 250;
export function getCSRFToken() {
const promise = new Promise((resolve, reject) => {
// eslint-disable-next-line consistent-return
let i = 0;
const waitingOnCSRF = setInterval(() => {
const metaTag = document.querySelector("meta[name='csrf-token']");
i += 1;
if (metaTag) {
clearInterval(waitingOnCSRF);
const authToken = metaTag.getAttribute('content');
return resolve(authToken);
}
if (i === MAX_RETRIES) {
clearInterval(waitingOnCSRF);
Honeybadger.notify(
`Could not locate CSRF metatag ${JSON.stringify(
localStorage.current_user,
)}`,
);
return reject(new Error('Could not locate CSRF meta tag on the page.'));
}
}, RETRY_INTERVAL);
});
return promise;
}