reducer.ts 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 { IWidgetState } from './types'
  22. import { ActionTypes } from './constants'
  23. import { ActionTypes as ViewActionTypes } from '../View/constants'
  24. import { WidgetActionType } from './actions'
  25. import { ViewActionType } from 'containers/View/actions'
  26. import { DisplayActionType } from 'containers/Display/actions'
  27. export const initialState: IWidgetState = {
  28. widgets: null,
  29. currentWidget: null,
  30. loading: false,
  31. dataLoading: false
  32. }
  33. const widgetReducer = (
  34. state = initialState,
  35. action: WidgetActionType | ViewActionType | DisplayActionType
  36. ) =>
  37. produce(state, (draft) => {
  38. switch (action.type) {
  39. case ActionTypes.LOAD_WIDGETS:
  40. draft.loading = true
  41. draft.widgets = null
  42. break
  43. case ActionTypes.LOAD_WIDGETS_SUCCESS:
  44. draft.loading = false
  45. draft.widgets = action.payload.widgets
  46. break
  47. case ActionTypes.LOAD_WIDGETS_FAILURE:
  48. draft.loading = false
  49. break
  50. case ActionTypes.ADD_WIDGET:
  51. draft.loading = true
  52. break
  53. case ActionTypes.ADD_WIDGET_SUCCESS:
  54. if (draft.widgets) {
  55. draft.widgets.push(action.payload.result)
  56. draft.loading = false
  57. } else {
  58. draft.loading = false
  59. draft.widgets = [action.payload.result]
  60. }
  61. break
  62. case ActionTypes.ADD_WIDGET_FAILURE:
  63. draft.loading = false
  64. break
  65. case ActionTypes.DELETE_WIDGET:
  66. draft.loading = true
  67. break
  68. case ActionTypes.DELETE_WIDGET_SUCCESS:
  69. draft.widgets = draft.widgets.filter((g) => g.id !== action.payload.id)
  70. draft.loading = false
  71. break
  72. case ActionTypes.DELETE_WIDGET_FAILURE:
  73. draft.loading = false
  74. break
  75. case ActionTypes.LOAD_WIDGET_DETAIL:
  76. draft.loading = true
  77. draft.currentWidget = null
  78. break
  79. case ActionTypes.LOAD_WIDGET_DETAIL_SUCCESS:
  80. draft.loading = false
  81. draft.currentWidget = action.payload.detail
  82. break
  83. case ActionTypes.LOAD_WIDGET_DETAIL_FAILURE:
  84. draft.loading = false
  85. break
  86. case ActionTypes.EDIT_WIDGET:
  87. draft.loading = true
  88. break
  89. case ActionTypes.EDIT_WIDGET_SUCCESS:
  90. draft.loading = false
  91. break
  92. case ActionTypes.EDIT_WIDGET_FAILURE:
  93. draft.loading = false
  94. break
  95. case ActionTypes.COPY_WIDGET:
  96. draft.loading = true
  97. break
  98. case ActionTypes.COPY_WIDGET_SUCCESS:
  99. const fromWidgetId = action.payload.fromWidgetId
  100. const copyWidgetIndex = draft.widgets.findIndex(({ id }) => id === fromWidgetId)
  101. draft.widgets.splice(
  102. copyWidgetIndex + 1,
  103. 0,
  104. {
  105. ...action.payload.result,
  106. viewName: draft.widgets[copyWidgetIndex].viewName
  107. }
  108. )
  109. draft.loading = false
  110. break
  111. case ActionTypes.COPY_WIDGET_FAILURE:
  112. draft.loading = false
  113. case ViewActionTypes.LOAD_VIEW_DATA:
  114. draft.dataLoading = true
  115. break
  116. case ViewActionTypes.LOAD_VIEW_DATA_SUCCESS:
  117. draft.dataLoading = false
  118. break
  119. case ViewActionTypes.LOAD_VIEW_DATA_FAILURE:
  120. draft.dataLoading = false
  121. break
  122. case ActionTypes.CLEAR_CURRENT_WIDGET:
  123. draft.currentWidget = null
  124. break
  125. }
  126. })
  127. export default widgetReducer