grafana/public/app/features/alerting/TestRuleResult.tsx
Torkel Ödegaard 8f78b0e7bc
Chore: Fix all Typescript strict null errors (#26204)
* Chore: Fix typescript strict null errors

* Added new limit

* Fixed ts issue

* fixed tests

* trying to fix type inference

* Fixing more ts errors

* Revert tsconfig option

* Fix

* Fixed code

* More fixes

* fix tests

* Updated snapshot

* Chore: More ts strict null fixes

* More fixes in some really messed up azure config components

* More fixes, current count: 441

* 419

* More fixes

* Fixed invalid initial state in explore

* Fixing tests

* Fixed tests

* Explore fix

* More fixes

* Progress

* Sub 300

* Now at 218

* Progress

* Update

* Progress

* Updated tests

* at 159

* fixed tests

* Progress

* YAy blow 100! at 94

* 10,9,8,7,6,5,4,3,2,1... lift off

* Fixed tests

* Fixed more type errors

Co-authored-by: Ryan McKinley <ryantxu@gmail.com>
2020-07-10 12:46:59 +02:00

126 lines
3.2 KiB
TypeScript

import React, { PureComponent } from 'react';
import { LoadingPlaceholder, JSONFormatter, Icon } from '@grafana/ui';
import appEvents from 'app/core/app_events';
import { CopyToClipboard } from 'app/core/components/CopyToClipboard/CopyToClipboard';
import { DashboardModel, PanelModel } from '../dashboard/state';
import { getBackendSrv } from '@grafana/runtime';
import { AppEvents } from '@grafana/data';
export interface Props {
dashboard: DashboardModel;
panel: PanelModel;
}
interface State {
isLoading: boolean;
allNodesExpanded: boolean | null;
testRuleResponse: {};
}
export class TestRuleResult extends PureComponent<Props, State> {
readonly state: State = {
isLoading: false,
allNodesExpanded: null,
testRuleResponse: {},
};
formattedJson: any;
clipboard: any;
componentDidMount() {
this.testRule();
}
async testRule() {
const { dashboard, panel } = this.props;
// dashboard save model
const model = dashboard.getSaveModelClone();
// now replace panel to get current edits
model.panels = model.panels.map(dashPanel => {
return dashPanel.id === panel.editSourceId ? panel.getSaveModel() : dashPanel;
});
const payload = { dashboard: model, panelId: panel.id };
this.setState({ isLoading: true });
const testRuleResponse = await getBackendSrv().post(`/api/alerts/test`, payload);
this.setState({ isLoading: false, testRuleResponse });
}
setFormattedJson = (formattedJson: any) => {
this.formattedJson = formattedJson;
};
getTextForClipboard = () => {
return JSON.stringify(this.formattedJson, null, 2);
};
onClipboardSuccess = () => {
appEvents.emit(AppEvents.alertSuccess, ['Content copied to clipboard']);
};
onToggleExpand = () => {
this.setState(prevState => ({
...prevState,
allNodesExpanded: !this.state.allNodesExpanded,
}));
};
getNrOfOpenNodes = () => {
if (this.state.allNodesExpanded === null) {
return 3; // 3 is default, ie when state is null
} else if (this.state.allNodesExpanded) {
return 20;
}
return 1;
};
renderExpandCollapse = () => {
const { allNodesExpanded } = this.state;
const collapse = (
<>
<Icon name="minus-circle" /> Collapse All
</>
);
const expand = (
<>
<Icon name="plus-circle" /> Expand All
</>
);
return allNodesExpanded ? collapse : expand;
};
render() {
const { testRuleResponse, isLoading } = this.state;
if (isLoading === true) {
return <LoadingPlaceholder text="Evaluating rule" />;
}
const openNodes = this.getNrOfOpenNodes();
return (
<>
<div className="pull-right">
<button className="btn btn-transparent btn-p-x-0 m-r-1" onClick={this.onToggleExpand}>
{this.renderExpandCollapse()}
</button>
<CopyToClipboard
className="btn btn-transparent btn-p-x-0"
text={this.getTextForClipboard}
onSuccess={this.onClipboardSuccess}
>
<Icon name="copy" /> Copy to Clipboard
</CopyToClipboard>
</div>
<JSONFormatter json={testRuleResponse} open={openNodes} onDidRender={this.setFormattedJson} />
</>
);
}
}