Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GN-117 Remove need to assert on dom #148

Merged
Merged
Show file tree
Hide file tree
Changes from 33 commits
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
b2958bf
context experiments
johndavey72 Nov 11, 2021
26594c1
basic context setup
johndavey72 Nov 12, 2021
3fe0e51
tidy up debugging
johndavey72 Nov 12, 2021
20a1974
uncommen event listener for expected closing behaviour
johndavey72 Nov 12, 2021
d03b29d
make debug clearer
johndavey72 Nov 12, 2021
8930967
clean up console logging for clarity
johndavey72 Nov 12, 2021
0a35a82
move globalnavcontext provider up to header component
johndavey72 Nov 12, 2021
1da7119
GN-117 add useCallback to clickOutside hook to maintain reference to …
johndavey72 Nov 12, 2021
eed5e6e
GN-117 Refactor Nav component to class. Add custom hook and context f…
johndavey72 Nov 12, 2021
4d61eb6
GN-117 Begin conversion of Account class component to functional comp…
johndavey72 Nov 16, 2021
f748f72
GN-117 Remove commented out lines
johndavey72 Nov 16, 2021
21e7714
GN-117 Reinstate propTypes for Naccount placeholder component
johndavey72 Nov 16, 2021
356e5df
Side effect sideline (#150)
wa-rren-dev Nov 17, 2021
e898e7f
Tests needed
wa-rren-dev Nov 17, 2021
2081a88
GN-117 Tidy up redundant arguments and commented out logic for useCli…
johndavey72 Nov 18, 2021
1a29976
GN-117 Change directory structure for hooks and react context. Make u…
johndavey72 Nov 18, 2021
f105916
GN-117 Import GlobalNavContextProvider from new location for Account …
johndavey72 Nov 18, 2021
2ff0d29
GN-117 Fixing broken unit tests
johndavey72 Nov 18, 2021
5d23e1e
GN-117 Fixing broken tests
johndavey72 Nov 18, 2021
993c43c
GN-117 Fixing broken tests
johndavey72 Nov 18, 2021
6cc73d3
Replace simulate click
wa-rren-dev Nov 18, 2021
be3d88c
GN-117 Revert some tests to shallow rendering after adding defaults t…
johndavey72 Nov 19, 2021
edab694
GN-117 Revert to shallow mounting for a previously failing unit test
johndavey72 Nov 19, 2021
eb1edec
GN-117 Convert unit test from shallow to mount due to issues around e…
johndavey72 Nov 19, 2021
d15dfa5
GN-117 Rename GlobalNavContext to HeaderContext
johndavey72 Nov 19, 2021
c9d6650
GN-117 remove unused dependencies from account test
johndavey72 Nov 19, 2021
5ac4075
GN-117 Tidy up unit tests
johndavey72 Nov 19, 2021
adf3196
Merge branch 'GN-115-Mega-nav-production-ready' into GN-117-Remove-ne…
johndavey72 Nov 19, 2021
ebc92be
testing custom hook useClickOutside
johndavey72 Nov 19, 2021
6b6e3cd
basic setup
johndavey72 Nov 19, 2021
1375de4
Custom hook test
wa-rren-dev Nov 19, 2021
79fe740
Update test
wa-rren-dev Nov 19, 2021
58ba03d
Rename test
wa-rren-dev Nov 19, 2021
a7af813
Merge branch 'GN-115-Mega-nav-production-ready' into GN-117-Remove-ne…
wa-rren-dev Nov 22, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 6 additions & 6 deletions src/Footer/Services/__snapshots__/Services.test.jsx.snap
Original file line number Diff line number Diff line change
Expand Up @@ -155,31 +155,31 @@ exports[`Services Matches snapshot 1`] = `
</TrackedLink>
</li>
<li
key="about-us"
key="about"
>
<TrackedLink
aria-current={null}
className="link"
eventAction="Footer click"
eventCategory="TopHat and footer"
eventLabel="About us"
eventLabel="About"
href="https://www.nice.org.uk/about"
>
About us
About
</TrackedLink>
</li>
<li
key="more-nice-services"
key="more"
>
<TrackedLink
aria-current={null}
className="link"
eventAction="Footer click"
eventCategory="TopHat and footer"
eventLabel="More NICE services"
eventLabel="More"
href={null}
>
More NICE services
More
</TrackedLink>
</li>
</ul>
Expand Down
245 changes: 111 additions & 134 deletions src/Header/Account/Account.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import React, { Component } from "react";
import React, {
useContext,
useEffect,
useState,
useCallback,
useRef,
} from "react";
import { HeaderContext } from "../context/HeaderContext";
import PropTypes from "prop-types";
import classnames from "classnames";

Expand All @@ -13,63 +20,42 @@ import {
headerClickEventAction,
} from "../../tracker";

const escapeKeyCode = 27;
function Account(props) {
// We've left this is state as per the pre-hook implementation
const [doesUseIdAM] = useState(props.provider == Account.providers.idam);

export default class Account extends Component {
constructor(props) {
super(props);
const { accountMenuIsExpanded, setAccountMenuIsExpanded } =
useContext(HeaderContext);

this.state = {
isExpanded: false,
useIdAM: this.props.provider == Account.providers.idam,
};
const keypress = useRef(),
myAccountButton = useRef(),
myAccountMenu = useRef();

this.handleMyAccountButtonClick =
this.handleMyAccountButtonClick.bind(this);
this.handleKeyDown = this.handleKeyDown.bind(this);
this.handleMenuItemClick = this.handleMenuItemClick.bind(this);
this.handleMegaMenuClick = this.handleMegaMenuClick.bind(this);
}
const handleMyAccountButtonClick = useCallback((e) => {
keypress.current = e.pageX;
setAccountMenuIsExpanded((prevState) => !prevState);
}, []);

handleMyAccountButtonClick(e) {
const isKeyboardEvent = !e.pageX;
this.setState(
function (prevState) {
return { isExpanded: !prevState.isExpanded };
},
function () {
if (this.state.isExpanded && isKeyboardEvent) {
const accountMenu = document.getElementById("my-account");
accountMenu.setAttribute("tabIndex", -1);
accountMenu.focus();
}
}.bind(this)
);
function focusAccount() {
myAccountMenu && myAccountMenu.current.setAttribute("tabIndex", -1);
myAccountMenu && myAccountMenu.current.focus();
}

handleKeyDown(event) {
if (event.keyCode === escapeKeyCode) {
event.preventDefault();
this.setState({
isExpanded: false,
});
document.getElementById("my-account-button").focus();
useEffect(() => {
if (!keypress.current && accountMenuIsExpanded) {
focusAccount();
}
}
}, [accountMenuIsExpanded]);

// NOTE: We would benefit from managing the state higher up
handleMegaMenuClick(event) {
let megaMenu = document.querySelector("#header-menu");

if (megaMenu.contains(event.target)) {
this.setState({
isExpanded: false,
});
megaMenu.focus();
function handleKeyDown(e) {
if (e.key === "Escape") {
e.preventDefault();
setAccountMenuIsExpanded(false);
myAccountButton && myAccountButton.current.focus();
}
}

handleMenuItemClick(e) {
function handleMenuItemClick(e) {
const href = e.currentTarget.getAttribute("href");

let eventLabel;
Expand Down Expand Up @@ -98,20 +84,20 @@ export default class Account extends Component {
}
}

componentDidMount() {
useEffect(() => {
const consultationsResponsesLink = {
"Consultation responses": "https://www.nice.org.uk/consultations/",
};

if (this.state.useIdAM) {
if (doesUseIdAM) {
//nice accounts supplies links like: {"John Holland":"https://accounts.nice.org.uk/users/143980/editprofile","Sign out":"https://accounts.nice.org.uk/signout"}
//idam supplies links like:[{ key: "My profile", value: "/Account/todo" },{ key: "Sign out", value: "/Account/Logout" }]
//the following just converts the idam format to the nice accounts format.

const { displayName } = this.props;
const { displayName } = props;
const isLoggedIn = !!displayName;

let links = this.props.links.reduce(function (links, link) {
let links = props.links.reduce(function (links, link) {
links[link.text] = link.url;
return links;
}, {});
Expand All @@ -125,99 +111,90 @@ export default class Account extends Component {
links: links,
};

if (this.props.onLoginStatusChecked) {
this.props.onLoginStatusChecked(convertedData);
if (props.onLoginStatusChecked) {
props.onLoginStatusChecked(convertedData);
}
} else {
//NICE accounts
niceAccountsLoggedIn(this.props.environment)
.then(
function (data) {
if (this.props.onLoginStatusChecked) {
data.links = { ...consultationsResponsesLink, ...data.links };

this.props.onLoginStatusChecked(data);
}
}.bind(this)
)
.catch(
function (e) {
console.warn("Couldn't load account data from NICE accounts", e);
}.bind(this)
);
niceAccountsLoggedIn(props.environment)
.then(function (data) {
if (props.onLoginStatusChecked) {
data.links = { ...consultationsResponsesLink, ...data.links };
props.onLoginStatusChecked(data);
}
})
.catch(function (e) {
console.warn("Couldn't load account data from NICE accounts", e);
});
}
}, []);

document.addEventListener("click", this.handleMegaMenuClick);
}

render() {
const { accountsData, environment } = this.props;
const { accountsData, environment } = props;

let signInLink = {};
if (this.state.useIdAM) {
signInLink = this.props.links[0];
} else {
signInLink["text"] = "Sign in";
signInLink["url"] = getDomainBaseUrl(environment) + "signin";
}
let signInLink = {};
if (doesUseIdAM) {
signInLink = props.links[0];
} else {
signInLink["text"] = "Sign in";
signInLink["url"] = getDomainBaseUrl(environment) + "signin";
}

return this.props.isLoggedIn ? (
<div className={styles.account}>
<button
className={classnames(styles.button, styles.myAccount)}
id="my-account-button"
aria-controls="my-account"
aria-haspopup="menu"
aria-expanded={this.state.isExpanded}
onClick={this.handleMyAccountButtonClick}
onKeyDown={this.handleKeyDown}
>
My account
</button>
<ul
className={styles.menu}
id="my-account"
role="menu"
aria-hidden={!this.state.isExpanded}
aria-labelledby="my-account-button"
onKeyDown={this.handleKeyDown}
>
{accountsData.links &&
Object.keys(accountsData.links).map(
function (text, i) {
return (
<li key={i} role="presentation">
<a
href={accountsData.links[text]}
role="menuitem"
onClick={this.handleMenuItemClick}
onKeyDown={this.handleKeyDown}
data-hj-suppress={
accountsData.links[text].indexOf("profile") > -1
? ""
: null
}
>
{text}
</a>
</li>
);
}.bind(this)
)}
</ul>
</div>
) : (
<a
href={signInLink.url}
className={styles.button}
onClick={this.handleMenuItemClick}
return props.isLoggedIn ? (
<div className={styles.account}>
<button
className={classnames(styles.button, styles.myAccount)}
id="my-account-button"
aria-controls="my-account"
aria-haspopup="menu"
aria-expanded={accountMenuIsExpanded}
onClick={handleMyAccountButtonClick}
onKeyDown={handleKeyDown}
ref={myAccountButton}
>
{signInLink.text}
</a>
);
}
My account
</button>
<ul
className={styles.menu}
id="my-account"
role="menu"
aria-hidden={!accountMenuIsExpanded}
aria-labelledby="my-account-button"
onKeyDown={handleKeyDown}
ref={myAccountMenu}
>
{accountsData.links &&
Object.keys(accountsData.links).map(function (text, i) {
return (
<li key={i} role="presentation">
<a
href={accountsData.links[text]}
role="menuitem"
onClick={handleMenuItemClick}
onKeyDown={handleKeyDown}
data-hj-suppress={
accountsData.links[text].indexOf("profile") > -1 ? "" : null
}
>
{text}
</a>
</li>
);
})}
</ul>
</div>
) : (
<a
href={signInLink.url}
className={styles.button}
onClick={handleMenuItemClick}
>
{signInLink.text}
</a>
);
}

export default Account;

Account.providers = {
idam: "idam",
niceAccounts: "niceAccounts",
Expand Down
Loading