orestbida / cookieconsent

:cookie: Simple cross-browser cookie-consent plugin written in vanilla js
https://playground.cookieconsent.orestbida.com/
MIT License
3.68k stars 387 forks source link

[Bug]: Clicking Save Preferences button rejects all services regardless of toggle state #700

Closed peaemaye closed 15 hours ago

peaemaye commented 1 week ago

Expected Behavior

Should save the current service selection.

Current Behavior

All services are rejected and fire their onReject event.

Steps to reproduce

See this StackBlitz for a minimal repro: https://stackblitz.com/edit/stackblitz-starters-xtbkbf?file=index.js

  1. Click Open Preview in new tab icon to open in new window that will retain cookies on reload.
  2. Open F12 developer tools to see console.log entries.
  3. Click Accept all to accept all categories/services.
  4. Press F5 to reload the page.
  5. Click the Show preferences modal button to open the preferences window.
  6. Expand the Analytics cookies section to see that Service1 and Service2 are active.
  7. Click Save preferences to save the current selection without making any changes.

Proposed fix or additional info.

See PR: https://github.com/orestbida/cookieconsent/pull/701

Version

3.0.1

On which browser do you see the issue?

Firefox, Chrome

peaemaye commented 6 days ago

I think I've found the cause of this issue. The retrieveState method of api.js currently sets the _enabledServices before _acceptedServices so the enabled services don't include services read from the cc-cookie. I think reversing this order is sufficient to fix it.

See pull request #701