generated from adobe/aem-boilerplate
-
Notifications
You must be signed in to change notification settings - Fork 177
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-145574 - Dynamic Nav Gnav Implementation #2180
Merged
milo-pr-merge
merged 5 commits into
adobecom:stage
from
JasonHowellSlavin:dynamic-nav-gnav-impl
May 23, 2024
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
6641845
Modifies dynamic nav to work inside the gnav, simplifies logic and co…
JasonHowellSlavin acd311d
Only load if key is present
JasonHowellSlavin 3336150
Linting issues
JasonHowellSlavin a835ea6
Condensing logic re: code review
JasonHowellSlavin f39ca19
Moving code out of init into function per code review
JasonHowellSlavin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { getMetadata } from '../utils/utils.js'; | ||
|
||
export default function dynamicNav(url, key) { | ||
const metadataContent = getMetadata('dynamic-nav'); | ||
|
||
if (metadataContent === 'entry') { | ||
window.sessionStorage.setItem('gnavSource', url); | ||
window.sessionStorage.setItem('dynamicNavKey', key); | ||
return url; | ||
} | ||
|
||
if (metadataContent !== 'on') return url; | ||
|
||
if (key !== window.sessionStorage.getItem('dynamicNavKey')) return url; | ||
|
||
const source = window.sessionStorage.getItem('gnavSource'); | ||
|
||
if (!source) return url; | ||
|
||
return source; | ||
JasonHowellSlavin marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import { readFile } from '@web/test-runner-commands'; | ||
import { expect } from '@esm-bundle/chai'; | ||
import { setConfig } from '../../../libs/utils/utils.js'; | ||
import dynamicNav from '../../../libs/features/dynamic-navigation.js'; | ||
|
||
describe('Dynamic nav', () => { | ||
beforeEach(() => { | ||
const conf = { dynamicNavKey: 'bacom' }; | ||
setConfig(conf); | ||
window.sessionStorage.setItem('gnavSource', 'some-source-string'); | ||
}); | ||
|
||
it('Saves the gnavSource and dynamicNavKey to session storage', async () => { | ||
document.head.innerHTML = await readFile({ path: './mocks/entry.html' }); | ||
window.sessionStorage.removeItem('gnavSource'); | ||
dynamicNav('gnav/aem-sites', 'bacom'); | ||
expect(window.sessionStorage.getItem('gnavSource')).to.equal('gnav/aem-sites'); | ||
expect(window.sessionStorage.getItem('dynamicNavKey')).to.equal('bacom'); | ||
}); | ||
|
||
it('Returns the provided url when the dynamic nav metadata is not present', async () => { | ||
document.head.innerHTML = await readFile({ path: './mocks/off.html' }); | ||
const url = dynamicNav('gnav/aem-sites', 'nocom'); | ||
expect(window.sessionStorage.getItem('gnavSource')).to.equal('some-source-string'); | ||
expect(url).to.equal('gnav/aem-sites'); | ||
}); | ||
|
||
it('Returns the provided url with when the wrong dynamicNavKey is passed', async () => { | ||
document.head.innerHTML = await readFile({ path: './mocks/on.html' }); | ||
const url = dynamicNav('gnav/aem-sites', 'nocom'); | ||
expect(window.sessionStorage.getItem('gnavSource')).to.equal('some-source-string'); | ||
expect(url).to.equal('gnav/aem-sites'); | ||
}); | ||
|
||
it('Returns the sessionStorage url if the item exists, the keys match, and dynamic nav is on', async () => { | ||
document.head.innerHTML = await readFile({ path: './mocks/on.html' }); | ||
const url = dynamicNav('gnav/aem-sites', 'bacom'); | ||
expect(window.sessionStorage.getItem('gnavSource')).to.equal('some-source-string'); | ||
expect(url).to.equal('some-source-string'); | ||
}); | ||
|
||
it('Returns the pprovided url if it does not find an item in sessionStorage and dynamic nav is on', async () => { | ||
document.head.innerHTML = await readFile({ path: './mocks/on.html' }); | ||
window.sessionStorage.removeItem('gnavSource'); | ||
const url = dynamicNav('gnav/aem-sites', 'bacom'); | ||
expect(url).to.equal('gnav/aem-sites'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<meta name="dynamic-nav" content="entry"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<meta name="stub" content="stub"> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<meta name="dynamic-nav" content="on"> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should probably have a new function
getGnavSource
(or something along those lines) that produces the url and contains all the logic for generating the url within it.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with Raghav, we should extract it into a separate function that generates the URL.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm. I am not so sure that I agree. The
dynamic nav
is an opt in feature requiring a consumer to update theirconfig
to have thedynamicNavKey
in order to kick off the experience.Though it may make the
init
function a bit cleaner, a function combining the two would mix both the default behavior and an opt in feature, which I am not certain aids in readability.Interested to see what you think.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the future we may have logic added or subtracted from our hypothetical
getSource
function.init
is and should be agnostic about the details of getting the url and that is communicated well by a separate function.The separation between default and opt in behaviour can be communicated well enough in the function itself by the if statement you'll have to add. And the notion of what constitutes default vs non default behavior now and in the future is malleable enough that it makes sense to cordon it off from our main
init
logic so that it's clear in the future where gnav url generation logic should go.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I appreciate your thoughtful and full response! I see what you're saying. To be honest, I am still on the fence because it feels a bit like premature optimization and we'd be adding 3 lines of code by pulling these 5 out into a function.
Maybe @mokimo can weigh in since he recommended the approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sharmrj So you'd think something like this?
@sharmrj and @bandana147 are the new gnav code owners 😁 So if that's how they'd prefer it, no objections.
Since the init function is growing already, it does look pretty clean indeed, extracting it into a small helper
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup. That's what I was envisioning.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I'll make those changes you requested @sharmrj. Any thoughts on modifying the unit tests? Since the function is called in
init
seems that the tests can remain as is.I'll be sure to update the docs as well @mokimo.
Thank you both for your input!