All files / web/modules/auth saga.ts

0% Statements 0/33
100% Branches 0/0
100% Functions 0/0
0% Lines 0/30

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68                                                                                                                                       
import message from 'antd/lib/message';
import { SagaIterator } from 'redux-saga';
import { call, put, select, takeLatest } from 'redux-saga/effects';
import { APP_MOUNTED } from '../app/constants';
import {
  authSetAuthDataAction,
  authSetAuthStateAction,
  authSetIsLoadingAction,
  IAuthSetNewPasswordActionResponse,
} from './actions';
import { currentAuthenticatedUser, setNewPassword, signOut } from './api';
import {
  AUTH_LOGOUT_USER,
  AUTH_SET_NEW_PASSWORD,
  AUTH_STATE_AUTHENTICATED,
  AUTH_STATE_SIGNED_OUT,
} from './constants';
import { selectAuthData } from './reducer';
 
function* setNewPasswordSaga({
  password,
}: IAuthSetNewPasswordActionResponse): SagaIterator {
  try {
    yield put(authSetIsLoadingAction(true));
    const authData = yield select(selectAuthData);
    const data = yield call(setNewPassword, authData, password);
 
    yield put(authSetAuthDataAction(data));
    yield put(authSetAuthStateAction(AUTH_STATE_AUTHENTICATED));
    message.success('Úspěšné přihlášení');
  } catch (e) {
    message.error(e.message);
  } finally {
    yield put(authSetIsLoadingAction(false));
  }
}
 
function* loginCurrentUserSaga(): SagaIterator {
  try {
    yield put(authSetIsLoadingAction(true));
    const data = yield call(currentAuthenticatedUser);
    yield put(authSetAuthStateAction(AUTH_STATE_AUTHENTICATED));
    yield put(authSetAuthDataAction(data));
  } catch (e) {
    console.log(e);
  } finally {
    yield put(authSetIsLoadingAction(false));
  }
}
 
function* logoutUserSaga(): SagaIterator {
  try {
    yield call(signOut);
    yield put(authSetAuthStateAction(AUTH_STATE_SIGNED_OUT));
    yield put(authSetAuthDataAction({}));
  } catch (e) {
    message.error(e.message);
  }
}
 
function* saga(): SagaIterator {
  yield takeLatest(AUTH_SET_NEW_PASSWORD, setNewPasswordSaga);
  yield takeLatest(AUTH_LOGOUT_USER, logoutUserSaga);
  yield takeLatest(APP_MOUNTED, loginCurrentUserSaga);
}
 
export default saga;