Merge pull request #40 from marshmalien/side-nav-bug

Update active nav item based on url
This commit is contained in:
Marliana Lara 2018-12-05 10:32:47 -05:00 committed by GitHub
commit e736cfab36
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 243 additions and 303 deletions

View File

@ -1,4 +1,5 @@
import React from 'react';
import { HashRouter as Router } from 'react-router-dom';
import { shallow, mount } from 'enzyme';
import App from '../src/App';
import api from '../src/api';
@ -21,7 +22,7 @@ describe('<App />', () => {
api.isAuthenticated = jest.fn();
api.isAuthenticated.mockReturnValue(false);
const appWrapper = mount(<App />);
const appWrapper = mount(<Router><App /></Router>);
const login = appWrapper.find(Login);
expect(login.length).toBe(1);
@ -33,7 +34,7 @@ describe('<App />', () => {
api.isAuthenticated = jest.fn();
api.isAuthenticated.mockReturnValue(true);
const appWrapper = mount(<App />);
const appWrapper = mount(<Router><App /></Router>);
const dashboard = appWrapper.find(Dashboard);
expect(dashboard.length).toBe(1);
@ -41,35 +42,26 @@ describe('<App />', () => {
expect(login.length).toBe(0);
});
test('onNavSelect sets state.activeItem and state.activeGroup', () => {
const appWrapper = shallow(<App />);
appWrapper.instance().onNavSelect({ itemId: 'foo', groupId: 'bar' });
expect(appWrapper.state().activeItem).toBe('foo');
expect(appWrapper.state().activeGroup).toBe('bar');
});
test('onNavToggle sets state.isNavOpen to opposite', () => {
const appWrapper = shallow(<App />);
const appWrapper = shallow(<App.WrappedComponent />);
expect(appWrapper.state().isNavOpen).toBe(true);
appWrapper.instance().onNavToggle();
expect(appWrapper.state().isNavOpen).toBe(false);
});
test('onLogoClick sets selected nav back to defaults', () => {
const appWrapper = shallow(<App />);
const appWrapper = shallow(<App.WrappedComponent />);
appWrapper.setState({ activeGroup: 'foo', activeItem: 'bar' });
expect(appWrapper.state().activeItem).toBe('bar');
expect(appWrapper.state().activeGroup).toBe('foo');
appWrapper.instance().onLogoClick();
expect(appWrapper.state().activeItem).toBe(DEFAULT_ACTIVE_ITEM);
expect(appWrapper.state().activeGroup).toBe(DEFAULT_ACTIVE_GROUP);
});
test('api.logout called from logout button', async () => {
api.get = jest.fn().mockImplementation(() => Promise.resolve({}));
const appWrapper = mount(<App />);
const logoutButton = appWrapper.find('LogoutButton');
logoutButton.props().onDevLogout();
const appWrapper = shallow(<App.WrappedComponent />);
appWrapper.instance().onDevLogout();
appWrapper.setState({ activeGroup: 'foo', activeItem: 'bar' });
expect(api.get).toHaveBeenCalledTimes(1);
expect(api.get).toHaveBeenCalledWith(API_LOGOUT);

View File

@ -1,8 +1,8 @@
import React, { Fragment } from 'react';
import {
HashRouter as Router,
Redirect,
Switch,
withRouter
} from 'react-router-dom';
import {
@ -53,31 +53,58 @@ import Teams from './pages/Teams';
import Templates from './pages/Templates';
import Users from './pages/Users';
const SideNavItems = ({ items, history }) => {
const currentPath = history.location.pathname.split('/')[1];
let activeGroup;
if (currentPath !== '') {
[{ groupName: activeGroup }] = items
.map(({ groupName, routes }) => ({
groupName,
paths: routes.map(({ path }) => path)
}))
.filter(({ paths }) => paths.indexOf(currentPath) > -1);
} else {
activeGroup = 'views';
}
return (items.map(({ title, groupName, routes }) => (
<NavExpandable
key={groupName}
title={title}
groupId={`${groupName}_group`}
isActive={`${activeGroup}_group` === `${groupName}_group`}
isExpanded={`${activeGroup}_group` === `${groupName}_group`}
>
{routes.map(({ path, title: itemTitle }) => (
<NavItem
key={path}
to={`#/${path}`}
groupId={`${groupName}_group`}
isActive={currentPath === path}
>
{itemTitle}
</NavItem>
))}
</NavExpandable>
)));
};
class App extends React.Component {
constructor (props) {
super(props);
const isNavOpen = typeof window !== 'undefined' && window.innerWidth >= parseInt(breakpointMd.value, 10);
this.state = {
isNavOpen,
activeGroup: 'views_group',
activeItem: 'views_group_dashboard'
isNavOpen
};
}
onNavSelect = result => {
this.setState({
activeItem: result.itemId,
activeGroup: result.groupId
});
};
onNavToggle = () => {
this.setState(({ isNavOpen }) => ({ isNavOpen: !isNavOpen }));
};
onLogoClick = () => {
this.setState({ activeGroup: 'views_group', activeItem: 'views_group_dashboard' });
this.setState({ activeGroup: 'views_group' });
}
onDevLogout = async () => {
@ -86,8 +113,8 @@ class App extends React.Component {
}
render () {
const { activeItem, activeGroup, isNavOpen } = this.state;
const { logo, loginInfo } = this.props;
const { isNavOpen } = this.state;
const { logo, loginInfo, history } = this.props;
const PageToolbar = (
<Toolbar>
@ -103,279 +130,197 @@ class App extends React.Component {
);
return (
<Router>
<Fragment>
<BackgroundImage
src={{
[BackgroundImageSrc.lg]: '/assets/images/pfbg_1200.jpg',
[BackgroundImageSrc.md]: '/assets/images/pfbg_992.jpg',
[BackgroundImageSrc.md2x]: '/assets/images/pfbg_992@2x.jpg',
[BackgroundImageSrc.sm]: '/assets/images/pfbg_768.jpg',
[BackgroundImageSrc.sm2x]: '/assets/images/pfbg_768@2x.jpg',
[BackgroundImageSrc.xl]: '/assets/images/pfbg_2000.jpg',
[BackgroundImageSrc.xs]: '/assets/images/pfbg_576.jpg',
[BackgroundImageSrc.xs2x]: '/assets/images/pfbg_576@2x.jpg',
[BackgroundImageSrc.filter]: '/assets/images/background-filter.svg'
}}
/>
<Switch>
<ConditionalRedirect shouldRedirect={() => api.isAuthenticated()} redirectPath="/" path="/login" component={() => <Login logo={logo} loginInfo={loginInfo} />} />
<Fragment>
<Page
header={(
<PageHeader
logo={<TowerLogo onClick={this.onLogoClick} />}
toolbar={PageToolbar}
showNavToggle
onNavToggle={this.onNavToggle}
/>
)}
sidebar={(
<PageSidebar
isNavOpen={isNavOpen}
nav={(
<Nav onSelect={this.onNavSelect} aria-label="Primary Navigation">
<NavList>
<NavExpandable
title="Views"
groupId="views_group"
isActive={activeGroup === 'views_group'}
isExpanded={activeGroup === 'views_group'}
>
<NavItem
to="#/home"
groupId="views_group"
itemId="views_group_dashboard"
isActive={activeItem === 'views_group_dashboard'}
>
Dashboard
</NavItem>
<NavItem
to="#/jobs"
groupId="views_group"
itemId="views_group_jobs"
isActive={activeItem === 'views_group_jobs'}
>
Jobs
</NavItem>
<NavItem
to="#/schedules"
groupId="views_group"
itemId="views_group_schedules"
isActive={activeItem === 'views_group_schedules'}
>
Schedules
</NavItem>
<NavItem
to="#/portal"
groupId="views_group"
itemId="views_group_portal"
isActive={activeItem === 'views_group_portal'}
>
My View
</NavItem>
</NavExpandable>
<NavExpandable
title="Resources"
groupId="resources_group"
isActive={activeGroup === 'resources_group'}
isExpanded={activeGroup === 'resources_group'}
>
<NavItem
to="#/templates"
groupId="resources_group"
itemId="resources_group_templates"
isActive={activeItem === 'resources_group_templates'}
>
Templates
</NavItem>
<NavItem
to="#/credentials"
groupId="resources_group"
itemId="resources_group_credentials"
isActive={activeItem === 'resources_group_credentials'}
>
Credentials
</NavItem>
<NavItem
to="#/projects"
groupId="resources_group"
itemId="resources_group_projects"
isActive={activeItem === 'resources_group_projects'}
>
Projects
</NavItem>
<NavItem
to="#/inventories"
groupId="resources_group"
itemId="resources_group_inventories"
isActive={activeItem === 'resources_group_inventories'}
>
Inventories
</NavItem>
<NavItem
to="#/inventory_scripts"
groupId="resources_group"
itemId="resources_group_inventory_scripts"
isActive={activeItem === 'resources_group_inventory_scripts'}
>
Inventory Scripts
</NavItem>
</NavExpandable>
<NavExpandable
title="Access"
groupId="access_group"
isActive={activeGroup === 'access_group'}
isExpanded={activeGroup === 'access_group'}
>
<NavItem
to="#/organizations"
groupId="access_group"
itemId="access_group_organizations"
isActive={activeItem === 'access_group_organizations'}
>
Organizations
</NavItem>
<NavItem
to="#/users"
groupId="access_group"
itemId="access_group_users"
isActive={activeItem === 'access_group_users'}
>
Users
</NavItem>
<NavItem
to="#/teams"
groupId="access_group"
itemId="access_group_teams"
isActive={activeItem === 'access_group_teams'}
>
Teams
</NavItem>
</NavExpandable>
<NavExpandable
title="Administration"
groupId="administration_group"
isActive={activeGroup === 'administration_group'}
isExpanded={activeGroup === 'administration_group'}
>
<NavItem
to="#/credential_types"
groupId="administration_group"
itemId="administration_group_credential_types"
isActive={activeItem === 'administration_group_credential_types'}
>
Credential Types
</NavItem>
<NavItem
to="#/notification_templates"
groupId="administration_group"
itemId="administration_group_notification_templates"
isActive={activeItem === 'administration_group_notification_templates'}
>
Notification Templates
</NavItem>
<NavItem
to="#/management_jobs"
groupId="administration_group"
itemId="administration_group_management_jobs"
isActive={activeItem === 'administration_group_management_jobs'}
>
Management Jobs
</NavItem>
<NavItem
to="#/instance_groups"
groupId="administration_group"
itemId="administration_group_instance_groups"
isActive={activeItem === 'administration_group_instance_groups'}
>
Instance Groups
</NavItem>
<NavItem
to="#/applications"
groupId="administration_group"
itemId="administration_group_applications"
isActive={activeItem === 'administration_group_applications'}
>
Applications
</NavItem>
</NavExpandable>
<NavExpandable
title="Settings"
groupId="settings_group"
isActive={activeGroup === 'settings_group'}
isExpanded={activeGroup === 'settings_group'}
>
<NavItem
to="#/auth_settings"
groupId="settings_group"
itemId="settings_group_auth"
isActive={activeItem === 'settings_group_auth'}
>
Authentication
</NavItem>
<NavItem
to="#/jobs_settings"
groupId="settings_group"
itemId="settings_group_jobs"
isActive={activeItem === 'settings_group_jobs'}
>
Jobs
</NavItem>
<NavItem
to="#/system_settings"
groupId="settings_group"
itemId="settings_group_system"
isActive={activeItem === 'settings_group_system'}
>
System
</NavItem>
<NavItem
to="#/ui_settings"
groupId="settings_group"
itemId="settings_group_ui"
isActive={activeItem === 'settings_group_ui'}
>
User Interface
</NavItem>
</NavExpandable>
</NavList>
</Nav>
)}
/>
)}
useCondensed
>
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" exact path="/" component={() => (<Redirect to="/home" />)} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/home" component={Dashboard} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/jobs" component={Jobs} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/schedules" component={Schedules} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/portal" component={Portal} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/templates" component={Templates} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/credentials" component={Credentials} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/projects" component={Projects} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/inventories" component={Inventories} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/inventory_scripts" component={InventoryScripts} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/organizations" component={Organizations} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/users" component={Users} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/teams" component={Teams} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/credential_types" component={CredentialTypes} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/notification_templates" component={NotificationTemplates} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/management_jobs" component={ManagementJobs} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/instance_groups" component={InstanceGroups} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/applications" component={Applications} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/auth_settings" component={AuthSettings} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/jobs_settings" component={JobsSettings} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/system_settings" component={SystemSettings} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/ui_settings" component={UISettings} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/license" component={License} />
</Page>
</Fragment>
</Switch>
</Fragment>
</Router>
<Fragment>
<BackgroundImage
src={{
[BackgroundImageSrc.lg]: '/assets/images/pfbg_1200.jpg',
[BackgroundImageSrc.md]: '/assets/images/pfbg_992.jpg',
[BackgroundImageSrc.md2x]: '/assets/images/pfbg_992@2x.jpg',
[BackgroundImageSrc.sm]: '/assets/images/pfbg_768.jpg',
[BackgroundImageSrc.sm2x]: '/assets/images/pfbg_768@2x.jpg',
[BackgroundImageSrc.xl]: '/assets/images/pfbg_2000.jpg',
[BackgroundImageSrc.xs]: '/assets/images/pfbg_576.jpg',
[BackgroundImageSrc.xs2x]: '/assets/images/pfbg_576@2x.jpg',
[BackgroundImageSrc.filter]: '/assets/images/background-filter.svg'
}}
/>
<Switch>
<ConditionalRedirect shouldRedirect={() => api.isAuthenticated()} redirectPath="/" path="/login" component={() => <Login logo={logo} loginInfo={loginInfo} />} />
<Fragment>
<Page
header={(
<PageHeader
logo={<TowerLogo onClick={this.onLogoClick} />}
toolbar={PageToolbar}
showNavToggle
onNavToggle={this.onNavToggle}
/>
)}
sidebar={(
<PageSidebar
isNavOpen={isNavOpen}
nav={(
<Nav aria-label="Primary Navigation">
<NavList>
<SideNavItems
history={history}
items={[
{
groupName: 'views',
title: 'Views',
routes: [
{
path: 'home',
title: 'Dashboard'
},
{
path: 'jobs',
title: 'Jobs'
},
{
path: 'schedules',
title: 'Schedules'
},
{
path: 'portal',
title: 'Portal Mode'
},
]
},
{
groupName: 'resources',
title: 'Resources',
routes: [
{
path: 'templates',
title: 'Templates'
},
{
path: 'credentials',
title: 'Credentials'
},
{
path: 'projects',
title: 'Projects'
},
{
path: 'inventories',
title: 'Inventories'
},
{
path: 'inventory_scripts',
title: 'Inventory Scripts'
}
]
},
{
groupName: 'access',
title: 'Access',
routes: [
{
path: 'organizations',
title: 'Organizations'
},
{
path: 'users',
title: 'Users'
},
{
path: 'teams',
title: 'Teams'
}
]
},
{
groupName: 'administration',
title: 'Administration',
routes: [
{
path: 'credential_types',
title: 'Credential Types',
},
{
path: 'notification_templates',
title: 'Notifications'
},
{
path: 'management_jobs',
title: 'Management Jobs'
},
{
path: 'instance_groups',
title: 'Instance Groups'
},
{
path: 'applications',
title: 'Integrations'
}
]
},
{
groupName: 'settings',
title: 'Settings',
routes: [
{
path: 'auth_settings',
title: 'Authentication',
},
{
path: 'jobs_settings',
title: 'Jobs'
},
{
path: 'system_settings',
title: 'System'
},
{
path: 'ui_settings',
title: 'User Interface'
},
{
path: 'license',
title: 'License'
}
]
}
]}
/>
</NavList>
</Nav>
)}
/>
)}
useCondensed
>
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" exact path="/" component={() => (<Redirect to="/home" />)} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/home" component={Dashboard} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/jobs" component={Jobs} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/schedules" component={Schedules} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/portal" component={Portal} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/templates" component={Templates} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/credentials" component={Credentials} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/projects" component={Projects} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/inventories" component={Inventories} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/inventory_scripts" component={InventoryScripts} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/organizations" component={Organizations} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/users" component={Users} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/teams" component={Teams} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/credential_types" component={CredentialTypes} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/notification_templates" component={NotificationTemplates} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/management_jobs" component={ManagementJobs} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/instance_groups" component={InstanceGroups} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/applications" component={Applications} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/auth_settings" component={AuthSettings} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/jobs_settings" component={JobsSettings} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/system_settings" component={SystemSettings} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/ui_settings" component={UISettings} />
<ConditionalRedirect shouldRedirect={() => !api.isAuthenticated()} redirectPath="/login" path="/license" component={License} />
</Page>
</Fragment>
</Switch>
</Fragment>
);
}
}
export default App;
export default withRouter(App);

View File

@ -1,6 +1,9 @@
import React from 'react';
import { render } from 'react-dom';
import {
HashRouter as Router
} from 'react-router-dom';
import App from './App';
import api from './api';
import { API_ROOT } from './endpoints';
@ -15,8 +18,8 @@ import './components/DataListToolbar/styles.scss';
const el = document.getElementById('app');
const main = async () => {
const { custom_logo, custom_login_info } = await api.get(API_ROOT);
render(<App logo={custom_logo} loginInfo={custom_login_info} />, el);
const { custom_logo, custom_login_info } = await api.get(API_ROOT);
render(<Router><App logo={custom_logo} loginInfo={custom_login_info} /></Router>, el);
};
main();