Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //@flow
- import { type Saga } from 'redux-saga';
- import type { Reducer } from 'redux';
- import { put, select, call, fork } from 'redux-saga/effects';
- import { GoogleSignin } from 'react-native-google-signin';
- import Config from 'react-native-config';
- import idx from 'idx';
- import ScreensBootstrap from 'Navigation';
- import { resetError, setError } from 'ActionsReducers/ErrorReducerActions';
- import { loginSuccess, loginFailure, googleLoginSuccess, googleLoginFailure, getUserSuccess, getUserFailure, getOfflineContentSuccess, getOfflineContentFailure, logoutSuccess, logoutFailure } from 'ActionsReducers/SessionReducerActions';
- import { login as loginApi, googleAuth, getUser as getUserApi, logout } from 'Api/Session';
- import { downloadFile } from 'Api/Download';
- import { type ActionSaga } from 'ActionsReducersType';
- import { GENERIC_ERROR } from 'Helpers/Constant';
- import { updateBootstrap } from 'ActionsReducers/AppConfigReducerActions';
- import { hash } from 'Helpers/String';
- import DB from 'Store/DB';
- import Reactotron from 'reactotron-react-native';
- const getAppConfigReducer = (state: Reducer) => state.appConfigReducer;
- const getSessionReducer = (state: Reducer) => state.sessionReducer;
- export function* getUserFromDB(action: ActionSaga): Saga<void> {
- const appConfigReducer = yield select(getAppConfigReducer);
- const { originalDomain } = appConfigReducer;
- const { username, password } = action.payload;
- const user = DB.getUserByCredentials({
- username,
- password,
- originalDomain
- });
- if (user) {
- yield put(resetError());
- yield put(loginSuccess(null, user));
- //SET ROOT SCREEN
- ScreensBootstrap.myDownload();
- } else {
- yield put(
- setError({
- code: GENERIC_ERROR,
- message: 'boh',
- actionSucc: action,
- actionFail: loginFailure()
- })
- );
- }
- }
- export function* loginSagas(action: ActionSaga): Saga<void> {
- try {
- const appConfigReducer = yield select(getAppConfigReducer);
- const { aliasDomain, originalDomain, bootstrap, offlineMode } = appConfigReducer;
- const { username, password } = action.payload;
- if (offlineMode) {
- yield fork(getUserFromDB, action);
- } else {
- const login = yield call(loginApi, aliasDomain, username, password);
- const user = yield call(getUserApi, aliasDomain, login.accessToken, login.ID);
- const logoRemote = idx(bootstrap, (_) => _.brandingElements.signin.logo.remote);
- const logoLocale = yield call(downloadFile, originalDomain, logoRemote);
- bootstrap.brandingElements.signin.logo.locale = logoLocale;
- yield put(updateBootstrap(bootstrap));
- const userAvatarRemote = idx(user, (_) => _.avatar.remote);
- const userAvatarLocale = yield call(downloadFile, originalDomain, userAvatarRemote);
- user.avatar.locale = userAvatarLocale;
- user.password = hash(password);
- user.originalDomain = originalDomain;
- yield call(DB.writeAlias, { aliasDomain, originalDomain });
- yield call(DB.writeLms, { originalDomain, bootstrap });
- yield call(DB.writeUser, user);
- yield put(resetError());
- yield put(loginSuccess(login, user));
- ScreensBootstrap.main();
- }
- } catch (error) {
- Reactotron.log('error: ' + JSON.stringify(error));
- const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
- const message = idx(error, (_) => _.response.data.message[0]);
- yield put(
- setError({
- code: status,
- message: message,
- actionSucc: action,
- actionFail: loginFailure()
- })
- );
- }
- }
- const googleSignin = (): Promise<string> => {
- return GoogleSignin.configure({
- iosClientId: Config.GOOGLE_IOS_CLIENT_ID,
- webClientId: Config.GOOGLE_WEB_CLIENT_ID
- }).then(() => GoogleSignin.signIn().then((response: Object) => response.idToken));
- };
- const googleSignout = (): Promise<string> => {
- return GoogleSignin.signOut();
- };
- export function* googleLoginSagas(action: ActionSaga): Saga<void> {
- try {
- const appConfigReducer = yield select(getAppConfigReducer);
- const { aliasDomain, originalDomain, bootstrap } = appConfigReducer;
- const googleToken = yield call(googleSignin);
- yield call(googleSignout);
- const login = yield call(googleAuth, aliasDomain, googleToken);
- const user = yield call(getUserApi, aliasDomain, login.accessToken, login.ID);
- const logoRemote = idx(bootstrap, (_) => _.brandingElements.signin.logo);
- const logoLocale = yield call(downloadFile, originalDomain, logoRemote);
- bootstrap.brandingElements.signin.logo = logoLocale;
- yield put(updateBootstrap(bootstrap));
- const userAvatarRemote = idx(user, (_) => _.avatar.remote);
- const userAvatarLocale = yield call(downloadFile, originalDomain, userAvatarRemote);
- user.avatar.locale = userAvatarLocale;
- user.originalDomain = originalDomain;
- yield call(DB.writeAlias, { aliasDomain, originalDomain });
- yield call(DB.writeLms, { originalDomain, bootstrap });
- yield call(DB.writeUser, user);
- yield put(resetError());
- yield put(googleLoginSuccess(login, user));
- ScreensBootstrap.main();
- } catch (error) {
- const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
- const message = idx(error, (_) => _.response.data.message[0]);
- yield put(
- setError({
- code: status,
- message: message,
- actionSucc: action,
- actionFail: googleLoginFailure()
- })
- );
- }
- }
- export function* getUserSagas(action: ActionSaga): Saga<void> {
- try {
- const appConfigReducer = yield select(getAppConfigReducer);
- const { aliasDomain } = appConfigReducer;
- const sessionReducer = yield select(getSessionReducer);
- const { accessToken } = sessionReducer.login;
- const { userId } = action.payload;
- const response = yield call(getUserApi, aliasDomain, accessToken, userId);
- yield put(resetError());
- yield put(getUserSuccess(response));
- } catch (error) {
- const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
- const message = idx(error, (_) => _.response.data.message[0]);
- yield put(
- setError({
- code: status,
- message: message,
- actionSucc: action,
- actionFail: getUserFailure()
- })
- );
- }
- }
- export function* refreshTokenSagas(): Saga<void> {
- // const userReducer = yield select(getAppConfigReducer);
- // const loginFromRedux = appConfig.login;
- // try {
- // const { accessToken, expiresIn, refreshToken } = yield call(postRefreshTokenApi, loginFromRedux.refreshToken);
- // yield put(refreshTokenSuccess(accessToken, expiresIn, refreshToken));
- // } catch (error) {
- // yield put(refreshTokenFailure());
- // }
- }
- export function* getOfflineContentSagas(action: ActionSaga): Saga<void> {
- try {
- const appConfigReducer = yield select(getAppConfigReducer);
- const { originalDomain } = appConfigReducer;
- const sessionReducer = yield select(getSessionReducer);
- const { ID: userId } = sessionReducer.login;
- var coursesDownloadedByUser = [];
- /*
- if (originalDomain && userId) {
- const coursesDownloaded = DB.getCoursesByOriginalDomain(originalDomain) || [];
- coursesDownloadedByUser = coursesDownloaded.filter((item) => DB.getCourseMetadataByKey(originalDomain, userId, item.courseId)).map((courseDownloadedByUser) => {
- const statusCourse = DB.getCourseStatusByKey(originalDomain, courseDownloadedByUser.courseId, userId);
- return { ...courseDownloadedByUser, status: statusCourse, locked };
- });
- }
- */
- if (originalDomain && userId) {
- const coursesDownloaded = DB.getCoursesByOriginalDomain(originalDomain);
- if (coursesDownloaded) {
- coursesDownloadedByUser = coursesDownloaded.reduce((courseDownloadedByUser, currentValue) => {
- const courseId = courseDownloadedByUser.courseId;
- const courseMetadata = DB.getCourseMetadataByKey(originalDomain, userId, courseId);
- if (courseMetadata) {
- currentValue.concat({ ...courseDownloadedByUser, status: courseMetadata.status, locked: courseMetadata.locked });
- }
- }, []);
- }
- }
- yield put(resetError());
- yield put(getOfflineContentSuccess(coursesDownloadedByUser));
- } catch (error) {
- const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
- const message = idx(error, (_) => _.response.data.message[0]);
- yield put(
- setError({
- code: status,
- message: message,
- actionSucc: action,
- actionFail: getOfflineContentFailure()
- })
- );
- }
- }
- export function* logoutSagas(action: ActionSaga): Saga<void> {
- try {
- const appConfigReducer = yield select(getAppConfigReducer);
- const { aliasDomain, offlineMode } = appConfigReducer;
- Reactotron.log('offline: ' + JSON.stringify(offlineMode));
- if (!offlineMode) {
- const sessionReducer = yield select(getSessionReducer);
- const { accessToken } = sessionReducer.login;
- yield call(logout, aliasDomain, accessToken);
- }
- yield put(resetError());
- yield put(logoutSuccess());
- ScreensBootstrap.login();
- } catch (error) {
- const status = idx(error, (_) => _.response.status) || GENERIC_ERROR;
- const message = idx(error, (_) => _.response.data.message[0]);
- yield put(
- setError({
- code: status,
- message: message,
- actionSucc: action,
- actionFail: logoutFailure()
- })
- );
- }
- }
Add Comment
Please, Sign In to add comment