Skip to content

Commit 98694a9

Browse files
author
Axel KIRK
committed
refactor(options): add props cookie config
1 parent 6225032 commit 98694a9

File tree

4 files changed

+39
-18
lines changed

4 files changed

+39
-18
lines changed

packages/cookie-consent/src/CookieConsentProvider/CookieConsentProvider.tsx

Lines changed: 37 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ import {
1010
} from 'react'
1111
import { uniq } from '../helpers/array'
1212
import { stringToHash } from '../helpers/misc'
13-
import type { CategoryKind, Config, Consent, Integrations } from './types'
13+
import type {
14+
CategoryKind,
15+
Consent,
16+
Integrations,
17+
SegmentConfig,
18+
} from './types'
1419
import { useSegmentIntegrations } from './useSegmentIntegrations'
1520

1621
const COOKIE_PREFIX = '_scw_rgpd'
@@ -53,17 +58,29 @@ export const CookieConsentProvider = ({
5358
children,
5459
isConsentRequired,
5560
essentialIntegrations,
56-
config,
61+
segmentConfig,
62+
cookiePrefix = COOKIE_PREFIX,
63+
consentMaxAge = CONSENT_MAX_AGE,
64+
consentAdvertisingMaxAge = CONSENT_ADVERTISING_MAX_AGE,
65+
cookiesOptions = COOKIES_OPTIONS,
5766
}: {
5867
children: ReactNode
5968
isConsentRequired: boolean
6069
essentialIntegrations: string[]
61-
config: Config
70+
segmentConfig: SegmentConfig
71+
cookiePrefix: string
72+
consentMaxAge: number
73+
consentAdvertisingMaxAge: number
74+
cookiesOptions: {
75+
sameSite: boolean | 'strict' | 'lax' | 'none' | undefined
76+
secure: boolean
77+
path: string
78+
}
6279
}) => {
6380
const [needConsent, setNeedsConsent] = useState(false)
6481

6582
const [cookies, setCookies] = useState<Record<string, string>>()
66-
const segmentIntegrations = useSegmentIntegrations(config)
83+
const segmentIntegrations = useSegmentIntegrations(segmentConfig)
6784

6885
useEffect(() => {
6986
setCookies(cookie.parse(document.cookie))
@@ -126,20 +143,20 @@ export const CookieConsentProvider = ({
126143
(acc, category) => ({
127144
...acc,
128145
[category]: isConsentRequired
129-
? cookies?.[`${COOKIE_PREFIX}_${category}`] === 'true'
146+
? cookies?.[`${cookiePrefix}_${category}`] === 'true'
130147
: true,
131148
}),
132149
{},
133150
),
134-
[isConsentRequired, categories, cookies],
151+
[isConsentRequired, categories, cookies, cookiePrefix],
135152
)
136153

137154
const saveConsent = useCallback(
138155
(categoriesConsent: Partial<Consent>) => {
139156
for (const [consentName, consentValue] of Object.entries(
140157
categoriesConsent,
141158
) as [CategoryKind, boolean][]) {
142-
const cookieName = `${COOKIE_PREFIX}_${consentName}`
159+
const cookieName = `${cookiePrefix}_${consentName}`
143160

144161
if (!consentValue) {
145162
// If consent is set to false we have to delete the cookie
@@ -148,14 +165,14 @@ export const CookieConsentProvider = ({
148165
})
149166
} else {
150167
document.cookie = cookie.serialize(
151-
`${COOKIE_PREFIX}_${consentName}`,
168+
`${cookiePrefix}_${consentName}`,
152169
consentValue.toString(),
153170
{
154-
...COOKIES_OPTIONS,
171+
...cookiesOptions,
155172
maxAge:
156173
consentName === 'advertising'
157-
? CONSENT_ADVERTISING_MAX_AGE
158-
: CONSENT_MAX_AGE,
174+
? consentAdvertisingMaxAge
175+
: consentMaxAge,
159176
},
160177
)
161178
}
@@ -165,14 +182,20 @@ export const CookieConsentProvider = ({
165182
HASH_COOKIE,
166183
integrationsHash.toString(),
167184
{
168-
...COOKIES_OPTIONS,
185+
...cookiesOptions,
169186
// Here we use the shortest max age to force to ask again for expired consent
170-
maxAge: CONSENT_ADVERTISING_MAX_AGE,
187+
maxAge: consentAdvertisingMaxAge,
171188
},
172189
)
173190
setNeedsConsent(false)
174191
},
175-
[integrationsHash],
192+
[
193+
integrationsHash,
194+
consentAdvertisingMaxAge,
195+
consentMaxAge,
196+
cookiePrefix,
197+
cookiesOptions,
198+
],
176199
)
177200

178201
const isSegmentAllowed = useMemo(

packages/cookie-consent/src/CookieConsentProvider/__tests__/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ const wrapper =
1313
<CookieConsentProvider
1414
isConsentRequired={isConsentRequired}
1515
essentialIntegrations={['Deskpro', 'Stripe', 'Sentry']}
16-
config={{
16+
segmentConfig={{
1717
segment: {
1818
cdnURL: 'url',
1919
writeKey: 'key',

packages/cookie-consent/src/CookieConsentProvider/types.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,7 @@ type Integration = { category: CategoryKind; name: string }
1111

1212
export type Integrations = Integration[]
1313

14-
// TODO: avoid duplicating this type in @shire/console
15-
export type Config = {
14+
export type SegmentConfig = {
1615
segment?: {
1716
writeKey: string
1817
cdnURL: string
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,2 @@
1-
// TODO: avoid duplicating this function in @shire/console
21
export const stringToHash = (str = ''): number =>
32
Array.from(str).reduce((s, c) => Math.imul(31, s) + c.charCodeAt(0) || 0, 0)

0 commit comments

Comments
 (0)