From b17f29e307ef00f3eb05c858e0f81045e8f47c8b Mon Sep 17 00:00:00 2001 From: Splines Date: Wed, 13 Sep 2023 00:10:18 +0200 Subject: [PATCH] Fetch and embed popup HTML as modal --- app/javascript/packs/application.js | 81 +++++++++++++++++++++++++---- public/news_popups/_container.html | 24 +++++++++ 2 files changed, 94 insertions(+), 11 deletions(-) create mode 100644 public/news_popups/_container.html diff --git a/app/javascript/packs/application.js b/app/javascript/packs/application.js index 0936e2964..62b9139a0 100644 --- a/app/javascript/packs/application.js +++ b/app/javascript/packs/application.js @@ -53,12 +53,14 @@ document.addEventListener("turbolinks:load", function () { percentPosition: true }); - checkForNewsPopups(); + // TODO (Splines): outsource news popups logic to separate file + handleNewsPopups(); }); const NEWS_POPUPS_BASE_PATH = '/news_popups/'; +const NEWS_POPUPS_CONTAINER_PATH = '/news_popups/_container.html'; -function checkForNewsPopups() { +function handleNewsPopups() { $.get({ url: Routes.news_popups_path(), type: 'GET', @@ -75,21 +77,78 @@ function checkForNewsPopups() { }); } -function openNewsPopups(popupNames) { - for (const popupName of popupNames) { - console.log(`Opening news popup: ${popupName}`) +async function openNewsPopups(popupNames) { + // Are there any unread news popups? + if (popupNames.length === 0) { + console.log('No unread news popups found. Aborting.'); + return; + } + + // Container + let containerHtml = ""; + try { + containerHtml = await fetchNewsPopupsContainerHtml(); + } catch (error) { + console.log('Container for news popups could not be fetched. Aborting.'); + console.log(error); + return; + } + + // Contents + const contentsHtml = await fetchNewsPopupsContentHtml(popupNames); + if (contentsHtml.length === 0) { + console.log('No news popups content could be fetched. Aborting.'); + return; + } + + showNewsPopupsInDom(containerHtml, contentsHtml); +} + +async function fetchNewsPopupsContainerHtml() { + return new Promise((resolve, reject) => { $.ajax({ - url: NEWS_POPUPS_BASE_PATH + popupName + '.html', + url: NEWS_POPUPS_CONTAINER_PATH, type: 'GET', dataType: 'html', success: (html, textStatus, xhr) => { - console.log(`News popup (${popupName}): ${html}`); + resolve(html); }, error: (xhr, status) => { - console.log(`ERROR getting HTML for news popup: ${popupName}`); console.log(xhr); console.log(status); + reject('ERROR getting HTML for news popups container'); } - }) - } -} \ No newline at end of file + }); + }); +} + +async function fetchNewsPopupsContentHtml(popupNames) { + return await Promise.all(popupNames.map(async (popupName) => { + return new Promise((resolve, reject) => { + $.ajax({ + url: NEWS_POPUPS_BASE_PATH + popupName + '.html', + type: 'GET', + dataType: 'html', + success: (html, textStatus, xhr) => { + resolve(html); + }, + error: (xhr, status) => { + // Note that this this not occur if the file does not exist. + // In that case, the success callback is called since MaMpf + // is redirecting and we get back the HTML of the redirect. + // This is why admins must check if the file exists before + // activating the news popup. + const errorReason = `ERROR getting HTML for news popup: ${popupName} `; + reject(errorReason); + } + }); + }); + })); +} + +function showNewsPopupsInDom(containerHtml, contentsHtml) { + const container = $(containerHtml); + $('#news-popup-body', container).append(contentsHtml); + $('body').append(container); + $('#news-popup-modal').modal('show'); +} diff --git a/public/news_popups/_container.html b/public/news_popups/_container.html new file mode 100644 index 000000000..eb92f3160 --- /dev/null +++ b/public/news_popups/_container.html @@ -0,0 +1,24 @@ + \ No newline at end of file