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
11 changes: 9 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,13 @@ const getIcon = (content) => {
return icons.company;
};

const geoRoutingActive = () => !!document.querySelector('.locale-modal-v2')
|| !!document.querySelector('.locale-modal');

const waitForClosedGRMThen = (loadPEP) => {
window.addEventListener('milo:modal:closed', loadPEP);
Copy link
Contributor

Choose a reason for hiding this comment

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

sorry for doing this, but I just realized that it's possible for two dialogs to be open.
see this link - https://stage--cc--adobecom.hlx.page/products/photoshop/ai?milolibs=pep-georouting&skipPepEntitlements=true#langnav
we need to add a check here that geoRoutingActive() is now false.

Copy link
Contributor Author

@sharmrj sharmrj Jun 4, 2024

Choose a reason for hiding this comment

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

I've discovered that when the event is emitted it won't necessarily trigger PEP's init method, since it's possible for the modal to still exist in the DOM at that point. I'm looking into why this might be.

Copy link
Contributor

Choose a reason for hiding this comment

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

completely agree.
In my opinion PEP should not be appearing if any modal is open, but since that makes your task more complicated I didn't mention it.
Maybe you can mention that to your PdM and if they deem it necessary then tackle that in a separate task.

Copy link
Contributor

Choose a reason for hiding this comment

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

oh and btw, you want to clean up your listener once you've triggered it once.
https://dev.to/js_bits_bill/addeventlistener-once-js-bits-565d

Copy link
Contributor Author

Choose a reason for hiding this comment

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

So it seems a bit implementation dependent (which means the whatwg spec doesn't seem to have information about this, but there is a chromium design document here), but on chrome once the event is dispatched it seems that the eventListener's callback is called first, and then the rest of the code that removes the modal fires (kind of like a delimited continuation). See the video below:

eb.mp4

I believe the behavior that targets specifically the GRM (or perhaps modals in general?) with a custom event might be better in this case, but this would involve touching a lot more code.

In the most general sense, it's not possible for me to predict if sometime between the moment a modal is closed and the moment where the user is redirected by pep a modal will pop up (as lang nav pops up very late). So I'm willing to compromise to allowing PEP to begin iff there are no modals on the screen, while making no guarantees about future modals.

Copy link
Contributor

Choose a reason for hiding this comment

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

@sharmrj it seems to me that the right solution would be to change the modal.js so that the close event is sent after removing the modal.
@JasonHowellSlavin can you weigh in if you see issues with moving the window.dispatchEvent of milo:modal:closed after removing the modal?

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @vhargrave, I do think both you and @sharmrj have identified a key issue with the milo:modal:closed dispatch being placed too high in the function and not reflecting what is actually occurring. I don't see an issue with moving it down, but since there are a few other files that rely on it, it might be good to raise that as an issue and specifically test for those implementations.

It also looks like @sharmrj has done some experimentation and found the performance impact of the current code to be negligible.

Copy link
Contributor

Choose a reason for hiding this comment

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

@JasonHowellSlavin ok, thanks for confirming 👍

};

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

this.init();
if (geoRoutingActive()) waitForClosedGRMThen(this.init);
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
if (geoRoutingActive()) waitForClosedGRMThen(this.init);
if (geoRoutingActive()) document.addEventListener('milo:modal:closed', loadPEP);

else this.init();
}

init = async () => {
Expand Down
21 changes: 21 additions & 0 deletions test/features/webapp-prompt/webapp-prompt.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,27 @@ describe('PEP', () => {
await clock.runAllAsync();
expect(document.querySelector(allSelectors.pepWrapper)).to.not.exist;
});

it('should not render PEP when the GRM is open', async () => {
document.body.insertAdjacentHTML('afterbegin', '<div class="locale-modal-v2"></div>');
document.body.insertAdjacentHTML('afterbegin', '<div class="locale-modal"></div>');

await initPep({});

try {
clock.runAll();
} catch (e) {
expect(document.querySelector(allSelectors.pepWrapper)).to.not.exist;
}

document.querySelector('.locale-modal-v2')?.remove();
document.querySelector('.locale-modal')?.remove();
const ev = new CustomEvent('milo:modal:closed');
window.dispatchEvent(ev);

await clock.runAllAsync();
expect(document.querySelector(allSelectors.pepWrapper)).to.exist;
});
});

describe('PEP configuration tests', () => {
Expand Down
Loading