Skip to content

Commit

Permalink
add blocker
Browse files Browse the repository at this point in the history
  • Loading branch information
hrenaud (NovaGaïa) committed Sep 21, 2022
1 parent cd34379 commit 98b6afe
Show file tree
Hide file tree
Showing 7 changed files with 225 additions and 15 deletions.
12 changes: 11 additions & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,18 @@ exports.pluginOptionsSchema = ({ Joi }) => {
.default('Enregistrer'),
mandatoryLabel: Joi.string()
.optional()
.description('mandatoryLabel alert')
.description('mandatoryLabel alert.')
.default('obligatoire'),
blockerWarnLabel: Joi.string()
.optional()
.description('Valid acceptance label button in RGPDBlocker Component.')
.default('Accepter'),
blockerWarnMessage: Joi.string()
.optional()
.description('Warning message in RGPDBlocker Component.')
.default(
`Vous n'avez pas accepté les cookies globalement ou celui-ci précisement.<br/> Pour afficher ce contenu, vous devez accepter.`
),
}),
cookiesList: Joi.array()
.items(
Expand Down
6 changes: 5 additions & 1 deletion index.js
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
// noop
import Banner from './src/components/Banner'
import RGPDBlocker from './src/components/RGPDBlocker'
import { resetAll } from './src/utils/ls'

export { Banner as RGPDCookieBanner, resetAll, RGPDBlocker }
10 changes: 9 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,20 @@
"scripts": {
"release": "release-it"
},
"repository": {
"type": "git",
"url": "git+https://github.com/NovaGaia/gatsby-plugin-rgpd-acceptance.git"
},
"keywords": [
"gatsby",
"gatsby-plugin"
"gatsby-plugin","RGPD"
],
"author": "Renaud Héluin / NovaGaïa <[email protected]>",
"license": "MIT",
"bugs": {
"url": "https://github.com/NovaGaia/gatsby-plugin-rgpd-acceptance/issues"
},
"homepage": "https://github.com/NovaGaia/gatsby-plugin-rgpd-acceptance#readme",
"devDependencies": {
"auto-changelog": "^2.4.0",
"release-it": "^15.4.2"
Expand Down
20 changes: 10 additions & 10 deletions src/components/Banner.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,12 @@ function InternalBanner({
setChecked({ ...checked, ...updatedValue })
}

console.log('showMiniBanner', showMiniBanner)
if (showMiniBanner) {
return (
<div className="rgpd--banner mini">
<div className="rgpd--icon">{icon}</div>
<button
className="rgpd--btn"
className="rgpd--link choose"
onClick={() => {
setShowMiniBanner(false)
}}
Expand Down Expand Up @@ -82,9 +81,10 @@ function InternalBanner({
</span>
)}
</div>
<div className="rgpd--cookie-description">
{publicDescription}
</div>
<p
className="rgpd--cookie-description"
dangerouslySetInnerHTML={{ __html: publicDescription }}
/>
</li>
<li className="rgpd--cookie-type">{type}</li>
<li className="rgpd--cookie-checkzone">
Expand All @@ -103,13 +103,13 @@ function InternalBanner({
})}
</div>
) : (
<p dangerouslySetInnerHTML={{ __html: descriptionBanner }}></p>
<p dangerouslySetInnerHTML={{ __html: descriptionBanner }} />
)}
</div>
<div className="rgpd--footer">
{!displayCookiesList && (
<button
className="rgpd--btn"
className="rgpd--btn choose"
onClick={() => {
setDisplayCookiesList(!displayCookiesList)
}}
Expand All @@ -118,15 +118,15 @@ function InternalBanner({
</button>
)}
<button
className="rgpd--btn"
className="rgpd--btn all"
onClick={() => {
acceptAllCookies(cookiesList)
}}
>
{acceptAllLabel}
</button>
<button
className="rgpd--btn"
className="rgpd--btn none"
onClick={() => {
acceptNoCookies(cookiesList)
}}
Expand All @@ -135,7 +135,7 @@ function InternalBanner({
</button>
{displayCookiesList && (
<button
className="rgpd--btn"
className="rgpd--btn save"
onClick={() => {
acceptSomeCookies(cookiesList, checked)
}}
Expand Down
86 changes: 86 additions & 0 deletions src/components/RGPDBlocker.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import { graphql, useStaticQuery } from 'gatsby'
import {
hasValidatedAcceptance,
simplyAcceptOneCookie,
simplyReadOneCookie,
} from '../utils/ls'

import React from 'react'

function RGPDBlocker({
cookieKey,
children,
className,
blockerWarnMessage,
blockerWarnLabel,
}) {
const data = useStaticQuery(graphql`
query {
sitePlugin(name: { eq: "gatsby-plugin-rgpd-acceptance" }) {
pluginOptions
}
}
`)
const validatedStatus = {
globalStatus: hasValidatedAcceptance(),
showBocker: true,
}
const {
labels: {
icon,
blockerWarnMessage: confBlockerWarnMessage,
blockerWarnLabel: confBlockerWarnLabel,
},
cookiesList,
} = data.sitePlugin.pluginOptions
const _blockerWarnMessage = blockerWarnMessage || confBlockerWarnMessage
const _blockerWarnLabel = blockerWarnLabel || confBlockerWarnLabel
validatedStatus['isMandatory'] = false
let friendlyTitle = 'cookie name'
let friendlyDescription = 'cookie Description'
cookiesList.forEach(element => {
if (element.key === cookieKey) {
friendlyTitle = element.publicName
friendlyDescription = element.publicDescription
validatedStatus['isMandatory'] = element.mandatory
validatedStatus['cookieStatus'] =
simplyReadOneCookie(cookieKey, element.mandatory) === 'true' || false
}
})
if (validatedStatus.isMandatory) {
validatedStatus['showBocker'] = false
} else {
if (validatedStatus.globalStatus && validatedStatus.cookieStatus) {
validatedStatus['showBocker'] = false
}
}

if (validatedStatus['showBocker']) {
return (
<div className={`rgpd--blocker--container ${className}`}>
<div className="rgpd--icon">{icon}</div>
<div className="rgpd--blocker--title">{friendlyTitle}</div>
<p
className="rgpd--blocker--cookie-description"
dangerouslySetInnerHTML={{ __html: friendlyDescription }}
/>
<p
className="rgpd--blocker--warn-message"
dangerouslySetInnerHTML={{ __html: _blockerWarnMessage }}
/>
<button
className="rgpd--btn some"
onClick={() => {
simplyAcceptOneCookie(cookieKey)
}}
>
{_blockerWarnLabel}
</button>
</div>
)
} else {
return <>{children}</>
}
}

export default RGPDBlocker
81 changes: 80 additions & 1 deletion src/styles/rgpd-acceptance.css
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@

.rgpd-acceptance-theme .rgpd--icon {
font-size: xx-large;
line-height: 2rem;
text-shadow: 2px 2px 2px rgba(0, 0, 0, 0.5);
}

.rgpd-acceptance-theme .rgpd--title {
Expand Down Expand Up @@ -102,9 +104,86 @@
}

.rgpd-acceptance-theme .rgpd--btn {
border: 1px solid red;
border: 2px solid red;
border-radius: 0.3rem;
font-weight: 500;
background-color: white;
padding: 0rem 0.4rem;
color: black;
}
.rgpd-acceptance-theme .rgpd--btn:hover {
box-shadow: 0px 10px 5px -6px rgba(0, 0, 0, 0.75);
-webkit-box-shadow: 0px 10px 5px -6px rgba(0, 0, 0, 0.75);
-moz-box-shadow: 0px 10px 5px -6px rgba(0, 0, 0, 0.75);
}

.rgpd-acceptance-theme .rgpd--btn.all {
background-color: red;
color: white;
}

.rgpd-acceptance-theme .rgpd--btn.some {
background-color: orange;
}

.rgpd-acceptance-theme .rgpd--btn.none {
background-color: aqua;
border: 2px solid black;
}

.rgpd-acceptance-theme .rgpd--btn.choose {
background-color: yellow;
border: 2px solid black;
}

.rgpd-acceptance-theme .rgpd--btn.save {
background-color: greenyellow;
border: 2px solid black;
}

.rgpd-acceptance-theme .rgpd--link {
font-weight: 500;
text-decoration: underline;
}

.rgpd-acceptance-theme .rgpd--link:hover {
text-decoration: none;
}

.rgpd-acceptance-theme .rgpd--blocker--container {
background-color: gray;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
padding: 1rem;
gap: 0.5rem;
border-radius: 1rem;
position: relative;
}

.rgpd-acceptance-theme .rgpd--blocker--container .rgpd--icon {
position: absolute;
top: 1rem;
left: 1rem;
}

.rgpd-acceptance-theme .rgpd--blocker--title {
color: white;
font-size: x-large;
font-weight: 600;
}

.rgpd-acceptance-theme .rgpd--blocker--warn-message,
.rgpd-acceptance-theme .rgpd--blocker--cookie-description {
color: white;
text-align: center;
}

.rgpd-acceptance-theme .rgpd--blocker--cookie-description {
font-weight: 500;
}

.rgpd-acceptance-theme .rgpd--blocker--warn-message {
font-size: smaller;
}
25 changes: 24 additions & 1 deletion src/utils/ls.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ function _setItemInLS(key, consent, mandatory) {

// Externals functions

/**
* Get acceptance status.
* @returns Boolean
*/
function hasValidatedAcceptance() {
const dt = _getAcceptanceDate()
return dt !== null
Expand Down Expand Up @@ -162,7 +166,9 @@ function resetAllAcceptanceByDate(cookiesList, cookieDuration) {
if (acceptanceDate === 0) return
const diff = now - acceptanceDate
if (diff > cookieDuration) {
console.info(`Cookies are perimed, reset to ask consent again.`)
console.info(
`The cookies have expired, they have been reset to ask for consent again.`
)
resetAll(cookiesList)
}
}
Expand Down Expand Up @@ -205,6 +211,21 @@ function readCookieAcceptance(cookiesList, key) {
return output
}

/**
* Accept one cookie
* @param {*} key
*/
function simplyAcceptOneCookie(key) {
localStorage.setItem(`rgpd-acceptance_${key}_consent`, true)
_setAcceptanceDate()
_reload()
}
function simplyReadOneCookie(key, mandatory) {
return localStorage.getItem(
`rgpd-acceptance_${key}_${mandatory ? 'mandatory' : 'consent'}`
)
}

export {
acceptAllCookies,
acceptNoCookies,
Expand All @@ -213,4 +234,6 @@ export {
resetAllAcceptanceByDate,
resetAll,
hasValidatedAcceptance,
simplyAcceptOneCookie,
simplyReadOneCookie,
}

0 comments on commit 98b6afe

Please sign in to comment.