Updating Header to use hooks

This commit is contained in:
Jeff Avallone 2019-03-28 06:35:00 -04:00
parent 1621e0e16a
commit 14c1e14f51
3 changed files with 58 additions and 68 deletions

View File

@ -1,8 +1,8 @@
import React from 'react'; import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import Modal from 'react-modal'; import Modal from 'react-modal';
import { Link } from 'gatsby'; import { Link } from 'gatsby';
import { withTranslation, Trans } from 'react-i18next'; import { Trans } from 'react-i18next';
import GitlabIcon from 'react-feather/dist/icons/gitlab'; import GitlabIcon from 'react-feather/dist/icons/gitlab';
@ -12,40 +12,25 @@ import PrivacyPolicy from 'components/PrivacyPolicy';
import style from './style.module.css'; import style from './style.module.css';
class Header extends React.PureComponent { const Header = ({ banner }) => {
state = { const [ showModal, updateShowModal] = useState(false);
showModal: false const handleClose = useCallback(() => {
} updateShowModal(false);
}, [updateShowModal]);
static propTypes = { const handleOpen = useCallback(event => {
banner: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string
]).isRequired
}
handleOpen = event => {
if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) { if (event.shiftKey || event.ctrlKey || event.altKey || event.metaKey) {
return; return;
} }
event.preventDefault(); event.preventDefault();
this.setState({ showModal: true }); updateShowModal(true);
} }, [updateShowModal]);
handleClose = () => {
this.setState({ showModal: false });
}
render() {
const { banner } = this.props;
const { showModal } = this.state;
return <> return <>
<Modal <Modal
isOpen={ showModal } isOpen={ showModal }
onRequestClose={ this.handleClose }> onRequestClose={ handleClose }>
<PrivacyPolicy onClose={ this.handleClose } /> <PrivacyPolicy onClose={ handleClose } />
</Modal> </Modal>
<header <header
className={ style.header } className={ style.header }
@ -66,7 +51,7 @@ class Header extends React.PureComponent {
<li> <li>
<Link to="/privacy" <Link to="/privacy"
data-testid="privacy-link" data-testid="privacy-link"
onClick={ this.handleOpen } onClick={ handleOpen }
> >
<Trans>Privacy Policy</Trans> <Trans>Privacy Policy</Trans>
</Link> </Link>
@ -80,8 +65,13 @@ class Header extends React.PureComponent {
</ul> </ul>
</header> </header>
</>; </>;
} };
}
export { Header }; Header.propTypes = {
export default withTranslation()(Header); banner: PropTypes.oneOfType([
PropTypes.bool,
PropTypes.string
]).isRequired
};
export default Header;

View File

@ -12,7 +12,7 @@ jest.mock('components/PrivacyPolicy', () =>
import React from 'react'; import React from 'react';
import { render, fireEvent } from 'react-testing-library'; import { render, fireEvent } from 'react-testing-library';
import { Header } from 'components/Header'; import Header from 'components/Header';
describe('Header', () => { describe('Header', () => {
test('rendering', () => { test('rendering', () => {

View File

@ -8,7 +8,7 @@ exports[`Layout rendering 1`] = `
> >
<noscript /> <noscript />
<span <span
data-component="withI18nextTranslation(Header)" data-component="Header"
data-props="{ data-props="{
\\"banner\\": \\"Test Banner\\" \\"banner\\": \\"Test Banner\\"
}" }"