reducer.ts 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 { IProjectState } from './types'
  22. import { ActionTypes } from './constants'
  23. import { ProjectActionType } from './actions'
  24. import { ActionTypes as OrganizationActionTypes } from 'containers/Organizations/constants'
  25. import { OrganizationActionType } from 'containers/Organizations/actions'
  26. export const initialState: IProjectState = {
  27. projects: null,
  28. currentProject: null,
  29. currentProjectLoading: false,
  30. searchProject: false,
  31. starUserList: null,
  32. collectProjects: null,
  33. currentProjectRole: null,
  34. projectRoles: null
  35. }
  36. const projectReducer = (
  37. state = initialState,
  38. action: ProjectActionType | OrganizationActionType
  39. ) =>
  40. produce(state, (draft) => {
  41. switch (action.type) {
  42. case ActionTypes.LOAD_PROJECTS_SUCCESS:
  43. draft.projects = action.payload.projects
  44. break
  45. case ActionTypes.LOAD_PROJECTS_FAILURE:
  46. break
  47. case ActionTypes.RELATION_ROLE_PROJECT_LOADED:
  48. draft.currentProjectRole = action.payload.result
  49. break
  50. case ActionTypes.UPDATE_RELATION_ROLE_PROJECT_SUCCESS:
  51. draft.currentProjectRole.permission = action.payload.result
  52. break
  53. case ActionTypes.ADD_PROJECT_SUCCESS:
  54. if (draft.projects) {
  55. draft.projects.unshift(action.payload.result)
  56. } else {
  57. draft.projects = [action.payload.result]
  58. }
  59. break
  60. case ActionTypes.ADD_PROJECT_FAILURE:
  61. break
  62. case ActionTypes.DELETE_PROJECT_SUCCESS:
  63. if (draft.projects) {
  64. draft.projects = draft.projects.filter(
  65. (d) => d.id !== action.payload.id
  66. )
  67. draft.collectProjects = draft.collectProjects.filter(
  68. (d) => d.id !== action.payload.id
  69. )
  70. }
  71. break
  72. case ActionTypes.LOAD_PROJECT_DETAIL:
  73. draft.currentProjectLoading = true
  74. break
  75. case ActionTypes.LOAD_PROJECT_DETAIL_SUCCESS:
  76. draft.currentProjectLoading = false
  77. draft.currentProject = action.payload.project
  78. break
  79. case ActionTypes.CLEAR_CURRENT_PROJECT:
  80. draft.currentProject = null
  81. break
  82. case ActionTypes.SEARCH_PROJECT_SUCCESS:
  83. draft.searchProject = action.payload.result
  84. break
  85. case ActionTypes.GET_PROJECT_STAR_USER_SUCCESS:
  86. draft.starUserList = action.payload.result
  87. break
  88. case ActionTypes.LOAD_COLLECT_PROJECTS:
  89. break
  90. case ActionTypes.LOAD_COLLECT_PROJECTS_SUCCESS:
  91. draft.collectProjects = action.payload.result
  92. break
  93. case ActionTypes.LOAD_COLLECT_PROJECTS_FAILURE:
  94. break
  95. case ActionTypes.CLICK_COLLECT_PROJECT_SUCCESS:
  96. break
  97. case OrganizationActionTypes.LOAD_PROJECT_ROLES_SUCCESS:
  98. draft.projectRoles = action.payload.result
  99. break
  100. }
  101. })
  102. export default projectReducer