Skip to content

Commit

Permalink
feat: reload page after a consent gets revoked (#817)
Browse files Browse the repository at this point in the history
* feat: reload page after a cookie gets revoked

* fix: remove any types

* test: cookiebar reload behavior

* chore: readability

---------

Co-authored-by: Maximilian Röll <[email protected]>
  • Loading branch information
FabianGerke and maxiroellplenty authored Nov 21, 2024
1 parent 2bf6bbe commit 4bb70d6
Show file tree
Hide file tree
Showing 5 changed files with 88 additions and 14 deletions.
2 changes: 1 addition & 1 deletion apps/web/composables/useJsonEditor/useJsonEditor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const useJsonEditor = (initialJson: string) => {
const updateLineCount = () => {
if (textarea.value) {
const lineBreaks = (jsonText.value.match(/\n/g) || []).length;
lineCount.value = Array.from({ length: lineBreaks + 1 }, (_, i) => i + 1);
lineCount.value = Array.from({ length: lineBreaks + 1 }, (_, index) => index + 1);
}
};

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import { mockNuxtImport } from '@nuxt/test-utils/runtime';
import { useReadCookieBar } from '../useReadCookieBar';

const { useCookie } = vi.hoisted(() => ({
useCookie: vi.fn().mockReturnValue({}),
}));

const { useRouter } = vi.hoisted(() => ({
useRouter: vi.fn().mockReturnValue({
go: vi.fn(),
}),
}));

mockNuxtImport('useRouter', () => useRouter);
mockNuxtImport('useCookie', () => useCookie);


describe('useReadCookieBar', () => {

describe('setConsent', () => {
beforeEach(() => {
useCookie.mockImplementation(() => ({
value: {
'CookieBar.functional.label': {
'CookieBar.functional.cookies.payPal.name': true,
'CookieBar.functional.cookies.scriptDemo.name': true,
}
},
}));
});

it('should reload the page if a consent cookie is revoked', () => {
const { setConsent, initializeCookies, data } = useReadCookieBar();
const RouterGoSpy = vi.fn();

useRouter.mockReturnValue({
go: RouterGoSpy,
});

initializeCookies();

data.value.groups[2].cookies[1].accepted = false;
setConsent();

expect(RouterGoSpy).toHaveBeenCalledTimes(1);
});

it('should not reload the page if a consent cookie is accepted', () => {
const { setConsent, initializeCookies } = useReadCookieBar();
const RouterGoSpy = vi.fn();

useRouter.mockReturnValue({
go: RouterGoSpy,
});

initializeCookies();
setConsent();

expect(RouterGoSpy).toHaveBeenCalledTimes(0);
});
});
});
31 changes: 18 additions & 13 deletions apps/web/composables/useReadCookieBar/useReadCookieBar.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { UseReadCookieBarState, UseReadCookieBarReturn } from './types';
import type { Cookie, CookieGroup, CookieGroupFromNuxtConfig } from '~/configuration/cookie.config';
import type { Cookie, CookieGroup, CookieGroupFromNuxtConfig, JsonCookie } from '~/configuration/cookie.config';
import type { ChangeVisibilityState, SetAllCookiesState, SetConsent, InitializeCookies } from './types';
import cookieScripts from '~/cookie-scripts.config';

Expand All @@ -18,7 +18,7 @@ const fetchScripts = (scripts: string[]) => {
} else if (cookieScripts[script]) {
cookieScripts[script]();
}
} catch (error: any) {
} catch (error: unknown) {
console.error(error);
}
});
Expand Down Expand Up @@ -75,11 +75,11 @@ export const useReadCookieBar: UseReadCookieBarReturn = () => {
const initializeCookies: InitializeCookies = () => {
const cookies = JSON.parse(JSON.stringify(initialCookies));

const browserCookies = useCookie('consent-cookie');
const browserCookies = useCookie('consent-cookie') as Ref<JsonCookie | null | undefined>;

cookies.groups.slice(1).forEach((group: CookieGroup) => {
group.cookies.forEach((cookie: Cookie) => {
cookie.accepted = !!browserCookies.value?.[group.name as any]?.[cookie.name as any] || false;
cookie.accepted = !!browserCookies.value?.[group.name]?.[cookie.name] || false;

const { consent } = useCookieConsent(cookie.name);
consent.value = cookie.accepted || false;
Expand Down Expand Up @@ -108,13 +108,22 @@ export const useReadCookieBar: UseReadCookieBarReturn = () => {
const setConsent: SetConsent = () => {
const { getMinimumLifeSpan } = cookieBarHelper();
const router = useRouter();
const browserCookies = useCookie('consent-cookie') as Ref<JsonCookie | null | undefined>;

const jsonCookie = state.value.data.groups.reduce((accumulator: any, group: CookieGroup) => {
accumulator[group.name] = group.cookies.reduce((childAccumulator: any, cookie: Cookie) => {
childAccumulator[cookie.name] = cookie.accepted;
let cookieRevoke = false;

const jsonCookie = state.value.data.groups.reduce((accumulator: JsonCookie, group: CookieGroup) => {
accumulator[group.name] = group.cookies.reduce((childAccumulator: { [key: string]: boolean }, cookie: Cookie) => {
const currentStatus = !!browserCookies.value?.[group.name]?.[cookie.name] || false;
const { consent } = useCookieConsent(cookie.name);

childAccumulator[cookie.name] = cookie.accepted || false;
consent.value = cookie.accepted || false;

if (currentStatus && !consent.value) {
cookieRevoke = true;
}

return childAccumulator;
}, {});

Expand All @@ -126,15 +135,11 @@ export const useReadCookieBar: UseReadCookieBarReturn = () => {
maxAge: getMinimumLifeSpan(state.value.data.groups),
});

const alreadySetCookie = Boolean(consentCookie.value);

consentCookie.value = jsonCookie;

consentCookie.value = JSON.stringify(jsonCookie);
changeVisibilityState();

loadThirdPartyScripts();

if (alreadySetCookie) {
if (cookieRevoke) {
router.go(0);
}
};
Expand Down
6 changes: 6 additions & 0 deletions apps/web/configuration/cookie.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ export interface CookieGroupFromNuxtConfig {
barDescription: string;
}

export type JsonCookie = {
[groupName: string]: {
[cookieName: string]: boolean;
};
};

export default {
barTitle: 'CookieBar.about.label',
barDescription: 'CookieBar.about.description',
Expand Down
1 change: 1 addition & 0 deletions docs/changelog/changelog_en.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
- The default data for the homepage is now available for both English and German.
- PayPal is now a functional cookie and has to be accepted by the user. Default behavior can be changed by changing accepted to true https://pwa-docs.plentymarkets.com/guide/how-to/cookie
- PayPal shows a message if the cookie is not accepted.
- We only reload the page after a cookie gets revoked.

#### GitHub Action: Upload

Expand Down

0 comments on commit 4bb70d6

Please sign in to comment.