sagas.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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 { AppActions, AppActionType } from './actions'
  23. import request, { IDavinciResponse, setTokenExpired } from 'utils/request'
  24. import { errorHandler } from 'utils/util'
  25. import { IServerConfigurations } from 'app/containers/App/types'
  26. import api from 'utils/api'
  27. export function* getExternalAuthProviders() {
  28. try {
  29. const asyncData = yield call(request, {
  30. method: 'get',
  31. url: api.externalAuthProviders
  32. })
  33. const providers = asyncData.payload
  34. yield put(AppActions.gotExternalAuthProviders(providers))
  35. return providers
  36. } catch (err) {
  37. errorHandler(err)
  38. }
  39. }
  40. export function* login(action: AppActionType) {
  41. if (action.type !== ActionTypes.LOGIN) {
  42. return
  43. }
  44. const { username, password, shareToken, resolve, reject } = action.payload
  45. const { logged, loginFail } = AppActions
  46. try {
  47. const userInfo = yield call(request, {
  48. method: 'post',
  49. url: `${api.share}/login/${shareToken}`,
  50. data: {
  51. username,
  52. password
  53. }
  54. })
  55. yield put(logged(userInfo.payload))
  56. localStorage.setItem('loginUser', JSON.stringify(userInfo.payload))
  57. if (resolve) {
  58. resolve()
  59. }
  60. } catch (err) {
  61. if (reject) {
  62. return reject()
  63. }
  64. yield put(loginFail(err))
  65. errorHandler(err)
  66. }
  67. }
  68. export function* interceptor(action: AppActionType) {
  69. if (action.type !== ActionTypes.INTERCEPTOR_PREFLIGHT) {
  70. return
  71. }
  72. const { token } = action.payload
  73. const { interceptored, interceptorFail } = AppActions
  74. try {
  75. const check = yield call(request, {
  76. method: 'get',
  77. url: `${api.share}/preflight/${token}`
  78. })
  79. const { type, vizType } = check.payload
  80. yield put(interceptored(type, vizType))
  81. } catch (error) {
  82. yield put(interceptorFail())
  83. errorHandler(error)
  84. }
  85. }
  86. export function* getPermissions(action: AppActionType) {
  87. if (action.type !== ActionTypes.GET_PERMISSIONS) {
  88. return
  89. }
  90. const { token, password, resolve, reject } = action.payload
  91. const { getPermissionsSuccess, getPermissionsFail } = AppActions
  92. try {
  93. const check = yield call(request, {
  94. method: 'get',
  95. url: `${api.share}/permissions/${token}`,
  96. params: { password }
  97. })
  98. yield put(getPermissionsSuccess(check?.payload?.download))
  99. if (resolve) {
  100. resolve()
  101. }
  102. } catch (error) {
  103. if (reject) {
  104. return reject()
  105. }
  106. yield put(getPermissionsFail())
  107. errorHandler(error)
  108. }
  109. }
  110. export function* getServerConfigurations(action: AppActionType) {
  111. if (action.type !== ActionTypes.GET_SERVER_CONFIGURATIONS) {
  112. return
  113. }
  114. const { serverConfigurationsGetted, getServerConfigurationsFail } = AppActions
  115. try {
  116. const result: IDavinciResponse<IServerConfigurations> = yield call(
  117. request,
  118. {
  119. method: 'get',
  120. url: api.configurations
  121. }
  122. )
  123. const configurations = result.payload
  124. setTokenExpired(configurations.jwtToken.timeout)
  125. yield put(serverConfigurationsGetted(configurations))
  126. } catch (err) {
  127. yield put(getServerConfigurationsFail(err))
  128. errorHandler(err)
  129. }
  130. }
  131. export default function* rootAppSaga() {
  132. yield all([
  133. takeLatest(ActionTypes.GET_EXTERNAL_AUTH_PROVIDERS, getExternalAuthProviders),
  134. takeLatest(ActionTypes.LOGIN, login),
  135. takeEvery(ActionTypes.INTERCEPTOR_PREFLIGHT, interceptor),
  136. takeEvery(ActionTypes.GET_PERMISSIONS, getPermissions),
  137. takeLatest(ActionTypes.GET_SERVER_CONFIGURATIONS, getServerConfigurations)
  138. ])
  139. }