2019-11-25 01:21:08 +07:00
|
|
|
import React, { useState, useContext } from 'react';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-11-25 01:21:08 +07:00
|
|
|
import storage from '../../utils/storage';
|
2019-02-03 17:23:33 +07:00
|
|
|
import { getRegistryURL } from '../../utils/url';
|
2019-11-25 01:21:08 +07:00
|
|
|
import { makeLogin } from '../../utils/login';
|
2019-10-06 22:17:36 +07:00
|
|
|
import Button from '../../muiComponents/Button';
|
2019-11-25 01:21:08 +07:00
|
|
|
import AppContext from '../../App/AppContext';
|
|
|
|
import LoginModal from '../Login';
|
|
|
|
import Search from '../Search';
|
2019-10-03 23:17:04 +07:00
|
|
|
|
2019-10-08 12:47:11 +07:00
|
|
|
import { NavBar, InnerNavBar, MobileNavBar, InnerMobileNavBar } from './styles';
|
|
|
|
import HeaderLeft from './HeaderLeft';
|
|
|
|
import HeaderRight from './HeaderRight';
|
|
|
|
import HeaderInfoDialog from './HeaderInfoDialog';
|
2019-02-03 17:23:33 +07:00
|
|
|
|
2019-06-20 19:37:28 +07:00
|
|
|
interface Props {
|
|
|
|
withoutSearch?: boolean;
|
|
|
|
}
|
|
|
|
|
2019-10-08 12:47:11 +07:00
|
|
|
/* eslint-disable react/jsx-max-depth */
|
|
|
|
/* eslint-disable react/jsx-no-bind*/
|
2019-11-25 01:21:08 +07:00
|
|
|
const Header: React.FC<Props> = ({ withoutSearch }) => {
|
|
|
|
const appContext = useContext(AppContext);
|
2019-10-08 12:47:11 +07:00
|
|
|
const [isInfoDialogOpen, setOpenInfoDialog] = useState();
|
|
|
|
const [showMobileNavBar, setShowMobileNavBar] = useState();
|
2019-11-25 01:21:08 +07:00
|
|
|
const [showLoginModal, setShowLoginModal] = useState(false);
|
|
|
|
|
|
|
|
if (!appContext) {
|
|
|
|
throw Error('The app Context was not correct used');
|
|
|
|
}
|
|
|
|
|
|
|
|
const { user, scope, error, setUser, setError } = appContext;
|
|
|
|
const logo = window.VERDACCIO_LOGO;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* handles login
|
|
|
|
* Required by: <Header />
|
|
|
|
*/
|
|
|
|
const handleDoLogin = async (usernameValue: string, passwordValue: string) => {
|
|
|
|
const { username, token, error } = await makeLogin(usernameValue, passwordValue);
|
|
|
|
|
|
|
|
if (username && token) {
|
|
|
|
storage.setItem('username', username);
|
|
|
|
storage.setItem('token', token);
|
|
|
|
setUser({ username });
|
|
|
|
setShowLoginModal(false);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (error) {
|
|
|
|
setUser(undefined);
|
|
|
|
setError(error);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Logouts user
|
|
|
|
* Required by: <Header />
|
|
|
|
*/
|
|
|
|
const handleLogout = () => {
|
|
|
|
storage.removeItem('username');
|
|
|
|
storage.removeItem('token');
|
|
|
|
setUser(undefined);
|
|
|
|
};
|
2019-10-08 12:47:11 +07:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2019-11-25 01:21:08 +07:00
|
|
|
<NavBar data-testid="header" position="static">
|
2019-10-08 12:47:11 +07:00
|
|
|
<InnerNavBar>
|
|
|
|
<HeaderLeft logo={logo} />
|
|
|
|
<HeaderRight
|
2019-11-25 01:21:08 +07:00
|
|
|
onLogout={handleLogout}
|
2019-10-08 12:47:11 +07:00
|
|
|
onOpenRegistryInfoDialog={() => setOpenInfoDialog(true)}
|
2019-11-25 01:21:08 +07:00
|
|
|
onToggleLogin={() => setShowLoginModal(!showLoginModal)}
|
2019-10-08 12:47:11 +07:00
|
|
|
onToggleMobileNav={() => setShowMobileNavBar(!showMobileNavBar)}
|
2019-11-25 01:21:08 +07:00
|
|
|
username={user && user.username}
|
2019-10-08 12:47:11 +07:00
|
|
|
withoutSearch={withoutSearch}
|
|
|
|
/>
|
|
|
|
</InnerNavBar>
|
2019-10-27 21:49:30 +07:00
|
|
|
<HeaderInfoDialog
|
|
|
|
isOpen={isInfoDialogOpen}
|
|
|
|
onCloseDialog={() => setOpenInfoDialog(false)}
|
|
|
|
registryUrl={getRegistryURL()}
|
|
|
|
scope={scope}
|
|
|
|
/>
|
2019-10-08 12:47:11 +07:00
|
|
|
</NavBar>
|
|
|
|
{showMobileNavBar && !withoutSearch && (
|
|
|
|
<MobileNavBar>
|
|
|
|
<InnerMobileNavBar>
|
2019-02-03 17:23:33 +07:00
|
|
|
<Search />
|
2019-10-08 12:47:11 +07:00
|
|
|
</InnerMobileNavBar>
|
2019-10-31 14:12:18 +07:00
|
|
|
<Button color="inherit" onClick={() => setShowMobileNavBar(false)}>
|
|
|
|
{'Cancel'}
|
|
|
|
</Button>
|
2019-10-08 12:47:11 +07:00
|
|
|
</MobileNavBar>
|
|
|
|
)}
|
2019-11-25 01:21:08 +07:00
|
|
|
<LoginModal
|
|
|
|
error={error}
|
|
|
|
onCancel={() => setShowLoginModal(false)}
|
|
|
|
onSubmit={handleDoLogin}
|
|
|
|
visibility={showLoginModal}
|
|
|
|
/>
|
2019-10-08 12:47:11 +07:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
};
|
2019-02-03 17:23:33 +07:00
|
|
|
|
|
|
|
export default Header;
|