convert CredentialTypeList to tables

This commit is contained in:
Keith Grant 2021-01-29 14:44:39 -08:00 committed by Keith J. Grant
parent 2a37430eab
commit b3cdefec23
4 changed files with 112 additions and 118 deletions

View File

@ -8,10 +8,14 @@ import { CredentialTypesAPI } from '../../../api';
import { getQSConfig, parseQueryString } from '../../../util/qs';
import useRequest, { useDeleteItems } from '../../../util/useRequest';
import useSelected from '../../../util/useSelected';
import PaginatedDataList, {
import {
ToolbarDeleteButton,
ToolbarAddButton,
} from '../../../components/PaginatedDataList';
import PaginatedTable, {
HeaderRow,
HeaderCell,
} from '../../../components/PaginatedTable';
import ErrorDetail from '../../../components/ErrorDetail';
import AlertModal from '../../../components/AlertModal';
import DatalistToolbar from '../../../components/DataListToolbar';
@ -106,7 +110,7 @@ function CredentialTypeList({ i18n }) {
<>
<PageSection>
<Card>
<PaginatedDataList
<PaginatedTable
contentError={contentError}
hasContentLoading={isLoading || deleteLoading}
items={credentialTypes}
@ -162,7 +166,13 @@ function CredentialTypeList({ i18n }) {
]}
/>
)}
renderItem={credentialType => (
headerRow={
<HeaderRow qsConfig={QS_CONFIG}>
<HeaderCell sortKey="name">{i18n._(t`Name`)}</HeaderCell>
<HeaderCell>{i18n._(t`Actions`)}</HeaderCell>
</HeaderRow>
}
renderRow={(credentialType, index) => (
<CredentialTypeListItem
key={credentialType.id}
value={credentialType.name}
@ -170,6 +180,7 @@ function CredentialTypeList({ i18n }) {
detailUrl={`${match.url}/${credentialType.id}/details`}
onSelect={() => handleSelect(credentialType)}
isSelected={selected.some(row => row.id === credentialType.id)}
rowIndex={index}
/>
)}
emptyStateControls={

View File

@ -72,12 +72,18 @@ describe('<CredentialTypeList', () => {
await waitForElement(wrapper, 'CredentialTypeList', el => el.length > 0);
wrapper
.find('input#select-credential-types-1')
.find('.pf-c-table__check')
.first()
.find('input')
.simulate('change', credentialTypes.data.results[0]);
wrapper.update();
expect(
wrapper.find('input#select-credential-types-1').prop('checked')
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('checked')
).toBe(true);
await act(async () => {
@ -133,10 +139,18 @@ describe('<CredentialTypeList', () => {
});
waitForElement(wrapper, 'CredentialTypeList', el => el.length > 0);
wrapper.find('input#select-credential-types-1').simulate('change', 'a');
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.simulate('change', 'a');
wrapper.update();
expect(
wrapper.find('input#select-credential-types-1').prop('checked')
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('checked')
).toBe(true);
await act(async () =>

View File

@ -3,82 +3,53 @@ import { string, bool, func } from 'prop-types';
import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro';
import { Link } from 'react-router-dom';
import {
Button,
DataListAction as _DataListAction,
DataListCheck,
DataListItem,
DataListItemRow,
DataListItemCells,
Tooltip,
} from '@patternfly/react-core';
import { Button } from '@patternfly/react-core';
import { Tr, Td } from '@patternfly/react-table';
import { PencilAltIcon } from '@patternfly/react-icons';
import styled from 'styled-components';
import DataListCell from '../../../components/DataListCell';
import { ActionsTd, ActionItem } from '../../../components/PaginatedTable';
import { CredentialType } from '../../../types';
const DataListAction = styled(_DataListAction)`
align-items: center;
display: grid;
grid-gap: 16px;
grid-template-columns: 40px;
`;
function CredentialTypeListItem({
credentialType,
detailUrl,
isSelected,
onSelect,
rowIndex,
i18n,
}) {
const labelId = `check-action-${credentialType.id}`;
return (
<DataListItem
key={credentialType.id}
aria-labelledby={labelId}
id={`${credentialType.id} `}
>
<DataListItemRow>
<DataListCheck
id={`select-credential-types-${credentialType.id}`}
checked={isSelected}
onChange={onSelect}
aria-labelledby={labelId}
/>
<DataListItemCells
dataListCells={[
<DataListCell
key="name"
aria-label={i18n._(t`credential type name`)}
>
<Link to={`${detailUrl}`}>
<b>{credentialType.name}</b>
</Link>
</DataListCell>,
]}
/>
<DataListAction
aria-label={i18n._(t`actions`)}
aria-labelledby={labelId}
id={labelId}
<Tr id={`credential-type-row-${credentialType.id}`}>
<Td
select={{
rowIndex,
isSelected,
onSelect,
}}
dataLabel={i18n._(t`Selected`)}
/>
<Td id={labelId} dataLabel={i18n._(t`Name`)}>
<Link to={`${detailUrl}`}>
<b>{credentialType.name}</b>
</Link>
</Td>
<ActionsTd dataLabel={i18n._(t`Actions`)}>
<ActionItem
visible={credentialType.summary_fields.user_capabilities.edit}
tooltip={i18n._(t`Edit credential type`)}
>
{credentialType.summary_fields.user_capabilities.edit && (
<Tooltip content={i18n._(t`Edit credential type`)} position="top">
<Button
aria-label={i18n._(t`Edit credential type`)}
variant="plain"
component={Link}
to={`/credential_types/${credentialType.id}/edit`}
>
<PencilAltIcon />
</Button>
</Tooltip>
)}
</DataListAction>
</DataListItemRow>
</DataListItem>
<Button
aria-label={i18n._(t`Edit credential type`)}
variant="plain"
component={Link}
to={`/credential_types/${credentialType.id}/edit`}
>
<PencilAltIcon />
</Button>
</ActionItem>
</ActionsTd>
</Tr>
);
}

View File

@ -17,12 +17,16 @@ describe('<CredentialTypeListItem/>', () => {
test('should mount successfully', async () => {
await act(async () => {
wrapper = mountWithContexts(
<CredentialTypeListItem
credentialType={credential_type}
detailUrl="credential_types/1/details"
isSelected={false}
onSelect={() => {}}
/>
<table>
<tbody>
<CredentialTypeListItem
credentialType={credential_type}
detailUrl="credential_types/1/details"
isSelected={false}
onSelect={() => {}}
/>
</tbody>
</table>
);
});
expect(wrapper.find('CredentialTypeListItem').length).toBe(1);
@ -31,48 +35,38 @@ describe('<CredentialTypeListItem/>', () => {
test('should render the proper data', async () => {
await act(async () => {
wrapper = mountWithContexts(
<CredentialTypeListItem
credentialType={credential_type}
detailsUrl="credential_types/1/details"
isSelected={false}
onSelect={() => {}}
/>
<table>
<tbody>
<CredentialTypeListItem
credentialType={credential_type}
detailsUrl="credential_types/1/details"
isSelected={false}
onSelect={() => {}}
/>
</tbody>
</table>
);
});
expect(
wrapper.find('DataListCell[aria-label="credential type name"]').text()
).toBe('Foo');
expect(wrapper.find('Td[dataLabel="Name"]').text()).toBe('Foo');
expect(wrapper.find('PencilAltIcon').length).toBe(1);
expect(
wrapper.find('input#select-credential-types-1').prop('checked')
).toBe(false);
});
test('should be checked', async () => {
await act(async () => {
wrapper = mountWithContexts(
<CredentialTypeListItem
credentialType={credential_type}
detailsUrl="credential_types/1/details"
isSelected
onSelect={() => {}}
/>
);
});
expect(
wrapper.find('input#select-credential-types-1').prop('checked')
).toBe(true);
expect(wrapper.find('.pf-c-table__check input').prop('checked')).toBe(
undefined
);
});
test('edit button shown to users with edit capabilities', async () => {
await act(async () => {
wrapper = mountWithContexts(
<CredentialTypeListItem
credentialType={credential_type}
detailsUrl="credential_types/1/details"
isSelected
onSelect={() => {}}
/>
<table>
<tbody>
<CredentialTypeListItem
credentialType={credential_type}
detailsUrl="credential_types/1/details"
isSelected
onSelect={() => {}}
/>
</tbody>
</table>
);
});
@ -82,15 +76,19 @@ describe('<CredentialTypeListItem/>', () => {
test('edit button hidden from users without edit capabilities', async () => {
await act(async () => {
wrapper = mountWithContexts(
<CredentialTypeListItem
credentialType={{
...credential_type,
summary_fields: { user_capabilities: { edit: false } },
}}
detailsUrl="credential_types/1/details"
isSelected
onSelect={() => {}}
/>
<table>
<tbody>
<CredentialTypeListItem
credentialType={{
...credential_type,
summary_fields: { user_capabilities: { edit: false } },
}}
detailsUrl="credential_types/1/details"
isSelected
onSelect={() => {}}
/>
</tbody>
</table>
);
});