ProjectsPage.tsx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  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 PageHeader from '@src/components/smart/PageHeader'
  21. import ProjectListItem from '@src/components/modules/ProjectModule/ProjectListItem'
  22. import type { Project, RoleAssignment } from '@src/@types/Project'
  23. import projectStore from '@src/stores/ProjectStore'
  24. import userStore from '@src/stores/UserStore'
  25. import configLoader from '@src/utils/Config'
  26. const Wrapper = styled.div<any>``
  27. type State = {
  28. modalIsOpen: boolean,
  29. }
  30. @observer
  31. class ProjectsPage 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 = 'Projects'
  39. this.stopPolling = false
  40. this.pollData(true)
  41. }
  42. componentWillUnmount() {
  43. clearTimeout(this.pollTimeout)
  44. this.stopPolling = true
  45. }
  46. getMembers(projectId: string): number {
  47. return projectStore.roleAssignments
  48. .filter(a => a.scope.project?.id === projectId)
  49. .reduce((uniqueRoles, role) => {
  50. if (!uniqueRoles.find(p => p.user.id === role.user.id)) {
  51. uniqueRoles.push(role)
  52. }
  53. return uniqueRoles
  54. }, [] as RoleAssignment[])
  55. .length
  56. }
  57. isCurrentProject(projectId: string): boolean {
  58. const project = userStore.loggedUser && userStore.loggedUser.project
  59. ? userStore.loggedUser.project : null
  60. return project ? project.id === projectId : false
  61. }
  62. handleModalOpen() {
  63. this.setState({ modalIsOpen: true })
  64. }
  65. handleModalClose() {
  66. this.setState({ modalIsOpen: false }, () => {
  67. this.pollData()
  68. })
  69. }
  70. handleReloadButtonClick() {
  71. projectStore.getProjects({ showLoading: true })
  72. projectStore.getRoleAssignments()
  73. }
  74. async handleSwitchProjectClick(projectId: string) {
  75. await userStore.switchProject(projectId)
  76. projectStore.getProjects()
  77. }
  78. async pollData(showLoading?: boolean) {
  79. if (this.state.modalIsOpen || this.stopPolling) {
  80. return
  81. }
  82. await Promise.all([
  83. projectStore.getProjects({ showLoading, skipLog: true }),
  84. projectStore.getRoleAssignments({ skipLog: true }),
  85. ])
  86. this.pollTimeout = window.setTimeout(() => { this.pollData() }, configLoader.config.requestPollTimeout)
  87. }
  88. itemFilterFunction(item: Project, _?: string | null, filterText?: string): boolean {
  89. const usabledFilterText = (filterText && filterText.toLowerCase()) || ''
  90. return (
  91. item.name.toLowerCase().indexOf(usabledFilterText) > -1
  92. || (item.description ? item.description.toLowerCase().indexOf(usabledFilterText) > -1 : false)
  93. )
  94. }
  95. render() {
  96. return (
  97. <Wrapper>
  98. <MainTemplate
  99. navigationComponent={<Navigation currentPage="projects" />}
  100. listNoMargin
  101. listComponent={(
  102. <FilterList
  103. filterItems={[{ label: 'All', value: 'all' }]}
  104. selectionLabel="user"
  105. loading={projectStore.loading}
  106. items={projectStore.projects}
  107. onItemClick={(user: Project) => { this.props.history.push(`/projects/${user.id}`) }}
  108. onReloadButtonClick={() => { this.handleReloadButtonClick() }}
  109. itemFilterFunction={(...args) => this.itemFilterFunction(...args)}
  110. renderItemComponent={component => (
  111. <ProjectListItem
  112. {...component}
  113. getMembers={projectId => this.getMembers(projectId)}
  114. isCurrentProject={projectId => this.isCurrentProject(projectId)}
  115. onSwitchProjectClick={projectId => this.handleSwitchProjectClick(projectId)}
  116. />
  117. )}
  118. />
  119. )}
  120. headerComponent={(
  121. <PageHeader
  122. title="Projects"
  123. onModalOpen={() => { this.handleModalOpen() }}
  124. onModalClose={() => { this.handleModalClose() }}
  125. />
  126. )}
  127. />
  128. </Wrapper>
  129. )
  130. }
  131. }
  132. export default ProjectsPage