Skip to content

Commit

Permalink
Configured view state
Browse files Browse the repository at this point in the history
  • Loading branch information
banzzai committed Aug 4, 2023
1 parent f59c810 commit cbe7df9
Show file tree
Hide file tree
Showing 3 changed files with 152 additions and 11 deletions.
124 changes: 124 additions & 0 deletions src/components/AcceptAllRejectAllToggle.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import { h, JSX } from 'preact';
import { useState } from 'preact/hooks';
import { useIntl } from 'react-intl';
import { CONSENT_OPTIONS } from '../constants';
import { useAirgap } from '../hooks';
import { messages } from '../messages';
import type { HandleSetViewState } from '../types';
import { GPCIndicator } from './GPCIndicator';
import { Switch } from './Switch';

// Timer for save state
let savingTimeout: ReturnType<typeof setTimeout>;

/**
* Component showing explanatory text before offering a way
* to opt out of the sale or share of data
*/
export function DoNotSellExplainer({
handleSetViewState,
fontColor,
}: {
/** Function to change viewState */
handleSetViewState: HandleSetViewState;
/** Font color */
fontColor: string;
}): JSX.Element {
const { airgap } = useAirgap();
const { formatMessage } = useIntl();
const [saving, setSaving] = useState<boolean | null>(null);
const [consentLocal, setConsentLocal] = useState(
!!airgap.getConsent().purposes.SaleOfInfo,
);
const switchId = `all-purposes-${consentLocal}`;

const handleSwitch = (
checked: boolean,
event: JSX.TargetedEvent<HTMLInputElement, Event>,
): void => {
if (checked) {
event.preventDefault();
airgap.optIn(event);
} else {
event.preventDefault();
airgap.optOut(event);
}

setConsentLocal(checked);
setSaving(true);

// Clear any existing timeouts still running
if (savingTimeout) {
clearTimeout(savingTimeout);
}
savingTimeout = setTimeout(() => {
setSaving(false);
}, 500);
};

return (
<div className="column-content">
<button
type="button"
aria-label={formatMessage(messages.close)}
className="do-not-sell-explainer-close"
onClick={() => {
handleSetViewState('close');
}}
>
<svg width="24" height="24" viewBox="0 0 32 32" aria-hidden="true">
<path
fill={fontColor}
// eslint-disable-next-line max-len
d="M25.71 24.29a.996.996 0 1 1-1.41 1.41L16 17.41 7.71 25.7a.996.996 0 1 1-1.41-1.41L14.59 16l-8.3-8.29A.996.996 0 1 1 7.7 6.3l8.3 8.29 8.29-8.29a.996.996 0 1 1 1.41 1.41L17.41 16l8.3 8.29z"
/>
</svg>
<span className="screen-reader">{formatMessage(messages.close)}</span>
</button>
<div>
<div>
<p className="text-title text-title-left">
{formatMessage(messages.consentTitleAcceptAllRejectAllToggle)}
</p>
</div>
<div>
<p className="paragraph">
<div
// eslint-disable-next-line react/no-danger
dangerouslySetInnerHTML={{
__html: formatMessage(
messages.acceptAllRejectAllToggleDescription,
),
}}
/>
</p>
</div>
<div className="margin-tops do-not-sell-explainer-interface">
<GPCIndicator />
<Switch
id={switchId}
checked={consentLocal}
handleSwitch={handleSwitch}
label={formatMessage(
consentLocal
? messages.doNotSellOptedIn
: messages.doNotSellOptedOut,
)}
/>

<p className="paragraph">
{typeof saving === 'boolean'
? formatMessage(
saving
? messages.saving
: consentLocal
? messages.preferencesSavedOptedIn
: messages.preferencesSaved,
)
: '\u200b'}
</p>
</div>
</div>
</div>
);
}
6 changes: 3 additions & 3 deletions src/components/BottomMenu.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ export function BottomMenu({
{viewState === 'LanguageOptions' ? (
<div className="bottom-menu-item-container">
<MenuItem
label={formatMessage(messages.backButtonLabel)}
label={formatMessage(bottomMenuMessages.backButtonTooltip)}
type="button"
onClick={() => handleSetViewState(firstSelectedViewState)}
onClick={(e) => handleSetViewState('back', e)}
>
{formatMessage(bottomMenuMessages.simplerChoicesButtonPrimary)}
{formatMessage(bottomMenuMessages.backButtonText)}
</MenuItem>
</div>
) : (
Expand Down
33 changes: 25 additions & 8 deletions src/messages.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,6 @@
import { defineMessages } from '@transcend-io/internationalization';

export const messages = defineMessages('ui.src.messages', {
backButtonLabel: {
defaultMessage: 'Go back',
description:
'Label for the back button in the footer of the consent banner language options.',
},
consentTitle: {
defaultMessage: 'What can we use data for?',
description:
Expand All @@ -25,7 +20,7 @@ export const messages = defineMessages('ui.src.messages', {
acceptAllDescription: {
defaultMessage:
// eslint-disable-next-line max-len
'BY clicking “Accept all”, you agree to the storing of cookies on your device for functional, analytics, and advertising purposes.<a href="www.google.com">google</a>',
'By clicking “Accept all”, you agree to the storing of cookies on your device for functional, analytics, and advertising purposes.',
description:
'The description displayed when asking the user to accept all data processing.',
},
Expand Down Expand Up @@ -152,6 +147,18 @@ export const messages = defineMessages('ui.src.messages', {
description:
'Button text for redirecting the user to more granular consent choices.',
},
consentTitleAcceptAllRejectAllToggle: {
defaultMessage: 'Your Privacy Choices',
description:
'The title displayed in the AcceptAllRejectAllToggle view state.',
},
acceptAllRejectAllToggleDescription: {
defaultMessage: 'Your Privacy Choices',
description:
/* eslint-disable max-len */
'By opting in below, you agree to the storing of cookies on your device for functional, analytics, and advertising purposes.',
/* eslint-enable max-len */
},
});

export const quickOptionsMessages = defineMessages('ui.src.quickOptions', {
Expand Down Expand Up @@ -268,6 +275,16 @@ export const optOutDisclosureMessages = defineMessages(
);

export const bottomMenuMessages = defineMessages('ui.src.bottomMenu', {
backButtonText: {
defaultMessage: 'Go back',
description:
'Hover text the back button in the footer of the consent banner language options.',
},
backButtonTooltip: {
defaultMessage: 'Go back',
description:
'Main text for the back button in the footer of the consent banner language options.',
},
moreChoicesButtonPrimary: {
defaultMessage: 'More choices',
description: 'Text for selecting specific more opt out choices.',
Expand All @@ -285,11 +302,11 @@ export const bottomMenuMessages = defineMessages('ui.src.bottomMenu', {
description: 'Hover/alt text for selecting simpler opt out choices.',
},
showPolicyButtonPrimary: {
defaultMessage: 'See Our Privacy Policy',
defaultMessage: 'See our privacy policy',
description: 'Text for linking out to privacy policy.',
},
showPolicyButtonCompleteOptionsInverted: {
defaultMessage: 'See Our Privacy Policy',
defaultMessage: 'See our privacy policy',
description:
'Text for linking out to privacy policy in CompleteOptionsInverted UI.',
},
Expand Down

0 comments on commit cbe7df9

Please sign in to comment.