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

🌐 [Story subscription] Subscription localization async #38204

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
6aabe91
Added tasts
mszylkowski Dec 21, 2021
dc1fa87
Undo
mszylkowski Dec 21, 2021
d707e5b
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Dec 28, 2021
0e3df75
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Dec 28, 2021
a3f7bd0
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Jan 4, 2022
8c5119c
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Jan 6, 2022
77f8435
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Jan 10, 2022
16e712e
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Jan 19, 2022
8088eb7
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Jan 25, 2022
3858b2a
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Jan 28, 2022
e12d2f6
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Jan 31, 2022
2a29105
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Feb 1, 2022
a5b781f
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Feb 10, 2022
b8006ee
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Mar 8, 2022
b570e86
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Mar 10, 2022
6b7e456
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Mar 22, 2022
50bdff8
Merge branch 'ampproject:main' into main
mszylkowski Mar 24, 2022
267ae0e
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Mar 29, 2022
c7edd8c
Merge branch 'main' of github.com:mszylkowski/amphtml
mszylkowski Mar 29, 2022
9842a23
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Mar 29, 2022
83e25bb
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Apr 4, 2022
9ded8f1
Merge branch 'main' of github.com:mszylkowski/amphtml; branch 'main' …
mszylkowski Apr 5, 2022
4f7b975
Merge branch 'ampproject:main' into main
mszylkowski Apr 6, 2022
cf38c3f
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski Apr 29, 2022
fe714b6
Merge branch 'main' of github.com:ampproject/amphtml
mszylkowski May 16, 2022
d5c17db
Make subscriptions localize the template async
mszylkowski May 16, 2022
2ad5bcb
Add dep check
mszylkowski May 16, 2022
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
1 change: 1 addition & 0 deletions build-system/test-configs/dep-check-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,7 @@ exports.rules = [
'extensions/amp-story-page-attachment/0.1/amp-story-form.js->extensions/amp-story/1.0/amp-story-localization-service.js',
'extensions/amp-story-page-attachment/0.1/amp-story-page-attachment.js->extensions/amp-story/1.0/amp-story-localization-service.js',
'extensions/amp-story-shopping/0.1/amp-story-shopping-attachment.js->extensions/amp-story/1.0/amp-story-localization-service.js',
'extensions/amp-story-subscriptions/0.1/amp-story-subscriptions.js->extensions/amp-story/1.0/amp-story-localization-service.js',

// Subscriptions.
'extensions/amp-subscriptions/0.1/expr.js->extensions/amp-access/0.1/access-expr.js',
Expand Down
129 changes: 68 additions & 61 deletions extensions/amp-story-subscriptions/0.1/amp-story-subscriptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import {LocalizedStringId_Enum} from '#service/localization/strings';

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

import {localizeTemplate} from 'extensions/amp-story/1.0/amp-story-localization-service';

import {CSS} from '../../../build/amp-story-subscriptions-0.1.css';
import {
Action,
Expand Down Expand Up @@ -43,9 +45,6 @@ export class AmpStorySubscriptions extends AMP.BaseElement {
/** @private {?../../../extensions/amp-subscriptions/0.1/amp-subscriptions.SubscriptionService} */
this.subscriptionService_ = null;

/** @private {?../../../src/service/localization.LocalizationService} */
this.localizationService_ = null;

/** @private {?../../../src/service/viewer-interface.ViewerInterface} */
this.viewer_ = null;

Expand All @@ -64,50 +63,53 @@ export class AmpStorySubscriptions extends AMP.BaseElement {
Services.storyStoreServiceForOrNull(this.win),
Services.subscriptionsServiceForDoc(this.element),
Services.localizationServiceForOrNull(this.element),
]).then(([storeService, subscriptionService, localizationService]) => {
this.storeService_ = storeService;
this.subscriptionService_ = subscriptionService;
this.localizationService_ = localizationService;

const pages = this.win.document.querySelectorAll(
'amp-story-page:not([ad])'
);
devAssert(
pages.length >= 4,
'The number of pages should be at least 4 to enable subscriptions feature, got %s',
pages.length
);
]).then(
ychsieh marked this conversation as resolved.
Show resolved Hide resolved
([storeService, subscriptionService, unusedLocalizationService]) => {
ychsieh marked this conversation as resolved.
Show resolved Hide resolved
this.storeService_ = storeService;
this.subscriptionService_ = subscriptionService;

let subscriptionsPageIndex = parseInt(
this.element.getAttribute('subscriptions-page-index'),
10
);
subscriptionsPageIndex = isNaN(subscriptionsPageIndex)
? DEFAULT_SUBSCRIPTIONS_PAGE_INDEX
: subscriptionsPageIndex;
this.storeService_.dispatch(
Action.SET_SUBSCRIPTIONS_PAGE_INDEX,
clamp(
subscriptionsPageIndex,
DEFAULT_SUBSCRIPTIONS_PAGE_INDEX,
pages.length - 1
)
);
const pages = this.win.document.querySelectorAll(
'amp-story-page:not([ad])'
);
devAssert(
pages.length >= 4,
'The number of pages should be at least 4 to enable subscriptions feature, got %s',
pages.length
);

// Get grant status immediately to set up the initial subscriptions state.
this.getGrantStatusAndUpdateState_();
// When the user finishes any of the actions, e.g. log in or subscribe, new entitlements would be
// re-fetched and this callback would be executed. Update states based on new entitlements.
this.subscriptionService_.addOnEntitlementResolvedCallback(() =>
this.getGrantStatusAndUpdateState_()
);
let subscriptionsPageIndex = parseInt(
this.element.getAttribute('subscriptions-page-index'),
10
);
subscriptionsPageIndex = isNaN(subscriptionsPageIndex)
? DEFAULT_SUBSCRIPTIONS_PAGE_INDEX
: subscriptionsPageIndex;
this.storeService_.dispatch(
Action.SET_SUBSCRIPTIONS_PAGE_INDEX,
clamp(
subscriptionsPageIndex,
DEFAULT_SUBSCRIPTIONS_PAGE_INDEX,
pages.length - 1
)
);

// Create a paywall dialog element that have required attributes to be able to be
// rendered by amp-subscriptions.
this.element.appendChild(this.renderSubscriptionsDialogTemplate_());
// Get grant status immediately to set up the initial subscriptions state.
this.getGrantStatusAndUpdateState_();
// When the user finishes any of the actions, e.g. log in or subscribe, new entitlements would be
// re-fetched and this callback would be executed. Update states based on new entitlements.
this.subscriptionService_.addOnEntitlementResolvedCallback(() =>
this.getGrantStatusAndUpdateState_()
);

this.initializeListeners_();
});
// Create a paywall dialog element that have required attributes to be able to be
// rendered by amp-subscriptions.
const template = this.renderSubscriptionsDialogTemplate_();
return localizeTemplate(template, this.element).then(() => {
this.element.appendChild(template);
this.initializeListeners_();
});
}
);
}

/** @override */
Expand Down Expand Up @@ -244,11 +246,12 @@ export class AmpStorySubscriptions extends AMP.BaseElement {
return (
<div subscriptions-dialog subscriptions-display="NOT granted">
<div class="i-amphtml-story-subscriptions-dialog-banner">
<button class="i-amphtml-story-subscriptions-dialog-banner-button">
{this.localizationService_.getLocalizedString(
<button
class="i-amphtml-story-subscriptions-dialog-banner-button"
i-amphtml-i18n-text-content={
LocalizedStringId_Enum.AMP_STORY_SUBSCRIPTIONS_SKIP
)}
</button>
}
></button>
</div>
<div class="i-amphtml-story-subscriptions-dialog-content">
<span class="i-amphtml-story-subscriptions-price">
Expand All @@ -274,9 +277,11 @@ export class AmpStorySubscriptions extends AMP.BaseElement {
>
<img class="i-amphtml-story-subscriptions-publisher-logo"></img>
<span class="i-amphtml-story-subscriptions-publisher-button-text">
{this.localizationService_.getLocalizedString(
LocalizedStringId_Enum.AMP_STORY_SUBSCRIPTIONS_CTA
)}
<span
i-amphtml-i18n-text-content={
LocalizedStringId_Enum.AMP_STORY_SUBSCRIPTIONS_CTA
}
></span>
&nbsp;
{getStoryAttributeSrc(this.element, 'publisher', /* warn */ true)}
</span>
Expand All @@ -289,25 +294,27 @@ export class AmpStorySubscriptions extends AMP.BaseElement {
subscriptions-decorate="false"
>
<span class="i-amphtml-story-subscriptions-google-logo"></span>
<span class="i-amphtml-story-subscriptions-google-button-text">
{this.localizationService_.getLocalizedString(
<span
class="i-amphtml-story-subscriptions-google-button-text"
i-amphtml-i18n-text-content={
LocalizedStringId_Enum.AMP_STORY_SUBSCRIPTIONS_SWG
)}
</span>
}
></span>
</button>
<span class="i-amphtml-story-subscriptions-signin">
{this.localizationService_.getLocalizedString(
LocalizedStringId_Enum.AMP_STORY_SUBSCRIPTIONS_SUBSCRIBER_QUESTION
)}
<span
i-amphtml-i18n-text-content={
LocalizedStringId_Enum.AMP_STORY_SUBSCRIPTIONS_SUBSCRIBER_QUESTION
}
></span>
&nbsp;
<button
subscriptions-action="login"
subscriptions-display="NOT granted"
>
{this.localizationService_.getLocalizedString(
i-amphtml-i18n-text-content={
LocalizedStringId_Enum.AMP_STORY_SUBSCRIPTIONS_SIGN_IN
)}
</button>
}
></button>
</span>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import * as Preact from '#core/dom/jsx';

import {Services} from '#service';
import {LocalizationService} from '#service/localization';

import {afterRenderPromise} from '#testing/helpers';

import {getLocalizationService} from 'extensions/amp-story/1.0/amp-story-localization-service';
import {AdvancementMode} from 'extensions/amp-story/1.0/story-analytics';

import LocalizedStringsEn from '../../../amp-story/1.0/_locales/en.json' assert {type: 'json'}; // lgtm[js/syntax-error]
import {
Action,
AmpStoryStoreService,
Expand Down Expand Up @@ -45,10 +46,9 @@ describes.realWin(
.returns(Promise.resolve(storeService));
env.sandbox.stub(Services, 'storyStoreService').returns(storeService);

const localizationService = new LocalizationService(doc.body);
env.sandbox
.stub(Services, 'localizationServiceForOrNull')
.returns(Promise.resolve(localizationService));
getLocalizationService(doc.body).registerLocalizedStringBundles({
'en': LocalizedStringsEn,
});

const platformConfig = {
'services': [
Expand Down