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-142935 [Project PEP] PEP <> Geo Routing Modal Interactions #2403

Merged
merged 12 commits into from
Jun 17, 2024
17 changes: 12 additions & 5 deletions libs/features/georouting/georouting.js
Original file line number Diff line number Diff line change
Expand Up @@ -137,27 +137,34 @@ export default async function loadGeoRouting(config, createTag, getMetadata) {
const storedLocale = storedInter === 'us' ? '' : storedInter;

const resp = await fetch(`${config.contentRoot ?? ''}/georouting.json`);
if (!resp.ok) return;
if (!resp.ok) return false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all these changes are unnecessary

const json = await resp.json();

const urlGeoData = json.data.find((d) => d.prefix === urlLocale);
if (!urlGeoData) return;
if (!urlGeoData) return false;

if (storedLocale || storedLocale === '') {
// Show modal when url and cookie disagree
if (urlLocale.split('_')[0] !== storedLocale.split('_')[0]) {
const localeMatches = json.data.filter((d) => d.prefix === storedLocale);
const details = await getDetails(urlGeoData, localeMatches, config, createTag, getMetadata);
if (details) { await showModal(details); }
if (details) {
await showModal(details);
return true;
}
}
return;
return false;
}

// Show modal when derived countries from url locale and akamai disagree
const akamaiCode = await getAkamaiCode();
if (akamaiCode && !getCodes(urlGeoData).includes(akamaiCode)) {
const localeMatches = getMatches(json.data, akamaiCode);
const details = await getDetails(urlGeoData, localeMatches, config, createTag, getMetadata);
if (details) { await showModal(details); }
if (details) {
await showModal(details);
return true;
}
}
return false;
}
12 changes: 7 additions & 5 deletions libs/features/georoutingv2/georoutingv2.js
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ export default async function loadGeoRouting(
loadBlockFunc,
loadStyleFunc,
) {
if (getGeoroutingOverride()) return;
if (getGeoroutingOverride()) return false;
config = conf;
createTag = createTagFunc;
getMetadata = getMetadataFunc;
Expand All @@ -285,8 +285,7 @@ export default async function loadGeoRouting(
if (!resp.ok) {
// eslint-disable-next-line import/no-cycle
const { default: loadGeoRoutingOld } = await import('../georouting/georouting.js');
loadGeoRoutingOld(config, createTag, getMetadata);
return;
return loadGeoRoutingOld(config, createTag, getMetadata);
}
const json = await resp.json();

Expand All @@ -297,7 +296,7 @@ export default async function loadGeoRouting(
const storedLocale = storedInter === 'us' ? '' : storedInter;

const urlGeoData = json.georouting.data.find((d) => d.prefix === urlLocale);
if (!urlGeoData) return;
if (!urlGeoData) return false;

if (storedLocale || storedLocale === '') {
const urlLocaleGeo = urlLocale.split('_')[0];
Expand All @@ -313,9 +312,10 @@ export default async function loadGeoRouting(
sendAnalyticsFunc(
new Event(`Load:${storedLocaleGeo || 'us'}-${urlLocaleGeo || 'us'}|Geo_Routing_Modal`),
);
return true;
}
}
return;
return false;
}

// Show modal when derived countries from url locale and akamai disagree
Expand All @@ -333,5 +333,7 @@ export default async function loadGeoRouting(
}
} catch (e) {
window.lana?.log(e.message);
return false;
}
return true;
}
14 changes: 12 additions & 2 deletions libs/features/webapp-prompt/webapp-prompt.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,16 @@
return icons.company;
};

const waitForClosedGRMThen = (loadPEP) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

instead of polling the DOM, you should listen for milo:modal:closed on the window object.
Whent that event is triggered you could then check if .locale-modal-v2 is no longer present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem is that the global-navigation.js/webapp-prompt.js might not have loaded by the time the user gets rid of the GRM, especially on slow connections, so the event would be missed and we would have to poll anyway unless we set a global flag. I'm not sure this would be all that much more readable than what we have now. Is there a particular compunction we should have against polling for the GRM?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can check to see if .locale-modal-v2 is present in the DOM first and if it's available there, then add an event listening for it to close.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in general polling is worse for performance than just listening to the right event.

const grExists = !!document.querySelector('.locale-modal-v2')
|| !!document.querySelector('.locale-modal');
if (grExists) {
setTimeout(() => waitForClosedGRMThen(loadPEP), 200);
return;
}
loadPEP();

Check warning on line 45 in libs/features/webapp-prompt/webapp-prompt.js

View check run for this annotation

Codecov / codecov/patch

libs/features/webapp-prompt/webapp-prompt.js#L39-L45

Added lines #L39 - L45 were not covered by tests
};

class AppPrompt {
constructor({ promptPath, entName, parent, getAnchorState } = {}) {
this.promptPath = promptPath;
Expand All @@ -43,8 +53,8 @@
this.getAnchorState = getAnchorState;
this.id = this.promptPath.split('/').pop();
this.elements = {};

this.init();
if (getConfig().grmActive) waitForClosedGRMThen(this.init);
else this.init();
}

init = async () => {
Expand Down
2 changes: 1 addition & 1 deletion libs/utils/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -940,7 +940,7 @@ async function loadPostLCP(config) {
const georouting = getMetadata('georouting') || config.geoRouting;
if (georouting === 'on') {
const { default: loadGeoRouting } = await import('../features/georoutingv2/georoutingv2.js');
await loadGeoRouting(config, createTag, getMetadata, loadBlock, loadStyle);
config.grmActive = await loadGeoRouting(config, createTag, getMetadata, loadBlock, loadStyle);
sharmrj marked this conversation as resolved.
Show resolved Hide resolved
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you don't need to make all these changes to the georouting files.
You're already awaiting a response here .
Just check if locale-modal-v2 has been added to the DOM right here
document.querySelector('.locale-modal-v2')

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually in retrospect I don't even think you need to have any code here. Just add that listener I described in your webapp-prompt file, no?

if (config.mep?.targetEnabled === 'gnav') {
await loadMartech({ persEnabled: true, postLCP: true });
Expand Down
Loading