Skip to content

Commit

Permalink
feat: Add cookies dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
timglaser committed Jul 2, 2020
1 parent f27ca69 commit a5c1d95
Showing 1 changed file with 55 additions and 30 deletions.
85 changes: 55 additions & 30 deletions src/components/CookieApprovalDialog.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,66 @@
import React from 'react';
import React, { useEffect, useState } from 'react';
import PropTypes from 'prop-types';
import Cookies from 'js-cookie';
import Button from './Button';
import styles from './CookieApprovalDialog.module.scss';

const gdprConsentCookieName = 'newrelic-gdpr-consent';

const CookieApprovalDialog = ({ className }) => {
const [isCookieSet, setIsCookieSet] = useState(true);

useEffect(() => {
setIsCookieSet(Cookies.get(gdprConsentCookieName) !== undefined);
}, []);

function writeCookie(answer) {
const currentEnvironment =
process.env.ENV || process.env.NODE_ENV || 'development';
const options = { expires: 365 /* days */ };
if (currentEnvironment !== 'development') {
options.domain = 'newrelic.com';
}

Cookies.set(gdprConsentCookieName, String(!!answer), options);
setIsCookieSet(true);
}

return (
<div className={`${styles.container} ${className || ''}`}>
<div className={styles.content}>
<div className={styles.primaryContent}>
<h4 className={styles.heading}>
This site uses cookies{' '}
<span role="img" aria-label="Cookie emoji">
🍪
</span>
</h4>
<p className={styles.description}>
We rely on cookies to play videos, remember your preferences, and
analyze our website traffic. You consent to our cookies if you click
“OK”.
</p>
</div>
<div className={styles.ctaContainer}>
<Button
className={`button ${styles.approvalButton}`}
variant={Button.VARIANT.PRIMARY}
>
OK
</Button>
<Button
className={`${styles.ignoreButton}`}
variant={Button.VARIANT.NORMAL}
>
Reject
</Button>
!isCookieSet && (
<div className={`${styles.container} ${className || ''}`}>
<div className={styles.content}>
<div className={styles.primaryContent}>
<h4 className={styles.heading}>
This site uses cookies{' '}
<span role="img" aria-label="Cookie emoji">
🍪
</span>
</h4>
<p className={styles.description}>
We rely on cookies to play videos, remember your preferences, and
analyze our website traffic. You consent to our cookies if you
click “OK”.
</p>
</div>
<div className={styles.ctaContainer}>
<Button
className={styles.approvalButton}
variant={Button.VARIANT.PRIMARY}
onClick={() => writeCookie(true)}
>
OK
</Button>
<Button
className={`${styles.ignoreButton}`}
variant={Button.VARIANT.NORMAL}
onClick={() => writeCookie(false)}
>
Reject
</Button>
</div>
</div>
</div>
</div>
)
);
};

Expand Down

0 comments on commit a5c1d95

Please sign in to comment.