import React, { PureComponent } from 'react'; import { SelectableValue } from '@grafana/data'; import { Button, Form, HorizontalGroup, Select } from '@grafana/ui'; import { CloseButton } from 'app/core/components/CloseButton/CloseButton'; import { TeamPicker } from 'app/core/components/Select/TeamPicker'; import { UserPicker } from 'app/core/components/Select/UserPicker'; import { Team, dataSourceAclLevels, DataSourcePermissionLevel, AclTarget } from 'app/types'; export interface Props { onAddPermission: (state: State) => void; onCancel: () => void; } export interface State { userId: number; teamId: number; type: AclTarget; permission: DataSourcePermissionLevel; } export class AddDataSourcePermissions extends PureComponent { cleanState(): State { return { userId: 0, teamId: 0, type: AclTarget.Team, permission: DataSourcePermissionLevel.Query, }; } state: State = this.cleanState(); isValid() { switch (this.state.type) { case AclTarget.Team: return this.state.teamId > 0; case AclTarget.User: return this.state.userId > 0; } return true; } onTeamSelected = (team: SelectableValue) => { const value = team?.value; this.setState({ teamId: value ? value.id : 0 }); }; onUserSelected = (user: SelectableValue) => { this.setState({ userId: user ? user.id : 0 }); }; onPermissionChanged = (permission: SelectableValue) => { this.setState({ permission: permission.value! }); }; onTypeChanged = (item: SelectableValue) => { this.setState({ type: item.value!, userId: 0, teamId: 0 }); }; onSubmit = async () => { await this.props.onAddPermission(this.state); this.setState(this.cleanState()); }; render() { const { onCancel } = this.props; const { type } = this.state; const pickerClassName = 'width-20'; const aclTargets = [ { value: AclTarget.Team, label: 'Team' }, { value: AclTarget.User, label: 'User' }, ]; return (
Add Permission For
{() => ( l.value === this.state.permission)} width={25} menuShouldPortal /> )}
); } }