Skip to content

Commit

Permalink
Merge pull request #761 from OndraM/get-language-code
Browse files Browse the repository at this point in the history
feat: store language code of the consent in the cookie
  • Loading branch information
orestbida authored Dec 18, 2024
2 parents f87853b + d5223e0 commit 8ff02e1
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 8 deletions.
3 changes: 2 additions & 1 deletion docs/reference/api-reference.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,7 @@ Returns the plugin's own cookie, or just one of the fields.
consentId: string
consentTimestamp: string,
lastConsentTimestamp: string,
languageCode: string,
services: {[key: string]: string[]}
}
```
Expand Down Expand Up @@ -514,4 +515,4 @@ Reset CookieConsent.
- **Example**:
```javascript
CookieConsent.reset(true);
```
```
7 changes: 4 additions & 3 deletions src/utils/cookies.js
Original file line number Diff line number Diff line change
Expand Up @@ -166,9 +166,10 @@ export const saveCookiePreferences = () => {
data: state._cookieData,
consentTimestamp: state._consentTimestamp.toISOString(),
consentId: state._consentId,
services: deepCopy(state._acceptedServices)
services: deepCopy(state._acceptedServices),
languageCode: globalObj._state._currentLanguageCode
};

if (state._lastConsentTimestamp) {
state._savedCookieContent.lastConsentTimestamp = state._lastConsentTimestamp.toISOString();
}
Expand Down Expand Up @@ -399,4 +400,4 @@ export const getAllCookies = (regex) => {
}

return cookieNames;
};
};
3 changes: 2 additions & 1 deletion tests/cookies.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ describe("Cookie should be created successfully", () => {
expect(typeof ccCookie.consentId).toBe('string');
expect(ccCookie.consentTimestamp).toBeDefined();
expect(ccCookie.lastConsentTimestamp).toBeDefined();
expect(ccCookie.languageCode).toEqual('en');
expect(ccCookie.categories).toEqual(['necessary', 'analytics', 'ads']);
expect(ccCookie.services).toMatchObject({
necessary: ['service1'],
Expand Down Expand Up @@ -64,4 +65,4 @@ describe("Cookie should be created successfully", () => {
expect(allCookies.length).toBe(2);
expect(allCookies).toContain('service1Cookie1', 'service1Cookie2');
})
})
})
11 changes: 8 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,11 @@ declare namespace CookieConsent {
*/
lastConsentTimestamp: string

/**
* Language code of the consent.
*/
languageCode: string

/**
* All enabled services.
*/
Expand Down Expand Up @@ -214,10 +219,10 @@ declare namespace CookieConsent {
* @default 'Lax'
*/
sameSite?: 'Lax' | 'Strict' | 'None'

/**
* Cookie secure flag.
*
*
* @default true
*/
secure?: boolean
Expand Down Expand Up @@ -688,4 +693,4 @@ declare global {
interface Window {
CookieConsent: typeof CookieConsent
}
}
}

0 comments on commit 8ff02e1

Please sign in to comment.