mirror of
https://github.com/grafana/grafana.git
synced 2025-02-16 18:34:52 -06:00
143 lines
4.8 KiB
TypeScript
143 lines
4.8 KiB
TypeScript
import React, { MouseEvent, PureComponent } from 'react';
|
|
import { NEW_VARIABLE_ID, toVariableIdentifier, toVariablePayload, VariableIdentifier } from '../state/types';
|
|
import { StoreState } from '../../../types';
|
|
import { e2e } from '@grafana/e2e';
|
|
import { VariableEditorList } from './VariableEditorList';
|
|
import { VariableEditorEditor } from './VariableEditorEditor';
|
|
import { MapDispatchToProps, MapStateToProps } from 'react-redux';
|
|
import { connectWithStore } from '../../../core/utils/connectWithReduxStore';
|
|
import { getVariables } from '../state/selectors';
|
|
import { VariableModel } from '../../templating/types';
|
|
import { switchToEditMode, switchToListMode, switchToNewMode } from './actions';
|
|
import { changeVariableOrder, duplicateVariable, removeVariable } from '../state/sharedReducer';
|
|
|
|
interface OwnProps {}
|
|
|
|
interface ConnectedProps {
|
|
idInEditor: string | null;
|
|
variables: VariableModel[];
|
|
}
|
|
|
|
interface DispatchProps {
|
|
changeVariableOrder: typeof changeVariableOrder;
|
|
duplicateVariable: typeof duplicateVariable;
|
|
removeVariable: typeof removeVariable;
|
|
switchToNewMode: typeof switchToNewMode;
|
|
switchToEditMode: typeof switchToEditMode;
|
|
switchToListMode: typeof switchToListMode;
|
|
}
|
|
|
|
type Props = OwnProps & ConnectedProps & DispatchProps;
|
|
|
|
class VariableEditorContainerUnconnected extends PureComponent<Props> {
|
|
componentDidMount(): void {
|
|
this.props.switchToListMode();
|
|
}
|
|
|
|
onChangeToListMode = (event: MouseEvent<HTMLAnchorElement>) => {
|
|
event.preventDefault();
|
|
this.props.switchToListMode();
|
|
};
|
|
|
|
onEditVariable = (identifier: VariableIdentifier) => {
|
|
this.props.switchToEditMode(identifier);
|
|
};
|
|
|
|
onNewVariable = (event: MouseEvent<HTMLAnchorElement>) => {
|
|
event.preventDefault();
|
|
this.props.switchToNewMode();
|
|
};
|
|
|
|
onChangeVariableOrder = (identifier: VariableIdentifier, fromIndex: number, toIndex: number) => {
|
|
this.props.changeVariableOrder(toVariablePayload(identifier, { fromIndex, toIndex }));
|
|
};
|
|
|
|
onDuplicateVariable = (identifier: VariableIdentifier) => {
|
|
this.props.duplicateVariable(toVariablePayload(identifier, { newId: (undefined as unknown) as string }));
|
|
};
|
|
|
|
onRemoveVariable = (identifier: VariableIdentifier) => {
|
|
this.props.removeVariable(toVariablePayload(identifier, { reIndex: true }));
|
|
};
|
|
|
|
render() {
|
|
const variableToEdit = this.props.variables.find(s => s.id === this.props.idInEditor) ?? null;
|
|
return (
|
|
<div>
|
|
<div className="page-action-bar">
|
|
<h3 className="dashboard-settings__header">
|
|
<a
|
|
onClick={this.onChangeToListMode}
|
|
aria-label={e2e.pages.Dashboard.Settings.Variables.Edit.General.selectors.headerLink}
|
|
>
|
|
Variables
|
|
</a>
|
|
{this.props.idInEditor === NEW_VARIABLE_ID && (
|
|
<span>
|
|
<i
|
|
className="fa fa-fw fa-chevron-right"
|
|
aria-label={e2e.pages.Dashboard.Settings.Variables.Edit.General.selectors.modeLabelNew}
|
|
/>
|
|
New
|
|
</span>
|
|
)}
|
|
{this.props.idInEditor && this.props.idInEditor !== NEW_VARIABLE_ID && (
|
|
<span>
|
|
<i
|
|
className="fa fa-fw fa-chevron-right"
|
|
aria-label={e2e.pages.Dashboard.Settings.Variables.Edit.General.selectors.modeLabelEdit}
|
|
/>
|
|
Edit
|
|
</span>
|
|
)}
|
|
</h3>
|
|
|
|
<div className="page-action-bar__spacer" />
|
|
{this.props.variables.length > 0 && variableToEdit === null && (
|
|
<a
|
|
type="button"
|
|
className="btn btn-primary"
|
|
onClick={this.onNewVariable}
|
|
aria-label={e2e.pages.Dashboard.Settings.Variables.List.selectors.newButton}
|
|
>
|
|
New
|
|
</a>
|
|
)}
|
|
</div>
|
|
|
|
{!variableToEdit && (
|
|
<VariableEditorList
|
|
variables={this.props.variables}
|
|
onAddClick={this.onNewVariable}
|
|
onEditClick={this.onEditVariable}
|
|
onChangeVariableOrder={this.onChangeVariableOrder}
|
|
onDuplicateVariable={this.onDuplicateVariable}
|
|
onRemoveVariable={this.onRemoveVariable}
|
|
/>
|
|
)}
|
|
{variableToEdit && <VariableEditorEditor identifier={toVariableIdentifier(variableToEdit)} />}
|
|
</div>
|
|
);
|
|
}
|
|
}
|
|
|
|
const mapStateToProps: MapStateToProps<ConnectedProps, OwnProps, StoreState> = state => ({
|
|
variables: getVariables(state, true),
|
|
idInEditor: state.templating.editor.id,
|
|
});
|
|
|
|
const mapDispatchToProps: MapDispatchToProps<DispatchProps, OwnProps> = {
|
|
changeVariableOrder,
|
|
duplicateVariable,
|
|
removeVariable,
|
|
switchToNewMode,
|
|
switchToEditMode,
|
|
switchToListMode,
|
|
};
|
|
|
|
export const VariableEditorContainer = connectWithStore(
|
|
VariableEditorContainerUnconnected,
|
|
mapStateToProps,
|
|
mapDispatchToProps
|
|
);
|