convert application list to tables

This commit is contained in:
Keith Grant 2021-01-29 15:52:16 -08:00 committed by Keith J. Grant
parent a481fc3cc9
commit 87cf797153
4 changed files with 113 additions and 143 deletions

View File

@ -48,6 +48,7 @@ describe('<ApplicationsList/>', () => {
});
await waitForElement(wrapper, 'ApplicationsList', el => el.length > 0);
});
test('should have data fetched and render 2 rows', async () => {
ApplicationsAPI.read.mockResolvedValue(applications);
ApplicationsAPI.readOptions.mockResolvedValue(options);
@ -69,14 +70,20 @@ describe('<ApplicationsList/>', () => {
waitForElement(wrapper, 'ApplicationsList', el => el.length > 0);
wrapper
.find('input#select-application-1')
.find('.pf-c-table__check')
.first()
.find('input')
.simulate('change', applications.data.results[0]);
wrapper.update();
expect(wrapper.find('input#select-application-1').prop('checked')).toBe(
true
);
expect(
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('checked')
).toBe(true);
await act(async () =>
wrapper.find('Button[aria-label="Delete"]').prop('onClick')()
);
@ -131,13 +138,21 @@ describe('<ApplicationsList/>', () => {
});
waitForElement(wrapper, 'ApplicationsList', el => el.length > 0);
wrapper.find('input#select-application-1').simulate('change', 'a');
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.simulate('change', 'a');
wrapper.update();
expect(wrapper.find('input#select-application-1').prop('checked')).toBe(
true
);
expect(
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('checked')
).toBe(true);
await act(async () =>
wrapper.find('Button[aria-label="Delete"]').prop('onClick')()
);
@ -163,6 +178,7 @@ describe('<ApplicationsList/>', () => {
waitForElement(wrapper, 'ApplicationsList', el => el.length > 0);
expect(wrapper.find('ToolbarAddButton').length).toBe(0);
});
test('should not render edit button for first list item', async () => {
applications.data.results[0].summary_fields.user_capabilities.edit = false;
ApplicationsAPI.read.mockResolvedValue(applications);

View File

@ -1,104 +1,65 @@
import React from 'react';
import { string, bool, func } from 'prop-types';
import { withI18n } from '@lingui/react';
import {
Button,
DataListAction as _DataListAction,
DataListCheck,
DataListItem,
DataListItemCells,
DataListItemRow,
Tooltip,
} from '@patternfly/react-core';
import { Button } from '@patternfly/react-core';
import { Tr, Td } from '@patternfly/react-table';
import { t } from '@lingui/macro';
import { Link } from 'react-router-dom';
import styled from 'styled-components';
import { PencilAltIcon } from '@patternfly/react-icons';
import { ActionsTd, ActionItem } from '../../../components/PaginatedTable';
import { formatDateString } from '../../../util/dates';
import { Application } from '../../../types';
import DataListCell from '../../../components/DataListCell';
const DataListAction = styled(_DataListAction)`
align-items: center;
display: grid;
grid-gap: 16px;
grid-template-columns: 40px;
`;
const Label = styled.b`
margin-right: 20px;
`;
function ApplicationListItem({
application,
isSelected,
onSelect,
detailUrl,
rowIndex,
i18n,
}) {
const labelId = `check-action-${application.id}`;
return (
<DataListItem
key={application.id}
aria-labelledby={labelId}
id={`${application.id}`}
>
<DataListItemRow>
<DataListCheck
id={`select-application-${application.id}`}
checked={isSelected}
onChange={onSelect}
aria-labelledby={labelId}
/>
<DataListItemCells
dataListCells={[
<DataListCell
key="divider"
aria-label={i18n._(t`application name`)}
>
<Link to={`${detailUrl}`}>
<b>{application.name}</b>
</Link>
</DataListCell>,
<DataListCell
key="organization"
aria-label={i18n._(t`organization name`)}
>
<Link
to={`/organizations/${application.summary_fields.organization.id}`}
>
<b>{application.summary_fields.organization.name}</b>
</Link>
</DataListCell>,
<DataListCell key="modified" aria-label={i18n._(t`last modified`)}>
<Label>{i18n._(t`Last Modified`)}</Label>
<span>{formatDateString(application.modified)}</span>
</DataListCell>,
]}
/>
<DataListAction
aria-label={i18n._(t`actions`)}
aria-labelledby={labelId}
id={labelId}
<Tr id={`application-row-${application.id}`}>
<Td
select={{
rowIndex,
isSelected,
onSelect,
}}
dataLabel={i18n._(t`Selected`)}
/>
<Td id={labelId} dataLabel={i18n._(t`Name`)}>
<Link to={`${detailUrl}`}>
<b>{application.name}</b>
</Link>
</Td>
<Td dataLabel={i18n._(t`Organization`)}>
<Link
to={`/organizations/${application.summary_fields.organization.id}`}
>
{application.summary_fields.user_capabilities.edit ? (
<Tooltip content={i18n._(t`Edit application`)} position="top">
<Button
aria-label={i18n._(t`Edit application`)}
variant="plain"
component={Link}
to={`/applications/${application.id}/edit`}
>
<PencilAltIcon />
</Button>
</Tooltip>
) : (
''
)}
</DataListAction>
</DataListItemRow>
</DataListItem>
<b>{application.summary_fields.organization.name}</b>
</Link>
</Td>
<Td dataLabel={i18n._(t`Last Modified`)}>
{formatDateString(application.modified)}
</Td>
<ActionsTd dataLabel={i18n._(t`Actions`)}>
<ActionItem
visible={application.summary_fields.user_capabilities.edit}
tooltip={i18n._(t`Edit application`)}
>
<Button
aria-label={i18n._(t`Edit application`)}
variant="plain"
component={Link}
to={`/applications/${application.id}/edit`}
>
<PencilAltIcon />
</Button>
</ActionItem>
</ActionsTd>
</Tr>
);
}

View File

@ -18,12 +18,16 @@ describe('<ApplicationListItem/>', () => {
test('should mount successfully', async () => {
await act(async () => {
wrapper = mountWithContexts(
<ApplicationListItem
application={application}
detailUrl="/organizations/2/details"
isSelected={false}
onSelect={() => {}}
/>
<table>
<tbody>
<ApplicationListItem
application={application}
detailUrl="/organizations/2/details"
isSelected={false}
onSelect={() => {}}
/>
</tbody>
</table>
);
});
expect(wrapper.find('ApplicationListItem').length).toBe(1);
@ -31,38 +35,30 @@ describe('<ApplicationListItem/>', () => {
test('should render the proper data', async () => {
await act(async () => {
wrapper = mountWithContexts(
<ApplicationListItem
application={application}
detailUrl="/organizations/2/details"
isSelected={false}
onSelect={() => {}}
/>
<table>
<tbody>
<ApplicationListItem
application={application}
detailUrl="/organizations/2/details"
isSelected={false}
onSelect={() => {}}
/>
</tbody>
</table>
);
});
expect(
wrapper.find('DataListCell[aria-label="application name"]').text()
wrapper
.find('Td')
.at(1)
.text()
).toBe('Foo');
expect(
wrapper.find('DataListCell[aria-label="organization name"]').text()
wrapper
.find('Td')
.at(2)
.text()
).toBe('Organization');
expect(wrapper.find('input#select-application-1').prop('checked')).toBe(
false
);
expect(wrapper.find('PencilAltIcon').length).toBe(1);
});
test('should be checked', async () => {
await act(async () => {
wrapper = mountWithContexts(
<ApplicationListItem
application={application}
detailUrl="/organizations/2/details"
isSelected
onSelect={() => {}}
/>
);
});
expect(wrapper.find('input#select-application-1').prop('checked')).toBe(
true
);
});
});

View File

@ -11,7 +11,11 @@ import AlertModal from '../../../components/AlertModal';
import DatalistToolbar from '../../../components/DataListToolbar';
import { ApplicationsAPI } from '../../../api';
import PaginatedDataList, {
import PaginatedTable, {
HeaderRow,
HeaderCell,
} from '../../../components/PaginatedTable';
import {
ToolbarDeleteButton,
ToolbarAddButton,
} from '../../../components/PaginatedDataList';
@ -104,7 +108,7 @@ function ApplicationsList({ i18n }) {
<>
<PageSection>
<Card>
<PaginatedDataList
<PaginatedTable
contentError={error}
hasContentLoading={isLoading || deleteLoading}
items={applications}
@ -123,24 +127,6 @@ function ApplicationsList({ i18n }) {
key: 'description__icontains',
},
]}
toolbarSortColumns={[
{
name: i18n._(t`Name`),
key: 'name',
},
{
name: i18n._(t`Created`),
key: 'created',
},
{
name: i18n._(t`Organization`),
key: 'organization',
},
{
name: i18n._(t`Description`),
key: 'description',
},
]}
toolbarSearchableKeys={searchableKeys}
toolbarRelatedSearchableKeys={relatedSearchableKeys}
renderToolbar={props => (
@ -170,7 +156,17 @@ function ApplicationsList({ i18n }) {
]}
/>
)}
renderItem={application => (
headerRow={
<HeaderRow qsConfig={QS_CONFIG}>
<HeaderCell sortKey="name">{i18n._(t`Name`)}</HeaderCell>
<HeaderCell sortKey="organization">
{i18n._(t`Organization`)}
</HeaderCell>
<HeaderCell>{i18n._(t`Last Modified`)}</HeaderCell>
<HeaderCell>{i18n._(t`Actions`)}</HeaderCell>
</HeaderRow>
}
renderRow={(application, index) => (
<ApplicationListItem
key={application.id}
value={application.name}
@ -178,6 +174,7 @@ function ApplicationsList({ i18n }) {
detailUrl={`${match.url}/${application.id}/details`}
onSelect={() => handleSelect(application)}
isSelected={selected.some(row => row.id === application.id)}
rowIndex={index}
/>
)}
emptyStateControls={