UsersPage.tsx 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. /*
  2. Copyright (C) 2017 Cloudbase Solutions SRL
  3. This program is free software: you can redistribute it and/or modify
  4. it under the terms of the GNU Affero General Public License as
  5. published by the Free Software Foundation, either version 3 of the
  6. License, or (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU Affero General Public License for more details.
  11. You should have received a copy of the GNU Affero General Public License
  12. along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. */
  14. import React from 'react'
  15. import styled from 'styled-components'
  16. import { observer } from 'mobx-react'
  17. import MainTemplate from '@src/components/modules/TemplateModule/MainTemplate'
  18. import Navigation from '@src/components/modules/NavigationModule/Navigation'
  19. import FilterList from '@src/components/ui/Lists/FilterList'
  20. import UserListItem from '@src/components/modules/UserModule/UserListItem'
  21. import type { User } from '@src/@types/User'
  22. import projectStore from '@src/stores/ProjectStore'
  23. import userStore from '@src/stores/UserStore'
  24. import configLoader from '@src/utils/Config'
  25. import PageHeader from '@src/components/smart/PageHeader'
  26. const Wrapper = styled.div<any>``
  27. type State = {
  28. modalIsOpen: boolean,
  29. }
  30. @observer
  31. class UsersPage extends React.Component<{ history: any }, State> {
  32. state = {
  33. modalIsOpen: false,
  34. }
  35. pollTimeout: number = 0
  36. stopPolling: boolean = false
  37. componentDidMount() {
  38. document.title = 'Users'
  39. projectStore.getProjects()
  40. userStore.getAllUsers()
  41. this.stopPolling = false
  42. this.pollData(true)
  43. }
  44. componentWillUnmount() {
  45. clearTimeout(this.pollTimeout)
  46. this.stopPolling = true
  47. }
  48. getProjectName(projectId?: string | null): string {
  49. if (!projectId) {
  50. return '-'
  51. }
  52. const project = projectStore.projects.find(p => p.id === projectId)
  53. return project ? project.name : '-'
  54. }
  55. handleModalOpen() {
  56. this.setState({ modalIsOpen: true })
  57. }
  58. handleModalClose() {
  59. this.setState({ modalIsOpen: false }, () => {
  60. this.pollData()
  61. })
  62. }
  63. handleReloadButtonClick() {
  64. projectStore.getProjects()
  65. userStore.getAllUsers({ showLoading: true })
  66. }
  67. async pollData(showLoading?: boolean) {
  68. if (this.state.modalIsOpen || this.stopPolling) {
  69. return
  70. }
  71. await userStore.getAllUsers({ showLoading, skipLog: true })
  72. this.pollTimeout = window.setTimeout(() => { this.pollData() }, configLoader.config.requestPollTimeout)
  73. }
  74. itemFilterFunction(item: User, filterItem?: string | null, filterText?: string): boolean {
  75. const usableFilterText = (filterText && filterText.toLowerCase()) || ''
  76. return (
  77. (
  78. filterItem === 'all'
  79. || item.project_id === filterItem
  80. ) && (
  81. item.name.toLowerCase().indexOf(usableFilterText) > -1
  82. || (
  83. item.description ? item.description.toLowerCase().indexOf(usableFilterText) > -1 : false
  84. )
  85. || (item.email ? item.email.toLowerCase().indexOf(usableFilterText) > -1 : false)
  86. ))
  87. }
  88. render() {
  89. const filterItems = projectStore.projects
  90. .map(p => ({ label: p.name, value: p.id }))
  91. .sort((a, b) => a.label.localeCompare(b.label))
  92. return (
  93. <Wrapper>
  94. <MainTemplate
  95. navigationComponent={<Navigation currentPage="users" />}
  96. listNoMargin
  97. listComponent={(
  98. <FilterList
  99. filterItems={[{ label: 'All', value: 'all' }].concat(filterItems)}
  100. selectionLabel="user"
  101. loading={userStore.allUsersLoading}
  102. items={userStore.users}
  103. onItemClick={(user: User) => { this.props.history.push(`/users/${user.id}`) }}
  104. onReloadButtonClick={() => { this.handleReloadButtonClick() }}
  105. itemFilterFunction={(...args) => this.itemFilterFunction(...args)}
  106. renderItemComponent={component => (
  107. <UserListItem
  108. {...component}
  109. getProjectName={projectId => this.getProjectName(projectId)}
  110. />
  111. )}
  112. />
  113. )}
  114. headerComponent={(
  115. <PageHeader
  116. title="Users"
  117. onModalOpen={() => { this.handleModalOpen() }}
  118. onModalClose={() => { this.handleModalClose() }}
  119. />
  120. )}
  121. />
  122. </Wrapper>
  123. )
  124. }
  125. }
  126. export default UsersPage