docbrown/app/javascript/packs/runtimeBanner.jsx
Fernando Valverde 8095fdb891
Runtime Banner v2 (#14119)
* Banner v2 - first working version

* Move pack_with_chunks_tag up + removing comments

* Trying to fix unlrelated tests

* Revert experiment

* Defer initialization until page is ready

* Test refactor + trying out UDL Server refactor

* Revert experiment - to be handled in separate PR

* refactor to use function component

* Cleanup unused lines in cypress test

* Update app/javascript/runtimeBanner/RuntimeBanner.jsx

Co-authored-by: Nick Taylor <nick@forem.com>

* Apply review feedback

* Update app/javascript/runtimeBanner/RuntimeBanner.jsx

Co-authored-by: Nick Taylor <nick@forem.com>

* Add import clause + extract functions outside of component

Co-authored-by: Nick Taylor <nick@forem.com>
2021-07-14 14:44:07 -06:00

32 lines
1 KiB
JavaScript

import { h, render } from 'preact';
import { RuntimeBanner } from '../runtimeBanner';
function loadElement() {
const container = document.getElementById('runtime-banner-container');
if (container) {
render(<RuntimeBanner />, container);
}
}
function initializeBannerWhenPageIsReady() {
setTimeout(() => {
if (document.body.getAttribute('data-loaded') === 'true') {
// We're ready to initialize
window.InstantClick.on('change', () => {
loadElement();
});
loadElement();
} else {
// Page hasn't initialized yet. We need to wait until the page is ready
initializeBannerWhenPageIsReady();
}
}, 100);
}
// This pack relies on the same logic as `packs/listings` & `packs/Chat`. The
// banner lives in every page (including the main feed) and a race condition
// occurs when the page initializes for the first time or when signing out (no
// cache request). In order to avoid this we defer the initialization until the
// page is actually ready.
initializeBannerWhenPageIsReady();