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
3 changed files with 243 additions and 303 deletions

View File

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

View File

@@ -1,8 +1,8 @@
import React, { Fragment } from 'react'; import React, { Fragment } from 'react';
import { import {
HashRouter as Router,
Redirect, Redirect,
Switch, Switch,
withRouter
} from 'react-router-dom'; } from 'react-router-dom';
import { import {
@@ -53,31 +53,58 @@ import Teams from './pages/Teams';
import Templates from './pages/Templates'; import Templates from './pages/Templates';
import Users from './pages/Users'; 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 { class App extends React.Component {
constructor (props) { constructor (props) {
super(props); super(props);
const isNavOpen = typeof window !== 'undefined' && window.innerWidth >= parseInt(breakpointMd.value, 10); const isNavOpen = typeof window !== 'undefined' && window.innerWidth >= parseInt(breakpointMd.value, 10);
this.state = { this.state = {
isNavOpen, isNavOpen
activeGroup: 'views_group',
activeItem: 'views_group_dashboard'
}; };
} }
onNavSelect = result => {
this.setState({
activeItem: result.itemId,
activeGroup: result.groupId
});
};
onNavToggle = () => { onNavToggle = () => {
this.setState(({ isNavOpen }) => ({ isNavOpen: !isNavOpen })); this.setState(({ isNavOpen }) => ({ isNavOpen: !isNavOpen }));
}; };
onLogoClick = () => { onLogoClick = () => {
this.setState({ activeGroup: 'views_group', activeItem: 'views_group_dashboard' }); this.setState({ activeGroup: 'views_group' });
} }
onDevLogout = async () => { onDevLogout = async () => {
@@ -86,8 +113,8 @@ class App extends React.Component {
} }
render () { render () {
const { activeItem, activeGroup, isNavOpen } = this.state; const { isNavOpen } = this.state;
const { logo, loginInfo } = this.props; const { logo, loginInfo, history } = this.props;
const PageToolbar = ( const PageToolbar = (
<Toolbar> <Toolbar>
@@ -103,7 +130,6 @@ class App extends React.Component {
); );
return ( return (
<Router>
<Fragment> <Fragment>
<BackgroundImage <BackgroundImage
src={{ src={{
@@ -134,211 +160,131 @@ class App extends React.Component {
<PageSidebar <PageSidebar
isNavOpen={isNavOpen} isNavOpen={isNavOpen}
nav={( nav={(
<Nav onSelect={this.onNavSelect} aria-label="Primary Navigation"> <Nav aria-label="Primary Navigation">
<NavList> <NavList>
<NavExpandable <SideNavItems
title="Views" history={history}
groupId="views_group" items={[
isActive={activeGroup === 'views_group'} {
isExpanded={activeGroup === 'views_group'} groupName: 'views',
> title: 'Views',
<NavItem routes: [
to="#/home" {
groupId="views_group" path: 'home',
itemId="views_group_dashboard" title: 'Dashboard'
isActive={activeItem === 'views_group_dashboard'} },
> {
Dashboard path: 'jobs',
</NavItem> title: 'Jobs'
<NavItem },
to="#/jobs" {
groupId="views_group" path: 'schedules',
itemId="views_group_jobs" title: 'Schedules'
isActive={activeItem === 'views_group_jobs'} },
> {
Jobs path: 'portal',
</NavItem> title: 'Portal Mode'
<NavItem },
to="#/schedules" ]
groupId="views_group" },
itemId="views_group_schedules" {
isActive={activeItem === 'views_group_schedules'} groupName: 'resources',
> title: 'Resources',
Schedules routes: [
</NavItem> {
<NavItem path: 'templates',
to="#/portal" title: 'Templates'
groupId="views_group" },
itemId="views_group_portal" {
isActive={activeItem === 'views_group_portal'} path: 'credentials',
> title: 'Credentials'
My View },
</NavItem> {
</NavExpandable> path: 'projects',
<NavExpandable title: 'Projects'
title="Resources" },
groupId="resources_group" {
isActive={activeGroup === 'resources_group'} path: 'inventories',
isExpanded={activeGroup === 'resources_group'} title: 'Inventories'
> },
<NavItem {
to="#/templates" path: 'inventory_scripts',
groupId="resources_group" title: 'Inventory Scripts'
itemId="resources_group_templates" }
isActive={activeItem === 'resources_group_templates'} ]
> },
Templates {
</NavItem> groupName: 'access',
<NavItem title: 'Access',
to="#/credentials" routes: [
groupId="resources_group" {
itemId="resources_group_credentials" path: 'organizations',
isActive={activeItem === 'resources_group_credentials'} title: 'Organizations'
> },
Credentials {
</NavItem> path: 'users',
<NavItem title: 'Users'
to="#/projects" },
groupId="resources_group" {
itemId="resources_group_projects" path: 'teams',
isActive={activeItem === 'resources_group_projects'} title: 'Teams'
> }
Projects ]
</NavItem> },
<NavItem {
to="#/inventories" groupName: 'administration',
groupId="resources_group" title: 'Administration',
itemId="resources_group_inventories" routes: [
isActive={activeItem === 'resources_group_inventories'} {
> path: 'credential_types',
Inventories title: 'Credential Types',
</NavItem> },
<NavItem {
to="#/inventory_scripts" path: 'notification_templates',
groupId="resources_group" title: 'Notifications'
itemId="resources_group_inventory_scripts" },
isActive={activeItem === 'resources_group_inventory_scripts'} {
> path: 'management_jobs',
Inventory Scripts title: 'Management Jobs'
</NavItem> },
</NavExpandable> {
<NavExpandable path: 'instance_groups',
title="Access" title: 'Instance Groups'
groupId="access_group" },
isActive={activeGroup === 'access_group'} {
isExpanded={activeGroup === 'access_group'} path: 'applications',
> title: 'Integrations'
<NavItem }
to="#/organizations" ]
groupId="access_group" },
itemId="access_group_organizations" {
isActive={activeItem === 'access_group_organizations'} groupName: 'settings',
> title: 'Settings',
Organizations routes: [
</NavItem> {
<NavItem path: 'auth_settings',
to="#/users" title: 'Authentication',
groupId="access_group" },
itemId="access_group_users" {
isActive={activeItem === 'access_group_users'} path: 'jobs_settings',
> title: 'Jobs'
Users },
</NavItem> {
<NavItem path: 'system_settings',
to="#/teams" title: 'System'
groupId="access_group" },
itemId="access_group_teams" {
isActive={activeItem === 'access_group_teams'} path: 'ui_settings',
> title: 'User Interface'
Teams },
</NavItem> {
</NavExpandable> path: 'license',
<NavExpandable title: 'License'
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> </NavList>
</Nav> </Nav>
)} )}
@@ -373,9 +319,8 @@ class App extends React.Component {
</Fragment> </Fragment>
</Switch> </Switch>
</Fragment> </Fragment>
</Router>
); );
} }
} }
export default App; export default withRouter(App);

View File

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