FolderPermissions.tsx 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { Tooltip, Icon, Button } from '@grafana/ui';
  4. import { SlideDown } from 'app/core/components/Animations/SlideDown';
  5. import Page from 'app/core/components/Page/Page';
  6. import AddPermission from 'app/core/components/PermissionList/AddPermission';
  7. import PermissionList from 'app/core/components/PermissionList/PermissionList';
  8. import PermissionsInfo from 'app/core/components/PermissionList/PermissionsInfo';
  9. import { GrafanaRouteComponentProps } from 'app/core/navigation/types';
  10. import { getNavModel } from 'app/core/selectors/navModel';
  11. import { StoreState } from 'app/types';
  12. import { DashboardAcl, PermissionLevel, NewDashboardAclItem } from 'app/types/acl';
  13. import {
  14. getFolderByUid,
  15. getFolderPermissions,
  16. updateFolderPermission,
  17. removeFolderPermission,
  18. addFolderPermission,
  19. } from './state/actions';
  20. import { getLoadingNav } from './state/navModel';
  21. export interface OwnProps extends GrafanaRouteComponentProps<{ uid: string }> {}
  22. const mapStateToProps = (state: StoreState, props: OwnProps) => {
  23. const uid = props.match.params.uid;
  24. return {
  25. navModel: getNavModel(state.navIndex, `folder-permissions-${uid}`, getLoadingNav(1)),
  26. folderUid: uid,
  27. folder: state.folder,
  28. };
  29. };
  30. const mapDispatchToProps = {
  31. getFolderByUid,
  32. getFolderPermissions,
  33. updateFolderPermission,
  34. removeFolderPermission,
  35. addFolderPermission,
  36. };
  37. const connector = connect(mapStateToProps, mapDispatchToProps);
  38. export type Props = OwnProps & ConnectedProps<typeof connector>;
  39. export interface State {
  40. isAdding: boolean;
  41. }
  42. export class FolderPermissions extends PureComponent<Props, State> {
  43. constructor(props: Props) {
  44. super(props);
  45. this.state = {
  46. isAdding: false,
  47. };
  48. }
  49. componentDidMount() {
  50. this.props.getFolderByUid(this.props.folderUid);
  51. this.props.getFolderPermissions(this.props.folderUid);
  52. }
  53. onOpenAddPermissions = () => {
  54. this.setState({ isAdding: true });
  55. };
  56. onRemoveItem = (item: DashboardAcl) => {
  57. this.props.removeFolderPermission(item);
  58. };
  59. onPermissionChanged = (item: DashboardAcl, level: PermissionLevel) => {
  60. this.props.updateFolderPermission(item, level);
  61. };
  62. onAddPermission = (newItem: NewDashboardAclItem) => {
  63. return this.props.addFolderPermission(newItem);
  64. };
  65. onCancelAddPermission = () => {
  66. this.setState({ isAdding: false });
  67. };
  68. render() {
  69. const { navModel, folder } = this.props;
  70. const { isAdding } = this.state;
  71. if (folder.id === 0) {
  72. return (
  73. <Page navModel={navModel}>
  74. <Page.Contents isLoading={true}>
  75. <span />
  76. </Page.Contents>
  77. </Page>
  78. );
  79. }
  80. const folderInfo = { title: folder.title, url: folder.url, id: folder.id };
  81. return (
  82. <Page navModel={navModel}>
  83. <Page.Contents>
  84. <div className="page-action-bar">
  85. <h3 className="page-sub-heading">Folder Permissions</h3>
  86. <Tooltip placement="auto" content={<PermissionsInfo />}>
  87. <Icon className="icon--has-hover page-sub-heading-icon" name="question-circle" />
  88. </Tooltip>
  89. <div className="page-action-bar__spacer" />
  90. <Button className="pull-right" onClick={this.onOpenAddPermissions} disabled={isAdding}>
  91. Add Permission
  92. </Button>
  93. </div>
  94. <SlideDown in={isAdding}>
  95. <AddPermission onAddPermission={this.onAddPermission} onCancel={this.onCancelAddPermission} />
  96. </SlideDown>
  97. <PermissionList
  98. items={folder.permissions}
  99. onRemoveItem={this.onRemoveItem}
  100. onPermissionChanged={this.onPermissionChanged}
  101. isFetching={false}
  102. folderInfo={folderInfo}
  103. />
  104. </Page.Contents>
  105. </Page>
  106. );
  107. }
  108. }
  109. export default connector(FolderPermissions);