ProjectStore.ts 4.7 KB

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