ProjectStore.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. // @flow
  15. import { observable, action, runInAction } from 'mobx'
  16. import type { Project, RoleAssignment, Role } from '../types/Project'
  17. import type { User } from '../types/User'
  18. import ProjectSource from '../sources/ProjectSource'
  19. import userStore from '../stores/UserStore'
  20. class ProjectStore {
  21. @observable projects: Project[] = []
  22. @observable loading: boolean = false
  23. @observable roleAssignments: RoleAssignment[] = []
  24. @observable roles: Role[] = []
  25. @observable projectDetails: ?Project = null
  26. @observable users: User[] = []
  27. @observable usersLoading: boolean = false
  28. @observable updating: boolean = false
  29. @action async getProjects(options?: { showLoading?: boolean, skipLog?: boolean }) {
  30. if (options && options.showLoading) this.loading = true
  31. try {
  32. let projects = await ProjectSource.getProjects(options && options.skipLog)
  33. runInAction(() => {
  34. this.loading = false
  35. this.projects = projects
  36. })
  37. } catch (e) {
  38. runInAction(() => { this.loading = false })
  39. }
  40. }
  41. @action async getRoleAssignments(options?: { skipLog?: boolean }) {
  42. let assignments = await ProjectSource.getRoleAssignments(options && options.skipLog)
  43. runInAction(() => {
  44. this.roleAssignments = assignments
  45. })
  46. }
  47. @action async getRoles() {
  48. let roles = await ProjectSource.getRoles()
  49. runInAction(() => { this.roles = roles })
  50. }
  51. @action async getProjectDetails(projectId: string) {
  52. this.loading = true
  53. try {
  54. let project = await ProjectSource.getProjectDetails(projectId)
  55. runInAction(() => {
  56. this.projectDetails = project
  57. this.loading = false
  58. })
  59. } catch (e) {
  60. runInAction(() => { this.loading = false })
  61. }
  62. }
  63. @action async getUsers(projectId: string, showLoading?: boolean) {
  64. if (showLoading) this.usersLoading = true
  65. try {
  66. let users = await ProjectSource.getUsers(projectId)
  67. runInAction(() => {
  68. this.usersLoading = false
  69. this.users = users
  70. })
  71. } catch (e) {
  72. runInAction(() => { this.usersLoading = false })
  73. }
  74. }
  75. @action clearProjectDetails() {
  76. this.projectDetails = null
  77. this.users = []
  78. }
  79. @action async removeUser(projectId: string, userId: string, roleIds: string[]) {
  80. await ProjectSource.removeUser(projectId, userId, roleIds)
  81. runInAction(() => { this.users = this.users.filter(u => u.id !== userId) })
  82. }
  83. @action async assignUserRole(projectId: string, userId: string, roleId: string) {
  84. await ProjectSource.assignUser(projectId, userId, roleId)
  85. }
  86. @action async removeUserRole(projectId: string, userId: string, roleId: string) {
  87. await ProjectSource.removeUser(projectId, userId, [roleId])
  88. }
  89. @action async update(projectId: string, project: Project) {
  90. this.updating = true
  91. try {
  92. let updatedProject = await ProjectSource.update(projectId, project)
  93. runInAction(() => {
  94. this.projectDetails = updatedProject
  95. this.updating = false
  96. })
  97. } catch (e) {
  98. runInAction(() => { this.updating = false })
  99. }
  100. }
  101. @action async delete(projectId: string) {
  102. await ProjectSource.delete(projectId)
  103. }
  104. @action async add(project: Project) {
  105. this.updating = true
  106. let userId = userStore.loggedUser ? userStore.loggedUser.id : 'undefined'
  107. try {
  108. let addedProject = await ProjectSource.add(project, userId)
  109. runInAction(() => {
  110. if (!this.projects.find(p => p.id === addedProject.id)) {
  111. let projects = this.projects
  112. projects = [
  113. ...projects,
  114. addedProject,
  115. ]
  116. projects.sort((a, b) => a.name.localeCompare(b.name))
  117. this.projects = [...projects]
  118. }
  119. this.updating = false
  120. })
  121. } catch (e) {
  122. runInAction(() => { this.updating = false })
  123. }
  124. }
  125. }
  126. export default new ProjectStore()