Skip to content

Commit 65e62ec

Browse files
authored
fix(cookie-consent): save consent doesn't trigger load of segment (#1946)
1 parent 7598cfa commit 65e62ec

File tree

3 files changed

+36
-11
lines changed

3 files changed

+36
-11
lines changed

.changeset/odd-peas-suffer.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@scaleway/cookie-consent": patch
3+
---
4+
5+
Fix issue with initialization on save content doens't load segment on first render.

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

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,6 @@ export const useCookieConsent = (): Context => {
5252
return context
5353
}
5454

55-
const getCookies = () => cookie.parse(document.cookie)
56-
5755
export const CookieConsentProvider = ({
5856
children,
5957
isConsentRequired,
@@ -73,6 +71,9 @@ export const CookieConsentProvider = ({
7371
cookiesOptions?: CookieSerializeOptions
7472
}>) => {
7573
const [needConsent, setNeedsConsent] = useState(false)
74+
const [cookies, setCookies] = useState<Record<string, string>>(
75+
cookie.parse(document.cookie),
76+
)
7677

7778
const {
7879
integrations: segmentIntegrations,
@@ -113,10 +114,10 @@ export const CookieConsentProvider = ({
113114
// to false after receiving segment answer and flicker the UI
114115
setNeedsConsent(
115116
isConsentRequired &&
116-
getCookies()[HASH_COOKIE] !== integrationsHash.toString() &&
117+
cookies[HASH_COOKIE] !== integrationsHash.toString() &&
117118
segmentIntegrations !== undefined,
118119
)
119-
}, [isConsentRequired, integrationsHash, segmentIntegrations])
120+
}, [isConsentRequired, integrationsHash, segmentIntegrations, cookies])
120121

121122
// We store unique categories names in an array
122123
const categories = useMemo(
@@ -135,13 +136,14 @@ export const CookieConsentProvider = ({
135136
categories.reduce<Partial<Consent>>(
136137
(acc, category) => ({
137138
...acc,
138-
[category]: isConsentRequired
139-
? getCookies()[`${cookiePrefix}_${category}`] === 'true'
140-
: true,
139+
[category]:
140+
isConsentRequired || needConsent
141+
? cookies[`${cookiePrefix}_${category}`] === 'true'
142+
: true,
141143
}),
142144
{},
143145
),
144-
[isConsentRequired, categories, cookiePrefix],
146+
[isConsentRequired, categories, cookiePrefix, needConsent, cookies],
145147
)
146148

147149
const saveConsent = useCallback(
@@ -163,7 +165,7 @@ export const CookieConsentProvider = ({
163165
})
164166
} else {
165167
document.cookie = cookie.serialize(
166-
`${cookiePrefix}_${consentCategoryName}`,
168+
cookieName,
167169
consentValue.toString(),
168170
{
169171
...cookiesOptions,
@@ -174,6 +176,10 @@ export const CookieConsentProvider = ({
174176
},
175177
)
176178
}
179+
setCookies(prevCookies => ({
180+
...prevCookies,
181+
[cookieName]: consentValue ? 'true' : 'false',
182+
}))
177183
}
178184
// We set the hash cookie to the current consented integrations
179185
document.cookie = cookie.serialize(
@@ -185,6 +191,10 @@ export const CookieConsentProvider = ({
185191
maxAge: consentAdvertisingMaxAge,
186192
},
187193
)
194+
setCookies(prevCookies => ({
195+
...prevCookies,
196+
[HASH_COOKIE]: integrationsHash.toString(),
197+
}))
188198
setNeedsConsent(false)
189199
},
190200
[
@@ -206,7 +216,6 @@ export const CookieConsentProvider = ({
206216
: true,
207217
[isConsentRequired, segmentIntegrations, cookieConsent, needConsent],
208218
)
209-
210219
// 'All': false tells Segment not to send data to any Destinations by default, unless they’re explicitly listed as true in the next lines.
211220
// In this case we should not have any integration, so we protect the user. Maybe unecessary as we always set true of false for an integration.
212221
const segmentEnabledIntegrations = useMemo(

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

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@ describe('CookieConsent - CookieConsentProvider', () => {
154154

155155
act(() => {
156156
result.current.saveConsent({
157-
advertising: true,
157+
analytics: true,
158158
marketing: true,
159159
})
160160
})
@@ -171,6 +171,17 @@ describe('CookieConsent - CookieConsentProvider', () => {
171171
maxAge: 15552000,
172172
})
173173

174+
act(() => {
175+
expect(result.current.categoriesConsent).toStrictEqual({
176+
analytics: true,
177+
marketing: true,
178+
})
179+
})
180+
181+
act(() => {
182+
expect(result.current.isSegmentAllowed).toBe(true)
183+
})
184+
174185
act(() => {
175186
result.current.saveConsent({
176187
advertising: false,

0 commit comments

Comments
 (0)