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

Add close button to newsletter publisher prompt #3253

Merged
merged 4 commits into from
Oct 13, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
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
50 changes: 35 additions & 15 deletions src/runtime/audience-action-local-flow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,11 @@ import {
} from './audience-action-flow';
import {AutoPromptType} from '../api/basic-subscriptions';
import {
CLOSE_BUTTON_HTML,
CONTRIBUTION_ICON,
ERROR_HTML,
LOADING_HTML,
OPT_IN_CLOSE_BUTTON_HTML,
REWARDED_AD_CLOSE_BUTTON_HTML,
REWARDED_AD_HTML,
REWARDED_AD_SIGN_IN_HTML,
REWARDED_AD_SUPPORT_HTML,
Expand Down Expand Up @@ -207,11 +208,11 @@ export class AudienceActionLocalFlow implements AudienceActionFlow {

private renderOptInPrompt_(codeSnippet: string) {
const optInPrompt = createElement(this.doc_, 'div', {});
optInPrompt./*OK*/ innerHTML = codeSnippet;
const closeHtml = this.getCloseButtonHtml_(OPT_IN_CLOSE_BUTTON_HTML);
optInPrompt./*OK*/ innerHTML = closeHtml.concat(codeSnippet);
const form = optInPrompt.querySelector('form');

if (form && this.wrapper_) {
//TODO: chuyangwang - add close button if prompt is closeable.
setImportantStyles(optInPrompt, {
'background-color': 'white',
'border': 'none',
Expand All @@ -223,13 +224,16 @@ export class AudienceActionLocalFlow implements AudienceActionFlow {
'max-width': '100%',
'pointer-events': 'auto',
'position': 'fixed',
'overflow': 'scroll',
'overflow': 'auto',
'text-align': 'center',
'transform': 'translate(-50%, 0)',
'z-index': '2147483646',
});
this.wrapper_.shadowRoot?.removeChild(this.prompt_);
this.wrapper_.shadowRoot?.appendChild(optInPrompt);
optInPrompt
.querySelector('.opt-in-close-button')
?.addEventListener('click', this.closeOptInPrompt_.bind(this));
form.addEventListener('submit', this.formSubmit_.bind(this));
} else {
this.eventManager_.logSwgEvent(
Expand All @@ -249,6 +253,19 @@ export class AudienceActionLocalFlow implements AudienceActionFlow {
await this.complete_();
}

private closeOptInPrompt_() {
this.eventManager_.logSwgEvent(
AnalyticsEvent.ACTION_BYOP_NEWSLETTER_OPT_IN_CLOSE
);

if (this.params_.isClosable) {
this.unlock_();
if (this.params_.onCancel) {
this.params_.onCancel();
}
kristenwang marked this conversation as resolved.
Show resolved Hide resolved
}
}

private async initRewardedAdWall_() {
this.eventManager_.logSwgEvent(AnalyticsEvent.IMPRESSION_REWARDED_AD);

Expand Down Expand Up @@ -360,16 +377,7 @@ export class AudienceActionLocalFlow implements AudienceActionFlow {

// verified existance in initRewardedAdWall_
const publication = config.publication!.name!;
const closeButtonDescription = msg(
SWG_I18N_STRINGS['CLOSE_BUTTON_DESCRIPTION'],
language
)!;
const closeHtml = this.params_.isClosable
? CLOSE_BUTTON_HTML.replace(
'$CLOSE_BUTTON_DESCRIPTION$',
closeButtonDescription
)
: '';
const closeHtml = this.getCloseButtonHtml_(REWARDED_AD_CLOSE_BUTTON_HTML);
const icon = isSubscription ? SUBSCRIPTION_ICON : CONTRIBUTION_ICON;
// verified existance in initRewardedAdWall_
const message = config.rewardedAdParameters!.customMessage!;
Expand All @@ -396,7 +404,7 @@ export class AudienceActionLocalFlow implements AudienceActionFlow {
'$TITLE$',
publication
)
.replace('$CLOSE_BUTTON_HTML$', closeHtml)
.replace('$REWARDED_AD_CLOSE_BUTTON_HTML$', closeHtml)
.replace('$ICON$', icon)
.replace('$MESSAGE$', message)
.replace('$VIEW_AN_AD$', viewad)
Expand Down Expand Up @@ -583,6 +591,18 @@ export class AudienceActionLocalFlow implements AudienceActionFlow {
await this.initPrompt_();
}

private getCloseButtonHtml_(html: string) {
const language = this.clientConfigManager_.getLanguage();
const closeButtonDescription = msg(
SWG_I18N_STRINGS['CLOSE_BUTTON_DESCRIPTION'],
language
)!;
const closeHtml = this.params_.isClosable
? html.replace('$CLOSE_BUTTON_DESCRIPTION$', closeButtonDescription)
: '';
return closeHtml;
}

showNoEntitlementFoundToast() {
const language = this.clientConfigManager_.getLanguage();
const customText = msg(
Expand Down
54 changes: 54 additions & 0 deletions src/runtime/audience-action-local-flows-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,60 @@ describes.realWin('AudienceActionLocalFlow', (env) => {
);
});

it('renders prompt with close button', async () => {
const params = {
action: 'TYPE_NEWSLETTER_SIGNUP',
autoPromptType: AutoPromptType.CONTRIBUTION_LARGE,
isClosable: true,
configurationId: 'newsletter_config_id',
};
const state = await renderNewsletterPrompt(params, NEWSLETTER_CONFIG);

const closeButton = state.wrapper.shadowRoot.querySelector(
'.opt-in-close-button'
);
expect(closeButton).not.to.be.null;
});

it('close button click', async () => {
const params = {
action: 'TYPE_NEWSLETTER_SIGNUP',
autoPromptType: AutoPromptType.CONTRIBUTION_LARGE,
isClosable: true,
configurationId: 'newsletter_config_id',
onCancel: sandbox.spy(),
};
const state = await renderNewsletterPrompt(params, NEWSLETTER_CONFIG);
const closeButton = state.wrapper.shadowRoot.querySelector(
'.opt-in-close-button'
);

await closeButton.click();
await tick();

expect(env.win.document.body.style.overflow).to.equal('');
const updatedWrapper = env.win.document.querySelector(
'.audience-action-local-wrapper'
);
expect(updatedWrapper).to.be.null;
expect(params.onCancel).to.be.calledOnce.calledWithExactly();
expect(eventManager.logSwgEvent).to.be.calledWith(
AnalyticsEvent.ACTION_BYOP_NEWSLETTER_OPT_IN_CLOSE
);
});

it('renders prompt without close button', async () => {
const state = await renderNewsletterPrompt(
NEWSLETTER_PARAMS,
NEWSLETTER_CONFIG
);

const closeButton = state.wrapper.shadowRoot.querySelector(
'.opt-in-close-button'
);
expect(closeButton).to.be.null;
});

it('will not render with Google prompt preference', async () => {
const NEWSLETTER_GOOGLE_PROMPT_CONFIG = `
{
Expand Down
50 changes: 44 additions & 6 deletions src/runtime/audience-action-local-ui.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ const DEFAULT_BUTTON = css`
}
`;

const CLOSE_BUTTON_CSS = css`
const REWARDED_AD_CLOSE_BUTTON_CSS = css`
.rewarded-ad-close-button {
padding: 12px;
height: 48px;
Expand All @@ -98,13 +98,51 @@ const CLOSE_BUTTON_CSS = css`
}
`;

export const CLOSE_BUTTON_HTML = html`<button
export const REWARDED_AD_CLOSE_BUTTON_HTML = html`<button
aria-label="$CLOSE_BUTTON_DESCRIPTION$"
class="rewarded-ad-close-button"
>
<div class="rewarded-ad-close-img"></div>
</button>`;

const OPT_IN_CLOSE_BUTTON_CSS = css`
.opt-in-close-button-container {
text-align: end !important;
}

.opt-in-close-button {
background: none;
border: none;
border-radius: 4px;
height: 48px;
padding: 12px;
width: 48px;
}

.opt-in-close-img {
border-radius: 20px;
height: 24px;
width: 24px;
background: #5f6368;
-webkit-mask: url(https://fonts.gstatic.com/s/i/short-term/release/googlesymbols/close/default/24px.svg)
center/contain no-repeat;
}

.opt-in-close-button:hover,
.opt-in-close-button:focus {
background-color: #f2f8ff;
}
`;

export const OPT_IN_CLOSE_BUTTON_HTML = html`<style>
${OPT_IN_CLOSE_BUTTON_CSS}
</style>
<div class="opt-in-close-button-container">
<button aria-label="$CLOSE_BUTTON_DESCRIPTION$" class="opt-in-close-button">
<div class="opt-in-close-img"></div>
</button>
</div>`;

// Error view for prompts that fail to init.
// TODO: mhkawano - Update once UX finished.
// TODO: mhkawano - allow error view to be closed.
Expand Down Expand Up @@ -232,7 +270,7 @@ export const LOADING_HTML = html`
// TODO: mhkawano - translate static strings.
const REWARDED_AD_CSS = css`
${DEFAULT_BUTTON}
${CLOSE_BUTTON_CSS}
${REWARDED_AD_CLOSE_BUTTON_CSS}
${REWARDED_AD_PROMPT}

.rewarded-ad-container {
Expand Down Expand Up @@ -368,7 +406,7 @@ export const REWARDED_AD_HTML = html`
<div class="rewarded-ad-container">
<div class="rewarded-ad-header">
<div class="rewarded-ad-title">$TITLE$</div>
$CLOSE_BUTTON_HTML$
$REWARDED_AD_CLOSE_BUTTON_HTML$
</div>
<div class="rewarded-ad-icon"></div>
<div class="rewarded-ad-message">$MESSAGE$</div>
Expand All @@ -390,7 +428,7 @@ export const REWARDED_AD_HTML = html`

const REWARDED_AD_THANKS_CSS = css`
${DEFAULT_BUTTON}
${CLOSE_BUTTON_CSS}
${REWARDED_AD_CLOSE_BUTTON_CSS}
${REWARDED_AD_PROMPT}

.rewarded-ad-prompt {
Expand Down Expand Up @@ -437,7 +475,7 @@ export const REWARDED_AD_THANKS_HTML = html`
<div class="rewarded-ad-thanks-container">
<div class="rewarded-ad-thanks-icon"></div>
<div class="rewarded-ad-thanks-message">$THANKS_FOR_VIEWING_THIS_AD$</div>
${CLOSE_BUTTON_HTML}
${REWARDED_AD_CLOSE_BUTTON_HTML}
<div></div>
</div>
</div>
Expand Down