cookieconsent
cookieconsent copied to clipboard
[Bug]: Accepting a service will reject others
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
acceptServiceimplies that I'm accepting a service for the given category, without changing the state of other services. Given the current behavior, a function name likesetAcceptedServices()orsetAcceptedServicesByCategory()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
youtubeservice in themediacategory, but there's no simple way to do that through the API. I have to get all accepted services, mergeyoutubeinto that, and then runacceptService:
const acceptedServices = CookieConsent.getUserPreferences().acceptedServices[category];
CookieConsent.acceptService([...new Set([service, ...acceptedServices])], category);
That's really inconvenient and unintuitive.
Steps to reproduce
- Define a category with multiple services.
- Accept some of the services through the interface.
- Execute
CookieConsent.acceptService()with one of the services and the above category.
Proposed fix or additional info.
Two options:
- 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 likesetAcceptedServices()that replicates the existing behavior of theacceptService()function. This would require a major release since it's a BC break. - Add another API function
acceptSingleService()or add a third boolean parameter toacceptService(rejectOtheror 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