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

(release-4.0.0) HDS-2532 fix events #1435

Merged
merged 3 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ export function useCookieConsent(props: CookieConsentReactProps): CookieConsentR
onReady,
});

const getAllConsentStatuses = () => {
const getAllConsentStatuses = useCallback(() => {
if (!instanceRef.current || !instanceRef.current.getAllConsentStatuses) {
return [];
}
return instanceRef.current.getAllConsentStatuses();
};
}, []);

const current = windowObjectGetter();
if (current) {
Expand All @@ -127,8 +127,7 @@ export function useCookieConsent(props: CookieConsentReactProps): CookieConsentR
return Promise.resolve(false);
}
createHasBeenCalled.current = true;
instanceRef.current = await CookieConsentCore.create(siteSettings, mergedOptions);
forceRender();
await CookieConsentCore.create(siteSettings, mergedOptions);
return Promise.resolve(true);
};
// useEffect cannot be async, so have to use this work-around
Expand Down Expand Up @@ -165,19 +164,19 @@ export function useCookieConsent(props: CookieConsentReactProps): CookieConsentR
[elementId],
);

const removePage = () => {
const removePage = useCallback(() => {
if (!instanceRef.current) {
return;
}
instanceRef.current.removePage();
};
}, []);

const removeBanner = () => {
const removeBanner = useCallback(() => {
if (!instanceRef.current) {
return;
}
instanceRef.current.removeBanner();
};
}, []);

return {
isReady: readyRef.current && !!instanceRef.current,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { MutableRefObject, useEffect, useMemo, useRef } from 'react';
import { useEffect, useMemo } from 'react';
import { noop } from 'lodash';

import { isSsrEnvironment } from '../../../utils/isSsrEnvironment';
import { cookieEventType } from '../../cookieConsentCore/cookieConsentCore';
Expand All @@ -18,20 +19,13 @@ export type CookieConsentEventsReturnType = () => void;

export function useCookieConsentEvents(props: CookieConsentEventsProps): CookieConsentEventsReturnType {
const { onChange, onReady, onMonitorEvent } = props;
const listenerDisposer: MutableRefObject<(() => void) | null> = useRef(null);

useMemo(() => {
if (listenerDisposer.current) {
listenerDisposer.current();
listenerDisposer.current = null;
}

const [attach, dispose] = useMemo(() => {
if (!onChange) {
return () => undefined;
return [noop, noop];
}

if (isSsrEnvironment()) {
return () => undefined;
return [noop, noop];
}

const getChangeProps = (e: Event): CookieConsentChangeEvent => {
Expand Down Expand Up @@ -59,29 +53,27 @@ export function useCookieConsentEvents(props: CookieConsentEventsProps): CookieC
onReady();
};

window.addEventListener(cookieEventType.CHANGE, cookieListener);
window.addEventListener(cookieEventType.MONITOR, monitorListener);
window.addEventListener(cookieEventType.READY, readyListener);
const addEventListeners = () => {
window.addEventListener(cookieEventType.CHANGE, cookieListener);
window.addEventListener(cookieEventType.MONITOR, monitorListener);
window.addEventListener(cookieEventType.READY, readyListener);
};

const disposer = () => {
const removeEventListeners = () => {
window.removeEventListener(cookieEventType.CHANGE, cookieListener);
window.removeEventListener(cookieEventType.MONITOR, monitorListener);
window.removeEventListener(cookieEventType.READY, readyListener);
listenerDisposer.current = null;
};

listenerDisposer.current = disposer;

return disposer;
return [addEventListeners, removeEventListeners];
}, [onChange, onMonitorEvent, onReady]);

useEffect(() => {
attach();
return () => {
if (listenerDisposer.current) {
listenerDisposer.current();
}
dispose();
};
}, []);

return listenerDisposer.current || (() => undefined);
return dispose;
}
14 changes: 7 additions & 7 deletions site/src/docs/components/cookie-consent/api.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -180,13 +180,13 @@ Events are dispatched with `window.dispatchEvent` and can be listened with `wind

The `hds-cookie-consent-ready` callback is not called with any arguments. Other callback receive the same arguments.

| Property | Description | Type | Default |
| ---------------------------------------------------- | ------------------------------------------------------------------------------------------------ | ---------- | ------- |
| `type` | Type of the event. Event types are listed in <InternalLink href="#events">events</InternalLink>. | `string` | - |
| `acceptedGroups` | Array of accepted group names. | `string[]` | `[]` |
| `storageType` | Only in monitor events. Type of storage where the unapproved item was found. | `string` | - |
| `storageKeys` | Only in monitor events. Keys of the unapproved items. | `string[]` | - |
| [Table 12: Properties of the useCookieConsentEvents] |
| Property | Description | Type | Default |
| ------------------------------------------------------ | ------------------------------------------------------------------------------------------------ | ---------- | ------- |
| `type` | Type of the event. Event types are listed in <InternalLink href="#events">events</InternalLink>. | `string` | - |
| `acceptedGroups` | Array of accepted group names. | `string[]` | `[]` |
| `storageType` | Only in monitor events. Type of storage where the unapproved item was found. | `string` | - |
| `storageKeys` | Only in monitor events. Keys of the unapproved items. | `string[]` | - |
| [Table 12: Properties of the CookieConsentChangeEvent] |

### CSS variables

Expand Down
Loading