Skip to content

Commit

Permalink
Fix types, lint
Browse files Browse the repository at this point in the history
  • Loading branch information
Dantemss committed Jan 14, 2025
1 parent e156263 commit a467aa4
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 31 deletions.
12 changes: 6 additions & 6 deletions src/app/auth/hooks/trackUser.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,19 +60,19 @@ describe('trackUser', () => {
});
});

describe('with osano', () => {
describe('with CookieYes', () => {
afterEach(() => {
delete window.Osano;
delete window.cookieYesActive;
});

it('doesn\'t prompt if osano is running', async() => {
window.Osano = {cm: {mode: 'production'}};
it('doesn\'t prompt if CookieYes is running', async() => {
window.cookieYesActive = true;
await (trackUserHookBody(helpers))(receiveUser(user));
expect(dispatchMock).not.toHaveBeenCalled();
});

it('prompts to accept cookies in debug', async() => {
window.Osano = {cm: {mode: 'debug'}};
it('prompts to accept if CookieYes is not running', async() => {
window.cookieYesActive = false;
await (trackUserHookBody(helpers))(receiveUser(user));
expect(dispatchMock).toHaveBeenCalledWith({
meta: undefined,
Expand Down
6 changes: 1 addition & 5 deletions src/app/notifications/acceptCookies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ export const doAcceptCookies = () => {
};

export const isAcceptCookiesNeeded = () => {
const osanoActive = typeof window !== 'undefined'
&& window.Osano
&& window.Osano.cm.mode !== 'debug';

return !osanoActive && Cookies.get(acknowledgedKey) !== 'true';
return !window?.cookieYesActive && Cookies.get(acknowledgedKey) !== 'true';
};

export const clearAcceptCookies = () => {
Expand Down
43 changes: 24 additions & 19 deletions src/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { HTMLInputElement } from '@openstax/types/lib.dom';
import type { ConsentPreferences, CookieYesBannerLoadEvent, CookieYesConsentUpdateEvent } from 'cookieyes';
import queryString from 'query-string';
import React from 'react';
import ReactDOM from 'react-dom';
Expand Down Expand Up @@ -126,11 +128,6 @@ function cookiesBlocked(e: Error) {
return e instanceof DOMException && ['SecurityError', 'NotSupportedError'].includes(e.name);
}

type ConsentPreferences = {
accepted: string[],
rejected: string[],
};

const sendConsentToAccounts = (consentPreferences: ConsentPreferences) => {
fetch(`/lti/api/v0/consent${window.location.search}`, {
body: JSON.stringify(consentPreferences),
Expand All @@ -148,11 +145,9 @@ const sendConsentToAccounts = (consentPreferences: ConsentPreferences) => {
};

app.services.userLoader.getCurrentUser().then((user) => {
document.addEventListener('cookieyes_banner_load', (eventData: unknown) => {
document.addEventListener('cookieyes_banner_load', (bannerLoadEventData: unknown) => {
const consentPreferences = user?.consent_preferences;
const typedEventData = eventData as {
detail: { categories: Record<string, boolean>, isUserActionCompleted: boolean; },
};
const typedEventData = bannerLoadEventData as CookieYesBannerLoadEvent;

if (consentPreferences) {
// Accounts remembers some state
Expand All @@ -164,13 +159,19 @@ app.services.userLoader.getCurrentUser().then((user) => {
consentPreferences.accepted.forEach((accepted) => {
// There is no "necessary" switch
if (accepted !== 'necessary') {
(document.getElementById(`ckySwitch${accepted}`) as any).checked = true;
if (!typedEventData.detail.categories[accepted]) { doUpdate = true; }
const checkbox = document.getElementById(`ckySwitch${accepted}`) as HTMLInputElement | null;
if (checkbox) {
checkbox.checked = true;
if (!typedEventData.detail.categories[accepted]) { doUpdate = true; }
}
}
});
consentPreferences.rejected.forEach((rejected) => {
(document.getElementById(`ckySwitch${rejected}`) as any).checked = false;
if (typedEventData.detail.categories[rejected]) { doUpdate = true; }
const checkbox = document.getElementById(`ckySwitch${rejected}`) as HTMLInputElement | null;
if (checkbox) {
checkbox.checked = false;
if (typedEventData.detail.categories[rejected]) { doUpdate = true; }
}
});
if (doUpdate) { performBannerAction('accept_partial'); }
} else if (typedEventData.detail.isUserActionCompleted) {
Expand All @@ -192,17 +193,21 @@ app.services.userLoader.getCurrentUser().then((user) => {
);
}

document.addEventListener('cookieyes_consent_update', (eventData: unknown) => {
if (user) { sendConsentToAccounts((eventData as { detail: ConsentPreferences }).detail); }
document.addEventListener('cookieyes_consent_update', (consentUpdateEventData: unknown) => {
if (user) { sendConsentToAccounts((consentUpdateEventData as CookieYesConsentUpdateEvent).detail); }
});

window.cookieYesActive = true;
});

// GTM snippet slightly modified to assume f.parentNode is not null and with const types so ts doesn't complain
// tslint:disable
(function(w,d,s,l,i){w[l]=w[l]||[];w[l].push({'gtm.start':
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode!.insertBefore(j,f);
})(window,document,'script' as const,'dataLayer' as const,'GTM-TFCS56G');
new Date().getTime(),event:'gtm.js'});var f=d.getElementsByTagName(s)[0],
j=d.createElement(s),dl=l!='dataLayer'?'&l='+l:'';j.async=true;j.src=
'https://www.googletagmanager.com/gtm.js?id='+i+dl;f.parentNode!.insertBefore(j,f);
})(window,document,'script' as const,'dataLayer' as const,'GTM-TFCS56G');
// tslint:enable
});

// Learn more about service workers: http://bit.ly/CRA-PWA
Expand Down
4 changes: 4 additions & 0 deletions src/test/mocks/userLoader.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import createUserLoader, { AccountsUser } from '../../gateways/createUserLoader'

export const testAccountsUser: AccountsUser = {
applications: [],
consent_preferences: {
accepted: [],
rejected: [],
},
contact_infos: [],
external_ids: [],
faculty_status: 'no_faculty_info',
Expand Down
14 changes: 14 additions & 0 deletions src/typings/cookieyes.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
declare module 'cookieyes' {
export type ConsentPreferences = {
accepted: string[],
rejected: string[],
};

export type CookieYesBannerLoadEvent = {
detail: { categories: Record<string, boolean>, isUserActionCompleted: boolean; },
};

export type CookieYesConsentUpdateEvent = {
detail: ConsentPreferences;
};
}
4 changes: 3 additions & 1 deletion src/typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ declare global {
ga: UniversalAnalytics.ga;
dataLayer: object[];
gtag: (eventKey?: string, eventVal?: string, eventObj?: object) => boolean | void;
cookieYesActive?: boolean;
}

var fetch: (input: dom.RequestInfo, init?: dom.RequestInit) => Promise<Response>;
Expand All @@ -74,5 +75,6 @@ declare global {
// tslint:disable-next-line:variable-name
var ResizeObserver: dom.ResizeObserverConstructor;

var performBannerAction: (action: string) => void;
const performBannerAction: (action: string) => void;

}

0 comments on commit a467aa4

Please sign in to comment.