reducer.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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, { setAutoFreeze } from 'immer'
  21. // @FIXME temporary not Object.freeze from immer produce to avoid current bugs
  22. setAutoFreeze(false)
  23. import {
  24. LOGIN,
  25. LOGGED,
  26. LOGIN_ERROR,
  27. LOGOUT,
  28. SHOW_NAVIGATOR,
  29. HIDE_NAVIGATOR,
  30. ACTIVE_SUCCESS,
  31. UPLOAD_AVATAR_SUCCESS,
  32. LOAD_DOWNLOAD_LIST,
  33. LOAD_DOWNLOAD_LIST_SUCCESS,
  34. LOAD_DOWNLOAD_LIST_FAILURE,
  35. DOWNLOAD_FILE_SUCCESS,
  36. UPDATE_PROFILE_SUCCESS,
  37. GET_EXTERNAL_AUTH_PROVIDERS_SUCESS,
  38. DownloadStatus,
  39. GET_SERVER_CONFIGURATIONS_SUCCESS
  40. } from './constants'
  41. const initialState = {
  42. externalAuthProviders: null,
  43. logged: null,
  44. loginUser: null,
  45. loginLoading: false,
  46. navigator: true,
  47. downloadListLoading: false,
  48. downloadList: null,
  49. downloadListInfo: null,
  50. version: '',
  51. oauth2Enabled: false
  52. }
  53. const appReducer = (state = initialState, action) =>
  54. produce(state, (draft) => {
  55. switch (action.type) {
  56. case GET_EXTERNAL_AUTH_PROVIDERS_SUCESS:
  57. draft.externalAuthProviders = action.payload.externalAuthProviders
  58. break
  59. case LOGIN:
  60. draft.loginLoading = true
  61. break
  62. case LOGGED:
  63. draft.loginLoading = false
  64. draft.logged = true
  65. draft.loginUser = action.payload.user
  66. break
  67. case LOGIN_ERROR:
  68. draft.loginLoading = false
  69. break
  70. case ACTIVE_SUCCESS:
  71. draft.logged = true
  72. draft.loginUser = action.payload.user
  73. break
  74. case GET_SERVER_CONFIGURATIONS_SUCCESS:
  75. draft.version = action.payload.configurations.version
  76. draft.oauth2Enabled =
  77. action.payload.configurations.security.oauth2.enable
  78. break
  79. case LOGOUT:
  80. draft.logged = false
  81. draft.loginUser = null
  82. break
  83. case UPLOAD_AVATAR_SUCCESS:
  84. draft.loginUser.avatar = action.payload.path
  85. break
  86. case UPDATE_PROFILE_SUCCESS:
  87. const { id, name, department, description } = action.payload.user
  88. draft.loginUser = {
  89. ...draft.loginUser,
  90. id,
  91. name,
  92. department,
  93. description
  94. }
  95. break
  96. case SHOW_NAVIGATOR:
  97. draft.navigator = true
  98. break
  99. case HIDE_NAVIGATOR:
  100. draft.navigator = false
  101. break
  102. case LOAD_DOWNLOAD_LIST:
  103. draft.downloadListLoading = true
  104. break
  105. case LOAD_DOWNLOAD_LIST_SUCCESS:
  106. draft.downloadListLoading = false
  107. draft.downloadList = action.payload.list
  108. draft.downloadListInfo = action.payload.list.reduce((info, item) => {
  109. info[item.id] = {
  110. loading: false
  111. }
  112. return info
  113. }, {})
  114. break
  115. case LOAD_DOWNLOAD_LIST_FAILURE:
  116. draft.downloadListLoading = false
  117. break
  118. case DOWNLOAD_FILE_SUCCESS:
  119. draft.downloadList.find(({ id }) => action.payload.id === id).status =
  120. DownloadStatus.Downloaded
  121. break
  122. }
  123. })
  124. export { initialState as appInitialState }
  125. export default appReducer