Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mwpw 155385 personalisation #36

Merged
merged 7 commits into from
Aug 16, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
81 changes: 81 additions & 0 deletions edsdme/scripts/personalization.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import {
getCurrentProgramType,
getPartnerDataCookieValue,
isMember,
partnerIsSignedIn,
getPartnerDataCookieObject,
signedInNonMember,
isResseler,
}
from './utils.js';

const PAGE_PERSONALIZATION_PLACEHOLDERS = { firstName: '//*[contains(text(), "$firstName")]' };

const LEVEL_CONDITION = 'partner-level';
const PERSONALIZATION_MARKER = 'partner-personalization';
const PROGRAM = getCurrentProgramType();
const PARTNER_LEVEL = getPartnerDataCookieValue(PROGRAM, 'level');
const COOKIE_OBJECT = getPartnerDataCookieObject(PROGRAM);
const PERSONALIZATION_CONDITIONS = {
'partner-not-member': signedInNonMember(),
'partner-not-signed-in': !partnerIsSignedIn(),
'partner-all-levels': isMember(),
'partner-reseller': isResseler(PARTNER_LEVEL),
'partner-level': (level) => PARTNER_LEVEL === level,
};

function getNodesByXPath(query, context = document) {
const nodes = [];
const xpathResult = document.evaluate(query, context);
let current = xpathResult?.iterateNext();
while (current) {
nodes.push(current);
current = xpathResult.iterateNext();
}
return nodes;
}

function personalizePlaceholders(placeholders, context = document) {
Object.entries(placeholders).forEach(([key, value]) => {
const placeholderValue = COOKIE_OBJECT[key];
getNodesByXPath(value, context).forEach((el) => {
if (!placeholderValue) el.remove();
el.textContent = el.textContent.replace(`$${key}`, placeholderValue);
});
});
}

function removeElement(element, conditions) {
if (!element || !conditions?.length) return;
const remove = conditions.every((condition) => {
const conditionLevel = condition.startsWith(LEVEL_CONDITION) ? condition.split('-').pop() : '';
return conditionLevel
? !PERSONALIZATION_CONDITIONS[LEVEL_CONDITION](conditionLevel) : !PERSONALIZATION_CONDITIONS[condition];
});
if (remove) element.remove();
zagi25 marked this conversation as resolved.
Show resolved Hide resolved
}

function personalizePage(page) {
const blocks = Array.from(page.getElementsByClassName(PERSONALIZATION_MARKER));
const sections = Array.from(page.getElementsByClassName('section-metadata'));
[...blocks, ...sections].forEach((el) => {
let conditions = Object.values(el.classList);
let elementToRemove = el;
if (el.classList.contains('section-metadata')) {
elementToRemove = el.parentElement;
Array.from(el.children).forEach((child) => {
const col1 = child.firstElementChild;
const col2 = child.lastElementChild;
if (col1?.textContent !== 'style' || !col2?.textContent.includes(PERSONALIZATION_MARKER)) return;
conditions = col2?.textContent?.split(',').map((text) => text.trim());
});
}
removeElement(elementToRemove, conditions);
});
}

export function applyPagePersonalization() {
const main = document.querySelector('main') ?? document;
personalizePlaceholders(PAGE_PERSONALIZATION_PLACEHOLDERS, main);
personalizePage(main);
}
2 changes: 2 additions & 0 deletions edsdme/scripts/scripts.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setLibs, redirectLoggedinPartner, updateIMSConfig, preloadResources, getRenewBanner } from './utils.js';
import { applyPagePersonalization } from './personalization.js';

// Add project-wide style path here.
const STYLES = '/edsdme/styles/styles.css';
Expand Down Expand Up @@ -72,6 +73,7 @@ const miloLibs = setLibs(LIBS);
}());

(async function loadPage() {
applyPagePersonalization();
redirectLoggedinPartner();
updateIMSConfig();
await preloadResources(CONFIG.locales, miloLibs);
Expand Down
23 changes: 23 additions & 0 deletions edsdme/scripts/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
/**
* The decision engine for where to get Milo's libs from.
*/

export const LEVELS = {
REGISTERED: 'registered',
CERTIFIED: 'certified',
GOLD: 'gold',
PLATINUM: 'platinum',
DISTRIBBUTOR: 'distributor',
};

export const RESSELER_LEVELS = [LEVELS.REGISTERED, LEVELS.CERTIFIED, LEVELS.GOLD, LEVELS.PLATINUM];

export const [setLibs, getLibs] = (() => {
let libs;
return [
Expand Down Expand Up @@ -115,6 +126,18 @@ export function isMember() {
return status === 'MEMBER';
}

export function partnerIsSignedIn() {
return getCookieValue('partner_data');
zagi25 marked this conversation as resolved.
Show resolved Hide resolved
}

export function signedInNonMember() {
return partnerIsSignedIn() && !isMember();
}

export function isResseler(level) {
zagi25 marked this conversation as resolved.
Show resolved Hide resolved
return RESSELER_LEVELS.includes(level?.toLowerCase());
}

export function getMetadataContent(name) {
return document.querySelector(`meta[name="${name}"]`)?.content;
}
Expand Down
Loading