cookieconsent icon indicating copy to clipboard operation
cookieconsent copied to clipboard

[Bug]: Accepting a service will reject others

Open MoritzLost opened this issue 10 months ago • 0 comments

Expected Behavior

Accepting a service will reject others in the same category:

CookieConsent.acceptService('youtube', 'media');

Current Behavior

If I have multiple services in the media category and have accepted some or all of them, the code above will reject all of them except for youtube. This is mentioned in passing in the docs, however, it's a very awkward API design:

  • The name acceptService implies that I'm accepting a service for the given category, without changing the state of other services. Given the current behavior, a function name like setAcceptedServices() or setAcceptedServicesByCategory() would be more appropriate to indicate I'm overwriting the existing state.
  • I have a custom button in a placeholder for YouTube videos. I only want to tell CookieConsent to accept the youtube service in the media category, but there's no simple way to do that through the API. I have to get all accepted services, merge youtube into that, and then run acceptService:
const acceptedServices = CookieConsent.getUserPreferences().acceptedServices[category];
CookieConsent.acceptService([...new Set([service, ...acceptedServices])], category);

That's really inconvenient and unintuitive.

Steps to reproduce

  1. Define a category with multiple services.
  2. Accept some of the services through the interface.
  3. Execute CookieConsent.acceptService() with one of the services and the above category.

Proposed fix or additional info.

Two options:

  1. Change the acceptService() function to not only accept the service/services passed as the first argument, but not change any of the other service's states. Add a second function like setAcceptedServices() that replicates the existing behavior of the acceptService() function. This would require a major release since it's a BC break.
  2. Add another API function acceptSingleService() or add a third boolean parameter to acceptService (rejectOther or similar) to make it possible to accept a single service without changing other services. Doesn't require a BC break, but retains the awkward API naming.

Version

3.1.0

On which browser do you see the issue?

Firefox

MoritzLost avatar Apr 16 '25 14:04 MoritzLost