-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduces usePii hook and withPii HOC to pass vendor-specific …
…masking HTML attributes
- Loading branch information
1 parent
c878cce
commit 148934c
Showing
10 changed files
with
499 additions
and
199 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
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,95 @@ | ||
import React, { | ||
forwardRef, useContext, useEffect, useRef, useState, | ||
} from 'react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
import { AppContext, usePii, withPii } from '@edx/frontend-platform/react'; | ||
|
||
// Example via `usePii` (hook) | ||
const UsernameWithPii = forwardRef(({ children, ...rest }, ref) => { | ||
const piiProps = usePii('username', rest); | ||
return <span ref={ref} {...piiProps}>{children}</span>; | ||
}); | ||
UsernameWithPii.displayName = 'UsernameWithPii'; | ||
UsernameWithPii.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
|
||
// Example via `withPii` (HOC) | ||
const Username = forwardRef(({ children, ...rest }, ref) => ( | ||
<span ref={ref} {...rest}>{children}</span> | ||
)); | ||
Username.displayName = 'Username'; | ||
Username.propTypes = { | ||
children: PropTypes.node.isRequired, | ||
}; | ||
const UsernameWithPii2 = withPii('username')(Username); | ||
|
||
export default function PiiPage() { | ||
const { authenticatedUser, config } = useContext(AppContext); | ||
const firstUsernameRef = useRef(null); | ||
const secondUsernameRef = useRef(null); | ||
const [firstUsernameNode, setFirstUsernameNode] = useState(null); | ||
const [secondUsernameNode, setSecondUsernameNode] = useState(null); | ||
|
||
useEffect(() => { | ||
if (firstUsernameRef.current) { | ||
setFirstUsernameNode(firstUsernameRef.current.outerHTML); | ||
} | ||
if (secondUsernameRef.current) { | ||
setSecondUsernameNode(secondUsernameRef.current.outerHTML); | ||
} | ||
}, []); | ||
|
||
const { piiOptions } = config; | ||
return ( | ||
<div> | ||
<h1>Privacy options to obfuscate PII</h1> | ||
|
||
<h2>Current PII configuration</h2> | ||
{piiOptions ? ( | ||
<pre> | ||
{JSON.stringify(piiOptions, null, 2)} | ||
</pre> | ||
) : ( | ||
<p> | ||
No PII-related configuration found. Consider updating this application's <code>env.config.js</code> to | ||
configure any vendor-specific PII-related attributes/values. | ||
</p> | ||
)} | ||
|
||
<h2>Examples</h2> | ||
<p> | ||
The following examples below demonstrate using <code>usePii</code> and <code>withPii</code> to | ||
obfuscate PII (e.g., username) based on the application's configuration. Inspect the DOM | ||
elements for the rendered usernames below to observe the configured PII attributes/values. | ||
</p> | ||
|
||
{/* Example 1 (usePii) */} | ||
<h3><code>usePii</code> (hook)</h3> | ||
<p> | ||
<i>Obfuscated username:</i>{' '} | ||
<UsernameWithPii ref={firstUsernameRef}> | ||
{authenticatedUser.username} | ||
</UsernameWithPii> | ||
</p> | ||
<i>Result:</i>{' '} | ||
<pre> | ||
<code>{firstUsernameNode}</code> | ||
</pre> | ||
|
||
{/* Example 2 (withPii) */} | ||
<h3><code>withPii</code> (HOC)</h3> | ||
<p> | ||
<i>Obfuscated username:</i>{' '} | ||
<UsernameWithPii2 ref={secondUsernameRef}> | ||
{authenticatedUser.username} | ||
</UsernameWithPii2> | ||
</p> | ||
<i>Result:</i>{' '} | ||
<pre> | ||
<code>{secondUsernameNode}</code> | ||
</pre> | ||
</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
Oops, something went wrong.