More TypeScript conversions

Signed-off-by: Julius Volz <julius.volz@gmail.com>
This commit is contained in:
Julius Volz 2019-02-14 00:00:51 +01:00
parent 583b89a005
commit 50c0bb0112
6 changed files with 31 additions and 17 deletions

View file

@ -1,6 +1,9 @@
import React, { Component } from 'react'; import React, { Component, ReactNode } from 'react';
import { Container } from 'reactstrap'; import { Container } from 'reactstrap';
import PanelList from './PanelList'; import PanelList from './PanelList';
import './App.css'; import './App.css';
class App extends Component { class App extends Component {

View file

@ -47,7 +47,7 @@ class DataTable extends PureComponent<QueryResult> {
return series; return series;
} }
render(): ReactNode { render() {
const data = this.props.data; const data = this.props.data;
if (data === null) { if (data === null) {

View file

@ -47,7 +47,7 @@ class ExpressionInput extends Component {
return null; return null;
} }
let matches = fuzzy.filter(downshift.inputValue.replace(/ /g, ''), this.props.metrics, { let matches = fuzzy.filter(downshift.inputValue.replace(/ /g, ''), this.props.metricNames, {
pre: "<strong>", pre: "<strong>",
post: "</strong>", post: "</strong>",
}); });

View file

@ -211,7 +211,7 @@ class Panel extends Component {
onChange={this.handleExpressionChange} onChange={this.handleExpressionChange}
executeQuery={this.executeQuery} executeQuery={this.executeQuery}
loading={this.state.loading} loading={this.state.loading}
metrics={this.props.metrics} metricNames={this.props.metricNames}
/> />
</Col> </Col>
</Row> </Row>

View file

@ -4,19 +4,29 @@ import { Alert, Button, Col, Row } from 'reactstrap';
import Panel from './Panel'; import Panel from './Panel';
class PanelList extends Component { interface PanelListState {
constructor(props) { panels: {
key: string,
}[],
metricNames: string[],
fetchMetricsError: string | null,
timeDriftError: string | null,
}
class PanelList extends Component<any, PanelListState> {
key: number;
constructor(props: []) {
super(props); super(props);
this.state = { this.state = {
panels: [], panels: [],
metrics: [], metricNames: [],
fetchMetricsError: null, fetchMetricsError: null,
timeDriftError: null, timeDriftError: null,
}; };
this.key = 0;
this.addPanel = this.addPanel.bind(this); this.key = 0;
this.removePanel = this.removePanel.bind(this);
} }
componentDidMount() { componentDidMount() {
@ -30,8 +40,8 @@ class PanelList extends Component {
throw new Error('Unexpected response status when fetching metric names: ' + resp.statusText); // TODO extract error throw new Error('Unexpected response status when fetching metric names: ' + resp.statusText); // TODO extract error
} }
}) })
.then(json => this.setState({ metrics: json.data })) .then(json => this.setState({ metricNames: json.data }))
.catch(error => this.setState({fetchMetricsError: error.message})); .catch(error => this.setState({ fetchMetricsError: error.message }));
const browserTime = new Date().getTime() / 1000; const browserTime = new Date().getTime() / 1000;
fetch("http://demo.robustperception.io:9090/api/v1/query?query=time()", {cache: "no-store"}) fetch("http://demo.robustperception.io:9090/api/v1/query?query=time()", {cache: "no-store"})
@ -50,21 +60,21 @@ class PanelList extends Component {
throw new Error('Detected ' + delta + ' seconds time difference between your browser and the server. Prometheus relies on accurate time and time drift might cause unexpected query results.'); throw new Error('Detected ' + delta + ' seconds time difference between your browser and the server. Prometheus relies on accurate time and time drift might cause unexpected query results.');
} }
}) })
.catch(error => this.setState({timeDriftError: error.message})); .catch(error => this.setState({ timeDriftError: error.message }));
} }
getKey() { getKey(): string {
return (this.key++).toString(); return (this.key++).toString();
} }
addPanel() { addPanel = (): void => {
const panels = this.state.panels.slice(); const panels = this.state.panels.slice();
const key = this.getKey(); const key = this.getKey();
panels.push({key: key}); panels.push({key: key});
this.setState({panels: panels}); this.setState({panels: panels});
} }
removePanel(key) { removePanel = (key: string): void => {
const panels = this.state.panels.filter(panel => { const panels = this.state.panels.filter(panel => {
return panel.key !== key; return panel.key !== key;
}); });
@ -85,7 +95,7 @@ class PanelList extends Component {
</Col> </Col>
</Row> </Row>
{this.state.panels.map(p => {this.state.panels.map(p =>
<Panel key={p.key} removePanel={() => this.removePanel(p.key)} metrics={this.state.metrics}/> <Panel key={p.key} removePanel={() => this.removePanel(p.key)} metricNames={this.state.metricNames}/>
)} )}
<Button color="primary" className="add-panel-btn" onClick={this.addPanel}>Add Panel</Button> <Button color="primary" className="add-panel-btn" onClick={this.addPanel}>Add Panel</Button>
</> </>

1
src/react-app-env.d.ts vendored Normal file
View file

@ -0,0 +1 @@
/// <reference types="react-scripts" />