2020-09-21 15:27:22 +00:00
|
|
|
import React, { useEffect } from "react";
|
|
|
|
import PropTypes from "prop-types";
|
2020-09-30 22:25:08 +00:00
|
|
|
import { Backdrop, Toast, Aside } from "asc-web-components";
|
2020-09-21 15:27:22 +00:00
|
|
|
import Header from "./sub-components/header";
|
|
|
|
import Nav from "./sub-components/nav";
|
|
|
|
import HeaderNav from "./sub-components/header-nav";
|
|
|
|
import NavLogoItem from "./sub-components/nav-logo-item";
|
|
|
|
import NavItem from "./sub-components/nav-item";
|
2020-09-30 22:25:08 +00:00
|
|
|
import HeaderUnAuth from "./sub-components/header-unauth";
|
2020-09-21 15:27:22 +00:00
|
|
|
|
2020-09-30 22:25:08 +00:00
|
|
|
import { I18nextProvider, withTranslation } from "react-i18next";
|
2020-09-21 15:27:22 +00:00
|
|
|
import i18n from "./i18n";
|
|
|
|
import { connect } from "react-redux";
|
|
|
|
|
|
|
|
import { withRouter } from "react-router";
|
|
|
|
import { logout } from "../../store/auth/actions";
|
2020-09-30 22:25:08 +00:00
|
|
|
import {
|
|
|
|
getCurrentUser,
|
|
|
|
getLanguage,
|
|
|
|
getIsolateModules,
|
|
|
|
getMainModules,
|
|
|
|
getCurrentProduct
|
|
|
|
} from "../../store/auth/selectors";
|
2020-09-21 15:27:22 +00:00
|
|
|
|
|
|
|
class NavMenu extends React.Component {
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.timeout = null;
|
|
|
|
|
2020-09-30 22:25:08 +00:00
|
|
|
const {
|
|
|
|
isBackdropVisible,
|
|
|
|
isNavHoverEnabled,
|
|
|
|
isNavOpened,
|
|
|
|
isAsideVisible
|
|
|
|
} = props;
|
|
|
|
|
|
|
|
this.state = {
|
|
|
|
isBackdropVisible,
|
|
|
|
isNavOpened,
|
|
|
|
isAsideVisible,
|
|
|
|
isNavHoverEnabled
|
2020-09-21 15:27:22 +00:00
|
|
|
};
|
2020-09-30 22:25:08 +00:00
|
|
|
}
|
2020-09-21 15:27:22 +00:00
|
|
|
|
2020-09-30 22:25:08 +00:00
|
|
|
getCurrentUserActions = () => {
|
|
|
|
const { t } = this.props;
|
2020-09-28 08:26:21 +00:00
|
|
|
|
2020-09-30 22:25:08 +00:00
|
|
|
const currentUserActions = [
|
|
|
|
{
|
2020-09-28 08:26:21 +00:00
|
|
|
key: "ProfileBtn",
|
|
|
|
label: t("Profile"),
|
|
|
|
onClick: this.onProfileClick,
|
|
|
|
url: "/products/people/view/@self"
|
2020-09-30 22:25:08 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "AboutBtn",
|
|
|
|
label: t("AboutCompanyTitle"),
|
|
|
|
onClick: this.onAboutClick,
|
|
|
|
url: "/about"
|
|
|
|
},
|
|
|
|
{
|
|
|
|
key: "LogoutBtn",
|
|
|
|
label: t("LogoutButton"),
|
|
|
|
onClick: this.onLogoutClick
|
|
|
|
}
|
|
|
|
];
|
2020-09-28 08:26:21 +00:00
|
|
|
|
2020-09-30 22:25:08 +00:00
|
|
|
return currentUserActions;
|
2020-09-21 15:27:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
backdropClick = () => {
|
|
|
|
this.setState({
|
|
|
|
isBackdropVisible: false,
|
|
|
|
isNavOpened: false,
|
|
|
|
isAsideVisible: false,
|
|
|
|
isNavHoverEnabled: !this.state.isNavHoverEnabled
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
showNav = () => {
|
|
|
|
this.setState({
|
|
|
|
isBackdropVisible: true,
|
|
|
|
isNavOpened: true,
|
|
|
|
isAsideVisible: false,
|
|
|
|
isNavHoverEnabled: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
clearNavTimeout = () => {
|
|
|
|
if (this.timeout == null) return;
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.timeout = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
handleNavMouseEnter = () => {
|
|
|
|
if (!this.state.isNavHoverEnabled) return;
|
|
|
|
this.timeout = setTimeout(() => {
|
|
|
|
this.setState({
|
|
|
|
isBackdropVisible: false,
|
|
|
|
isNavOpened: true,
|
|
|
|
isAsideVisible: false
|
|
|
|
});
|
|
|
|
}, 1000);
|
|
|
|
};
|
|
|
|
|
|
|
|
handleNavMouseLeave = () => {
|
|
|
|
if (!this.state.isNavHoverEnabled) return;
|
|
|
|
this.clearNavTimeout();
|
|
|
|
this.setState({
|
|
|
|
isBackdropVisible: false,
|
|
|
|
isNavOpened: false,
|
|
|
|
isAsideVisible: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
toggleAside = () => {
|
|
|
|
this.clearNavTimeout();
|
|
|
|
this.setState({
|
|
|
|
isBackdropVisible: true,
|
|
|
|
isNavOpened: false,
|
|
|
|
isAsideVisible: true,
|
|
|
|
isNavHoverEnabled: false
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2020-09-28 08:26:21 +00:00
|
|
|
onProfileClick = () => {
|
2020-09-30 22:25:08 +00:00
|
|
|
const { history, homepage } = this.props;
|
|
|
|
if (homepage == "/products/people") {
|
2020-09-28 08:26:21 +00:00
|
|
|
history.push("/products/people/view/@self");
|
|
|
|
} else {
|
|
|
|
window.open("/products/people/view/@self", "_self");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
onAboutClick = () => {
|
|
|
|
window.open("/about", "_self");
|
|
|
|
};
|
|
|
|
|
|
|
|
onLogoutClick = () => {
|
|
|
|
this.props.logout();
|
|
|
|
};
|
|
|
|
|
2020-09-21 15:27:22 +00:00
|
|
|
render() {
|
2020-09-30 22:25:08 +00:00
|
|
|
const { isBackdropVisible, isNavOpened, isAsideVisible } = this.state;
|
|
|
|
|
|
|
|
const {
|
|
|
|
isAuthenticated,
|
|
|
|
isLoaded,
|
|
|
|
isolateModules,
|
|
|
|
mainModules,
|
|
|
|
currentUser,
|
|
|
|
asideContent,
|
|
|
|
currentProduct
|
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
const isBackdropAvailable = mainModules.length > 0 || !!asideContent;
|
|
|
|
const isNavAvailable = mainModules.length > 0;
|
|
|
|
const isAsideAvailable = !!asideContent;
|
|
|
|
|
|
|
|
console.log("NavMenu render", this.state, this.props);
|
2020-09-21 15:27:22 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
2020-09-28 08:26:21 +00:00
|
|
|
<Toast />
|
2020-09-30 22:25:08 +00:00
|
|
|
{isBackdropAvailable && (
|
|
|
|
<Backdrop visible={isBackdropVisible} onClick={this.backdropClick} />
|
2020-09-21 15:27:22 +00:00
|
|
|
)}
|
|
|
|
|
2020-09-30 22:25:08 +00:00
|
|
|
{isAuthenticated ? (
|
|
|
|
<>
|
|
|
|
<HeaderNav
|
|
|
|
modules={isolateModules}
|
|
|
|
user={currentUser}
|
|
|
|
userActions={this.getCurrentUserActions()}
|
|
|
|
/>
|
|
|
|
<Header onClick={this.showNav} />
|
|
|
|
</>
|
|
|
|
) : (
|
|
|
|
isLoaded && <HeaderUnAuth />
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isNavAvailable && (
|
|
|
|
<Nav
|
|
|
|
opened={isNavOpened}
|
|
|
|
onMouseEnter={this.handleNavMouseEnter}
|
|
|
|
onMouseLeave={this.handleNavMouseLeave}
|
2020-09-21 15:27:22 +00:00
|
|
|
>
|
2020-09-30 22:25:08 +00:00
|
|
|
<NavLogoItem opened={isNavOpened} onClick={this.onLogoClick} />
|
|
|
|
{mainModules.map(item => (
|
|
|
|
<NavItem
|
|
|
|
separator={!!item.separator}
|
|
|
|
key={item.id}
|
|
|
|
opened={isNavOpened}
|
|
|
|
active={item.id == currentProduct.id}
|
|
|
|
iconName={item.iconName}
|
|
|
|
iconUrl={item.iconUrl}
|
|
|
|
badgeNumber={item.notifications}
|
|
|
|
onClick={item.onClick}
|
|
|
|
onBadgeClick={e => {
|
|
|
|
this.toggleAside();
|
|
|
|
item.onBadgeClick(e);
|
|
|
|
}}
|
|
|
|
url={item.url}
|
|
|
|
>
|
|
|
|
{item.title}
|
|
|
|
</NavItem>
|
|
|
|
))}
|
|
|
|
</Nav>
|
|
|
|
)}
|
|
|
|
|
|
|
|
{isAsideAvailable && (
|
|
|
|
<Aside visible={isAsideVisible} onClick={this.backdropClick}>
|
|
|
|
{asideContent}
|
2020-09-21 15:27:22 +00:00
|
|
|
</Aside>
|
2020-09-30 22:25:08 +00:00
|
|
|
)}
|
2020-09-21 15:27:22 +00:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NavMenu.propTypes = {
|
|
|
|
isBackdropVisible: PropTypes.bool,
|
|
|
|
isNavHoverEnabled: PropTypes.bool,
|
|
|
|
isNavOpened: PropTypes.bool,
|
|
|
|
isAsideVisible: PropTypes.bool,
|
|
|
|
|
|
|
|
asideContent: PropTypes.oneOfType([
|
|
|
|
PropTypes.arrayOf(PropTypes.node),
|
|
|
|
PropTypes.node
|
|
|
|
]),
|
|
|
|
|
|
|
|
currentUser: PropTypes.object,
|
2020-09-30 22:25:08 +00:00
|
|
|
homepage: PropTypes.string,
|
|
|
|
isAuthenticated: PropTypes.bool,
|
|
|
|
isLoaded: PropTypes.bool,
|
|
|
|
isolateModules: PropTypes.array,
|
|
|
|
mainModules: PropTypes.array,
|
|
|
|
currentProduct: PropTypes.object,
|
|
|
|
|
|
|
|
history: PropTypes.object,
|
|
|
|
t: PropTypes.func,
|
|
|
|
logout: PropTypes.func
|
2020-09-21 15:27:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
NavMenu.defaultProps = {
|
|
|
|
isBackdropVisible: false,
|
|
|
|
isNavHoverEnabled: true,
|
|
|
|
isNavOpened: false,
|
|
|
|
isAsideVisible: false,
|
|
|
|
|
2020-09-30 22:25:08 +00:00
|
|
|
currentUser: null
|
2020-09-21 15:27:22 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const NavMenuTranslationWrapper = withTranslation()(NavMenu);
|
|
|
|
|
|
|
|
const NavMenuWrapper = props => {
|
|
|
|
const { language } = props;
|
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
i18n.changeLanguage(language);
|
|
|
|
}, [language]);
|
|
|
|
|
2020-09-30 22:25:08 +00:00
|
|
|
return (
|
|
|
|
<I18nextProvider i18n={i18n}>
|
|
|
|
<NavMenuTranslationWrapper {...props} />
|
|
|
|
</I18nextProvider>
|
|
|
|
);
|
2020-09-28 08:26:21 +00:00
|
|
|
};
|
2020-09-21 15:27:22 +00:00
|
|
|
|
|
|
|
NavMenuWrapper.propTypes = {
|
|
|
|
language: PropTypes.string.isRequired
|
|
|
|
};
|
|
|
|
|
|
|
|
function mapStateToProps(state) {
|
2020-09-30 22:25:08 +00:00
|
|
|
const { isAuthenticated, isLoaded, settings } = state.auth;
|
|
|
|
const { defaultPage, homepage } = settings;
|
2020-09-21 15:27:22 +00:00
|
|
|
|
|
|
|
return {
|
2020-09-30 22:25:08 +00:00
|
|
|
isAuthenticated,
|
|
|
|
isLoaded,
|
|
|
|
homepage,
|
2020-09-21 15:27:22 +00:00
|
|
|
defaultPage: defaultPage || "/",
|
2020-09-30 22:25:08 +00:00
|
|
|
currentUser: getCurrentUser(state),
|
|
|
|
currentProduct: getCurrentProduct(state),
|
|
|
|
isolateModules: getIsolateModules(state),
|
|
|
|
mainModules: getMainModules(state),
|
|
|
|
language: getLanguage(state)
|
2020-09-21 15:27:22 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2020-09-25 16:14:50 +00:00
|
|
|
export default connect(mapStateToProps, { logout })(withRouter(NavMenuWrapper));
|