/* // Custom JS | written by https://github.com/wdzajicek // © 2020 Kankakee Community College // =================================================== */ // JS module to build alert message using data from Google Sheets API v4 // // This exported module requires you pass it's default-function the `response` object from the API call, as the only argument // import parseMarkdownToHTML from './parseMarkdownToHTML.js'; // Parses a simplified markdown into html & creates the paragraph el's with appropriate class // const CAMPUS_ALERTS_DIV_ID_STRING = 'emergencyAlerts'; // ID of the div to house campus alerts - already built into the page. const ALERTS_VISIBLE_CLASS = 'position__offset-alert--visible'; const TARGET = document.getElementById(CAMPUS_ALERTS_DIV_ID_STRING); // This targets an element built into the DOM that we inject everything into. function injectAlert(target, alert, resolve) { target.innerHTML = alert; target.classList.add(ALERTS_VISIBLE_CLASS); return resolve != undefined ? resolve() : null; } function checkAlertType(type) { return type == 'SCHOOL EMERGENCY/CLOSURE - red' ? 'danger' : type == 'SCHOOL INFO - blue' ? 'primary' : type == 'SCHOOL INFO - cyan' ? 'info' : 'warning'; } function createAlertsHtml(response, resolve) { // Incoming response from our Google Sheet via the Sheets API let [visibility, allPages, content, expire, start, end, type] = response.result.values[2]; // The 3rd row has our table's data if (visibility === 'FALSE') // Predefined dropdown options in the Sheet are `'TRUE'` & `'FALSE'` return; const d = new Date; const s = new Date(start); const e = new Date(end); const alertTypeClass = checkAlertType(type); const alertType = checkAlertType(type); const alertIsActive = expire === 'FALSE' || expire === 'TRUE' && s.getTime() <= d.getTime() && e.getTime() > d.getTime(); const indexPageOnly = allPages === 'TRUE' || allPages === 'FALSE' && window.location.pathname == '/'; let alert = `
`; [d,s,e].map(d => d.setHours(0, 0, 0, 0)); alertIsActive && indexPageOnly ? injectAlert(TARGET, alert, resolve) : null; } export default createAlertsHtml;