diff --git a/awx/ui_next/src/api/models/NotificationTemplates.js b/awx/ui_next/src/api/models/NotificationTemplates.js index 7736921ad2..69cd5f4022 100644 --- a/awx/ui_next/src/api/models/NotificationTemplates.js +++ b/awx/ui_next/src/api/models/NotificationTemplates.js @@ -5,6 +5,10 @@ class NotificationTemplates extends Base { super(http); this.baseUrl = '/api/v2/notification_templates/'; } + + test(id) { + return this.http.post(`${this.baseUrl}${id}/test/`); + } } export default NotificationTemplates; diff --git a/awx/ui_next/src/components/StatusLabel/StatusLabel.jsx b/awx/ui_next/src/components/StatusLabel/StatusLabel.jsx index 95ba558cea..0f2be56fdc 100644 --- a/awx/ui_next/src/components/StatusLabel/StatusLabel.jsx +++ b/awx/ui_next/src/components/StatusLabel/StatusLabel.jsx @@ -62,6 +62,7 @@ StatusLabel.propTypes = { 'error', 'running', 'pending', + 'waiting', 'canceled', ]).isRequired, }; diff --git a/awx/ui_next/src/screens/NotificationTemplate/NotificationTemplateList/NotificationTemplateListItem.jsx b/awx/ui_next/src/screens/NotificationTemplate/NotificationTemplateList/NotificationTemplateListItem.jsx index 102e4b9777..ed26638ed6 100644 --- a/awx/ui_next/src/screens/NotificationTemplate/NotificationTemplateList/NotificationTemplateListItem.jsx +++ b/awx/ui_next/src/screens/NotificationTemplate/NotificationTemplateList/NotificationTemplateListItem.jsx @@ -1,5 +1,5 @@ import 'styled-components/macro'; -import React from 'react'; +import React, { useState, useEffect, useCallback } from 'react'; import { withI18n } from '@lingui/react'; import { t } from '@lingui/macro'; import { Link } from 'react-router-dom'; @@ -14,8 +14,10 @@ import { Tooltip, } from '@patternfly/react-core'; import { PencilAltIcon, BellIcon } from '@patternfly/react-icons'; +import { NotificationTemplatesAPI } from '../../../api'; import DataListCell from '../../../components/DataListCell'; import StatusLabel from '../../../components/StatusLabel'; +import useRequest from '../../../util/useRequest'; import { NOTIFICATION_TYPES } from '../constants'; const DataListAction = styled(_DataListAction)` @@ -32,10 +34,27 @@ function NotificationTemplateListItem({ onSelect, i18n, }) { - const sendTestNotification = () => {}; - const labelId = `template-name-${template.id}`; + const latestStatus = template.summary_fields?.recent_notifications[0]?.status; + 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 ( @@ -54,9 +73,7 @@ function NotificationTemplateListItem({ , - {lastNotification && ( - - )} + {status && } , {i18n._(t`Type`)} @@ -71,6 +88,7 @@ function NotificationTemplateListItem({ aria-label={i18n._(t`Test Notification`)} variant="plain" onClick={sendTestNotification} + disabled={isLoading} >