-
-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cookie consent dialog and user setting (#3301)
"C is for cookie, that's good enough for me" - Cookie Monster
- Loading branch information
1 parent
a683036
commit bfc9230
Showing
14 changed files
with
159 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Fragment, type ReactElement } from "react"; | ||
|
||
import useCookieConsent from "cookies/useCookieConsent"; | ||
|
||
/** Empty component for running useCookieConsent within a <Suspense>, | ||
* because it depends on i18n localization loading first. */ | ||
export default function CookieConsent(): ReactElement { | ||
useCookieConsent(); | ||
return <Fragment />; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
#cc-main { | ||
--primary: #1e88e5; /* themeColors.primary: blue[600] */ | ||
--dark-shade: #0d47a1; /* themeColors.darkShade: blue[900] */ | ||
|
||
--cc-btn-primary-bg: var(--primary); | ||
--cc-btn-primary-border-color: var(--primary); | ||
--cc-btn-primary-hover-bg: var(--dark-shade); | ||
--cc-btn-primary-hover-border-color: var(--dark-shade); | ||
--cc-font-family: "Noto Sans", "Open Sans", Roboto, Helvetica, Arial, sans-serif; | ||
--cc-primary-color: var(--primary); | ||
--cc-secondary-color: #000000 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { useCallback, useEffect } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { eraseCookies, run } from "vanilla-cookieconsent"; | ||
|
||
import "vanilla-cookieconsent/dist/cookieconsent.css"; | ||
import "cookies/cc.css"; | ||
|
||
import { useAppDispatch } from "rootRedux/hooks"; | ||
import { updateConsent } from "types/Redux/analytics"; | ||
|
||
export default function useCookieConsent(): void { | ||
const dispatch = useAppDispatch(); | ||
const { t } = useTranslation(); | ||
|
||
const updateAnalytics = useCallback( | ||
(param: { cookie: CookieConsent.CookieValue }): void => { | ||
console.info("C is for Cookie..."); | ||
dispatch(updateConsent()); | ||
if (!param.cookie.categories.includes("analytics")) { | ||
eraseCookies(/^(?!cookie_consent$)/); // Only keep cookie with name "cookie_consent" | ||
} | ||
}, | ||
[dispatch] | ||
); | ||
|
||
useEffect(() => { | ||
run({ | ||
categories: { analytics: {}, necessary: {} }, | ||
cookie: { expiresAfterDays: 365, name: "cookie_consent" }, | ||
guiOptions: { consentModal: { layout: "bar inline" } }, | ||
language: { | ||
default: "i18n", | ||
translations: { | ||
i18n: { | ||
consentModal: { | ||
acceptAllBtn: t( | ||
"userSettings.analyticsConsent.consentModal.acceptAllBtn" | ||
), | ||
acceptNecessaryBtn: t( | ||
"userSettings.analyticsConsent.consentModal.acceptNecessaryBtn" | ||
), | ||
description: t( | ||
"userSettings.analyticsConsent.consentModal.description" | ||
), | ||
title: t("userSettings.analyticsConsent.consentModal.title"), | ||
}, | ||
preferencesModal: { sections: [] }, | ||
}, | ||
}, | ||
}, | ||
onChange: updateAnalytics, | ||
onFirstConsent: updateAnalytics, | ||
}).then(() => dispatch(updateConsent())); | ||
}, [dispatch, t, updateAnalytics]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,9 @@ | ||
export interface AnalyticsState { | ||
consent: boolean; | ||
currentPage: string; | ||
} | ||
|
||
export const defaultState: AnalyticsState = { | ||
consent: false, | ||
currentPage: "", | ||
}; |