2018-07-10 13:40:32 +00:00
|
|
|
import { put, call, takeLatest } from 'redux-saga/effects';
|
2017-08-21 03:00:41 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2018-09-26 13:56:36 +00:00
|
|
|
import { Navigation } from 'react-native-navigation';
|
|
|
|
import { Provider } from 'react-redux';
|
2018-11-14 21:42:03 +00:00
|
|
|
import { gestureHandlerRootHOC } from 'react-native-gesture-handler';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
import { SERVER } from '../actions/actionsTypes';
|
2017-12-27 15:22:06 +00:00
|
|
|
import * as actions from '../actions';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { connectRequest } from '../actions/connect';
|
2018-08-10 17:26:36 +00:00
|
|
|
import { serverFailure, selectServerRequest, selectServerSuccess } from '../actions/server';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { setRoles } from '../actions/roles';
|
2017-09-01 19:42:50 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../lib/realm';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2018-09-26 13:56:36 +00:00
|
|
|
import store from '../lib/createStore';
|
|
|
|
|
|
|
|
let LoginSignupView = null;
|
2018-11-14 21:42:03 +00:00
|
|
|
let LoginView = null;
|
2017-08-18 21:30:16 +00:00
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
const handleSelectServer = function* handleSelectServer({ server }) {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
|
|
|
yield database.setActiveDB(server);
|
2018-11-16 11:06:29 +00:00
|
|
|
yield put(connectRequest());
|
2018-04-24 19:34:03 +00:00
|
|
|
yield call([AsyncStorage, 'setItem'], 'currentServer', server);
|
2018-08-01 19:35:06 +00:00
|
|
|
const token = yield AsyncStorage.getItem(`${ RocketChat.TOKEN_KEY }-${ server }`);
|
|
|
|
if (token) {
|
|
|
|
yield put(actions.appStart('inside'));
|
|
|
|
}
|
|
|
|
|
2018-04-24 19:34:03 +00:00
|
|
|
const settings = database.objects('settings');
|
2018-11-14 21:42:03 +00:00
|
|
|
yield put(actions.setAllSettings(RocketChat.parseSettings(RocketChat._filterSettings(settings.slice(0, settings.length)))));
|
2018-04-24 19:34:03 +00:00
|
|
|
const emojis = database.objects('customEmojis');
|
|
|
|
yield put(actions.setCustomEmojis(RocketChat.parseEmojis(emojis.slice(0, emojis.length))));
|
|
|
|
const roles = database.objects('roles');
|
|
|
|
yield put(setRoles(roles.reduce((result, role) => {
|
|
|
|
result[role._id] = role.description;
|
|
|
|
return result;
|
|
|
|
}, {})));
|
|
|
|
|
2018-08-01 19:35:06 +00:00
|
|
|
yield put(selectServerSuccess(server));
|
2018-04-24 19:34:03 +00:00
|
|
|
} catch (e) {
|
2018-07-10 13:40:32 +00:00
|
|
|
log('handleSelectServer', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
};
|
|
|
|
|
2018-08-10 17:26:36 +00:00
|
|
|
const handleServerRequest = function* handleServerRequest({ server }) {
|
2017-09-01 19:42:50 +00:00
|
|
|
try {
|
2018-11-14 21:42:03 +00:00
|
|
|
yield RocketChat.testServer(server);
|
|
|
|
const loginServicesLength = yield RocketChat.getLoginServices(server);
|
|
|
|
if (loginServicesLength === 0) {
|
|
|
|
if (LoginView == null) {
|
|
|
|
LoginView = require('../views/LoginView').default;
|
|
|
|
Navigation.registerComponentWithRedux('LoginView', () => gestureHandlerRootHOC(LoginView), Provider, store);
|
|
|
|
}
|
|
|
|
yield Navigation.push('NewServerView', {
|
|
|
|
component: {
|
|
|
|
name: 'LoginView'
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
2018-11-14 21:42:03 +00:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
if (LoginSignupView == null) {
|
|
|
|
LoginSignupView = require('../views/LoginSignupView').default;
|
|
|
|
Navigation.registerComponentWithRedux('LoginSignupView', () => gestureHandlerRootHOC(LoginSignupView), Provider, store);
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
2018-11-14 21:42:03 +00:00
|
|
|
yield Navigation.push('NewServerView', {
|
|
|
|
component: {
|
|
|
|
name: 'LoginSignupView'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
2018-10-23 21:39:48 +00:00
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
database.databases.serversDB.write(() => {
|
2018-09-05 18:15:03 +00:00
|
|
|
database.databases.serversDB.create('servers', { id: server }, true);
|
2018-05-18 17:55:08 +00:00
|
|
|
});
|
2018-08-01 19:35:06 +00:00
|
|
|
yield put(selectServerRequest(server));
|
2018-05-18 17:55:08 +00:00
|
|
|
} catch (e) {
|
2018-08-10 17:26:36 +00:00
|
|
|
yield put(serverFailure());
|
|
|
|
log('handleServerRequest', e);
|
2018-05-18 17:55:08 +00:00
|
|
|
}
|
2017-11-13 13:53:45 +00:00
|
|
|
};
|
2017-09-01 19:42:50 +00:00
|
|
|
|
2017-08-18 21:30:16 +00:00
|
|
|
const root = function* root() {
|
2018-08-01 19:35:06 +00:00
|
|
|
yield takeLatest(SERVER.SELECT_REQUEST, handleSelectServer);
|
2018-08-10 17:26:36 +00:00
|
|
|
yield takeLatest(SERVER.REQUEST, handleServerRequest);
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
|
|
|
export default root;
|