Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for toggling approval notifications on orgs and wfjts #7803

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions awx/ui_next/src/api/mixins/Notifications.mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ const NotificationsMixin = parent =>
notificationId,
notificationType
) {
if (notificationType === 'approvals') {
return this.associateNotificationTemplatesApprovals(
resourceId,
notificationId
);
}

if (notificationType === 'started') {
return this.associateNotificationTemplatesStarted(
resourceId,
Expand Down Expand Up @@ -126,6 +133,13 @@ const NotificationsMixin = parent =>
notificationId,
notificationType
) {
if (notificationType === 'approvals') {
return this.disassociateNotificationTemplatesApprovals(
resourceId,
notificationId
);
}

if (notificationType === 'started') {
return this.disassociateNotificationTemplatesStarted(
resourceId,
Expand Down
21 changes: 21 additions & 0 deletions awx/ui_next/src/api/models/Organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,27 @@ class Organizations extends InstanceGroupsMixin(NotificationsMixin(Base)) {
createUser(id, data) {
return this.http.post(`${this.baseUrl}${id}/users/`, data);
}

readNotificationTemplatesApprovals(id, params) {
return this.http.get(
`${this.baseUrl}${id}/notification_templates_approvals/`,
{ params }
);
}

associateNotificationTemplatesApprovals(resourceId, notificationId) {
return this.http.post(
`${this.baseUrl}${resourceId}/notification_templates_approvals/`,
{ id: notificationId }
);
}

disassociateNotificationTemplatesApprovals(resourceId, notificationId) {
return this.http.post(
`${this.baseUrl}${resourceId}/notification_templates_approvals/`,
{ id: notificationId, disassociate: true }
);
}
}

export default Organizations;
21 changes: 21 additions & 0 deletions awx/ui_next/src/api/models/WorkflowJobTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,27 @@ class WorkflowJobTemplates extends SchedulesMixin(NotificationsMixin(Base)) {
destroySurvey(id) {
return this.http.delete(`${this.baseUrl}${id}/survey_spec/`);
}

readNotificationTemplatesApprovals(id, params) {
return this.http.get(
`${this.baseUrl}${id}/notification_templates_approvals/`,
{ params }
);
}

associateNotificationTemplatesApprovals(resourceId, notificationId) {
return this.http.post(
`${this.baseUrl}${resourceId}/notification_templates_approvals/`,
{ id: notificationId }
);
}

disassociateNotificationTemplatesApprovals(resourceId, notificationId) {
return this.http.post(
`${this.baseUrl}${resourceId}/notification_templates_approvals/`,
{ id: notificationId, disassociate: true }
);
}
}

export default WorkflowJobTemplates;
52 changes: 44 additions & 8 deletions awx/ui_next/src/components/NotificationList/NotificationList.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,23 @@ const QS_CONFIG = getQSConfig('notification', {
order_by: 'name',
});

function NotificationList({ apiModel, canToggleNotifications, id, i18n }) {
function NotificationList({
apiModel,
canToggleNotifications,
id,
i18n,
showApprovalsToggle,
}) {
const location = useLocation();
const [isToggleLoading, setIsToggleLoading] = useState(false);
const [loadingToggleIds, setLoadingToggleIds] = useState([]);
const [toggleError, setToggleError] = useState(null);

const {
result: fetchNotificationsResult,
result: {
notifications,
itemCount,
approvalsTemplateIds,
startedTemplateIds,
successTemplateIds,
errorTemplateIds,
Expand Down Expand Up @@ -71,18 +78,35 @@ function NotificationList({ apiModel, canToggleNotifications, id, i18n }) {
apiModel.readNotificationTemplatesError(id, idMatchParams),
]);

return {
const rtnObj = {
notifications: notificationsResults,
itemCount: notificationsCount,
startedTemplateIds: startedTemplates.results.map(st => st.id),
successTemplateIds: successTemplates.results.map(su => su.id),
errorTemplateIds: errorTemplates.results.map(e => e.id),
typeLabels: labels,
};
}, [apiModel, id, location]),

if (showApprovalsToggle) {
const {
data: approvalsTemplates,
} = await apiModel.readNotificationTemplatesApprovals(
id,
idMatchParams
);
rtnObj.approvalsTemplateIds = approvalsTemplates.results.map(
st => st.id
);
} else {
rtnObj.approvalsTemplateIds = [];
}

return rtnObj;
}, [apiModel, id, location, showApprovalsToggle]),
{
notifications: [],
itemCount: 0,
approvalsTemplateIds: [],
startedTemplateIds: [],
successTemplateIds: [],
errorTemplateIds: [],
Expand All @@ -99,7 +123,7 @@ function NotificationList({ apiModel, canToggleNotifications, id, i18n }) {
isCurrentlyOn,
status
) => {
setIsToggleLoading(true);
setLoadingToggleIds(loadingToggleIds.concat([notificationId]));
try {
if (isCurrentlyOn) {
await apiModel.disassociateNotificationTemplate(
Expand Down Expand Up @@ -129,7 +153,9 @@ function NotificationList({ apiModel, canToggleNotifications, id, i18n }) {
} catch (err) {
setToggleError(err);
} finally {
setIsToggleLoading(false);
setLoadingToggleIds(
loadingToggleIds.filter(item => item !== notificationId)
);
}
};

Expand Down Expand Up @@ -184,20 +210,25 @@ function NotificationList({ apiModel, canToggleNotifications, id, i18n }) {
key={notification.id}
notification={notification}
detailUrl={`/notifications/${notification.id}`}
canToggleNotifications={canToggleNotifications && !isToggleLoading}
canToggleNotifications={
canToggleNotifications &&
!loadingToggleIds.includes(notification.id)
}
toggleNotification={handleNotificationToggle}
approvalsTurnedOn={approvalsTemplateIds.includes(notification.id)}
errorTurnedOn={errorTemplateIds.includes(notification.id)}
startedTurnedOn={startedTemplateIds.includes(notification.id)}
successTurnedOn={successTemplateIds.includes(notification.id)}
typeLabels={typeLabels}
showApprovalsToggle={showApprovalsToggle}
/>
)}
/>
{toggleError && (
<AlertModal
variant="error"
title={i18n._(t`Error!`)}
isOpen={!isToggleLoading}
isOpen={loadingToggleIds.length === 0}
onClose={() => setToggleError(null)}
>
{i18n._(t`Failed to toggle notification.`)}
Expand All @@ -212,6 +243,11 @@ NotificationList.propTypes = {
apiModel: shape({}).isRequired,
id: number.isRequired,
canToggleNotifications: bool.isRequired,
showApprovalsToggle: bool,
};

NotificationList.defaultProps = {
showApprovalsToggle: false,
};

export default withI18n()(NotificationList);
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,25 @@ const DataListAction = styled(_DataListAction)`
align-items: center;
display: grid;
grid-gap: 16px;
grid-template-columns: repeat(3, max-content);
grid-template-columns: ${props => `repeat(${props.columns}, max-content)`};
`;
const Label = styled.b`
margin-right: 20px;
`;

function NotificationListItem(props) {
const {
canToggleNotifications,
notification,
detailUrl,
startedTurnedOn,
successTurnedOn,
errorTurnedOn,
toggleNotification,
i18n,
typeLabels,
} = props;

function NotificationListItem({
canToggleNotifications,
notification,
detailUrl,
approvalsTurnedOn,
startedTurnedOn,
successTurnedOn,
errorTurnedOn,
toggleNotification,
i18n,
typeLabels,
showApprovalsToggle,
}) {
return (
<DataListItem
aria-labelledby={`items-list-item-${notification.id}`}
Expand Down Expand Up @@ -66,7 +66,25 @@ function NotificationListItem(props) {
aria-label="actions"
aria-labelledby={`items-list-item-${notification.id}`}
id={`items-list-item-${notification.id}`}
columns={showApprovalsToggle ? 4 : 3}
>
{showApprovalsToggle && (
<Switch
id={`notification-${notification.id}-approvals-toggle`}
label={i18n._(t`Approval`)}
labelOff={i18n._(t`Approval`)}
isChecked={approvalsTurnedOn}
isDisabled={!canToggleNotifications}
onChange={() =>
toggleNotification(
notification.id,
approvalsTurnedOn,
'approvals'
)
}
aria-label={i18n._(t`Toggle notification approvals`)}
/>
)}
<Switch
id={`notification-${notification.id}-started-toggle`}
label={i18n._(t`Start`)}
Expand Down Expand Up @@ -114,17 +132,21 @@ NotificationListItem.propTypes = {
}).isRequired,
canToggleNotifications: bool.isRequired,
detailUrl: string.isRequired,
approvalsTurnedOn: bool,
errorTurnedOn: bool,
startedTurnedOn: bool,
successTurnedOn: bool,
toggleNotification: func.isRequired,
typeLabels: shape().isRequired,
showApprovalsToggle: bool,
};

NotificationListItem.defaultProps = {
approvalsTurnedOn: false,
errorTurnedOn: false,
startedTurnedOn: false,
successTurnedOn: false,
showApprovalsToggle: false,
};

export default withI18n()(NotificationListItem);
Loading