convert notification template list to tables

This commit is contained in:
Keith Grant
2021-01-29 15:04:11 -08:00
committed by Keith J. Grant
parent b3cdefec23
commit c3bab52a61
4 changed files with 152 additions and 143 deletions

View File

@@ -4,7 +4,11 @@ import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Card, PageSection } from '@patternfly/react-core'; import { Card, PageSection } from '@patternfly/react-core';
import { NotificationTemplatesAPI } from '../../../api'; import { NotificationTemplatesAPI } from '../../../api';
import PaginatedDataList, { import PaginatedTable, {
HeaderRow,
HeaderCell,
} from '../../../components/PaginatedTable';
import {
ToolbarAddButton, ToolbarAddButton,
ToolbarDeleteButton, ToolbarDeleteButton,
} from '../../../components/PaginatedDataList'; } from '../../../components/PaginatedDataList';
@@ -104,7 +108,7 @@ function NotificationTemplatesList({ i18n }) {
<> <>
<PageSection> <PageSection>
<Card> <Card>
<PaginatedDataList <PaginatedTable
contentError={contentError} contentError={contentError}
hasContentLoading={isTemplatesLoading || isDeleteLoading} hasContentLoading={isTemplatesLoading || isDeleteLoading}
items={templates} items={templates}
@@ -149,16 +153,6 @@ function NotificationTemplatesList({ i18n }) {
]} ]}
toolbarSearchableKeys={searchableKeys} toolbarSearchableKeys={searchableKeys}
toolbarRelatedSearchableKeys={relatedSearchableKeys} toolbarRelatedSearchableKeys={relatedSearchableKeys}
toolbarSortColumns={[
{
name: i18n._(t`Name`),
key: 'name',
},
{
name: i18n._(t`Type`),
key: 'notification_type',
},
]}
renderToolbar={props => ( renderToolbar={props => (
<DataListToolbar <DataListToolbar
{...props} {...props}
@@ -179,7 +173,17 @@ function NotificationTemplatesList({ i18n }) {
]} ]}
/> />
)} )}
renderItem={template => ( headerRow={
<HeaderRow qsConfig={QS_CONFIG}>
<HeaderCell sortKey="name">{i18n._(t`Name`)}</HeaderCell>
<HeaderCell>{i18n._(t`Status`)}</HeaderCell>
<HeaderCell sortKey="notification_type">
{i18n._(t`Type`)}
</HeaderCell>
<HeaderCell>{i18n._(t`Actions`)}</HeaderCell>
</HeaderRow>
}
renderRow={(template, index) => (
<NotificationTemplateListItem <NotificationTemplateListItem
key={template.id} key={template.id}
fetchTemplates={fetchTemplates} fetchTemplates={fetchTemplates}
@@ -187,6 +191,7 @@ function NotificationTemplatesList({ i18n }) {
detailUrl={`${match.url}/${template.id}`} detailUrl={`${match.url}/${template.id}`}
isSelected={selected.some(row => row.id === template.id)} isSelected={selected.some(row => row.id === template.id)}
onSelect={() => handleSelect(template)} onSelect={() => handleSelect(template)}
rowIndex={index}
/> />
)} )}
emptyStateControls={ emptyStateControls={

View File

@@ -89,21 +89,33 @@ describe('<NotificationTemplateList />', () => {
}); });
test('should select item', async () => { test('should select item', async () => {
const itemCheckboxInput = 'input#select-template-1';
await act(async () => { await act(async () => {
wrapper = mountWithContexts(<NotificationTemplateList />); wrapper = mountWithContexts(<NotificationTemplateList />);
}); });
wrapper.update(); wrapper.update();
expect(wrapper.find(itemCheckboxInput).prop('checked')).toEqual(false); expect(
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('checked')
).toEqual(false);
await act(async () => { await act(async () => {
wrapper wrapper
.find(itemCheckboxInput) .find('.pf-c-table__check')
.closest('DataListCheck') .first()
.find('input')
.props() .props()
.onChange(); .onChange();
}); });
wrapper.update(); wrapper.update();
expect(wrapper.find(itemCheckboxInput).prop('checked')).toEqual(true); expect(
wrapper
.find('.pf-c-table__check')
.first()
.find('input')
.prop('checked')
).toEqual(true);
}); });
test('should delete notifications', async () => { test('should delete notifications', async () => {
@@ -135,7 +147,6 @@ describe('<NotificationTemplateList />', () => {
}); });
test('should show error dialog shown for failed deletion', async () => { test('should show error dialog shown for failed deletion', async () => {
const itemCheckboxInput = 'input#select-template-1';
OrganizationsAPI.destroy.mockRejectedValue( OrganizationsAPI.destroy.mockRejectedValue(
new Error({ new Error({
response: { response: {
@@ -153,8 +164,9 @@ describe('<NotificationTemplateList />', () => {
wrapper.update(); wrapper.update();
await act(async () => { await act(async () => {
wrapper wrapper
.find(itemCheckboxInput) .find('.pf-c-table__check')
.closest('DataListCheck') .first()
.find('input')
.props() .props()
.onChange(); .onChange();
}); });

View File

@@ -3,32 +3,17 @@ import React, { useState, useEffect, useCallback } from 'react';
import { withI18n } from '@lingui/react'; import { withI18n } from '@lingui/react';
import { t } from '@lingui/macro'; import { t } from '@lingui/macro';
import { Link } from 'react-router-dom'; import { Link } from 'react-router-dom';
import styled from 'styled-components'; import { Button } from '@patternfly/react-core';
import { import { Tr, Td } from '@patternfly/react-table';
Button,
DataListAction as _DataListAction,
DataListCheck,
DataListItem,
DataListItemCells,
DataListItemRow,
Tooltip,
} from '@patternfly/react-core';
import { PencilAltIcon, BellIcon } from '@patternfly/react-icons'; import { PencilAltIcon, BellIcon } from '@patternfly/react-icons';
import { ActionsTd, ActionItem } from '../../../components/PaginatedTable';
import { timeOfDay } from '../../../util/dates'; import { timeOfDay } from '../../../util/dates';
import { NotificationTemplatesAPI, NotificationsAPI } from '../../../api'; import { NotificationTemplatesAPI, NotificationsAPI } from '../../../api';
import DataListCell from '../../../components/DataListCell';
import StatusLabel from '../../../components/StatusLabel'; import StatusLabel from '../../../components/StatusLabel';
import CopyButton from '../../../components/CopyButton'; import CopyButton from '../../../components/CopyButton';
import useRequest from '../../../util/useRequest'; import useRequest from '../../../util/useRequest';
import { NOTIFICATION_TYPES } from '../constants'; import { NOTIFICATION_TYPES } from '../constants';
const DataListAction = styled(_DataListAction)`
align-items: center;
display: grid;
grid-gap: 16px;
grid-template-columns: repeat(3, 40px);
`;
const NUM_RETRIES = 25; const NUM_RETRIES = 25;
const RETRY_TIMEOUT = 5000; const RETRY_TIMEOUT = 5000;
@@ -38,6 +23,7 @@ function NotificationTemplateListItem({
fetchTemplates, fetchTemplates,
isSelected, isSelected,
onSelect, onSelect,
rowIndex,
i18n, i18n,
}) { }) {
const recentNotifications = template.summary_fields?.recent_notifications; const recentNotifications = template.summary_fields?.recent_notifications;
@@ -102,36 +88,29 @@ function NotificationTemplateListItem({
const labelId = `template-name-${template.id}`; const labelId = `template-name-${template.id}`;
return ( return (
<DataListItem key={template.id} aria-labelledby={labelId} id={template.id}> <Tr id={`notification-template-row-${template.id}`}>
<DataListItemRow> <Td
<DataListCheck select={{
id={`select-template-${template.id}`} rowIndex,
checked={isSelected} isSelected,
onChange={onSelect} onSelect,
aria-labelledby={labelId} }}
dataLabel={i18n._(t`Selected`)}
/> />
<DataListItemCells <Td id={labelId} dataLabel={i18n._(t`Name`)}>
dataListCells={[ <Link to={`${detailUrl}`}>
<DataListCell key="name" id={labelId}>
<Link to={detailUrl}>
<b>{template.name}</b> <b>{template.name}</b>
</Link> </Link>
</DataListCell>, </Td>
<DataListCell key="status"> <Td dataLabel={i18n._(t`Status`)}>
{status && <StatusLabel status={status} />} {status && <StatusLabel status={status} />}
</DataListCell>, </Td>
<DataListCell key="type"> <Td dataLabel={i18n._(t`Type`)}>
<strong>{i18n._(t`Type:`)}</strong>{' '}
{NOTIFICATION_TYPES[template.notification_type] || {NOTIFICATION_TYPES[template.notification_type] ||
template.notification_type} template.notification_type}
</DataListCell>, </Td>
]} <ActionsTd dataLabel={i18n._(t`Actions`)}>
/> <ActionItem visible tooltip={i18n._(t`Test notification`)}>
<DataListAction
aria-label={i18n._(t`actions`)}
aria-labelledby={labelId}
>
<Tooltip content={i18n._(t`Test Notification`)} position="top">
<Button <Button
aria-label={i18n._(t`Test Notification`)} aria-label={i18n._(t`Test Notification`)}
variant="plain" variant="plain"
@@ -140,11 +119,10 @@ function NotificationTemplateListItem({
> >
<BellIcon /> <BellIcon />
</Button> </Button>
</Tooltip> </ActionItem>
{template.summary_fields.user_capabilities.edit ? ( <ActionItem
<Tooltip visible={template.summary_fields.user_capabilities.edit}
content={i18n._(t`Edit Notification Template`)} tooltip={i18n._(t`Edit`)}
position="top"
> >
<Button <Button
aria-label={i18n._(t`Edit Notification Template`)} aria-label={i18n._(t`Edit Notification Template`)}
@@ -154,12 +132,8 @@ function NotificationTemplateListItem({
> >
<PencilAltIcon /> <PencilAltIcon />
</Button> </Button>
</Tooltip> </ActionItem>
) : ( <ActionItem visible={template.summary_fields.user_capabilities.copy}>
<div />
)}
{template.summary_fields.user_capabilities.copy && (
<Tooltip content={i18n._(t`Copy Notification Template`)}>
<CopyButton <CopyButton
copyItem={copyTemplate} copyItem={copyTemplate}
isCopyDisabled={isCopyDisabled} isCopyDisabled={isCopyDisabled}
@@ -167,11 +141,9 @@ function NotificationTemplateListItem({
onCopyFinish={handleCopyFinish} onCopyFinish={handleCopyFinish}
errorMessage={i18n._(t`Failed to copy template.`)} errorMessage={i18n._(t`Failed to copy template.`)}
/> />
</Tooltip> </ActionItem>
)} </ActionsTd>
</DataListAction> </Tr>
</DataListItemRow>
</DataListItem>
); );
} }

View File

@@ -26,17 +26,21 @@ const template = {
describe('<NotificationTemplateListItem />', () => { describe('<NotificationTemplateListItem />', () => {
test('should render template row', () => { test('should render template row', () => {
const wrapper = mountWithContexts( const wrapper = mountWithContexts(
<table>
<tbody>
<NotificationTemplateListItem <NotificationTemplateListItem
template={template} template={template}
detailUrl="/notification_templates/3/detail" detailUrl="/notification_templates/3/detail"
/> />
</tbody>
</table>
); );
const cells = wrapper.find('DataListCell'); const cells = wrapper.find('Td');
expect(cells).toHaveLength(3); expect(cells).toHaveLength(5);
expect(cells.at(0).text()).toEqual('Test Notification'); expect(cells.at(1).text()).toEqual('Test Notification');
expect(cells.at(1).text()).toEqual('Success'); expect(cells.at(2).text()).toEqual('Success');
expect(cells.at(2).text()).toEqual('Type: Slack'); expect(cells.at(3).text()).toEqual('Slack');
}); });
test('should send test notification', async () => { test('should send test notification', async () => {
@@ -45,10 +49,14 @@ describe('<NotificationTemplateListItem />', () => {
}); });
const wrapper = mountWithContexts( const wrapper = mountWithContexts(
<table>
<tbody>
<NotificationTemplateListItem <NotificationTemplateListItem
template={template} template={template}
detailUrl="/notification_templates/3/detail" detailUrl="/notification_templates/3/detail"
/> />
</tbody>
</table>
); );
await act(async () => { await act(async () => {
wrapper wrapper
@@ -59,8 +67,8 @@ describe('<NotificationTemplateListItem />', () => {
expect(NotificationTemplatesAPI.test).toHaveBeenCalledTimes(1); expect(NotificationTemplatesAPI.test).toHaveBeenCalledTimes(1);
expect( expect(
wrapper wrapper
.find('DataListCell') .find('Td')
.at(1) .at(2)
.text() .text()
).toEqual('Running'); ).toEqual('Running');
}); });
@@ -69,10 +77,14 @@ describe('<NotificationTemplateListItem />', () => {
NotificationTemplatesAPI.copy.mockResolvedValue(); NotificationTemplatesAPI.copy.mockResolvedValue();
const wrapper = mountWithContexts( const wrapper = mountWithContexts(
<table>
<tbody>
<NotificationTemplateListItem <NotificationTemplateListItem
template={template} template={template}
detailUrl="/notification_templates/3/detail" detailUrl="/notification_templates/3/detail"
/> />
</tbody>
</table>
); );
await act(async () => await act(async () =>
@@ -86,10 +98,14 @@ describe('<NotificationTemplateListItem />', () => {
NotificationTemplatesAPI.copy.mockRejectedValue(new Error()); NotificationTemplatesAPI.copy.mockRejectedValue(new Error());
const wrapper = mountWithContexts( const wrapper = mountWithContexts(
<table>
<tbody>
<NotificationTemplateListItem <NotificationTemplateListItem
template={template} template={template}
detailUrl="/notification_templates/3/detail" detailUrl="/notification_templates/3/detail"
/> />
</tbody>
</table>
); );
await act(async () => await act(async () =>
wrapper.find('Button[aria-label="Copy"]').prop('onClick')() wrapper.find('Button[aria-label="Copy"]').prop('onClick')()
@@ -101,6 +117,8 @@ describe('<NotificationTemplateListItem />', () => {
test('should not render copy button', async () => { test('should not render copy button', async () => {
const wrapper = mountWithContexts( const wrapper = mountWithContexts(
<table>
<tbody>
<NotificationTemplateListItem <NotificationTemplateListItem
template={{ template={{
...template, ...template,
@@ -113,6 +131,8 @@ describe('<NotificationTemplateListItem />', () => {
}} }}
detailUrl="/notification_templates/3/detail" detailUrl="/notification_templates/3/detail"
/> />
</tbody>
</table>
); );
expect(wrapper.find('CopyButton').length).toBe(0); expect(wrapper.find('CopyButton').length).toBe(0);
}); });