actions.ts 9.4 KB

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