-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating cookie consent component (#1127)
**Related Ticket:**[ Cookie Consent Form](https://github.com/orgs/NASA-IMPACT/projects/17/views/6?pane=issue&itemId=62543604) ### Description of Changes USWDS Alert component used as a cookie consent form. This form should render on any page of the site if a user has not consented to the use of cookies. If a user _Accepts Cookies_ or _Declines Cookies_ a cookie will be made to catalog the response by the user. The cookie will expire after 3 months and the form will re-render for the user. ### Notes & Questions About Changes Figma Designs: https://www.figma.com/design/xaZSp74DKFGYm0k2w2BbVb/Shared-VEDA-file?node-id=0-1&t=wGNpe3xrUyLTU1pB-1 CookieConsent form is stored behind a feature flag to allow for each instance to opt in to leveraging the component. To turn the CookieConsentForm **ON** in the `veda-ui/.env` set `COOKIE_CONSENT_FORM='TRUE'` To change contents of the consent form change the contents of `cookieConsentForm` within the `veda-ui/mock/veda.config.js` to reflect desired content. To add link in the content, it must be added in the following format `[Link text](URL)` **Example prop:** 'We use cookies to enhance your browsing experience and to help us understand how our website is used. These cookies allow us to collect data on site usage and improve our services based on your interactions. To learn more about it, see our `[Privacy Policy](https://www.nasa.gov/privacy/#cookies)`' ### Validation / Testing Run unit test CookieConsent.spec.js. To test in browser, run locally navigate to browser cookies you should see the following values for specific interactions: - **Decline Cookie:** {"responded":true,"answer":false} - **Accept Cookie:** {"responded":true,"answer":true} - **Close out:** {"responded":false,"answer":false}
- Loading branch information
Showing
16 changed files
with
302 additions
and
25 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
63 changes: 63 additions & 0 deletions
63
app/scripts/components/common/cookie-consent/cookieConsent.spec.js
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,63 @@ | ||
import React from 'react'; | ||
import '@testing-library/jest-dom'; | ||
|
||
import { render, screen, fireEvent } from '@testing-library/react'; | ||
import { COOKIE_CONSENT_KEY } from './utils'; | ||
import { CookieConsent } from './index'; | ||
|
||
describe('Cookie consent form should render with correct content.', () => { | ||
const cookieData = { | ||
title: 'Cookie Consent', | ||
copy: '<p>We use cookies to enhance your browsing experience and to help us understand how our website is used. These cookies allow us to collect data on site usage and improve our services based on your interactions. To learn more about it, see our <a href="https://www.nasa.gov/privacy/#cookies">Privacy Policy</a></p>We use cookies to enhance your browsing experience and to help us understand how our website is used. These cookies allow us to collect data on site usage and improve our services based on your interactions. To learn more about it, see our [Privacy Policy](https://www.nasa.gov/privacy/#cookies)' | ||
}; | ||
const onFormInteraction = jest.fn(); | ||
beforeEach(() => { | ||
render( | ||
<CookieConsent {...cookieData} onFormInteraction={onFormInteraction} /> | ||
); | ||
}); | ||
it('Renders correct content', () => { | ||
expect( | ||
screen.getByRole('link', { name: 'Privacy Policy' }) | ||
).toHaveAttribute('href', 'https://www.nasa.gov/privacy/#cookies'); | ||
expect( | ||
screen.getByRole('button', { name: 'Decline Cookies' }) | ||
).toBeInTheDocument(); | ||
expect( | ||
screen.getByRole('button', { name: 'Accept Cookies' }) | ||
).toBeInTheDocument(); | ||
expect(screen.getByText('Cookie Consent')).toBeInTheDocument(); | ||
expect( | ||
screen.getByText( | ||
'We use cookies to enhance your browsing experience and to help us understand how our website is used. These cookies allow us to collect data on site usage and improve our services based on your interactions. To learn more about it, see our' | ||
) | ||
).toBeInTheDocument(); | ||
}); | ||
|
||
it('Check correct cookie initialization', () => { | ||
const resultCookie = document.cookie; | ||
|
||
expect(resultCookie).toBe( | ||
`${COOKIE_CONSENT_KEY}={"responded":false,"answer":false}` | ||
); | ||
}); | ||
|
||
it('Check correct cookie content on Decline click', () => { | ||
const button = screen.getByRole('button', { name: 'Decline Cookies' }); | ||
fireEvent.click(button); | ||
const resultCookie = document.cookie; | ||
expect(resultCookie).toBe( | ||
`${COOKIE_CONSENT_KEY}={"responded":true,"answer":false}` | ||
); | ||
}); | ||
|
||
it('Check correct cookie content on Accept click', () => { | ||
const button = screen.getByRole('button', { name: 'Accept Cookies' }); | ||
fireEvent.click(button); | ||
const resultCookie = document.cookie; | ||
|
||
expect(resultCookie).toBe( | ||
`${COOKIE_CONSENT_KEY}={"responded":true,"answer":true}` | ||
); | ||
}); | ||
}); |
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,3 @@ | ||
.animation--fade-out { | ||
transition: opacity 0.5s ease-in-out 0.125s; | ||
} |
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,108 @@ | ||
import React, { useState, useEffect } from 'react'; | ||
import { Icon } from '@trussworks/react-uswds'; | ||
import { COOKIE_CONSENT_KEY } from './utils'; | ||
import { | ||
USWDSAlert, | ||
USWDSButton, | ||
USWDSButtonGroup | ||
} from '$components/common/uswds'; | ||
|
||
import './index.scss'; | ||
|
||
interface CookieConsentProps { | ||
title?: string | undefined; | ||
copy?: string | undefined; | ||
onFormInteraction: () => void; | ||
} | ||
|
||
function addAttribute (copy) { | ||
return copy.replaceAll('<a', `<a target="_blank" rel="noopener" role="link"`); | ||
} | ||
|
||
export const CookieConsent = ({ | ||
title, | ||
copy, | ||
onFormInteraction | ||
}: CookieConsentProps) => { | ||
const [cookieConsentResponded, SetCookieConsentResponded] = | ||
useState<boolean>(false); | ||
const [cookieConsentAnswer, SetCookieConsentAnswer] = | ||
useState<boolean>(false); | ||
const [closeConsent, setCloseConsent] = useState<boolean>(false); | ||
//Setting expiration date for cookie to expire and re-ask user for consent. | ||
const setCookieExpiration = () => { | ||
const today = new Date(); | ||
today.setMonth(today.getMonth() + 3); | ||
return today.toUTCString(); | ||
}; | ||
|
||
const setCookie = (cookieValue, closeConsent) => { | ||
document.cookie = `${COOKIE_CONSENT_KEY}=${JSON.stringify( | ||
cookieValue | ||
)}; path=/; expires=${closeConsent ? '0' : setCookieExpiration()}`; | ||
}; | ||
|
||
useEffect(() => { | ||
const cookieValue = { | ||
responded: cookieConsentResponded, | ||
answer: cookieConsentAnswer | ||
}; | ||
setCookie(cookieValue, closeConsent); | ||
onFormInteraction(); | ||
// Ignoring setcookie for now sine it will make infinite rendering | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [cookieConsentResponded, cookieConsentAnswer, closeConsent, onFormInteraction]); | ||
|
||
return ( | ||
<div | ||
id='cookie-consent' | ||
className={`margin-0 tablet:margin-2 shadow-2 position-fixed z-top maxw-full tablet:maxw-tablet-lg animation--fade-out right-0 bottom-0 ${ | ||
cookieConsentResponded || closeConsent ? ' opacity-0' : 'opacity-1' | ||
}`} | ||
> | ||
<USWDSAlert | ||
type='info' | ||
heading={title && title} | ||
headingLevel='h2' | ||
noIcon={true} | ||
className='radius-lg' | ||
> | ||
<USWDSButton | ||
type='button ' | ||
className='width-3 height-3 padding-0 position-absolute right-2 top-2' | ||
onClick={() => { | ||
setCloseConsent(true); | ||
}} | ||
unstyled | ||
> | ||
<Icon.Close size={3} /> | ||
</USWDSButton> | ||
|
||
{copy && ( | ||
<div dangerouslySetInnerHTML={{ __html: addAttribute(copy) }} /> | ||
)} | ||
<USWDSButtonGroup className='padding-top-2'> | ||
<USWDSButton | ||
onClick={() => { | ||
SetCookieConsentResponded(true); | ||
SetCookieConsentAnswer(false); | ||
}} | ||
outline={true} | ||
type='button' | ||
> | ||
Decline Cookies | ||
</USWDSButton> | ||
<USWDSButton | ||
onClick={() => { | ||
SetCookieConsentResponded(true); | ||
SetCookieConsentAnswer(true); | ||
}} | ||
type='button' | ||
> | ||
Accept Cookies | ||
</USWDSButton> | ||
</USWDSButtonGroup> | ||
</USWDSAlert> | ||
</div> | ||
); | ||
}; |
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,2 @@ | ||
export const NO_COOKIE = 'NO COOKIE'; | ||
export const COOKIE_CONSENT_KEY = `veda--CookieConsent`; |
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,6 @@ | ||
import React from "react"; | ||
import { Alert } from "@trussworks/react-uswds"; | ||
|
||
export function USWDSAlert (props) { | ||
return <Alert {...props} />; | ||
} |
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,9 @@ | ||
import React from "react"; | ||
import { Button, ButtonGroup} from "@trussworks/react-uswds"; | ||
|
||
export function USWDSButton (props) { | ||
return <Button {...props} />; | ||
} | ||
export function USWDSButtonGroup (props) { | ||
return <ButtonGroup {...props} />; | ||
} |
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,4 @@ | ||
export { USWDSAlert } from './alert'; | ||
export { USWDSButtonGroup, USWDSButton } from './button'; | ||
export { USWDSLink } from './link'; | ||
export { USWDSBanner, USWDSBannerContent } from './banner'; |
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,6 @@ | ||
import React from "react"; | ||
import { Link } from "@trussworks/react-uswds"; | ||
|
||
export function USWDSLink (props) { | ||
return <Link {...props} />; | ||
} |
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
Oops, something went wrong.