UsersListPage.tsx 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. import React, { PureComponent } from 'react';
  2. import { connect, ConnectedProps } from 'react-redux';
  3. import { renderMarkdown } from '@grafana/data';
  4. import { HorizontalGroup, Pagination, VerticalGroup } from '@grafana/ui';
  5. import Page from 'app/core/components/Page/Page';
  6. import { getNavModel } from 'app/core/selectors/navModel';
  7. import { contextSrv } from 'app/core/services/context_srv';
  8. import { OrgUser, OrgRole, StoreState } from 'app/types';
  9. import InviteesTable from '../invites/InviteesTable';
  10. import { fetchInvitees } from '../invites/state/actions';
  11. import { selectInvitesMatchingQuery } from '../invites/state/selectors';
  12. import UsersActionBar from './UsersActionBar';
  13. import UsersTable from './UsersTable';
  14. import { loadUsers, removeUser, updateUser } from './state/actions';
  15. import { setUsersSearchQuery, setUsersSearchPage } from './state/reducers';
  16. import { getUsers, getUsersSearchQuery, getUsersSearchPage } from './state/selectors';
  17. function mapStateToProps(state: StoreState) {
  18. const searchQuery = getUsersSearchQuery(state.users);
  19. return {
  20. navModel: getNavModel(state.navIndex, 'users'),
  21. users: getUsers(state.users),
  22. searchQuery: getUsersSearchQuery(state.users),
  23. searchPage: getUsersSearchPage(state.users),
  24. invitees: selectInvitesMatchingQuery(state.invites, searchQuery),
  25. externalUserMngInfo: state.users.externalUserMngInfo,
  26. hasFetched: state.users.hasFetched,
  27. };
  28. }
  29. const mapDispatchToProps = {
  30. loadUsers,
  31. fetchInvitees,
  32. setUsersSearchQuery,
  33. setUsersSearchPage,
  34. updateUser,
  35. removeUser,
  36. };
  37. const connector = connect(mapStateToProps, mapDispatchToProps);
  38. export type Props = ConnectedProps<typeof connector>;
  39. export interface State {
  40. showInvites: boolean;
  41. }
  42. const pageLimit = 30;
  43. export class UsersListPage extends PureComponent<Props, State> {
  44. declare externalUserMngInfoHtml: string;
  45. constructor(props: Props) {
  46. super(props);
  47. if (this.props.externalUserMngInfo) {
  48. this.externalUserMngInfoHtml = renderMarkdown(this.props.externalUserMngInfo);
  49. }
  50. this.state = {
  51. showInvites: false,
  52. };
  53. }
  54. componentDidMount() {
  55. this.fetchUsers();
  56. this.fetchInvitees();
  57. }
  58. async fetchUsers() {
  59. return await this.props.loadUsers();
  60. }
  61. async fetchInvitees() {
  62. return await this.props.fetchInvitees();
  63. }
  64. onRoleChange = (role: OrgRole, user: OrgUser) => {
  65. const updatedUser = { ...user, role: role };
  66. this.props.updateUser(updatedUser);
  67. };
  68. onShowInvites = () => {
  69. this.setState((prevState) => ({
  70. showInvites: !prevState.showInvites,
  71. }));
  72. };
  73. getPaginatedUsers = (users: OrgUser[]) => {
  74. const offset = (this.props.searchPage - 1) * pageLimit;
  75. return users.slice(offset, offset + pageLimit);
  76. };
  77. renderTable() {
  78. const { invitees, users, setUsersSearchPage } = this.props;
  79. const paginatedUsers = this.getPaginatedUsers(users);
  80. const totalPages = Math.ceil(users.length / pageLimit);
  81. if (this.state.showInvites) {
  82. return <InviteesTable invitees={invitees} />;
  83. } else {
  84. return (
  85. <VerticalGroup spacing="md">
  86. <UsersTable
  87. users={paginatedUsers}
  88. orgId={contextSrv.user.orgId}
  89. onRoleChange={(role, user) => this.onRoleChange(role, user)}
  90. onRemoveUser={(user) => this.props.removeUser(user.userId)}
  91. />
  92. <HorizontalGroup justify="flex-end">
  93. <Pagination
  94. onNavigate={setUsersSearchPage}
  95. currentPage={this.props.searchPage}
  96. numberOfPages={totalPages}
  97. hideWhenSinglePage={true}
  98. />
  99. </HorizontalGroup>
  100. </VerticalGroup>
  101. );
  102. }
  103. }
  104. render() {
  105. const { navModel, hasFetched } = this.props;
  106. const externalUserMngInfoHtml = this.externalUserMngInfoHtml;
  107. return (
  108. <Page navModel={navModel}>
  109. <Page.Contents isLoading={!hasFetched}>
  110. <>
  111. <UsersActionBar onShowInvites={this.onShowInvites} showInvites={this.state.showInvites} />
  112. {externalUserMngInfoHtml && (
  113. <div className="grafana-info-box" dangerouslySetInnerHTML={{ __html: externalUserMngInfoHtml }} />
  114. )}
  115. {hasFetched && this.renderTable()}
  116. </>
  117. </Page.Contents>
  118. </Page>
  119. );
  120. }
  121. }
  122. export default connector(UsersListPage);