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

✨ [amp story shopping] Product description page template (PDP) #37535

Merged
merged 51 commits into from
Feb 10, 2022

Conversation

processprocess
Copy link
Contributor

@processprocess processprocess commented Feb 1, 2022

Screen Shot 2022-02-04 at 1 00 03 PM

Demo

The details module will be integrated in #36740

@amp-owners-bot
Copy link

amp-owners-bot bot commented Feb 7, 2022

Hey @gmajoulet! These files were changed:

extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.css
extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js
extensions/amp-story-shopping/0.1/test/test-amp-story-shopping-config.js
extensions/amp-story/1.0/_locales/en.json
src/service/localization/strings.js

Hey @newmuis! These files were changed:

extensions/amp-story/1.0/_locales/en.json
src/service/localization/strings.js

Copy link
Contributor

@gmajoulet gmajoulet left a 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

@@ -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(
Copy link
Contributor

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)

Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to this

Comment on lines 171 to 204
// 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])
);
Copy link
Contributor

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
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

isConnected 🔥 TIL.

Comment on lines 191 to 204
// 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])
);
Copy link
Contributor

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?

extensions/amp-story/1.0/_locales/en.json 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(
Copy link
Contributor

Choose a reason for hiding this comment

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

+1 to this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants