Skip to content

Commit

Permalink
Extracted alert menu button (#4273)
Browse files Browse the repository at this point in the history
  • Loading branch information
ranbena authored Oct 22, 2019
1 parent 0659ef1 commit 3dfad87
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 59 deletions.
2 changes: 1 addition & 1 deletion client/app/assets/less/ant.less
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@
}

.@{btn-prefix-cls} .@{iconfont-css-prefix}-ellipsis {
margin: 0 -7px;
margin: 0 -7px 0 -8px;
}

// Collapse
Expand Down
36 changes: 15 additions & 21 deletions client/app/pages/alert/Alert.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import { Alert as AlertService } from '@/services/alert';
import { Query as QueryService } from '@/services/query';

import LoadingState from '@/components/items-list/components/LoadingState';
import MenuButton from './components/MenuButton';
import AlertView from './AlertView';
import AlertEdit from './AlertEdit';
import AlertNew from './AlertNew';

import Modal from 'antd/lib/modal';

import { routesToAngularRoutes } from '@/lib/utils';
import PromiseRejectionError from '@/lib/promise-rejection-error';

Expand Down Expand Up @@ -152,24 +151,11 @@ class AlertPage extends React.Component {

delete = () => {
const { alert } = this.state;

const doDelete = () => {
alert.$delete(() => {
notification.success('Alert deleted successfully.');
navigateTo('/alerts');
}, () => {
notification.error('Failed deleting alert.');
});
};

Modal.confirm({
title: 'Delete Alert',
content: 'Are you sure you want to delete this alert?',
okText: 'Delete',
okType: 'danger',
onOk: doDelete,
maskClosable: true,
autoFocusButton: null,
return alert.$delete(() => {
notification.success('Alert deleted successfully.');
navigateTo('/alerts');
}, () => {
notification.error('Failed deleting alert.');
});
};

Expand All @@ -192,12 +178,20 @@ class AlertPage extends React.Component {
}

const { queryResult, mode, canEdit, pendingRearm } = this.state;

const menuButton = (
<MenuButton
doDelete={this.delete}
canEdit={canEdit}
/>
);

const commonProps = {
alert,
queryResult,
pendingRearm,
delete: this.delete,
save: this.save,
menuButton,
onQuerySelected: this.onQuerySelected,
onRearmChange: this.onRearmChange,
onNameChange: this.onNameChange,
Expand Down
22 changes: 3 additions & 19 deletions client/app/pages/alert/AlertEdit.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,6 @@ import { Alert as AlertType } from '@/components/proptypes';

import Form from 'antd/lib/form';
import Button from 'antd/lib/button';
import Icon from 'antd/lib/icon';
import Dropdown from 'antd/lib/dropdown';
import Menu from 'antd/lib/menu';

import Title from './components/Title';
import Criteria from './components/Criteria';
Expand Down Expand Up @@ -51,7 +48,7 @@ export default class AlertEdit extends React.Component {
};

render() {
const { alert, queryResult, pendingRearm, onNotificationTemplateChange } = this.props;
const { alert, queryResult, pendingRearm, onNotificationTemplateChange, menuButton } = this.props;
const { onQuerySelected, onNameChange, onRearmChange, onCriteriaChange } = this.props;
const { query, name, options } = alert;
const { saving, canceling } = this.state;
Expand All @@ -67,20 +64,7 @@ export default class AlertEdit extends React.Component {
{saving ? spinnerIcon : <i className="fa fa-check m-r-5" />}
Save Changes
</Button>
<Dropdown
className="m-l-5"
trigger={['click']}
placement="bottomRight"
overlay={(
<Menu>
<Menu.Item>
<a onClick={this.props.delete}>Delete Alert</a>
</Menu.Item>
</Menu>
)}
>
<Button><Icon type="ellipsis" rotate={90} /></Button>
</Dropdown>
{menuButton}
</Title>
<div className="row bg-white tiled p-20">
<div className="d-flex">
Expand Down Expand Up @@ -131,7 +115,7 @@ AlertEdit.propTypes = {
alert: AlertType.isRequired,
queryResult: PropTypes.object, // eslint-disable-line react/forbid-prop-types,
pendingRearm: PropTypes.number,
delete: PropTypes.func.isRequired,
menuButton: PropTypes.node.isRequired,
save: PropTypes.func.isRequired,
cancel: PropTypes.func.isRequired,
onQuerySelected: PropTypes.func.isRequired,
Expand Down
21 changes: 3 additions & 18 deletions client/app/pages/alert/AlertView.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import { Alert as AlertType } from '@/components/proptypes';
import Form from 'antd/lib/form';
import Button from 'antd/lib/button';
import Icon from 'antd/lib/icon';
import Dropdown from 'antd/lib/dropdown';
import Menu from 'antd/lib/menu';
import Tooltip from 'antd/lib/tooltip';

import Title from './components/Title';
Expand Down Expand Up @@ -50,28 +48,15 @@ AlertState.defaultProps = {

export default class AlertView extends React.Component {
render() {
const { alert, queryResult, canEdit, onEdit } = this.props;
const { alert, queryResult, canEdit, onEdit, menuButton } = this.props;
const { query, name, options, rearm } = alert;

return (
<>
<Title name={name} alert={alert}>
<Tooltip title={canEdit ? '' : 'You do not have sufficient permissions to edit this alert'}>
<Button type="default" onClick={canEdit ? onEdit : null} className={cx({ disabled: !canEdit })}><i className="fa fa-edit m-r-5" />Edit</Button>
<Dropdown
className={cx('m-l-5', { disabled: !canEdit })}
trigger={[canEdit ? 'click' : undefined]}
placement="bottomRight"
overlay={(
<Menu>
<Menu.Item>
<a onClick={this.props.delete}>Delete Alert</a>
</Menu.Item>
</Menu>
)}
>
<Button><Icon type="ellipsis" rotate={90} /></Button>
</Dropdown>
{menuButton}
</Tooltip>
</Title>
<div className="row bg-white tiled p-20">
Expand Down Expand Up @@ -126,8 +111,8 @@ AlertView.propTypes = {
alert: AlertType.isRequired,
queryResult: PropTypes.object, // eslint-disable-line react/forbid-prop-types,
canEdit: PropTypes.bool.isRequired,
delete: PropTypes.func.isRequired,
onEdit: PropTypes.func.isRequired,
menuButton: PropTypes.node.isRequired,
};

AlertView.defaultProps = {
Expand Down
55 changes: 55 additions & 0 deletions client/app/pages/alert/components/MenuButton.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React, { useState, useCallback } from 'react';
import PropTypes from 'prop-types';
import cx from 'classnames';

import Modal from 'antd/lib/modal';
import Dropdown from 'antd/lib/dropdown';
import Menu from 'antd/lib/menu';
import Button from 'antd/lib/button';
import Icon from 'antd/lib/icon';


export default function MenuButton({ doDelete, canEdit }) {
const [loading, setLoading] = useState(false);

const confirmDelete = useCallback(() => {
Modal.confirm({
title: 'Delete Alert',
content: 'Are you sure you want to delete this alert?',
okText: 'Delete',
okType: 'danger',
onOk: () => {
setLoading(true);
doDelete().catch(() => {
setLoading(false);
});
},
maskClosable: true,
autoFocusButton: null,
});
}, []);

return (
<Dropdown
className={cx('m-l-5', { disabled: !canEdit })}
trigger={[canEdit ? 'click' : undefined]}
placement="bottomRight"
overlay={(
<Menu>
<Menu.Item>
<a onClick={confirmDelete}>Delete Alert</a>
</Menu.Item>
</Menu>
)}
>
<Button>
{loading ? <Icon type="loading" /> : <Icon type="ellipsis" rotate={90} />}
</Button>
</Dropdown>
);
}

MenuButton.propTypes = {
doDelete: PropTypes.func.isRequired,
canEdit: PropTypes.bool.isRequired,
};

0 comments on commit 3dfad87

Please sign in to comment.