AddDataSourcePermissions.tsx 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import React, { PureComponent } from 'react';
  2. import { SelectableValue } from '@grafana/data';
  3. import { Button, Form, HorizontalGroup, Select } from '@grafana/ui';
  4. import { CloseButton } from 'app/core/components/CloseButton/CloseButton';
  5. import { TeamPicker } from 'app/core/components/Select/TeamPicker';
  6. import { UserPicker } from 'app/core/components/Select/UserPicker';
  7. import { Team, dataSourceAclLevels, DataSourcePermissionLevel, AclTarget } from 'app/types';
  8. export interface Props {
  9. onAddPermission: (state: State) => void;
  10. onCancel: () => void;
  11. }
  12. export interface State {
  13. userId: number;
  14. teamId: number;
  15. type: AclTarget;
  16. permission: DataSourcePermissionLevel;
  17. }
  18. export class AddDataSourcePermissions extends PureComponent<Props, State> {
  19. cleanState(): State {
  20. return {
  21. userId: 0,
  22. teamId: 0,
  23. type: AclTarget.Team,
  24. permission: DataSourcePermissionLevel.Query,
  25. };
  26. }
  27. state: State = this.cleanState();
  28. isValid() {
  29. switch (this.state.type) {
  30. case AclTarget.Team:
  31. return this.state.teamId > 0;
  32. case AclTarget.User:
  33. return this.state.userId > 0;
  34. }
  35. return true;
  36. }
  37. onTeamSelected = (team: SelectableValue<Team>) => {
  38. const value = team?.value;
  39. this.setState({ teamId: value ? value.id : 0 });
  40. };
  41. onUserSelected = (user: SelectableValue<number>) => {
  42. this.setState({ userId: user ? user.id : 0 });
  43. };
  44. onPermissionChanged = (permission: SelectableValue<DataSourcePermissionLevel>) => {
  45. this.setState({ permission: permission.value! });
  46. };
  47. onTypeChanged = (item: SelectableValue<AclTarget>) => {
  48. this.setState({ type: item.value!, userId: 0, teamId: 0 });
  49. };
  50. onSubmit = async () => {
  51. await this.props.onAddPermission(this.state);
  52. this.setState(this.cleanState());
  53. };
  54. render() {
  55. const { onCancel } = this.props;
  56. const { type } = this.state;
  57. const pickerClassName = 'width-20';
  58. const aclTargets = [
  59. { value: AclTarget.Team, label: 'Team' },
  60. { value: AclTarget.User, label: 'User' },
  61. ];
  62. return (
  63. <div className="cta-form" aria-label="Permissions slider">
  64. <CloseButton onClick={onCancel} />
  65. <h5>Add Permission For</h5>
  66. <Form name="addPermission" maxWidth="none" onSubmit={this.onSubmit}>
  67. {() => (
  68. <HorizontalGroup>
  69. <Select
  70. aria-label="Role to add new permission to"
  71. isSearchable={false}
  72. value={type}
  73. options={aclTargets}
  74. onChange={this.onTypeChanged}
  75. menuShouldPortal
  76. />
  77. {type === AclTarget.User && <UserPicker onSelected={this.onUserSelected} className={pickerClassName} />}
  78. {type === AclTarget.Team && <TeamPicker onSelected={this.onTeamSelected} className={pickerClassName} />}
  79. <Select
  80. isSearchable={false}
  81. options={dataSourceAclLevels}
  82. onChange={this.onPermissionChanged}
  83. value={dataSourceAclLevels.find((l) => l.value === this.state.permission)}
  84. width={25}
  85. menuShouldPortal
  86. />
  87. <Button type="submit" disabled={!this.isValid()}>
  88. Save
  89. </Button>
  90. </HorizontalGroup>
  91. )}
  92. </Form>
  93. </div>
  94. );
  95. }
  96. }