sagas.ts 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  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 { call, put, all, takeLatest, takeEvery } from 'redux-saga/effects'
  21. import { ActionTypes } from './constants'
  22. import omit from 'lodash/omit'
  23. import { WidgetActions, WidgetActionType } from './actions'
  24. import { IWidgetRaw, IWidgetFormed } from './types'
  25. import request from 'utils/request'
  26. import api from 'utils/api'
  27. import { errorHandler } from 'utils/util'
  28. import { widgetConfigMigrationRecorder } from 'app/utils/migrationRecorders'
  29. export function* getWidgets(action: WidgetActionType) {
  30. if (action.type !== ActionTypes.LOAD_WIDGETS) {
  31. return
  32. }
  33. const { projectId } = action.payload
  34. try {
  35. const result = yield call(request, `${api.widget}?projectId=${projectId}`)
  36. const formedWidgets: IWidgetFormed[] = result.payload.map(
  37. (widget: IWidgetRaw) => {
  38. const parsedConfig = JSON.parse(widget.config)
  39. return {
  40. ...widget,
  41. config: widgetConfigMigrationRecorder(parsedConfig, {
  42. viewId: widget.viewId
  43. })
  44. }
  45. }
  46. )
  47. yield put(WidgetActions.widgetsLoaded(formedWidgets))
  48. } catch (err) {
  49. yield put(WidgetActions.widgetsLoadedFail())
  50. errorHandler(err)
  51. }
  52. }
  53. export function* addWidget(action: WidgetActionType) {
  54. if (action.type !== ActionTypes.ADD_WIDGET) {
  55. return
  56. }
  57. const { widget, resolve } = action.payload
  58. try {
  59. const result = yield call(request, {
  60. method: 'post',
  61. url: api.widget,
  62. data: widget
  63. })
  64. const addedWidget: IWidgetRaw = result.payload
  65. const formdWidget: IWidgetFormed = {
  66. ...addedWidget,
  67. config: JSON.parse(addedWidget.config)
  68. }
  69. yield put(WidgetActions.widgetAdded(formdWidget))
  70. resolve()
  71. } catch (err) {
  72. yield put(WidgetActions.addWidgetFail())
  73. errorHandler(err)
  74. }
  75. }
  76. export function* deleteWidget(action: WidgetActionType) {
  77. if (action.type !== ActionTypes.DELETE_WIDGET) {
  78. return
  79. }
  80. const { id } = action.payload
  81. try {
  82. yield call(request, {
  83. method: 'delete',
  84. url: `${api.widget}/${id}`
  85. })
  86. yield put(WidgetActions.widgetDeleted(id))
  87. } catch (err) {
  88. yield put(WidgetActions.deleteWidgetFail())
  89. errorHandler(err)
  90. }
  91. }
  92. export function* getWidgetDetail(action: WidgetActionType) {
  93. if (action.type !== ActionTypes.LOAD_WIDGET_DETAIL) {
  94. return
  95. }
  96. const { id } = action.payload
  97. try {
  98. const widgetResult = yield call(request, `${api.widget}/${id}`)
  99. const widget: IWidgetRaw = widgetResult.payload
  100. const parsedConfig = JSON.parse(widget.config)
  101. const formedWidget: IWidgetFormed = {
  102. ...widget,
  103. config: widgetConfigMigrationRecorder(parsedConfig, {
  104. viewId: widget.viewId
  105. })
  106. }
  107. const viewResult = yield call(request, `${api.view}/${widget.viewId}`)
  108. yield put(
  109. WidgetActions.widgetDetailLoaded(formedWidget, viewResult.payload)
  110. )
  111. } catch (err) {
  112. yield put(WidgetActions.loadWidgetDetailFail(err))
  113. errorHandler(err)
  114. }
  115. }
  116. export function* editWidget(action: WidgetActionType) {
  117. if (action.type !== ActionTypes.EDIT_WIDGET) {
  118. return
  119. }
  120. const { widget, resolve } = action.payload
  121. try {
  122. yield call(request, {
  123. method: 'put',
  124. url: `${api.widget}/${widget.id}`,
  125. data: widget
  126. })
  127. yield put(WidgetActions.widgetEdited())
  128. resolve()
  129. } catch (err) {
  130. yield put(WidgetActions.editWidgetFail())
  131. errorHandler(err)
  132. }
  133. }
  134. export function* copyWidget(action: WidgetActionType) {
  135. if (action.type !== ActionTypes.COPY_WIDGET) {
  136. return
  137. }
  138. const { widget, resolve } = action.payload
  139. try {
  140. const result = yield call(request, {
  141. method: 'post',
  142. url: api.widget,
  143. data: omit(widget, 'id')
  144. })
  145. const copiedWidget: IWidgetRaw = result.payload
  146. const formdWidget: IWidgetFormed = {
  147. ...copiedWidget,
  148. config: JSON.parse(copiedWidget.config)
  149. }
  150. yield put(WidgetActions.widgetCopied(widget.id, formdWidget))
  151. resolve()
  152. } catch (err) {
  153. yield put(WidgetActions.copyWidgetFail())
  154. errorHandler(err)
  155. }
  156. }
  157. export function* executeComputed(action: WidgetActionType) {
  158. if (action.type !== ActionTypes.EXECUTE_COMPUTED_SQL) {
  159. return
  160. }
  161. const { sql } = action.payload
  162. try {
  163. const result = yield call(request, {
  164. method: 'post',
  165. // url: api.widget,
  166. data: sql
  167. })
  168. // todo 返回sql校验结果
  169. } catch (err) {
  170. errorHandler(err)
  171. }
  172. }
  173. export default function* rootWidgetSaga() {
  174. yield all([
  175. takeLatest(ActionTypes.LOAD_WIDGETS, getWidgets),
  176. takeEvery(ActionTypes.ADD_WIDGET, addWidget),
  177. takeEvery(ActionTypes.DELETE_WIDGET, deleteWidget),
  178. takeLatest(ActionTypes.LOAD_WIDGET_DETAIL, getWidgetDetail),
  179. takeEvery(ActionTypes.EDIT_WIDGET, editWidget),
  180. takeEvery(ActionTypes.COPY_WIDGET, copyWidget),
  181. takeEvery(ActionTypes.EXECUTE_COMPUTED_SQL, executeComputed)
  182. ])
  183. }