Skip to content

Commit

Permalink
(hds-2529) Send event when cookie change via setGroupsStatusToAccepted
Browse files Browse the repository at this point in the history
Also dispatch event in React stories when cookies are wiped.
  • Loading branch information
NikoHelle committed Nov 20, 2024
1 parent d2cc1b5 commit 8f555e6
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { StoryComponent } from './components/StoryComponent';
// importing the json because load won't work in e2e
import siteSettings from '../cookieConsentCore/example/helfi_sitesettings.json';
import { ToggleButton } from '../toggleButton/ToggleButton';
import { cookieEventType } from '../cookieConsentCore/cookieConsentCore';

export default {
component: StoryComponent,
Expand Down Expand Up @@ -57,6 +58,7 @@ const Actions = () => {
document.cookie = `${cookieName}=; expires=Thu, 01 Jan 1970 00:00:01 GMT;`;
// eslint-disable-next-line no-console
console.log('Cookie removed:', cookieName);
window.dispatchEvent(new CustomEvent(cookieEventType.CHANGE, { detail: { acceptedGroups: [] } }));
};
const openBanner = async () => {
// eslint-disable-next-line no-console
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ const Actions = () => {
};
const addUnallowedCookie = async () => {
// eslint-disable-next-line no-console
console.log('Adding chat cookie:', await window.hds.cookieConsent.setGroupsStatusToAccepted(['unallowed']));
console.log(
'Trying to add an unallowed cookie:',
await window.hds.cookieConsent.setGroupsStatusToAccepted(['unallowed']),
);
};
const removeConsentCookie = async () => {
const cookieName = `helfi-cookie-consents`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,14 @@ export class CookieConsentCore {
* @return {Promise<boolean>} - A promise that resolves to true if the groups' status is successfully set to accepted, otherwise false.
*/
async setGroupsStatusToAccepted(acceptedGroupsArray) {
return this.#cookieHandler.setGroupsStatusToAccepted(acceptedGroupsArray);
const success = await this.#cookieHandler.setGroupsStatusToAccepted(acceptedGroupsArray);
if (success) {
const acceptedGroups = this.getAllConsentStatuses()
.filter((item) => item.consented)
.map((item) => item.group);
window.dispatchEvent(new CustomEvent(cookieEventType.CHANGE, { detail: { acceptedGroups } }));
}
return Promise.resolve(success);
}

/**
Expand Down

0 comments on commit 8f555e6

Please sign in to comment.