Skip to content

Commit

Permalink
🐛 amp-story-shopping does not display CTA if all tags are invalid and…
Browse files Browse the repository at this point in the history
… Fixes remote shopping data loading on refresh with page attachment open (#38035)

* added fixed for shopping tag rendering

* added shopping json patch that fixed regression

* resolved promise .all

* updated template

* refactored state update

* fixed promise resolution

* added more descriptive error message

* cahnged order of ternary

* added history check for page refresh

* updated hsitory service to store all data, not jsut the activeData

* added shopping element

* added shopping attachment logic for page refresh, added back active product only

* added set history state test

* cleaned up some minor nits

* added unit test for shopping tag history

* history stub

* added shopping if clause optimizations

* added dependencies

* added dependencies

* added dependencies

* updated unit tests

* updated unit tests

* updated unit tests

* removed check

* browser test safari

* browser test safari

* added better url checks

* added stuff
  • Loading branch information
jshamble authored Apr 19, 2022
1 parent 79b5c24 commit 2ef2d96
Show file tree
Hide file tree
Showing 7 changed files with 77 additions and 11 deletions.
2 changes: 2 additions & 0 deletions build-system/test-configs/dep-check-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,10 @@ exports.rules = [
'extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js->extensions/amp-story/1.0/amp-story-store-service.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js->extensions/amp-story/1.0/variable-service.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js->extensions/amp-story/1.0/story-analytics.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js->extensions/amp-story/1.0/history.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js->extensions/amp-story/1.0/variable-service.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js->extensions/amp-story/1.0/story-analytics.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js->extensions/amp-story/1.0/history.js',

// Interactive components that depend on story functionality.
'extensions/amp-story-interactive/0.1/amp-story-interactive-abstract.js->extensions/amp-story/1.0/amp-story-store-service.js',
Expand Down
30 changes: 19 additions & 11 deletions extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {Services} from '#service';
import {LocalizedStringId_Enum} from '#service/localization/strings';

import {localizeTemplate} from 'extensions/amp-story/1.0/amp-story-localization-service';
import {HistoryState, setHistoryState} from 'extensions/amp-story/1.0/history';

import {formatI18nNumber, loadFonts} from './amp-story-shopping';
import {
Expand Down Expand Up @@ -82,17 +83,25 @@ export class AmpStoryShoppingAttachment extends AMP.BaseElement {
this.pageEl_.querySelectorAll('amp-story-shopping-tag')
);

getShoppingConfig(this.element, this.pageEl_.id).then((config) =>
storeShoppingConfig(this.pageEl_, config)
);

if (this.shoppingTags_.length === 0) {
return;
return Promise.reject(
new Error(`No shopping tags on page ${this.pageEl_.id}.`)
);
}

return this.localizationService_
.getLocalizedStringAsync(
LocalizedStringId_Enum.AMP_STORY_SHOPPING_CTA_LABEL
return getShoppingConfig(this.element, this.pageEl_.id)
.then((config) => {
if (Object.keys(config).length === 0) {
return Promise.reject(
new Error(`No valid shopping data on page ${this.pageEl_.id}.`)
);
}
storeShoppingConfig(this.pageEl_, config);
})
.then(() =>
this.localizationService_.getLocalizedStringAsync(
LocalizedStringId_Enum.AMP_STORY_SHOPPING_CTA_LABEL
)
)
.then((ctaText) => {
this.attachmentEl_ = (
Expand All @@ -110,9 +119,6 @@ export class AmpStoryShoppingAttachment extends AMP.BaseElement {

/** @override */
layoutCallback() {
if (this.shoppingTags_.length === 0) {
return;
}
loadFonts(this.win, FONTS_TO_LOAD);
// Update template on attachment state update or shopping data update.
this.storeService_.subscribe(
Expand Down Expand Up @@ -299,6 +305,8 @@ export class AmpStoryShoppingAttachment extends AMP.BaseElement {
this.storeService_.dispatch(Action.ADD_SHOPPING_DATA, {
'activeProductData': shoppingData,
});

setHistoryState(this.win, HistoryState.SHOPPING_DATA, shoppingData);
}

/**
Expand Down
4 changes: 4 additions & 0 deletions extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {

import {Services} from '#service';

import {HistoryState, setHistoryState} from 'extensions/amp-story/1.0/history';

import {formatI18nNumber, loadFonts} from './amp-story-shopping';

import {CSS as shoppingSharedCSS} from '../../../build/amp-story-shopping-shared-0.1.css';
Expand Down Expand Up @@ -170,6 +172,8 @@ export class AmpStoryShoppingTag extends AMP.BaseElement {
'activeProductData': this.tagData_,
});

setHistoryState(this.win, HistoryState.SHOPPING_DATA, this.tagData_);

this.variableService_.onVariableUpdate(
AnalyticsVariable.STORY_SHOPPING_PRODUCT_ID,
this.tagData_.productId
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ import * as Preact from '#core/dom/jsx';

import {Services} from '#service';

import {HistoryState} from 'extensions/amp-story/1.0/history';
import * as history from 'extensions/amp-story/1.0/history';

import '../../../amp-story/1.0/amp-story';
import '../../../amp-story/1.0/amp-story-page';
import '../amp-story-shopping';
Expand Down Expand Up @@ -275,5 +278,21 @@ describes.realWin(
StoryAnalyticsEvent.SHOPPING_PDP_VIEW
);
});

it('should call history service on Product Listing Page card click', async () => {
const historyStub = env.sandbox.stub(history, 'setHistoryState');

await layoutShoppingImplAndAttachmentChildImpl();
storeService.dispatch(Action.TOGGLE_PAGE_ATTACHMENT_STATE, true);
const plpCard = attachmentChildEl.querySelector(
'.i-amphtml-amp-story-shopping-plp-card'
);
plpCard.dispatchEvent(new Event('click'));
expect(historyStub).to.have.been.called.calledWith(
win,
HistoryState.SHOPPING_DATA,
shoppingData.items[0]
);
});
}
);
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,11 @@ import '../amp-story-shopping';
import {Services} from '#service';
import {LocalizationService} from '#service/localization';

import '../../../amp-story-page-attachment/0.1/amp-story-page-attachment';

import * as history from 'extensions/amp-story/1.0/history';
import {HistoryState} from 'extensions/amp-story/1.0/history';

import {registerServiceBuilder} from '../../../../src/service-helpers';
import {
Action,
Expand Down Expand Up @@ -128,11 +133,27 @@ describes.realWin(

it('should call analytics service on tag click', async () => {
const trigger = env.sandbox.stub(analytics, 'triggerEvent');
env.sandbox.stub(history, 'setHistoryState');
await setupShoppingTagAndData();
await shoppingTag.shoppingTagEl_.click();
expect(trigger).to.have.been.calledWith(
StoryAnalyticsEvent.SHOPPING_TAG_CLICK
);
});

it('should call history service on tag click', async () => {
const tagData = {
'productTitle': 'Spectacular Spectacles',
};

const historyStub = env.sandbox.stub(history, 'setHistoryState');
await setupShoppingTagAndData();
await shoppingTag.shoppingTagEl_.click();
expect(historyStub).to.have.been.called.calledWith(
win,
HistoryState.SHOPPING_DATA,
tagData
);
});
}
);
11 changes: 11 additions & 0 deletions extensions/amp-story/1.0/amp-story.js
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,17 @@ export class AmpStory extends AMP.BaseElement {
attachmentImpl.open(false /** shouldAnimate */)
);
}

const shoppingData = getHistoryState(
this.win,
HistoryState.SHOPPING_DATA
);

if (shoppingData) {
this.storeService_.dispatch(Action.ADD_SHOPPING_DATA, {
'activeProductData': shoppingData,
});
}
}

if (
Expand Down
1 change: 1 addition & 0 deletions extensions/amp-story/1.0/history.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export const LOCAL_STORAGE_KEY = 'amp-story-state';
export const HistoryState = {
ATTACHMENT_PAGE_ID: 'ampStoryAttachmentPageId',
NAVIGATION_PATH: 'ampStoryNavigationPath',
SHOPPING_DATA: 'ampStoryShoppingData',
};

/**
Expand Down

0 comments on commit 2ef2d96

Please sign in to comment.