Merge pull request #6090 from marshmalien/6085-hostListItem-remove-duplicates

HostListItem - Remove duplicate action items

Reviewed-by: https://github.com/apps/softwarefactory-project-zuul
This commit is contained in:
softwarefactory-project-zuul[bot]
2020-02-26 22:39:43 +00:00
committed by GitHub
3 changed files with 136 additions and 164 deletions

View File

@@ -27,23 +27,13 @@ const DataListAction = styled(_DataListAction)`
grid-template-columns: min-content 40px; grid-template-columns: min-content 40px;
`; `;
class HostListItem extends React.Component { function HostListItem({ i18n, host, isSelected, onSelect, detailUrl }) {
static propTypes = { const labelId = `check-action-${host.id}`;
host: Host.isRequired,
detailUrl: string.isRequired,
isSelected: bool.isRequired,
onSelect: func.isRequired,
};
render() {
const { host, isSelected, onSelect, detailUrl, i18n } = this.props;
const recentPlaybookJobs = host.summary_fields.recent_jobs.map(job => ({ const recentPlaybookJobs = host.summary_fields.recent_jobs.map(job => ({
...job, ...job,
type: 'job', type: 'job',
})); }));
const labelId = `check-action-${host.id}`;
return ( return (
<DataListItem key={host.id} aria-labelledby={labelId} id={`${host.id}`}> <DataListItem key={host.id} aria-labelledby={labelId} id={`${host.id}`}>
<DataListItemRow> <DataListItemRow>
@@ -79,22 +69,6 @@ class HostListItem extends React.Component {
</Fragment> </Fragment>
)} )}
</DataListCell>, </DataListCell>,
<DataListCell key="enable" alignRight isFilled={false}>
<HostToggle host={host} />
</DataListCell>,
<DataListCell key="edit" alignRight isFilled={false}>
{host.summary_fields.user_capabilities.edit && (
<Tooltip content={i18n._(t`Edit Host`)} position="top">
<Button
variant="plain"
component={Link}
to={`/hosts/${host.id}/edit`}
>
<PencilAltIcon />
</Button>
</Tooltip>
)}
</DataListCell>,
]} ]}
/> />
<DataListAction <DataListAction
@@ -119,5 +93,12 @@ class HostListItem extends React.Component {
</DataListItem> </DataListItem>
); );
} }
}
HostListItem.propTypes = {
host: Host.isRequired,
detailUrl: string.isRequired,
isSelected: bool.isRequired,
onSelect: func.isRequired,
};
export default withI18n()(HostListItem); export default withI18n()(HostListItem);

View File

@@ -3,7 +3,7 @@ import { mountWithContexts } from '@testUtils/enzymeHelpers';
import HostsListItem from './HostListItem'; import HostsListItem from './HostListItem';
let onToggleHost; const onToggleHost = jest.fn();
const mockHost = { const mockHost = {
id: 1, id: 1,
@@ -23,16 +23,10 @@ const mockHost = {
}; };
describe('<HostsListItem />', () => { describe('<HostsListItem />', () => {
let wrapper;
beforeEach(() => { beforeEach(() => {
onToggleHost = jest.fn(); wrapper = mountWithContexts(
});
afterEach(() => {
jest.clearAllMocks();
});
test('edit button shown to users with edit capabilities', () => {
const wrapper = mountWithContexts(
<HostsListItem <HostsListItem
isSelected={false} isSelected={false}
detailUrl="/host/1" detailUrl="/host/1"
@@ -41,13 +35,20 @@ describe('<HostsListItem />', () => {
onToggleHost={onToggleHost} onToggleHost={onToggleHost}
/> />
); );
});
afterEach(() => {
jest.clearAllMocks();
});
test('edit button shown to users with edit capabilities', () => {
expect(wrapper.find('PencilAltIcon').exists()).toBeTruthy(); expect(wrapper.find('PencilAltIcon').exists()).toBeTruthy();
}); });
test('edit button hidden from users without edit capabilities', () => { test('edit button hidden from users without edit capabilities', () => {
const copyMockHost = Object.assign({}, mockHost); const copyMockHost = Object.assign({}, mockHost);
copyMockHost.summary_fields.user_capabilities.edit = false; copyMockHost.summary_fields.user_capabilities.edit = false;
const wrapper = mountWithContexts( wrapper = mountWithContexts(
<HostsListItem <HostsListItem
isSelected={false} isSelected={false}
detailUrl="/host/1" detailUrl="/host/1"
@@ -58,4 +59,8 @@ describe('<HostsListItem />', () => {
); );
expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy(); expect(wrapper.find('PencilAltIcon').exists()).toBeFalsy();
}); });
test('should display host toggle', () => {
expect(wrapper.find('HostToggle').length).toBe(1);
});
}); });

View File

@@ -1,5 +1,5 @@
import React, { Component, Fragment } from 'react'; import React, { useState, useCallback } from 'react';
import { Route, withRouter, Switch } from 'react-router-dom'; import { Route, Switch } from 'react-router-dom';
import { withI18n } from '@lingui/react'; import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
@@ -11,28 +11,18 @@ import HostList from './HostList';
import HostAdd from './HostAdd'; import HostAdd from './HostAdd';
import Host from './Host'; import Host from './Host';
class Hosts extends Component { function Hosts({ i18n }) {
constructor(props) { const [breadcrumbConfig, setBreadcrumbConfig] = useState({
super(props);
const { i18n } = props;
this.state = {
breadcrumbConfig: {
'/hosts': i18n._(t`Hosts`), '/hosts': i18n._(t`Hosts`),
'/hosts/add': i18n._(t`Create New Host`), '/hosts/add': i18n._(t`Create New Host`),
}, });
};
}
setBreadcrumbConfig = host => {
const { i18n } = this.props;
const buildBreadcrumbConfig = useCallback(
host => {
if (!host) { if (!host) {
return; return;
} }
setBreadcrumbConfig({
const breadcrumbConfig = {
'/hosts': i18n._(t`Hosts`), '/hosts': i18n._(t`Hosts`),
'/hosts/add': i18n._(t`Create New Host`), '/hosts/add': i18n._(t`Create New Host`),
[`/hosts/${host.id}`]: `${host.name}`, [`/hosts/${host.id}`]: `${host.name}`,
@@ -41,40 +31,36 @@ class Hosts extends Component {
[`/hosts/${host.id}/facts`]: i18n._(t`Facts`), [`/hosts/${host.id}/facts`]: i18n._(t`Facts`),
[`/hosts/${host.id}/groups`]: i18n._(t`Groups`), [`/hosts/${host.id}/groups`]: i18n._(t`Groups`),
[`/hosts/${host.id}/completed_jobs`]: i18n._(t`Completed Jobs`), [`/hosts/${host.id}/completed_jobs`]: i18n._(t`Completed Jobs`),
}; });
},
this.setState({ breadcrumbConfig }); [i18n]
}; );
render() {
const { match } = this.props;
const { breadcrumbConfig } = this.state;
return ( return (
<Fragment> <>
<Breadcrumbs breadcrumbConfig={breadcrumbConfig} /> <Breadcrumbs breadcrumbConfig={breadcrumbConfig} />
<PageSection> <PageSection>
<Card> <Card>
<Switch> <Switch>
<Route path={`${match.path}/add`} render={() => <HostAdd />} /> <Route path="/hosts/add">
<Route path={`${match.path}/:id`}> <HostAdd />
</Route>
<Route path="/hosts/:id">
<Config> <Config>
{({ me }) => ( {({ me }) => (
<Host <Host setBreadcrumb={buildBreadcrumbConfig} me={me || {}} />
setBreadcrumb={this.setBreadcrumbConfig}
me={me || {}}
/>
)} )}
</Config> </Config>
</Route> </Route>
<Route path={`${match.path}`} render={() => <HostList />} /> <Route path="/hosts">
<HostList />
</Route>
</Switch> </Switch>
</Card> </Card>
</PageSection> </PageSection>
</Fragment> </>
); );
} }
}
export { Hosts as _Hosts }; export { Hosts as _Hosts };
export default withI18n()(withRouter(Hosts)); export default withI18n()(Hosts);