Skip to content

Commit

Permalink
fix: set new accepted after migration
Browse files Browse the repository at this point in the history
  • Loading branch information
robertlyall committed Oct 25, 2024
1 parent e7e0a55 commit 2943d06
Showing 1 changed file with 23 additions and 9 deletions.
32 changes: 23 additions & 9 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export default class Crumbs extends EventEmitter {
this.version = version;

this.cookie = this.load();
this.populateAccepted();
this.populate();

// Broadcast consent state to Google Tag Manager
this.broadcast();
Expand All @@ -96,15 +96,17 @@ export default class Crumbs extends EventEmitter {
return this.cookie.version < this.version;
}

populateAccepted = () => {
populate = () => {
if (!this.cookie) {
this.accepted = [];
return;
}

this.accepted = this.outdated()
? this.migrate()
: parseAccepted(this.cookie.value);
if (this.outdated()) {
this.migrate();
} else {
this.accepted = parseAccepted(this.cookie.value);
}
};

/**
Expand All @@ -123,12 +125,24 @@ export default class Crumbs extends EventEmitter {
if (!migration) {
// There is no known migration path for the old cookie value
// Let's clear it
this.accepted = [];
this.clear();
} else {
// There is a know migration path
// Run the migration function and return the result
return migration.call(this, this.cookie.value);
return;
}

// There is a know migration path
// Run the migration function and return the result
const accepted = migration.call(this, this.cookie.value);

// The migration returned null rather than a collection of consent, let's clear the cookie
if (accepted === null || !Array.isArray(accepted)) {
this.accepted = [];
this.clear();
return;
}

this.accepted = accepted;
this.save();
}

payload = () => {
Expand Down

0 comments on commit 2943d06

Please sign in to comment.