docbrown/app/javascript/packs/billboard.js
Anna Buianova 89b3b8127e
Rename display ads to billboards: css and js code (#19887)
* Renamed js vars/css names

* Renamed display_ads to billboards in js functions, variables, components
2023-08-04 17:30:59 +03:00

42 lines
1.3 KiB
JavaScript

import { setupBillboardDropdown } from '../utilities/billboardDropdown';
import { observeBillboards } from './billboardAfterRenderActions';
async function getBillboard() {
const placeholderElements = document.getElementsByClassName(
'js-billboard-container',
);
const promises = [...placeholderElements].map(generateBillboard);
await Promise.all(promises);
}
async function generateBillboard(element) {
const { asyncUrl } = element.dataset;
if (asyncUrl) {
try {
const response = await window.fetch(`${asyncUrl}`);
const htmlContent = await response.text();
const generatedElement = document.createElement('div');
generatedElement.innerHTML = htmlContent;
element.innerHTML = '';
element.appendChild(generatedElement);
setupBillboardDropdown();
// This is called here because the ad is loaded asynchronously.
// The original code is still in the asset pipeline, so is not importable.
// This could be refactored to be importable as we continue that migration.
// eslint-disable-next-line no-undef
observeBillboards();
} catch (error) {
if (!/NetworkError/i.test(error.message)) {
Honeybadger.notify(error);
}
}
}
}
getBillboard();
export { getBillboard };