reducer.ts 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  1. /*
  2. * <<
  3. * Davinci
  4. * ==
  5. * Copyright (C) 2016 - 2017 EDP
  6. * ==
  7. * Licensed under the Apache License, Version 2.0 (the "License");
  8. * you may not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS,
  15. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. * >>
  19. */
  20. import produce from 'immer'
  21. import { IOrganizationState } from './types'
  22. import { ActionTypes } from './constants'
  23. import { ActionTypes as ProjectActionTypes } from 'containers/Projects/constants'
  24. import { OrganizationActionType } from './actions'
  25. import { ProjectActionType } from 'containers/Projects/actions'
  26. export const initialState: IOrganizationState = {
  27. organizations: [],
  28. currentOrganization: null,
  29. currentOrganizationLoading: false,
  30. currentOrganizationProjects: [],
  31. currentOrganizationProjectsDetail: null,
  32. currentOrganizationMembers: null,
  33. currentOrganizationRole: null,
  34. inviteMemberLists: null,
  35. roleModalLoading: false,
  36. projectDetail: null,
  37. projectAdmins: null,
  38. projectRoles: null,
  39. inviteMemberfetching: false
  40. }
  41. const organizationReducer = (
  42. state = initialState,
  43. action: OrganizationActionType | ProjectActionType
  44. ) =>
  45. produce(state, (draft) => {
  46. switch (action.type) {
  47. case ActionTypes.DELETE_ORGANIZATION_MEMBER_SUCCESS:
  48. if (draft.currentOrganizationMembers) {
  49. draft.currentOrganizationMembers = draft.currentOrganizationMembers.filter(
  50. (d) => d.id !== action.payload.id
  51. )
  52. }
  53. break
  54. case ActionTypes.LOAD_ORGANIZATIONS_PROJECTS_SUCCESS:
  55. draft.currentOrganizationProjects = action.payload.projects.list
  56. draft.currentOrganizationProjectsDetail = action.payload.projects
  57. break
  58. case ActionTypes.LOAD_ORGANIZATIONS_MEMBERS_SUCCESS:
  59. draft.currentOrganizationMembers = action.payload.members.map((member) => {
  60. return {
  61. ...member,
  62. roles: 'loading'
  63. }
  64. })
  65. break
  66. case ActionTypes.GET_ROLELISTS_BY_MEMBERID_ERROR:
  67. const mId = action.payload.memberId
  68. if (draft.currentOrganizationMembers) {
  69. draft.currentOrganizationMembers = draft.currentOrganizationMembers.map(
  70. (member) => member.user.id === mId ? {...member, roles: undefined} : member
  71. )
  72. }
  73. break
  74. case ActionTypes.GET_ROLELISTS_BY_MEMBERID_SUCCESS:
  75. const { result, memberId} = action.payload
  76. if (draft.currentOrganizationMembers) {
  77. draft.currentOrganizationMembers = draft.currentOrganizationMembers.map(
  78. (member) => member.user.id === memberId ? {...member, roles: result} : member
  79. )
  80. }
  81. break
  82. case ActionTypes.LOAD_ORGANIZATIONS_ROLE_SUCCESS:
  83. draft.currentOrganizationRole = action.payload.role
  84. break
  85. case ActionTypes.LOAD_ORGANIZATIONS_SUCCESS:
  86. draft.organizations = action.payload.organizations
  87. break
  88. case ProjectActionTypes.ADD_PROJECT_SUCCESS:
  89. if (draft.currentOrganizationProjects) {
  90. draft.currentOrganizationProjects.unshift(action.payload.result)
  91. } else {
  92. draft.currentOrganizationProjects = [action.payload.result]
  93. }
  94. break
  95. case ProjectActionTypes.DELETE_PROJECT_SUCCESS:
  96. if (draft.currentOrganizationProjects) {
  97. draft.currentOrganizationProjects = draft.currentOrganizationProjects.filter(
  98. (d) => d.id !== action.payload.id
  99. )
  100. }
  101. break
  102. case ActionTypes.LOAD_ORGANIZATIONS_FAILURE:
  103. break
  104. case ActionTypes.ADD_ORGANIZATION_SUCCESS:
  105. if (draft.organizations) {
  106. draft.organizations.unshift(action.payload.result)
  107. } else {
  108. draft.organizations = [action.payload.result]
  109. }
  110. break
  111. case ActionTypes.ADD_ORGANIZATION_FAILURE:
  112. break
  113. case ActionTypes.EDIT_ORGANIZATION_SUCCESS:
  114. draft.organizations.splice(
  115. draft.organizations.findIndex(
  116. (d) => d.id === action.payload.result.id
  117. ),
  118. 1,
  119. action.payload.result
  120. )
  121. break
  122. case ActionTypes.DELETE_ORGANIZATION_SUCCESS:
  123. draft.organizations = draft.organizations.filter(
  124. (d) => d.id !== action.payload.id
  125. )
  126. break
  127. case ActionTypes.LOAD_ORGANIZATION_DETAIL:
  128. draft.currentOrganizationLoading = true
  129. break
  130. case ActionTypes.LOAD_ORGANIZATION_DETAIL_SUCCESS:
  131. draft.currentOrganizationLoading = false
  132. draft.currentOrganization = action.payload.organization
  133. break
  134. case ActionTypes.LOAD_ORGANIZATION_DETAIL_FAILURE:
  135. break
  136. case ActionTypes.ADD_ROLE:
  137. draft.roleModalLoading = true
  138. break
  139. case ActionTypes.ADD_ROLE_SUCCESS:
  140. draft.roleModalLoading = false
  141. break
  142. case ActionTypes.ADD_ROLE_FAILURE:
  143. draft.roleModalLoading = false
  144. break
  145. case ActionTypes.SEARCH_MEMBER:
  146. draft.inviteMemberfetching = true
  147. break
  148. case ActionTypes.SEARCH_MEMBER_SUCCESS:
  149. draft.inviteMemberLists = action.payload.result
  150. draft.inviteMemberfetching = false
  151. break
  152. case ActionTypes.SEARCH_MEMBER_FAILURE:
  153. draft.inviteMemberfetching = true
  154. break
  155. case ActionTypes.SET_CURRENT_ORIGANIZATION_PROJECT:
  156. draft.projectDetail = action.payload.option
  157. break
  158. case ActionTypes.LOAD_PROJECT_ADMINS_SUCCESS:
  159. draft.projectAdmins = action.payload.result
  160. break
  161. case ActionTypes.LOAD_PROJECT_ROLES_SUCCESS:
  162. draft.projectRoles = action.payload.result
  163. break
  164. }
  165. })
  166. export default organizationReducer