Skip to content
This repository has been archived by the owner on Dec 11, 2024. It is now read-only.

chore: adding more selective partner logic #86

Merged
merged 3 commits into from
Mar 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 15 additions & 19 deletions blocks/footer/footer.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import {
decorateIcons,
decorateLinkedPictures,
createOptimizedPicture,
lookupPages,
wrapImgsInLinks,
fetchPlaceholders,
} from '../../scripts/scripts.js';

import { setupSponsors } from '../sponsors/sponsors.js';

function setupCookieChoices(section) {
const cookieLink = section.querySelector('a[href*="onetrust-link"]');
if (cookieLink) {
Expand All @@ -26,27 +26,23 @@ function setupSocialButtons(section) {
}

async function setupPartners(section) {
const pages = await lookupPages();
const { sponsorOrder } = await fetchPlaceholders();
const sponsors = pages.filter((e) => e.path.startsWith('/sponsors/'));
const orderedSponsors = [];
if (sponsorOrder) {
sponsorOrder.split(',').forEach((sp) => {
// eslint-disable-next-line no-param-reassign
sp = sp.trim();
const match = sponsors.find((sponsor) => sponsor.title === sp);
if (match) {
// remove match from sponsors
sponsors.splice(sponsors.indexOf(match), 1);
// add match to ordered sponsors
orderedSponsors.push(match);
}
});
let sponsorLinks = [];
try {
const resp = await fetch('/sponsors');
// eslint-disable-next-line no-await-in-loop
const html = await resp.text();
const dp = new DOMParser();
const sponsorsDoc = dp.parseFromString(html, 'text/html');
sponsorLinks = [...sponsorsDoc.querySelectorAll('.sponsors a')].map((a) => a.href);
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
}
const sponsors = await setupSponsors(sponsorLinks);

const wrapper = document.createElement('div');
// combine ordered sponsors with any remaining unordered sponsors
[...orderedSponsors, ...sponsors].forEach((sponsor) => {
sponsors.forEach((sponsor) => {
const partner = document.createElement('div');
partner.className = 'footer-partner';
const link = document.createElement('a');
Expand Down
18 changes: 15 additions & 3 deletions blocks/header/header.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import {
readBlockConfig,
fetchPlaceholders,
decorateIcons,
lookupPages,
createOptimizedPicture,
wrapImgsInLinks,
decorateLinkedPictures,
} from '../../scripts/scripts.js';

import { setupSponsors } from '../sponsors/sponsors.js';

/**
* collapses all open nav sections
* @param {Element} sections The container element
Expand All @@ -26,8 +27,19 @@ function displayNextPartner(proud) {
}

async function setupPartners(section) {
const pages = await lookupPages();
const sponsors = pages.filter((e) => e.path.startsWith('/sponsors/'));
let sponsorLinks = [];
try {
const resp = await fetch('/sponsors');
// eslint-disable-next-line no-await-in-loop
const html = await resp.text();
const dp = new DOMParser();
const sponsorsDoc = dp.parseFromString(html, 'text/html');
sponsorLinks = [...sponsorsDoc.querySelectorAll('.sponsors a')].map((a) => a.href);
} catch (err) {
// eslint-disable-next-line no-console
console.log(err);
}
const sponsors = await setupSponsors(sponsorLinks);

if (sponsors.length > 0) {
const hasWhiteBg = [...section.classList].includes('white');
Expand Down
66 changes: 48 additions & 18 deletions blocks/sponsors/sponsors.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,58 @@ import {
toClassName,
} from '../../scripts/scripts.js';

export async function setupSponsors(sponsorLinks = []) {
let sponsors = [];
let orderedSponsors = [];

if (sponsorLinks.length) {
for (let i = 0; i < sponsorLinks.length; i += 1) {
const sponsorLink = sponsorLinks[i];
// eslint-disable-next-line no-await-in-loop
const resp = await fetch(sponsorLink);
// eslint-disable-next-line no-await-in-loop
const html = await resp.text();
const dp = new DOMParser();
const sponsorDoc = dp.parseFromString(html, 'text/html');
const title = sponsorDoc.querySelector('title').textContent;
const image = sponsorDoc.querySelector('meta[property="og:image"]').content;
const description = sponsorDoc.querySelector('meta[property="og:description"]').content;
const link = sponsorDoc.querySelector('meta[name="external-link"]').content;
const logoWhite = sponsorDoc.querySelector('meta[name="logo-white"]').content;

sponsors.push({
title, image, description, link, logoWhite,
});
}
} else {
const pages = await lookupPages();
const { sponsorOrder } = await fetchPlaceholders();
sponsors = pages.filter((e) => e.path.startsWith('/sponsors/'));
orderedSponsors = [];
if (sponsorOrder) {
sponsorOrder.split(',').forEach((sp) => {
// eslint-disable-next-line no-param-reassign
sp = sp.trim();
const match = sponsors.find((sponsor) => sponsor.title === sp);
if (match) {
// remove match from sponsors
sponsors.splice(sponsors.indexOf(match), 1);
// add match to ordered sponsors
orderedSponsors.push(match);
}
});
}
}
return ([...orderedSponsors, ...sponsors]);
}

export default async function decorate(block) {
const sponsorLinks = [...block.querySelectorAll('a')].map((a) => a.href);
const sponsors = await setupSponsors(sponsorLinks);
block.textContent = '';
const pages = await lookupPages();
const { sponsorOrder } = await fetchPlaceholders();
const sponsors = pages.filter((e) => e.path.startsWith('/sponsors/'));
const orderedSponsors = [];
if (sponsorOrder) {
sponsorOrder.split(',').forEach((sp) => {
// eslint-disable-next-line no-param-reassign
sp = sp.trim();
const match = sponsors.find((sponsor) => sponsor.title === sp);
if (match) {
// remove match from sponsors
sponsors.splice(sponsors.indexOf(match), 1);
// add match to ordered sponsors
orderedSponsors.push(match);
}
});
}

// combine ordered sponsors with any remaining unordered sponsors
[...orderedSponsors, ...sponsors].forEach((sponsor) => {
sponsors.forEach((sponsor) => {
const card = document.createElement('div');
card.className = 'sponsors-sponsor';
const wrapper = document.createElement('div');
Expand Down