Merge pull request #10056 from keithjgrant/6189-user-sublist-tables

Convert user sub-lists to tables

SUMMARY
Converts User Organizations, Teams, and Roles lists to tables
Addresses #6189
ISSUE TYPE

Feature Pull Request

COMPONENT NAME

UI

Reviewed-by: Alex Corey <Alex.swansboro@gmail.com>
Reviewed-by: Tiago Góes <tiago.goes2009@gmail.com>
This commit is contained in:
softwarefactory-project-zuul[bot]
2021-05-03 20:20:11 +00:00
committed by GitHub
12 changed files with 204 additions and 213 deletions

View File

@@ -96,7 +96,10 @@ function PaginatedTable({
Content = ( Content = (
<div css="overflow: auto"> <div css="overflow: auto">
{hasContentLoading && <LoadingSpinner />} {hasContentLoading && <LoadingSpinner />}
<TableComposable aria-label={dataListLabel} ouiaId={ouiaId}> <TableComposable
aria-label={dataListLabel}
ouiaId={ouiaId || `paginated-table-${pluralizedItemName}`}
>
{headerRow} {headerRow}
<Tbody>{items.map(renderRow)}</Tbody> <Tbody>{items.map(renderRow)}</Tbody>
</TableComposable> </TableComposable>

View File

@@ -1,9 +1,11 @@
import React, { useCallback, useEffect } from 'react'; import React, { useCallback, useEffect } from 'react';
import { useLocation, useParams } from 'react-router-dom'; import { useLocation, useParams } from 'react-router-dom';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import PaginatedDataList from '../../../components/PaginatedDataList'; import PaginatedTable, {
HeaderRow,
HeaderCell,
} from '../../../components/PaginatedTable';
import useRequest from '../../../util/useRequest'; import useRequest from '../../../util/useRequest';
import { UsersAPI } from '../../../api'; import { UsersAPI } from '../../../api';
import { getQSConfig, parseQueryString } from '../../../util/qs'; import { getQSConfig, parseQueryString } from '../../../util/qs';
@@ -16,7 +18,7 @@ const QS_CONFIG = getQSConfig('organizations', {
type: 'organization', type: 'organization',
}); });
function UserOrganizationsList() { function UserOrganizationList() {
const location = useLocation(); const location = useLocation();
const { id: userId } = useParams(); const { id: userId } = useParams();
@@ -47,14 +49,23 @@ function UserOrganizationsList() {
}, [fetchOrgs]); }, [fetchOrgs]);
return ( return (
<PaginatedDataList <PaginatedTable
items={organizations} items={organizations}
contentError={contentError} contentError={contentError}
hasContentLoading={isLoading} hasContentLoading={isLoading}
itemCount={count} itemCount={count}
pluralizedItemName={t`Organizations`} pluralizedItemName={t`Organizations`}
qsConfig={QS_CONFIG} qsConfig={QS_CONFIG}
renderItem={organization => ( toolbarSearchColumns={[
{ name: t`Name`, key: 'name__icontains', isDefault: true },
]}
headerRow={
<HeaderRow qsConfig={QS_CONFIG} isSelectable={false}>
<HeaderCell sortKey="name">{t`Name`}</HeaderCell>
<HeaderCell>{t`Description`}</HeaderCell>
</HeaderRow>
}
renderRow={(organization, index) => (
<UserOrganizationListItem <UserOrganizationListItem
key={organization.id} key={organization.id}
value={organization.name} value={organization.name}
@@ -62,10 +73,11 @@ function UserOrganizationsList() {
detailUrl={`/organizations/${organization.id}/details`} detailUrl={`/organizations/${organization.id}/details`}
onSelect={() => {}} onSelect={() => {}}
isSelected={false} isSelected={false}
rowIndex={index}
/> />
)} )}
/> />
); );
} }
export default UserOrganizationsList; export default UserOrganizationList;

View File

@@ -7,7 +7,7 @@ import {
waitForElement, waitForElement,
} from '../../../../testUtils/enzymeHelpers'; } from '../../../../testUtils/enzymeHelpers';
import UserOrganizationsList from './UserOrganizationsList'; import UserOrganizationList from './UserOrganizationList';
import { UsersAPI } from '../../../api'; import { UsersAPI } from '../../../api';
jest.mock('../../../api/models/Users'); jest.mock('../../../api/models/Users');
@@ -15,6 +15,7 @@ jest.mock('../../../api/models/Users');
describe('<UserOrganizationlist />', () => { describe('<UserOrganizationlist />', () => {
let history; let history;
let wrapper; let wrapper;
beforeEach(async () => { beforeEach(async () => {
history = createMemoryHistory({ history = createMemoryHistory({
initialEntries: ['/users/1/organizations'], initialEntries: ['/users/1/organizations'],
@@ -36,7 +37,7 @@ describe('<UserOrganizationlist />', () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<Route <Route
path="/users/:id/organizations" path="/users/:id/organizations"
component={() => <UserOrganizationsList />} component={() => <UserOrganizationList />}
/>, />,
{ {
context: { context: {
@@ -52,12 +53,15 @@ describe('<UserOrganizationlist />', () => {
); );
}); });
}); });
afterEach(() => { afterEach(() => {
jest.clearAllMocks(); jest.clearAllMocks();
}); });
test('successfully mounts', async () => { test('successfully mounts', async () => {
await waitForElement(wrapper, 'UserOrganizationListItem'); await waitForElement(wrapper, 'UserOrganizationListItem');
}); });
test('calls api to get organizations', () => { test('calls api to get organizations', () => {
expect(UsersAPI.readOrganizations).toBeCalledWith('1', { expect(UsersAPI.readOrganizations).toBeCalledWith('1', {
order_by: 'name', order_by: 'name',

View File

@@ -1,33 +1,18 @@
import React from 'react'; import React from 'react';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { import { t } from '@lingui/macro';
DataListItemCells, import { Tr, Td } from '@patternfly/react-table';
DataListItemRow,
DataListItem,
} from '@patternfly/react-core';
import DataListCell from '../../../components/DataListCell';
export default function UserOrganizationListItem({ organization }) { export default function UserOrganizationListItem({ organization }) {
const labelId = `organization-${organization.id}`; const labelId = `organization-${organization.id}`;
return ( return (
<DataListItem aria-labelledby={labelId}> <Tr id={`user-org-row-${organization.id}`}>
<DataListItemRow> <Td id={labelId} dataLabel={t`Name`}>
<DataListItemCells <Link to={`/organizations/${organization.id}/details`} id={labelId}>
dataListCells={[ {organization.name}
<DataListCell key={organization.id}> </Link>
<Link </Td>
to={`/organizations/${organization.id}/details`} <Td dataLabel={t`Description`}>{organization.description}</Td>
id={labelId} </Tr>
>
<b>{organization.name}</b>
</Link>
</DataListCell>,
<DataListCell key={organization.description}>
{organization.description}
</DataListCell>,
]}
/>
</DataListItemRow>
</DataListItem>
); );
} }

View File

@@ -9,9 +9,13 @@ describe('<UserOrganizationListItem />', () => {
let wrapper; let wrapper;
act(() => { act(() => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<UserOrganizationListItem <table>
organization={{ name: 'foo', id: 1, description: 'Bar' }} <tbody>
/> <UserOrganizationListItem
organization={{ name: 'foo', id: 1, description: 'Bar' }}
/>
</tbody>
</table>
); );
}); });
expect(wrapper.find('UserOrganizationListItem').length).toBe(1); expect(wrapper.find('UserOrganizationListItem').length).toBe(1);
@@ -20,20 +24,24 @@ describe('<UserOrganizationListItem />', () => {
let wrapper; let wrapper;
act(() => { act(() => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<UserOrganizationListItem <table>
organization={{ name: 'foo', id: 1, description: 'Bar' }} <tbody>
/> <UserOrganizationListItem
organization={{ name: 'foo', id: 1, description: 'Bar' }}
/>
</tbody>
</table>
); );
}); });
expect( expect(
wrapper wrapper
.find('DataListCell') .find('Td')
.at(0) .at(0)
.text() .text()
).toBe('foo'); ).toBe('foo');
expect( expect(
wrapper wrapper
.find('DataListCell') .find('Td')
.at(1) .at(1)
.text() .text()
).toBe('Bar'); ).toBe('Bar');

View File

@@ -1,7 +1,7 @@
import React from 'react'; import React from 'react';
import UserOrganizationsList from './UserOrganizationsList'; import UserOrganizationList from './UserOrganizationList';
function UserOrganizations() { function UserOrganizations() {
return <UserOrganizationsList />; return <UserOrganizationList />;
} }
export default UserOrganizations; export default UserOrganizations;

View File

@@ -1,6 +1,5 @@
import React, { useCallback, useEffect, useState } from 'react'; import React, { useCallback, useEffect, useState } from 'react';
import { useLocation } from 'react-router-dom'; import { useLocation } from 'react-router-dom';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { import {
Button, Button,
@@ -13,7 +12,10 @@ import { CubesIcon } from '@patternfly/react-icons';
import { getQSConfig, parseQueryString } from '../../../util/qs'; import { getQSConfig, parseQueryString } from '../../../util/qs';
import { UsersAPI, RolesAPI } from '../../../api'; import { UsersAPI, RolesAPI } from '../../../api';
import useRequest, { useDeleteItems } from '../../../util/useRequest'; import useRequest, { useDeleteItems } from '../../../util/useRequest';
import PaginatedDataList from '../../../components/PaginatedDataList'; import PaginatedTable, {
HeaderRow,
HeaderCell,
} from '../../../components/PaginatedTable';
import ErrorDetail from '../../../components/ErrorDetail'; import ErrorDetail from '../../../components/ErrorDetail';
import AlertModal from '../../../components/AlertModal'; import AlertModal from '../../../components/AlertModal';
@@ -131,7 +133,7 @@ function UserRolesList({ user }) {
} }
return ( return (
<> <>
<PaginatedDataList <PaginatedTable
contentError={error} contentError={error}
hasContentLoading={isLoading || isDisassociateLoading} hasContentLoading={isLoading || isDisassociateLoading}
items={roles} items={roles}
@@ -145,15 +147,16 @@ function UserRolesList({ user }) {
isDefault: true, isDefault: true,
}, },
]} ]}
toolbarSortColumns={[
{
name: t`ID`,
key: 'id',
},
]}
toolbarSearchableKeys={searchableKeys} toolbarSearchableKeys={searchableKeys}
toolbarRelatedSearchableKeys={relatedSearchableKeys} toolbarRelatedSearchableKeys={relatedSearchableKeys}
renderItem={role => { headerRow={
<HeaderRow qsConfig={QS_CONFIG} isSelectable={false}>
<HeaderCell>{t`Name`}</HeaderCell>
<HeaderCell>{t`Type`}</HeaderCell>
<HeaderCell>{t`Role`}</HeaderCell>
</HeaderRow>
}
renderRow={(role, index) => {
return ( return (
<UserRolesListItem <UserRolesListItem
key={role.id} key={role.id}
@@ -164,6 +167,7 @@ function UserRolesList({ user }) {
onSelect={item => { onSelect={item => {
setRoleToDisassociate(item); setRoleToDisassociate(item);
}} }}
rowIndex={index}
/> />
); );
}} }}

View File

@@ -1,67 +1,41 @@
import React from 'react'; import React from 'react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { import { Chip } from '@patternfly/react-core';
DataListItem, import { Tr, Td } from '@patternfly/react-table';
DataListItemCells,
DataListItemRow,
Chip,
} from '@patternfly/react-core';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { DetailList, Detail } from '../../../components/DetailList';
import DataListCell from '../../../components/DataListCell';
function UserRolesListItem({ role, detailUrl, onSelect }) { function UserRolesListItem({ role, detailUrl, onSelect }) {
const labelId = `userRole-${role.id}`; const labelId = `userRole-${role.id}`;
return ( return (
<DataListItem key={role.id} aria-labelledby={labelId} id={`${role.id}`}> <Tr id={`user-role-row-${role.id}`}>
<DataListItemRow> <Td id={labelId} dataLabel={t`Name`}>
<DataListItemCells {role.summary_fields.resource_name ? (
dataListCells={[ <Link to={`${detailUrl}`} id={labelId}>
<DataListCell key="name" aria-label={t`Resource name`}> {role.summary_fields.resource_name}
{role.summary_fields.resource_name ? ( </Link>
<Link to={`${detailUrl}`} id={labelId}> ) : (
<b>{role.summary_fields.resource_name}</b> t`System`
</Link> )}
) : ( </Td>
<b>{t`System`}</b> <Td dataLabel={t`Type`}>
)} {role.summary_fields
</DataListCell>, ? role.summary_fields.resource_type_display_name
<DataListCell key="type" aria-label={t`Resource type`}> : null}
{role.summary_fields && ( </Td>
<DetailList stacked> <Td dataLabel={t`Role`}>
<Detail {role.name ? (
label={t`Type`} <Chip
value={role.summary_fields.resource_type_display_name} key={role.name}
/> aria-label={role.name}
</DetailList> onClick={() => onSelect(role)}
)} isReadOnly={!role.summary_fields.user_capabilities.unattach}
</DataListCell>, >
<DataListCell key="role" aria-label={t`Resource role`}> {role.name}
{role.name && ( </Chip>
<DetailList stacked> ) : null}
<Detail </Td>
label={t`Role`} </Tr>
value={
<Chip
key={role.name}
aria-label={role.name}
onClick={() => onSelect(role)}
isReadOnly={
!role.summary_fields.user_capabilities.unattach
}
>
{role.name}
</Chip>
}
/>
</DetailList>
)}
</DataListCell>,
]}
/>
</DataListItemRow>
</DataListItem>
); );
} }

View File

@@ -19,63 +19,84 @@ describe('<UserRolesListItem/>', () => {
}; };
test('should mount properly', () => { test('should mount properly', () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<UserRolesListItem <table>
role={role} <tbody>
detailUrl="/templates/job_template/15/details" <UserRolesListItem
/> role={role}
detailUrl="/templates/job_template/15/details"
/>
</tbody>
</table>
); );
expect(wrapper.length).toBe(1); expect(wrapper.length).toBe(1);
}); });
test('should render proper list item data', () => { test('should render proper list item data', () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<UserRolesListItem <table>
role={role} <tbody>
detailUrl="/templates/job_template/15/details" <UserRolesListItem
/> role={role}
detailUrl="/templates/job_template/15/details"
/>
</tbody>
</table>
); );
expect( const cells = wrapper.find('Td');
wrapper.find('PFDataListCell[aria-label="Resource name"]').text() expect(cells.at(0).text()).toBe('template delete project');
).toBe('template delete project'); expect(cells.at(1).text()).toContain('Job Template');
expect( expect(cells.at(2).text()).toContain('Admin');
wrapper.find('PFDataListCell[aria-label="Resource type"]').text()
).toContain('Job Template');
expect(
wrapper.find('PFDataListCell[aria-label="Resource role"]').text()
).toContain('Admin');
}); });
test('should render deletable chip', () => { test('should render deletable chip', () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<UserRolesListItem <table>
role={role} <tbody>
detailUrl="/templates/job_template/15/details" <UserRolesListItem
/> role={role}
detailUrl="/templates/job_template/15/details"
/>
</tbody>
</table>
); );
expect(wrapper.find('Chip').prop('isReadOnly')).toBe(false); expect(wrapper.find('Chip').prop('isReadOnly')).toBe(false);
}); });
test('should render read only chip', () => { test('should render read only chip', () => {
role.summary_fields.user_capabilities.unattach = false; role.summary_fields.user_capabilities.unattach = false;
wrapper = mountWithContexts( wrapper = mountWithContexts(
<UserRolesListItem <table>
role={role} <tbody>
detailUrl="/templates/job_template/15/details" <UserRolesListItem
/> role={role}
detailUrl="/templates/job_template/15/details"
/>
</tbody>
</table>
); );
expect(wrapper.find('Chip').prop('isReadOnly')).toBe(true); expect(wrapper.find('Chip').prop('isReadOnly')).toBe(true);
}); });
test('should display System as name when no resource_name is present in summary_fields', () => { test('should display System as name when no resource_name is present in summary_fields', () => {
wrapper = mountWithContexts( wrapper = mountWithContexts(
<UserRolesListItem <table>
role={{ <tbody>
...role, <UserRolesListItem
summary_fields: { role={{
user_capabilities: { unattach: false }, ...role,
}, summary_fields: {
}} user_capabilities: { unattach: false },
/> },
}}
/>
</tbody>
</table>
); );
expect( expect(
wrapper.find('PFDataListCell[aria-label="Resource name"]').text() wrapper
.find('Td')
.at(0)
.text()
).toBe('System'); ).toBe('System');
}); });
}); });

View File

@@ -1,11 +1,12 @@
import React, { useState, useCallback, useEffect } from 'react'; import React, { useState, useCallback, useEffect } from 'react';
import { useLocation, useParams } from 'react-router-dom'; import { useLocation, useParams } from 'react-router-dom';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import PaginatedDataList, { import PaginatedTable, {
ToolbarAddButton, HeaderRow,
} from '../../../components/PaginatedDataList'; HeaderCell,
} from '../../../components/PaginatedTable';
import { ToolbarAddButton } from '../../../components/PaginatedDataList';
import DataListToolbar from '../../../components/DataListToolbar'; import DataListToolbar from '../../../components/DataListToolbar';
import DisassociateButton from '../../../components/DisassociateButton'; import DisassociateButton from '../../../components/DisassociateButton';
import AssociateModal from '../../../components/AssociateModal'; import AssociateModal from '../../../components/AssociateModal';
@@ -168,7 +169,7 @@ function UserTeamList() {
return ( return (
<> <>
<PaginatedDataList <PaginatedTable
items={teams} items={teams}
contentError={contentError} contentError={contentError}
hasContentLoading={isLoading || isDisassociateLoading} hasContentLoading={isLoading || isDisassociateLoading}
@@ -176,7 +177,14 @@ function UserTeamList() {
pluralizedItemName={t`Teams`} pluralizedItemName={t`Teams`}
qsConfig={QS_CONFIG} qsConfig={QS_CONFIG}
onRowClick={handleSelect} onRowClick={handleSelect}
renderItem={team => ( headerRow={
<HeaderRow qsConfig={QS_CONFIG}>
<HeaderCell sortKey="name">{t`Name`}</HeaderCell>
<HeaderCell>{t`Organization`}</HeaderCell>
<HeaderCell>{t`Description`}</HeaderCell>
</HeaderRow>
}
renderRow={(team, index) => (
<UserTeamListItem <UserTeamListItem
key={team.id} key={team.id}
value={team.name} value={team.name}
@@ -184,6 +192,7 @@ function UserTeamList() {
detailUrl={`/teams/${team.id}/details`} detailUrl={`/teams/${team.id}/details`}
onSelect={() => handleSelect(team)} onSelect={() => handleSelect(team)}
isSelected={selected.some(row => row.id === team.id)} isSelected={selected.some(row => row.id === team.id)}
rowIndex={index}
/> />
)} )}
renderToolbar={props => ( renderToolbar={props => (

View File

@@ -1,61 +1,34 @@
import React from 'react'; import React from 'react';
import { bool, func } from 'prop-types'; import { bool, func } from 'prop-types';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { import { Tr, Td } from '@patternfly/react-table';
DataListItemCells,
DataListItemRow,
DataListItem,
DataListCheck,
Split,
SplitItem,
} from '@patternfly/react-core';
import DataListCell from '../../../components/DataListCell';
import { Team } from '../../../types'; import { Team } from '../../../types';
function UserTeamListItem({ team, isSelected, onSelect }) { function UserTeamListItem({ team, isSelected, onSelect, rowIndex }) {
return ( return (
<DataListItem <Tr id={`user-team-row-${team.id}`}>
key={team.id} <Td
id={`${team.id}`} select={{
aria-labelledby={`team-${team.id}`} rowIndex,
> isSelected,
<DataListItemRow> onSelect,
<DataListCheck }}
aria-labelledby={`team-${team.id}`} />
checked={isSelected} <Td id={`team-${team.id}`} dataLabel={t`Name`}>
id={`team-${team.id}`} <Link to={`/teams/${team.id}/details`}>{team.name}</Link>
onChange={onSelect} </Td>
/> <Td dataLabel={t`Organization`}>
<DataListItemCells {team.summary_fields.organization ? (
dataListCells={[ <Link
<DataListCell key="name"> to={`/organizations/${team.summary_fields.organization.id}/details`}
<Link to={`/teams/${team.id}/details`} id={`team-${team.id}`}> >
<b>{team.name}</b> {team.summary_fields.organization.name}
</Link> </Link>
</DataListCell>, ) : null}
<DataListCell key="organization"> </Td>
{team.summary_fields.organization && ( <Td dataLabel={t`Description`}>{team.description}</Td>
<Split hasGutter> </Tr>
<SplitItem>
<b>{t`Organization`}</b>{' '}
</SplitItem>
<SplitItem>
<Link
to={`/organizations/${team.summary_fields.organization.id}/details`}
>
<b>{team.summary_fields.organization.name}</b>
</Link>
</SplitItem>
</Split>
)}
</DataListCell>,
<DataListCell key="description">{team.description}</DataListCell>,
]}
/>
</DataListItemRow>
</DataListItem>
); );
} }

View File

@@ -1,6 +1,4 @@
import React from 'react'; import React from 'react';
import { MemoryRouter } from 'react-router-dom';
import { I18nProvider } from '@lingui/react';
import { i18n } from '@lingui/core'; import { i18n } from '@lingui/core';
import { en } from 'make-plural/plurals'; import { en } from 'make-plural/plurals';
import { mountWithContexts } from '../../../../testUtils/enzymeHelpers'; import { mountWithContexts } from '../../../../testUtils/enzymeHelpers';
@@ -14,8 +12,8 @@ i18n.activate('en');
describe('<UserTeamListItem />', () => { describe('<UserTeamListItem />', () => {
test('should render item', () => { test('should render item', () => {
const wrapper = mountWithContexts( const wrapper = mountWithContexts(
<I18nProvider i18n={i18n}> <table>
<MemoryRouter initialEntries={['/teams']} initialIndex={0}> <tbody>
<UserTeamListItem <UserTeamListItem
team={{ team={{
id: 1, id: 1,
@@ -32,14 +30,14 @@ describe('<UserTeamListItem />', () => {
isSelected={false} isSelected={false}
onSelect={() => {}} onSelect={() => {}}
/> />
</MemoryRouter> </tbody>
</I18nProvider> </table>
); );
const cells = wrapper.find('DataListCell'); const cells = wrapper.find('Td');
expect(cells).toHaveLength(3); expect(cells).toHaveLength(4);
expect(cells.at(0).text()).toEqual('Team 1'); expect(cells.at(1).text()).toEqual('Team 1');
expect(cells.at(1).text()).toEqual('Organization The Org'); expect(cells.at(2).text()).toEqual('The Org');
expect(cells.at(2).text()).toEqual('something something team'); expect(cells.at(3).text()).toEqual('something something team');
}); });
}); });