-
Notifications
You must be signed in to change notification settings - Fork 3.9k
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
✨ [amp story shopping] Product description page template (PDP) #37535
Conversation
Hey @gmajoulet! These files were changed:
Hey @newmuis! These files were changed:
|
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.
Overall really great, just some small nits
extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.css
Outdated
Show resolved
Hide resolved
extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.css
Outdated
Show resolved
Hide resolved
@@ -105,45 +109,213 @@ export class AmpStoryShoppingAttachment extends AMP.BaseElement { | |||
} | |||
this.storeService_.subscribe( | |||
StateProperty.PAGE_ATTACHMENT_STATE, | |||
(isOpen) => this.onPageAttachmentStateUpdate_(isOpen) | |||
(isOpen) => { | |||
const shoppingData = this.storeService_.get( |
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 know you're checking if (isOnActivePage)
in all the methods, but I wouldn't mind either moving the check here, or duplicating the check if you want to be super safe (or have other code paths calling these methods)
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.
+1 to this
extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js
Outdated
Show resolved
Hide resolved
// Iterate over built templates and set active attribute. | ||
// If a template is already built, return early. | ||
let templateAlreadyBuilt = false; | ||
Object.values(this.builtTemplates_).forEach((template) => { | ||
if (template === this.builtTemplates_[templateId]) { | ||
templateAlreadyBuilt = true; | ||
this.mutateElement(() => { | ||
template.setAttribute('active', true); | ||
this.resetCarouselScroll_(template); | ||
}); | ||
} else { | ||
this.mutateElement(() => template.removeAttribute('active')); | ||
} | ||
}); | ||
|
||
// Early return if template is already built. | ||
if (templateAlreadyBuilt) { | ||
return; | ||
} | ||
|
||
// If not already built, build template and assign it to builtTemplates_ by templateId. | ||
this.builtTemplates_[templateId] = ( | ||
<div class="i-amphtml-amp-story-shopping" active> | ||
{featuredProduct && this.renderPdpTemplate_(featuredProduct)} | ||
{shoppingDataForPage.length > 1 && | ||
this.renderPlpTemplate_( | ||
shoppingDataForPage.filter((item) => item !== featuredProduct) | ||
)} | ||
</div> | ||
); | ||
|
||
this.mutateElement(() => | ||
this.templateContainer_.appendChild(this.builtTemplates_[templateId]) | ||
); |
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.
This piece of code is mostly great but has a slight design issue, it creates two code paths depending on if the template is created or re-used.
Down the line this creates more complexity, e.g. you only resetCarouselScroll when re-using the template, not creating it. If you scroll on a PLP and click on a product, the scroll reset behavior would already be different.
You also shouldn't set active
directly on the template as this is an option, if you wanted to prerender the templates that wouldn't work out of the box.
You could extract the template creation into a separate method, like
// pseudocode
getTemplate(templateId, featuredProduct, shoppingDataForPage) {
return this.builtTemplates_[templateId] || (jsx);
}
And then in your updateTemplate:
// Pseudocode
updateTemplate(...) {
...
forEach(template => removeAttr('active'));
const template = getTemplate(...);
// append template if !template.isConnected, set template active, resetScroll
}
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.
isConnected 🔥 TIL.
extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js
Outdated
Show resolved
Hide resolved
// If not already built, build template and assign it to builtTemplates_ by templateId. | ||
this.builtTemplates_[templateId] = ( | ||
<div class="i-amphtml-amp-story-shopping" active> | ||
{featuredProduct && this.renderPdpTemplate_(featuredProduct)} | ||
{shoppingDataForPage.length > 1 && | ||
this.renderPlpTemplate_( | ||
shoppingDataForPage.filter((item) => item !== featuredProduct) | ||
)} | ||
</div> | ||
); | ||
|
||
this.mutateElement(() => | ||
this.templateContainer_.appendChild(this.builtTemplates_[templateId]) | ||
); |
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.
Mind pulling this out into a new buildTemplate_(templateId, featuredProduct, shoppingDataForPage)
method?
@@ -105,45 +109,213 @@ export class AmpStoryShoppingAttachment extends AMP.BaseElement { | |||
} | |||
this.storeService_.subscribe( | |||
StateProperty.PAGE_ATTACHMENT_STATE, | |||
(isOpen) => this.onPageAttachmentStateUpdate_(isOpen) | |||
(isOpen) => { | |||
const shoppingData = this.storeService_.get( |
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.
+1 to this
Demo
The details module will be integrated in #36740