grafana/public/app/features/variables/editor/VariableEditorContainer.tsx
Torkel Ödegaard 264645eecd
TopNav: Dashboard settings (#52682)
* Scenes: Support new top nav

* Page: Make Page component support new and old dashboard page layouts

* Pass scrollbar props

* Fixing flex layout for dashboard

* Progress on dashboard settings working with topnav

* Updated

* Annotations working

* Starting to work fully

* Fix merge issue

* Fixed tests

* Added buttons to annotations editor

* Updating tests

* Move Page component to each page

* fixed general settings page

* Fixed versions

* Fixed annotation item page

* Variables section working

* Fixed tests

* Minor fixes to versions

* Update

* Fixing unit tests

* Adding add variable button

* Restore annotations edit form so it's the same as before

* Fixed semicolon in dashboard permissions

* Fixing unit tests

* Fixing tests

* Minor test update

* Fixing unit test

* Fixing e2e tests

* fix for e2e test

* fix a11y issues

* Changing places Settings -> General

* Trying to fix a11y

* I hope this fixes the e2e test

* Fixing merge issue

* tweak
2022-08-24 18:05:12 +02:00

118 lines
4.4 KiB
TypeScript

import React, { PureComponent } from 'react';
import { connect, ConnectedProps } from 'react-redux';
import { bindActionCreators } from 'redux';
import { locationService } from '@grafana/runtime';
import { Page } from 'app/core/components/PageNew/Page';
import { SettingsPageProps } from 'app/features/dashboard/components/DashboardSettings/types';
import { StoreState, ThunkDispatch } from '../../../types';
import { VariablesUnknownTable } from '../inspect/VariablesUnknownTable';
import { toKeyedAction } from '../state/keyedVariablesReducer';
import { getEditorVariables, getVariablesState } from '../state/selectors';
import { changeVariableOrder, duplicateVariable, removeVariable } from '../state/sharedReducer';
import { KeyedVariableIdentifier } from '../state/types';
import { toKeyedVariableIdentifier, toVariablePayload } from '../utils';
import { VariableEditorEditor } from './VariableEditorEditor';
import { VariableEditorList } from './VariableEditorList';
import { createNewVariable, initListMode } from './actions';
const mapStateToProps = (state: StoreState, ownProps: OwnProps) => {
const { uid } = ownProps.dashboard;
const templatingState = getVariablesState(uid, state);
return {
variables: getEditorVariables(uid, state),
idInEditor: templatingState.editor.id,
usagesNetwork: templatingState.inspect.usagesNetwork,
usages: templatingState.inspect.usages,
};
};
const mapDispatchToProps = (dispatch: ThunkDispatch) => {
return {
...bindActionCreators({ createNewVariable, initListMode }, dispatch),
changeVariableOrder: (identifier: KeyedVariableIdentifier, fromIndex: number, toIndex: number) =>
dispatch(
toKeyedAction(
identifier.rootStateKey,
changeVariableOrder(toVariablePayload(identifier, { fromIndex, toIndex }))
)
),
duplicateVariable: (identifier: KeyedVariableIdentifier) =>
dispatch(
toKeyedAction(
identifier.rootStateKey,
duplicateVariable(toVariablePayload(identifier, { newId: undefined as unknown as string }))
)
),
removeVariable: (identifier: KeyedVariableIdentifier) => {
dispatch(
toKeyedAction(identifier.rootStateKey, removeVariable(toVariablePayload(identifier, { reIndex: true })))
);
},
};
};
interface OwnProps extends SettingsPageProps {}
const connector = connect(mapStateToProps, mapDispatchToProps);
type Props = OwnProps & ConnectedProps<typeof connector>;
class VariableEditorContainerUnconnected extends PureComponent<Props> {
componentDidMount() {
this.props.initListMode(this.props.dashboard.uid);
}
onEditVariable = (identifier: KeyedVariableIdentifier) => {
const index = this.props.variables.findIndex((x) => x.id === identifier.id);
locationService.partial({ editIndex: index });
};
onNewVariable = () => {
this.props.createNewVariable(this.props.dashboard.uid);
};
onChangeVariableOrder = (identifier: KeyedVariableIdentifier, fromIndex: number, toIndex: number) => {
this.props.changeVariableOrder(identifier, fromIndex, toIndex);
};
onDuplicateVariable = (identifier: KeyedVariableIdentifier) => {
this.props.duplicateVariable(identifier);
};
onRemoveVariable = (identifier: KeyedVariableIdentifier) => {
this.props.removeVariable(identifier);
};
render() {
const { editIndex, variables } = this.props;
const variableToEdit = editIndex != null ? variables[editIndex] : undefined;
const subPageNav = variableToEdit ? { text: variableToEdit.name } : undefined;
return (
<Page navModel={this.props.sectionNav} pageNav={subPageNav}>
{!variableToEdit && (
<VariableEditorList
variables={this.props.variables}
onAdd={this.onNewVariable}
onEdit={this.onEditVariable}
onChangeOrder={this.onChangeVariableOrder}
onDuplicate={this.onDuplicateVariable}
onDelete={this.onRemoveVariable}
usages={this.props.usages}
usagesNetwork={this.props.usagesNetwork}
/>
)}
{!variableToEdit && this.props.variables.length > 0 && (
<VariablesUnknownTable variables={this.props.variables} dashboard={this.props.dashboard} />
)}
{variableToEdit && <VariableEditorEditor identifier={toKeyedVariableIdentifier(variableToEdit)} />}
</Page>
);
}
}
export const VariableEditorContainer = connector(VariableEditorContainerUnconnected);