reducer.ts 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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 { ActionTypes, DashboardItemStatus } from './constants'
  22. import { DashboardActionType } from './actions'
  23. import { DownloadStatus } from 'containers/App/constants'
  24. import { fieldGroupedSort } from 'containers/Widget/components/Config/Sort'
  25. import { IShareDashboardState } from './types'
  26. import {
  27. getShareInitialItemInfo,
  28. initDefaultValuesFromShareParams
  29. } from './util'
  30. import { getGlobalControlInitialValues } from 'app/containers/Dashboard/util'
  31. const initialState: IShareDashboardState = {
  32. dashboard: null,
  33. title: '',
  34. widgets: null,
  35. formedViews: {},
  36. items: null,
  37. itemsInfo: null,
  38. downloadListLoading: false,
  39. downloadList: null,
  40. downloadListInfo: null,
  41. shareParams: null,
  42. fullScreenPanelItemId: null
  43. }
  44. const shareReducer = (state = initialState, action: DashboardActionType) =>
  45. produce(state, (draft) => {
  46. let itemInfo
  47. let drillHistory
  48. switch (action.type) {
  49. case ActionTypes.SEND_SHARE_PARAMS:
  50. draft.shareParams = action.payload.params
  51. break
  52. case ActionTypes.LOAD_SHARE_DASHBOARD_SUCCESS:
  53. const { dashboard, items, widgets, formedViews } = action.payload
  54. initDefaultValuesFromShareParams(
  55. dashboard.config.filters,
  56. draft.shareParams
  57. )
  58. const globalControlsInitialValue = getGlobalControlInitialValues(
  59. dashboard.config.filters,
  60. formedViews
  61. )
  62. draft.title = dashboard.name
  63. draft.dashboard = dashboard
  64. draft.widgets = widgets
  65. draft.formedViews = formedViews
  66. draft.items = items
  67. draft.itemsInfo = items.reduce((info, item) => {
  68. const relatedWidget = widgets.find((w) => w.id === item.widgetId)
  69. const initialItemInfo = getShareInitialItemInfo(
  70. relatedWidget,
  71. formedViews
  72. )
  73. if (globalControlsInitialValue[item.id]) {
  74. const {
  75. globalFilters,
  76. globalVariables
  77. } = globalControlsInitialValue[item.id]
  78. initialItemInfo.queryConditions = {
  79. ...initialItemInfo.queryConditions,
  80. globalFilters,
  81. globalVariables
  82. }
  83. }
  84. info[item.id] = initialItemInfo
  85. return info
  86. }, {})
  87. break
  88. case ActionTypes.SET_INDIVIDUAL_DASHBOARD:
  89. draft.title = action.payload.widget.name
  90. draft.items = [
  91. {
  92. id: 1,
  93. x: 0,
  94. y: 0,
  95. width: 12,
  96. height: 12,
  97. polling: false,
  98. frequency: 0,
  99. widgetId: action.payload.widget.id,
  100. dashboardId: 0,
  101. config: ''
  102. }
  103. ]
  104. draft.itemsInfo = {
  105. 1: getShareInitialItemInfo(
  106. action.payload.widget,
  107. action.payload.formedViews
  108. )
  109. }
  110. break
  111. case ActionTypes.LOAD_SHARE_WIDGET_SUCCESS:
  112. if (!draft.widgets) {
  113. draft.widgets = []
  114. }
  115. draft.widgets = draft.widgets.concat(action.payload.widget)
  116. draft.formedViews = action.payload.formedViews
  117. break
  118. case ActionTypes.SELECT_DASHBOARD_ITEM_CHART:
  119. draft.itemsInfo[action.payload.itemId].renderType =
  120. action.payload.renderType
  121. draft.itemsInfo[action.payload.itemId].selectedItems =
  122. action.payload.selectedItems
  123. break
  124. case ActionTypes.LOAD_SHARE_RESULTSET:
  125. draft.itemsInfo[action.payload.itemId].status =
  126. DashboardItemStatus.Pending
  127. draft.itemsInfo[action.payload.itemId].loading = true
  128. draft.itemsInfo[action.payload.itemId].errorMessage = ''
  129. break
  130. case ActionTypes.DRILL_DASHBOARDITEM:
  131. drillHistory =
  132. draft.itemsInfo[action.payload.itemId].queryConditions.drillHistory
  133. if (!drillHistory) {
  134. draft.itemsInfo[
  135. action.payload.itemId
  136. ].queryConditions.drillHistory = []
  137. }
  138. draft.itemsInfo[
  139. action.payload.itemId
  140. ].queryConditions.drillHistory.push(action.payload.drillHistory)
  141. break
  142. case ActionTypes.DELETE_DRILL_HISTORY:
  143. drillHistory =
  144. draft.itemsInfo[action.payload.itemId].queryConditions.drillHistory
  145. if (Array.isArray(drillHistory)) {
  146. drillHistory.splice(action.payload.index + 1)
  147. }
  148. break
  149. case ActionTypes.LOAD_SHARE_RESULTSET_SUCCESS:
  150. const {
  151. tempFilters,
  152. linkageFilters,
  153. globalFilters,
  154. variables,
  155. linkageVariables,
  156. globalVariables,
  157. pagination,
  158. nativeQuery,
  159. customOrders
  160. } = action.payload.requestParams
  161. fieldGroupedSort(action.payload.result.resultList, customOrders)
  162. draft.itemsInfo[action.payload.itemId] = {
  163. ...draft.itemsInfo[action.payload.itemId],
  164. status: DashboardItemStatus.Fulfilled,
  165. loading: false,
  166. datasource: action.payload.result,
  167. renderType: action.payload.renderType,
  168. queryConditions: {
  169. ...draft.itemsInfo[action.payload.itemId].queryConditions,
  170. tempFilters,
  171. linkageFilters,
  172. globalFilters,
  173. variables,
  174. linkageVariables,
  175. globalVariables,
  176. pagination,
  177. nativeQuery
  178. },
  179. selectedItems: []
  180. }
  181. break
  182. case ActionTypes.LOAD_SHARE_RESULTSET_FAILURE:
  183. draft.itemsInfo[action.payload.itemId] = {
  184. ...draft.itemsInfo[action.payload.itemId],
  185. status: DashboardItemStatus.Error,
  186. loading: false,
  187. errorMessage: action.payload.errorMessage
  188. }
  189. break
  190. case ActionTypes.LOAD_BATCH_DATA_WITH_CONTROL_VALUES:
  191. action.payload.relatedItems.forEach((itemId) => {
  192. draft.itemsInfo[itemId].status = DashboardItemStatus.Pending
  193. draft.itemsInfo[itemId].loading = true
  194. draft.itemsInfo[itemId].errorMessage = ''
  195. })
  196. break
  197. case ActionTypes.LOAD_WIDGET_CSV:
  198. draft.itemsInfo[action.payload.itemId].downloadCsvLoading = true
  199. break
  200. case ActionTypes.LOAD_WIDGET_CSV_SUCCESS:
  201. case ActionTypes.LOAD_WIDGET_CSV_FAILURE:
  202. draft.itemsInfo[action.payload.itemId].downloadCsvLoading = false
  203. break
  204. case ActionTypes.INITIATE_DOWNLOAD_TASK:
  205. draft.itemsInfo[action.payload.itemId].downloadCsvLoading = true
  206. break
  207. case ActionTypes.INITIATE_DOWNLOAD_TASK_SUCCESS:
  208. case ActionTypes.INITIATE_DOWNLOAD_TASK_FAILURE:
  209. draft.itemsInfo[action.payload.itemId].downloadCsvLoading = false
  210. break
  211. case ActionTypes.RESIZE_DASHBOARDITEM:
  212. itemInfo = draft.itemsInfo[action.payload.itemId]
  213. itemInfo.renderType = 'resize'
  214. itemInfo.datasource = { ...itemInfo.datasource }
  215. break
  216. case ActionTypes.RESIZE_ALL_DASHBOARDITEM:
  217. Object.values(draft.itemsInfo).forEach((itemInfo: any) => {
  218. itemInfo.renderType = 'resize'
  219. itemInfo.datasource = { ...itemInfo.datasource }
  220. })
  221. break
  222. case ActionTypes.RENDER_CHART_ERROR:
  223. draft.itemsInfo[
  224. action.payload.itemId
  225. ].errorMessage = action.payload.error.toString()
  226. break
  227. case ActionTypes.LOAD_DOWNLOAD_LIST:
  228. draft.downloadListLoading = true
  229. break
  230. case ActionTypes.LOAD_DOWNLOAD_LIST_SUCCESS:
  231. draft.downloadListLoading = false
  232. draft.downloadList = action.payload.list
  233. draft.downloadListInfo = action.payload.list.reduce((info, item) => {
  234. info[item.id] = {
  235. loading: false
  236. }
  237. return info
  238. }, {})
  239. break
  240. case ActionTypes.LOAD_DOWNLOAD_LIST_FAILURE:
  241. draft.downloadListLoading = false
  242. break
  243. case ActionTypes.DOWNLOAD_FILE_SUCCESS:
  244. draft.downloadList.find(({ id }) => id === action.payload.id).status =
  245. DownloadStatus.Downloaded
  246. break
  247. case ActionTypes.SET_FULL_SCREEN_PANEL_ITEM_ID:
  248. draft.fullScreenPanelItemId = action.payload.itemId
  249. break
  250. }
  251. })
  252. export default shareReducer