actions.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482
  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 axios from 'axios'
  21. import { ActionTypes } from './constants'
  22. import { returnType } from 'utils/redux'
  23. import { IDavinciResponse } from 'utils/request'
  24. import {
  25. IViewBase,
  26. IView,
  27. IExecuteSqlParams,
  28. IExecuteSqlResponse,
  29. IViewInfo,
  30. IDacChannel,
  31. IDacTenant,
  32. IDacBiz,
  33. IViewQueryResponse
  34. } from './types'
  35. import { IDataRequestBody } from '../Dashboard/types'
  36. import { RenderType } from 'containers/Widget/components/Widget'
  37. import { IDistinctValueReqeustParams } from 'app/components/Control/types'
  38. import { EExecuteType } from './Editor'
  39. const CancelToken = axios.CancelToken
  40. export const ViewActions = {
  41. viewsLoaded (views: IViewBase[]) {
  42. return {
  43. type: ActionTypes.LOAD_VIEWS_SUCCESS,
  44. payload: {
  45. views
  46. }
  47. }
  48. },
  49. loadViews (projectId: number, resolve?: (views: IViewBase[]) => void) {
  50. return {
  51. type: ActionTypes.LOAD_VIEWS,
  52. payload: {
  53. projectId,
  54. resolve
  55. }
  56. }
  57. },
  58. loadViewsFail () {
  59. return {
  60. type: ActionTypes.LOAD_VIEWS_FAILURE,
  61. payload: {}
  62. }
  63. },
  64. viewsDetailLoaded (views: IView[], isEditing: boolean) {
  65. return {
  66. type: ActionTypes.LOAD_VIEWS_DETAIL_SUCCESS,
  67. payload: {
  68. views,
  69. isEditing
  70. }
  71. }
  72. },
  73. loadViewsDetail (
  74. viewIds: number[],
  75. resolve?: (views: IView[]) => void,
  76. isEditing: boolean = false
  77. ) {
  78. return {
  79. type: ActionTypes.LOAD_VIEWS_DETAIL,
  80. payload: {
  81. viewIds,
  82. isEditing,
  83. resolve
  84. }
  85. }
  86. },
  87. loadViewsDetailFail () {
  88. return {
  89. type: ActionTypes.LOAD_VIEWS_DETAIL_FAILURE,
  90. payload: {}
  91. }
  92. },
  93. addView (view: IView, resolve: () => void) {
  94. return {
  95. type: ActionTypes.ADD_VIEW,
  96. payload: {
  97. view,
  98. resolve
  99. }
  100. }
  101. },
  102. viewAdded (result: IView) {
  103. return {
  104. type: ActionTypes.ADD_VIEW_SUCCESS,
  105. payload: {
  106. result
  107. }
  108. }
  109. },
  110. addViewFail () {
  111. return {
  112. type: ActionTypes.ADD_VIEW_FAILURE,
  113. payload: {}
  114. }
  115. },
  116. editView (view: IView, resolve: () => void) {
  117. return {
  118. type: ActionTypes.EDIT_VIEW,
  119. payload: {
  120. view,
  121. resolve
  122. }
  123. }
  124. },
  125. viewEdited (result: IView) {
  126. return {
  127. type: ActionTypes.EDIT_VIEW_SUCCESS,
  128. payload: {
  129. result
  130. }
  131. }
  132. },
  133. editViewFail () {
  134. return {
  135. type: ActionTypes.EDIT_VIEW_FAILURE,
  136. payload: {}
  137. }
  138. },
  139. deleteView (id: number, resolve: (id: number) => void) {
  140. return {
  141. type: ActionTypes.DELETE_VIEW,
  142. payload: {
  143. id,
  144. resolve
  145. }
  146. }
  147. },
  148. viewDeleted (id: number) {
  149. return {
  150. type: ActionTypes.DELETE_VIEW_SUCCESS,
  151. payload: {
  152. id
  153. }
  154. }
  155. },
  156. deleteViewFail () {
  157. return {
  158. type: ActionTypes.DELETE_VIEW_FAILURE,
  159. payload: {}
  160. }
  161. },
  162. copyView (view: IViewBase, resolve: () => void) {
  163. return {
  164. type: ActionTypes.COPY_VIEW,
  165. payload: {
  166. view,
  167. resolve
  168. }
  169. }
  170. },
  171. viewCopied (fromViewId: number, result: IView) {
  172. return {
  173. type: ActionTypes.COPY_VIEW_SUCCESS,
  174. payload: {
  175. fromViewId,
  176. result
  177. }
  178. }
  179. },
  180. copyViewFail () {
  181. return {
  182. type: ActionTypes.COPY_VIEW_FAILURE,
  183. payload: {}
  184. }
  185. },
  186. setIsLastExecuteWholeSql (isLastExecuteWholeSql: boolean) {
  187. return {
  188. type: ActionTypes.IS_LAST_EXECUTE_WHOLE_SQL,
  189. payload: {
  190. isLastExecuteWholeSql
  191. }
  192. }
  193. },
  194. executeSql (params: IExecuteSqlParams, exeType: EExecuteType) {
  195. return {
  196. type: ActionTypes.EXECUTE_SQL,
  197. payload: {
  198. params,
  199. exeType
  200. }
  201. }
  202. },
  203. sqlExecuted (result: IDavinciResponse<IExecuteSqlResponse>) {
  204. return {
  205. type: ActionTypes.EXECUTE_SQL_SUCCESS,
  206. payload: {
  207. result
  208. }
  209. }
  210. },
  211. executeSqlFail (err: IDavinciResponse<any>['header']) {
  212. return {
  213. type: ActionTypes.EXECUTE_SQL_FAILURE,
  214. payload: {
  215. err
  216. }
  217. }
  218. },
  219. executeSqlCancel () {
  220. return {
  221. type: ActionTypes.EXECUTE_SQL_CANCEL,
  222. payload: {}
  223. }
  224. },
  225. updateEditingView (view: IView) {
  226. return {
  227. type: ActionTypes.UPDATE_EDITING_VIEW,
  228. payload: {
  229. view
  230. }
  231. }
  232. },
  233. updateEditingViewInfo (viewInfo: IViewInfo) {
  234. return {
  235. type: ActionTypes.UPDATE_EDITING_VIEW_INFO,
  236. payload: {
  237. viewInfo
  238. }
  239. }
  240. },
  241. setSqlLimit (limit: number) {
  242. return {
  243. type: ActionTypes.SET_SQL_LIMIT,
  244. payload: {
  245. limit
  246. }
  247. }
  248. },
  249. resetViewState () {
  250. return {
  251. type: ActionTypes.RESET_VIEW_STATE,
  252. payload: {}
  253. }
  254. },
  255. /** Actions for fetch external authorization variables values */
  256. loadDacChannels () {
  257. return {
  258. type: ActionTypes.LOAD_DAC_CHANNELS,
  259. payload: {}
  260. }
  261. },
  262. dacChannelsLoaded (channels: IDacChannel[]) {
  263. return {
  264. type: ActionTypes.LOAD_DAC_CHANNELS_SUCCESS,
  265. payload: {
  266. channels
  267. }
  268. }
  269. },
  270. loadDacChannelsFail () {
  271. return {
  272. type: ActionTypes.LOAD_DAC_CHANNELS_FAILURE,
  273. payload: {}
  274. }
  275. },
  276. loadDacTenants (channelName: string) {
  277. return {
  278. type: ActionTypes.LOAD_DAC_TENANTS,
  279. payload: {
  280. channelName
  281. }
  282. }
  283. },
  284. dacTenantsLoaded (tenants: IDacTenant[]) {
  285. return {
  286. type: ActionTypes.LOAD_DAC_TENANTS_SUCCESS,
  287. payload: {
  288. tenants
  289. }
  290. }
  291. },
  292. loadDacTenantsFail () {
  293. return {
  294. type: ActionTypes.LOAD_DAC_TENANTS_FAILURE,
  295. payload: {}
  296. }
  297. },
  298. loadDacBizs (channelName: string, tenantId: number) {
  299. return {
  300. type: ActionTypes.LOAD_DAC_BIZS,
  301. payload: {
  302. channelName,
  303. tenantId
  304. }
  305. }
  306. },
  307. dacBizsLoaded (bizs: IDacBiz[]) {
  308. return {
  309. type: ActionTypes.LOAD_DAC_BIZS_SUCCESS,
  310. payload: {
  311. bizs
  312. }
  313. }
  314. },
  315. loadDacBizsFail () {
  316. return {
  317. type: ActionTypes.LOAD_DAC_BIZS_FAILURE,
  318. payload: {}
  319. }
  320. },
  321. /** */
  322. /** Actions for external usages */
  323. loadSelectOptions (
  324. controlKey: string,
  325. requestParams: { [viewId: string]: IDistinctValueReqeustParams },
  326. itemId?: number
  327. ) {
  328. return {
  329. type: ActionTypes.LOAD_SELECT_OPTIONS,
  330. payload: {
  331. controlKey,
  332. requestParams,
  333. itemId,
  334. cancelTokenSource: CancelToken.source()
  335. }
  336. }
  337. },
  338. selectOptionsLoaded (
  339. controlKey: string,
  340. values: object[],
  341. itemId?: number
  342. ) {
  343. return {
  344. type: ActionTypes.LOAD_SELECT_OPTIONS_SUCCESS,
  345. payload: {
  346. controlKey,
  347. values,
  348. itemId
  349. }
  350. }
  351. },
  352. loadSelectOptionsFail (err) {
  353. return {
  354. type: ActionTypes.LOAD_SELECT_OPTIONS_FAILURE,
  355. payload: {
  356. err
  357. }
  358. }
  359. },
  360. loadViewData (
  361. id: number,
  362. requestParams: IDataRequestBody,
  363. resolve: (data: any[]) => void,
  364. reject: (error) => void
  365. ) {
  366. return {
  367. type: ActionTypes.LOAD_VIEW_DATA,
  368. payload: {
  369. id,
  370. requestParams,
  371. resolve,
  372. reject
  373. }
  374. }
  375. },
  376. viewDataLoaded () {
  377. return {
  378. type: ActionTypes.LOAD_VIEW_DATA_SUCCESS
  379. }
  380. },
  381. loadViewDataFail (err) {
  382. return {
  383. type: ActionTypes.LOAD_VIEW_DATA_FAILURE,
  384. payload: {
  385. err
  386. }
  387. }
  388. },
  389. loadColumnDistinctValue(
  390. paramsByViewId: {
  391. [viewId: string]: Omit<IDistinctValueReqeustParams, 'cache' | 'expired'>
  392. },
  393. callback: (options?: object[]) => void
  394. ) {
  395. return {
  396. type: ActionTypes.LOAD_COLUMN_DISTINCT_VALUE,
  397. payload: {
  398. paramsByViewId,
  399. callback
  400. }
  401. }
  402. },
  403. loadViewDataFromVizItem (
  404. renderType: RenderType,
  405. itemId: number | [number, number],
  406. viewId: number,
  407. requestParams: any,
  408. vizType: 'dashboard' | 'display',
  409. statistic
  410. ) {
  411. return {
  412. type: ActionTypes.LOAD_VIEW_DATA_FROM_VIZ_ITEM,
  413. payload: {
  414. renderType,
  415. itemId,
  416. viewId,
  417. requestParams,
  418. vizType,
  419. cancelTokenSource: CancelToken.source()
  420. },
  421. statistic
  422. }
  423. },
  424. viewDataFromVizItemLoaded (
  425. renderType: RenderType,
  426. itemId: number | [number, number],
  427. requestParams: any,
  428. result: IViewQueryResponse,
  429. vizType: 'dashboard' | 'display',
  430. statistic
  431. ) {
  432. return {
  433. type: ActionTypes.LOAD_VIEW_DATA_FROM_VIZ_ITEM_SUCCESS,
  434. payload: {
  435. renderType,
  436. itemId,
  437. requestParams,
  438. result,
  439. vizType
  440. },
  441. statistic
  442. }
  443. },
  444. loadViewDataFromVizItemFail (
  445. itemId: number | [number, number],
  446. vizType: 'dashboard' | 'display',
  447. errorMessage: string
  448. ) {
  449. return {
  450. type: ActionTypes.LOAD_VIEW_DATA_FROM_VIZ_ITEM_FAILURE,
  451. payload: {
  452. itemId,
  453. vizType,
  454. errorMessage
  455. }
  456. }
  457. }
  458. /** */
  459. }
  460. const mockAction = returnType(ViewActions)
  461. export type ViewActionType = typeof mockAction
  462. export default ViewActions