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-config data json handle get and set config data #36757

Merged
merged 57 commits into from
Nov 18, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
57 commits
Select commit Hold shift + click to select a range
f34a46c
Added examples for inline and remote config loading of json files.
jshamble Nov 3, 2021
087b53f
finished tests for shopping config
jshamble Nov 4, 2021
e490dad
removed unnescessary function call
jshamble Nov 4, 2021
8e0502f
fixed merge conflict dleeting base shopping file
jshamble Nov 4, 2021
593250b
removed chai import
jshamble Nov 4, 2021
03879e9
removed unnesc assignment
jshamble Nov 4, 2021
aa5eb26
fixed prettier lint warnings
jshamble Nov 4, 2021
2ec626a
added dependencies for store service
jshamble Nov 4, 2021
87835e6
cleanup up code and tests to optmize code flow
jshamble Nov 4, 2021
5328ecd
cleaned up config files and test files
jshamble Nov 5, 2021
4993c22
added more refactoring of shopping story
jshamble Nov 5, 2021
dcd51d4
Updated test code with new tag id
jshamble Nov 5, 2021
58aa953
fixed formatting of remote file
jshamble Nov 5, 2021
dbef7ff
added remote config fiel formatting fix
jshamble Nov 5, 2021
5a67a03
added visual diff test
jshamble Nov 5, 2021
e711b93
updated visual diff test
jshamble Nov 6, 2021
fed6aab
updated unit tests to use public facing apis, removed dead code
jshamble Nov 9, 2021
c4baea7
updated unit tests to use public facing apis, removed dead code
jshamble Nov 9, 2021
8e0c94b
updated unit tests to use public facing apis, removed dead code
jshamble Nov 9, 2021
33d72d2
added page id
jshamble Nov 9, 2021
35bacc3
added load config impl (both solutions: one exposed export public met…
jshamble Nov 10, 2021
f7627d4
removed export ()may need to put back if want to use public facing apit
jshamble Nov 10, 2021
97f38e0
modified laodconfig to handle 404 errors
jshamble Nov 10, 2021
d555d92
removed unused var
jshamble Nov 10, 2021
6805ccb
added dependency for request service
jshamble Nov 10, 2021
606f109
Merge branch 'main' into shopDataJsonHandle
jshamble Nov 10, 2021
1508aa2
added a few recotoring shareconfig menthod
jshamble Nov 10, 2021
172c6a7
refactored code to use new request service.
jshamble Nov 10, 2021
194effe
Merge branch 'main' into shopDataJsonHandle
jshamble Nov 10, 2021
72e64a6
removed unused allow list entry
jshamble Nov 10, 2021
b8bf02a
added buidlcallback resolve
jshamble Nov 11, 2021
c0278c6
added implicit return
jshamble Nov 11, 2021
988ad88
added in code refactos
jshamble Nov 11, 2021
4c6283f
updated shoo[[ing state to shopping data
jshamble Nov 11, 2021
e2284fc
removed promise.resolve
jshamble Nov 11, 2021
0821aba
recatored shopping and shared config unit test code
jshamble Nov 11, 2021
152d97f
added in config refactor logic
jshamble Nov 11, 2021
0ba6f8c
streamlined commens and code
jshamble Nov 12, 2021
0120f15
fixed comment for circleci
jshamble Nov 12, 2021
517e53d
optmized code and unit tests
jshamble Nov 12, 2021
6371fd2
added shared config code
jshamble Nov 12, 2021
6a75291
refactored dead code
jshamble Nov 12, 2021
af5fdf4
added typedefs
jshamble Nov 15, 2021
cf5d899
added in refactor of social share widget
jshamble Nov 16, 2021
3e0c132
added shopping story test features
jshamble Nov 16, 2021
0599a2d
removed unnneded export
jshamble Nov 16, 2021
e0bb71b
udpated product images array
jshamble Nov 16, 2021
fbf7464
Merge branch 'main' into shopDataJsonHandle
jshamble Nov 16, 2021
f53685c
removed trailign whitespace
jshamble Nov 16, 2021
fe86615
removed unused vars
jshamble Nov 16, 2021
0184f18
removed unused config for now
jshamble Nov 17, 2021
fafa4a9
added back in dev asserts
jshamble Nov 17, 2021
d70055e
code compactification
jshamble Nov 17, 2021
732f9ab
Merge branch 'main' into shopDataJsonHandle
jshamble Nov 17, 2021
fde3e0d
stripped dev assert
jshamble Nov 17, 2021
793ccfd
fixed social share config
jshamble Nov 18, 2021
d86af91
Merge branch 'main' into shopDataJsonHandle
jshamble Nov 18, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -265,6 +265,8 @@ exports.rules = [

// Story Shopping
'extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js->extensions/amp-story/1.0/amp-story-page-attachment.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-config.js->extensions/amp-story/1.0/amp-story-store-service.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js->extensions/amp-story/1.0/amp-story-store-service.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
66 changes: 55 additions & 11 deletions examples/amp-story/amp-story-shopping.html
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,65 @@
publisher="AMP Story Shopping"
publisher-logo-src="example.com/logo.png"
poster-portrait-src="example.com/poster.jpg">
<amp-story-page id="default">
<amp-story-shopping-config layout='nodisplay'></amp-story-shopping-config>
<amp-story-page id="example">
jshamble marked this conversation as resolved.
Show resolved Hide resolved
<!--
This is an example of a JSON config retrieved from a source file.
To use the inline JSON config, remove the src attribute.
-->
jshamble marked this conversation as resolved.
Show resolved Hide resolved
<amp-story-shopping-config layout='nodisplay' src="/examples/amp-story/shopping/remote.json" >
<script type="application/json">
jshamble marked this conversation as resolved.
Show resolved Hide resolved
{
"items" : [
{
"product-tag-id": "hat",
"brand-label": "...",
"brand-favicon": "...",
"product-title": "Hootenanny Hat",
"product-price": "...",
"product-images": "...",
"product-details": "...",
"reviews-page": "...",
"reviews-data": "...",
"cta-text": 1,
"shipping-text": 1
},
{
"product-tag-id": "sunglasses",
"brand-label": "...",
"brand-favicon": "...",
"product-title": "Spectacular Spectacles",
"product-price": "...",
"product-images": "...",
"product-details": "...",
"reviews-page": "...",
"reviews-data": "...",
"cta-text": 1,
"shipping-text": 1
},
{
"product-tag-id": "backpack",
"brand-label": "...",
"brand-favicon": "...",
"product-title": "Beastly Backpack",
"product-price": "...",
"product-images": "...",
"product-details": "...",
"reviews-page": "...",
"reviews-data": "...",
"cta-text": 1,
"shipping-text": 1
}
]
}
</script>
</amp-story-shopping-config>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag></amp-story-shopping-tag>
<amp-story-shopping-tag tag-data-id="hat" ></amp-story-shopping-tag>
<amp-story-shopping-tag tag-data-id="sunglasses" ></amp-story-shopping-tag>
<amp-story-shopping-tag tag-data-id="backpack" ></amp-story-shopping-tag>
jshamble marked this conversation as resolved.
Show resolved Hide resolved
</amp-story-grid-layer>
<amp-story-shopping-attachment layout='nodisplay'></amp-story-shopping-attachment>
</amp-story-page>

<amp-story-page id="dark-theme">
<amp-story-shopping-config layout='nodisplay'></amp-story-shopping-config>
<amp-story-grid-layer template="vertical">
<amp-story-shopping-tag></amp-story-shopping-tag>
</amp-story-grid-layer>
<amp-story-shopping-attachment layout='nodisplay' theme="dark"></amp-story-shopping-attachment>
</amp-story-page>
jshamble marked this conversation as resolved.
Show resolved Hide resolved
</amp-story>
</body>
</html>
43 changes: 43 additions & 0 deletions examples/amp-story/shopping/remote.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
{
"items": [
{
"product-tag-id": "hat",
"brand-label": "...",
"brand-favicon": "...",
"product-title": "Hootenanny Hat",
"product-price": "...",
"product-images": "...",
"product-details": "...",
"reviews-page": "...",
"reviews-data": "...",
"cta-text": 1,
"shipping-text": 1
},
{
"product-tag-id": "sunglasses",
"brand-label": "...",
"brand-favicon": "...",
"product-title": "Spectacular Spectacles",
"product-price": "...",
"product-images": "...",
"product-details": "...",
"reviews-page": "...",
"reviews-data": "...",
"cta-text": 1,
"shipping-text": 1
},
{
"product-tag-id": "backpack",
"brand-label": "...",
"brand-favicon": "...",
"product-title": "Beastly Backpack",
"product-price": "...",
"product-images": "...",
"product-details": "...",
"reviews-page": "...",
"reviews-data": "...",
"cta-text": 1,
"shipping-text": 1
}
]
}
100 changes: 92 additions & 8 deletions extensions/amp-story-shopping/0.1/amp-story-shopping-config.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
import {Layout, applyFillContent} from '#core/dom/layout';
import {isJsonScriptTag} from '#core/dom';
import {Layout} from '#core/dom/layout';
import {parseJson} from '#core/types/object/json';

jshamble marked this conversation as resolved.
Show resolved Hide resolved
import {Services} from '#service';

import {devAssert, user, userAssert} from '#utils/log';

import {Action} from '../../amp-story/1.0/amp-story-store-service';

const TAG = 'amp-story-shopping-config';

Expand All @@ -8,23 +16,99 @@ export class AmpStoryShoppingConfig extends AMP.BaseElement {
super(element);

/** @private {string} */
this.myText_ = TAG;
this.element_ = element;
jshamble marked this conversation as resolved.
Show resolved Hide resolved

/** @private @const {!./amp-story-store-service.AmpStoryStoreService} */
jshamble marked this conversation as resolved.
Show resolved Hide resolved
this.storeService_ = null;
}

/** @private {?Element} */
this.container_ = null;
/**
* Keys product data to product-ids and adds them to the store service.
* @private
jshamble marked this conversation as resolved.
Show resolved Hide resolved
* @param {!JsonObject} storyConfig
*/
dispatchConfig_(storyConfig) {
jshamble marked this conversation as resolved.
Show resolved Hide resolved
const productIDtoProduct = {};

for (const item of storyConfig['items']) {
productIDtoProduct[item['product-tag-id']] = item;
}

this.storeService_.dispatch(Action.ADD_SHOPPING_STATE, productIDtoProduct);

//TODO: add call to validate config here. See validation I2I #36412 for implementation details
jshamble marked this conversation as resolved.
Show resolved Hide resolved
}

/** @override */
buildCallback() {
super.buildCallback();
this.container_ = this.element.ownerDocument.createElement('div');
this.container_.textContent = this.myText_;
this.element.appendChild(this.container_);
applyFillContent(this.container_, /* replacedContent */ true);
return Services.storyStoreServiceForOrNull(this.win).then(
(storeService) => {
devAssert(storeService, 'Could not retrieve AmpStoryStoreService');

this.storeService_ = storeService;

this.getConfig().then((storyConfig) => {
//if remote config is not valid, try using the inline config
if (storyConfig == null && this.element_.hasAttribute('src')) {
this.getInlineConfig_(this.element_.firstElementChild).then(
(storyInlineconfig) => {
this.dispatchConfig_(storyInlineconfig);
}
);
} else {
this.dispatchConfig_(storyConfig);
}
});
jshamble marked this conversation as resolved.
Show resolved Hide resolved
}
);
}

/** @override */
isLayoutSupported(layout) {
return layout === Layout.NODISPLAY;
}

/**
* @return {!JsonObject}
*/
jshamble marked this conversation as resolved.
Show resolved Hide resolved
getConfig() {
const configData = this.element_.hasAttribute('src')
? this.getRemoteConfig_()
: this.getInlineConfig_(this.element_.firstElementChild);

return configData;
jshamble marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @param {!Element} child
* @return {!JsonObject}
*/
getInlineConfig_(child) {
jshamble marked this conversation as resolved.
Show resolved Hide resolved
userAssert(
child && isJsonScriptTag(child),
`The ${TAG} should ` +
'be inside a <script> tag with type="application/json"'
jshamble marked this conversation as resolved.
Show resolved Hide resolved
);

const inlineJSONConfig = parseJson(child.textContent);

return Promise.resolve(inlineJSONConfig);
jshamble marked this conversation as resolved.
Show resolved Hide resolved
}

/**
* @return {!JsonObject}
jshamble marked this conversation as resolved.
Show resolved Hide resolved
*/
jshamble marked this conversation as resolved.
Show resolved Hide resolved
getRemoteConfig_() {
return Services.xhrFor(this.win)
.fetchJson(this.element_.getAttribute('src'))
.then((response) => response.json())
.catch((err) => {
user().error(
TAG,
'error determining if remote config is valid json: bad url or bad json',
err
);
});
}
}
37 changes: 28 additions & 9 deletions extensions/amp-story-shopping/0.1/amp-story-shopping-tag.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,49 @@
import {Layout, applyFillContent} from '#core/dom/layout';

import {Services} from '#service';

import {StateProperty} from '../../amp-story/1.0/amp-story-store-service';

const TAG = 'amp-story-shopping-tag';

export class AmpStoryShoppingTag extends AMP.BaseElement {
/** @param {!AmpElement} element */
constructor(element) {
super(element);

/** @private {string} */
this.myText_ = TAG;

/** @private {?Element} */
this.container_ = null;
/** @private @const {!./amp-story-store-service.AmpStoryStoreService} */
jshamble marked this conversation as resolved.
Show resolved Hide resolved
this.storeService_ = null;
}

/** @override */
buildCallback() {
super.buildCallback();
this.container_ = this.element.ownerDocument.createElement('div');
this.container_.textContent = this.myText_;
this.element.appendChild(this.container_);
applyFillContent(this.container_, /* replacedContent */ true);
this.element.textContent = this.myText_;
applyFillContent(this.element, /* replacedContent */ true);
jshamble marked this conversation as resolved.
Show resolved Hide resolved
return Promise.all([
jshamble marked this conversation as resolved.
Show resolved Hide resolved
Services.storyStoreServiceForOrNull(this.win).then((storeService) => {
this.storeService_ = storeService;
jshamble marked this conversation as resolved.
Show resolved Hide resolved
this.storeService_.subscribe(
jshamble marked this conversation as resolved.
Show resolved Hide resolved
StateProperty.SHOPPING_STATE,
(shoppingState) => this.updateShoppingTag_(shoppingState)
);
}),
]);
}

/** @override */
isLayoutSupported(layout) {
return layout === Layout.CONTAINER;
}

/**
* @param {Object} shoppingState
jshamble marked this conversation as resolved.
Show resolved Hide resolved
* @private
jshamble marked this conversation as resolved.
Show resolved Hide resolved
*/
updateShoppingTag_(shoppingState) {
const tagData = shoppingState[this.element.getAttribute('tag-data-id')];
if (tagData != null) {
jshamble marked this conversation as resolved.
Show resolved Hide resolved
this.element.textContent = tagData['product-title'];
jshamble marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
2 changes: 1 addition & 1 deletion extensions/amp-story-shopping/0.1/amp-story-shopping.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import {AmpStoryShoppingTag} from './amp-story-shopping-tag';
import {CSS as shoppingCSS} from '../../../build/amp-story-shopping-0.1.css';

AMP.extension('amp-story-shopping', '0.1', (AMP) => {
AMP.registerElement('amp-story-shopping-config', AmpStoryShoppingConfig);
AMP.registerElement(
'amp-story-shopping-tag',
AmpStoryShoppingTag,
Expand All @@ -14,5 +15,4 @@ AMP.extension('amp-story-shopping', '0.1', (AMP) => {
'amp-story-shopping-attachment',
AmpStoryShoppingAttachment
);
AMP.registerElement('amp-story-shopping-config', AmpStoryShoppingConfig);
});
Loading