muuvmuuv / gdpr-cookie-consent-banner

A full featured cookie consent plugin which meets GDPR and other requirements.
https://gdpr-cookie-consent-banner.now.sh/
MIT License
1 stars 0 forks source link

Errors out on adding a capability #3

Open ghost opened 4 years ago

ghost commented 4 years ago

Following error is encountered on adding a new capability:

 Uncaught TypeError: Cannot read property 'value' of undefined
        at o.c._runValueEventsFor (cookie-consent.mjs:1)
        at cookie-consent.mjs:1
        at Array.forEach (<anonymous>)
        at HTMLButtonElement.<anonymous> (cookie-consent.mjs:1)

Instantiated like this:

window.consent = new  CookieConsent({

    capabilities: [

        {

            name:  'functional',

            checked:  true,

            onAccept: (consent) => {

                consent.saveUserOptions({ consented:  true })

            },

            onReject: (consent) => {

                consent.saveUserOptions({ consented:  false })

            }

        },

        {

            name:  'optional',

            checked:  false,

            onAccept: (consent) => {

                consent.saveUserOptions({ consented:  true })

            },

            onReject: (consent) => {

                consent.saveUserOptions({ consented:  false })

            }

        }

    ],

});

With following HTML

  <div

    id="cookiebanner"

    class="cookiebanner"

    aria-label="Edit your cookie settings"

    role="banner"

    >

        <div  class="info">

            <div  class="title">Your Cookie Controls</div>

            <div  class="description">

            This displays the plugin with its default configuration.

            </div>

        </div>

        <div  class="choices">

            <label  class="choice"  for="choice-functional">

                <input  type="checkbox"  name="choice:functional"  id="choice-functional"  value="1"  checked />

                <span  class="name">Functional</span>

            </label>

            <label  class="choice"  for="choice-optional">

                <input  type="checkbox"  name="choice:optional"  id="choice-optional"  value="1"  checked />

                <span  class="name">Optional</span>

            </label>

        </div>

        <div  class="buttons">

            <button  class="reject">Reject choices</button>

            <button  class="accept">Accept choices</button>

        </div>

    </div>

<div

id="cookienotice"

class="cookienotice"

aria-label="Show the cookie settings again"

role="button"

>
Show
</div>
muuvmuuv commented 4 years ago

What is value="1" for? Try to remove it and check it again.

ghost commented 4 years ago

Hey @muuvmuuv thanks for replying. I realised meanwhile, that the issue occurs if I add capabilities after the cookie is set once with less number of capabilities. I deleted the cookie once I added two other capabilities and now it works fine. Maybe something you can check at your end too.

muuvmuuv commented 4 years ago

Good point!

btw. you don't need to save the user options twice, there are new events onRejectAll, onAcceptAll and onConsent which you can use for those! :)

ghost commented 4 years ago

Ah I did not know about the these methods. Are they already released? I do not see it in the consent.js supplied so far.

muuvmuuv commented 4 years ago

Pushed it just now :) have a look at the update README

muuvmuuv commented 4 years ago

The issue you have described should be gone as well, I have checked it and as soon as the user reloads the page again, the error disappears. Could you check that also?

ghost commented 4 years ago

Thanks for the push. Seems like the checkboxes are automatically handled now too! Cool :) The error still persists though. Steps to reproduce are:

  1. There should be an existing cookie with at least one capability already set.
  2. Add a new checkbox for the capability with respective name.
  3. Define the added capability in the script and reload the browser.

The error goes away however if you delete the cookie. It seems like the added capability errors out because it is not in the cookie yet. A solution might be to read the cookie on load, set capabilities respectively and update the cookie with the new parameters (which if the user never changed will be the same old values)?