Skip to content

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

Merged
processprocess merged 51 commits intoampproject:mainfrom
processprocess:pdp
Feb 10, 2022
Merged

✨ [amp story shopping] Product description page template (PDP)#37535
processprocess merged 51 commits intoampproject:mainfrom
processprocess:pdp

Conversation

@processprocess
Copy link
Copy Markdown
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

@processprocess processprocess requested review from coreymasanto, gmajoulet, jshamble and mszylkowski and removed request for gmajoulet February 4, 2022 19:00
@processprocess processprocess marked this pull request as ready for review February 7, 2022 17:32
@amp-owners-bot
Copy link
Copy Markdown

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
Copy Markdown
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

StateProperty.PAGE_ATTACHMENT_STATE,
(isOpen) => this.onPageAttachmentStateUpdate_(isOpen)
(isOpen) => {
const shoppingData = this.storeService_.get(
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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?

StateProperty.PAGE_ATTACHMENT_STATE,
(isOpen) => this.onPageAttachmentStateUpdate_(isOpen)
(isOpen) => {
const shoppingData = this.storeService_.get(
Copy link
Copy Markdown
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