|
| 1 | +import { call, all, put, fork, select, takeEvery } from 'redux-saga/effects' |
| 2 | +import { FETCH } from '../../audit/UserActionType' |
| 3 | +import { |
| 4 | + isFourZeroOneError, |
| 5 | + addAdditionalData, |
| 6 | +} from '../../utils/TokenController' |
| 7 | +import { UM_GET_USERS } from '../actions/types' |
| 8 | +import UserApi from '../api/UserApi' |
| 9 | +import { getClient } from '../api/base' |
| 10 | +const JansConfigApi = require('jans_config_api') |
| 11 | +import { initAudit } from './SagaUtils' |
| 12 | +import { updateUserResponse, UMupdateUserLoading } from '../actions/UserActions' |
| 13 | +import { postUserAction } from '../api/backend-api' |
| 14 | +import { getAPIAccessToken } from '../actions' |
| 15 | +const API_USERS = 'api-users' |
| 16 | + |
| 17 | +function* newFunction() { |
| 18 | + const token = yield select((state) => state.authReducer.token.access_token) |
| 19 | + const issuer = yield select((state) => state.authReducer.issuer) |
| 20 | + const api = new JansConfigApi.ConfigurationUserManagementApi( |
| 21 | + getClient(JansConfigApi, token, issuer), |
| 22 | + ) |
| 23 | + return new UserApi(api) |
| 24 | +} |
| 25 | + |
| 26 | +export function* getUsersSaga({ payload }) { |
| 27 | + const audit = yield* initAudit() |
| 28 | + try { |
| 29 | + addAdditionalData(audit, FETCH, API_USERS, payload) |
| 30 | + const userApi = yield* newFunction() |
| 31 | + const data = yield call(userApi.getUsers) |
| 32 | + yield put(updateUserResponse(data)) |
| 33 | + yield put(UMupdateUserLoading(false)) |
| 34 | + yield call(postUserAction, audit) |
| 35 | + } catch (e) { |
| 36 | + yield put(UMupdateUserLoading(false)) |
| 37 | + if (isFourZeroOneError(e)) { |
| 38 | + const jwt = yield select((state) => state.authReducer.userinfo_jwt) |
| 39 | + yield put(getAPIAccessToken(jwt)) |
| 40 | + } |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +export function* watchGetUsers() { |
| 45 | + yield takeEvery(UM_GET_USERS, getUsersSaga) |
| 46 | +} |
| 47 | + |
| 48 | +export default function* rootSaga() { |
| 49 | + yield all([fork(watchGetUsers)]) |
| 50 | +} |
0 commit comments