reducer.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 { ISourceState } from './types'
  22. import { ActionTypes } from './constants'
  23. import { SourceActionType } from './actions'
  24. export function getSourceInitialState(): ISourceState {
  25. return {
  26. sources: null,
  27. listLoading: false,
  28. formLoading: false,
  29. testLoading: false,
  30. resetLoading: false,
  31. datasourcesInfo: []
  32. }
  33. }
  34. const initialState: ISourceState = getSourceInitialState()
  35. const sourceReducer = (state = initialState, action: SourceActionType) =>
  36. produce(state, (draft) => {
  37. switch (action.type) {
  38. case ActionTypes.LOAD_SOURCES:
  39. draft.listLoading = true
  40. break
  41. case ActionTypes.LOAD_SOURCES_SUCCESS:
  42. draft.listLoading = false
  43. draft.sources = action.payload.sources
  44. break
  45. case ActionTypes.LOAD_SOURCES_FAILURE:
  46. draft.listLoading = false
  47. break
  48. case ActionTypes.ADD_SOURCE:
  49. draft.formLoading = true
  50. break
  51. case ActionTypes.ADD_SOURCE_SUCCESS:
  52. draft.sources = [action.payload.result].concat(draft.sources || [])
  53. draft.formLoading = false
  54. break
  55. case ActionTypes.ADD_SOURCE_FAILURE:
  56. draft.formLoading = false
  57. break
  58. case ActionTypes.DELETE_SOURCE:
  59. draft.listLoading = true
  60. break
  61. case ActionTypes.DELETE_SOURCE_SUCCESS:
  62. draft.listLoading = false
  63. draft.sources = draft.sources.filter((g) => g.id !== action.payload.id)
  64. break
  65. case ActionTypes.DELETE_SOURCE_FAILURE:
  66. draft.listLoading = false
  67. break
  68. case ActionTypes.EDIT_SOURCE:
  69. draft.formLoading = true
  70. break
  71. case ActionTypes.EDIT_SOURCE_SUCCESS:
  72. draft.sources.splice(
  73. draft.sources.findIndex((g) => g.id === action.payload.result.id),
  74. 1,
  75. action.payload.result
  76. )
  77. draft.formLoading = false
  78. break
  79. case ActionTypes.EDIT_SOURCE_FAILURE:
  80. draft.formLoading = false
  81. break
  82. case ActionTypes.TEST_SOURCE_CONNECTION:
  83. draft.testLoading = true
  84. break
  85. case ActionTypes.TEST_SOURCE_CONNECTION_SUCCESS:
  86. case ActionTypes.TEST_SOURCE_CONNECTION_FAILURE:
  87. draft.testLoading = false
  88. break
  89. case ActionTypes.RESET_SOURCE_CONNECTION:
  90. draft.resetLoading = true
  91. break
  92. case ActionTypes.RESET_SOURCE_CONNECTION_SUCCESS:
  93. case ActionTypes.RESET_SOURCE_CONNECTION_FAILURE:
  94. draft.resetLoading = false
  95. break
  96. case ActionTypes.LOAD_DATASOURCES_INFO_SUCCESS:
  97. draft.datasourcesInfo = action.payload.info
  98. break
  99. }
  100. })
  101. export default sourceReducer