send test notifications

This commit is contained in:
Keith Grant
2020-08-07 11:28:01 -07:00
parent 1405f6ca51
commit 8bb1c985c0
3 changed files with 30 additions and 7 deletions

View File

@@ -5,6 +5,10 @@ class NotificationTemplates extends Base {
super(http); super(http);
this.baseUrl = '/api/v2/notification_templates/'; this.baseUrl = '/api/v2/notification_templates/';
} }
test(id) {
return this.http.post(`${this.baseUrl}${id}/test/`);
}
} }
export default NotificationTemplates; export default NotificationTemplates;

View File

@@ -62,6 +62,7 @@ StatusLabel.propTypes = {
'error', 'error',
'running', 'running',
'pending', 'pending',
'waiting',
'canceled', 'canceled',
]).isRequired, ]).isRequired,
}; };

View File

@@ -1,5 +1,5 @@
import 'styled-components/macro'; import 'styled-components/macro';
import React from 'react'; 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';
@@ -14,8 +14,10 @@ import {
Tooltip, Tooltip,
} from '@patternfly/react-core'; } from '@patternfly/react-core';
import { PencilAltIcon, BellIcon } from '@patternfly/react-icons'; import { PencilAltIcon, BellIcon } from '@patternfly/react-icons';
import { NotificationTemplatesAPI } from '../../../api';
import DataListCell from '../../../components/DataListCell'; import DataListCell from '../../../components/DataListCell';
import StatusLabel from '../../../components/StatusLabel'; import StatusLabel from '../../../components/StatusLabel';
import useRequest from '../../../util/useRequest';
import { NOTIFICATION_TYPES } from '../constants'; import { NOTIFICATION_TYPES } from '../constants';
const DataListAction = styled(_DataListAction)` const DataListAction = styled(_DataListAction)`
@@ -32,10 +34,27 @@ function NotificationTemplateListItem({
onSelect, onSelect,
i18n, i18n,
}) { }) {
const sendTestNotification = () => {}; const latestStatus = template.summary_fields?.recent_notifications[0]?.status;
const labelId = `template-name-${template.id}`; const [status, setStatus] = useState(latestStatus);
const lastNotification = template.summary_fields?.recent_notifications[0]; useEffect(() => {
setStatus(latestStatus);
}, [latestStatus]);
const { request: sendTestNotification, isLoading, error } = useRequest(
useCallback(() => {
NotificationTemplatesAPI.test(template.id);
setStatus('pending');
}, [template.id])
);
useEffect(() => {
if (error) {
setStatus('error');
}
}, [error]);
const labelId = `template-name-${template.id}`;
return ( return (
<DataListItem key={template.id} aria-labelledby={labelId} id={template.id}> <DataListItem key={template.id} aria-labelledby={labelId} id={template.id}>
@@ -54,9 +73,7 @@ function NotificationTemplateListItem({
</Link> </Link>
</DataListCell>, </DataListCell>,
<DataListCell> <DataListCell>
{lastNotification && ( {status && <StatusLabel status={status} />}
<StatusLabel status={lastNotification.status} />
)}
</DataListCell>, </DataListCell>,
<DataListCell key="type"> <DataListCell key="type">
<strong css="margin-right: 24px">{i18n._(t`Type`)}</strong> <strong css="margin-right: 24px">{i18n._(t`Type`)}</strong>
@@ -71,6 +88,7 @@ function NotificationTemplateListItem({
aria-label={i18n._(t`Test Notification`)} aria-label={i18n._(t`Test Notification`)}
variant="plain" variant="plain"
onClick={sendTestNotification} onClick={sendTestNotification}
disabled={isLoading}
> >
<BellIcon /> <BellIcon />
</Button> </Button>