2018-05-07 20:43:26 +00:00
|
|
|
import { put, call, takeLatest, take } from 'redux-saga/effects';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { delay } from 'redux-saga';
|
2017-08-21 03:00:41 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2018-05-07 20:43:26 +00:00
|
|
|
import { SERVER, LOGIN } 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-05-07 20:43:26 +00:00
|
|
|
import { serverSuccess, serverFailure, setServer } 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-07 20:43:26 +00:00
|
|
|
import { navigate } from '../containers/routes/NavigationService';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2017-09-01 19:42:50 +00:00
|
|
|
|
|
|
|
const validate = function* validate(server) {
|
|
|
|
return yield RocketChat.testServer(server);
|
|
|
|
};
|
2017-08-18 21:30:16 +00:00
|
|
|
|
2017-08-21 03:00:41 +00:00
|
|
|
const selectServer = function* selectServer({ server }) {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
|
|
|
yield database.setActiveDB(server);
|
|
|
|
|
|
|
|
// yield RocketChat.disconnect();
|
|
|
|
|
|
|
|
yield call([AsyncStorage, 'setItem'], 'currentServer', server);
|
2018-05-07 20:43:26 +00:00
|
|
|
// yield AsyncStorage.removeItem(RocketChat.TOKEN_KEY);
|
2018-04-24 19:34:03 +00:00
|
|
|
const settings = database.objects('settings');
|
|
|
|
yield put(actions.setAllSettings(RocketChat.parseSettings(settings.slice(0, settings.length))));
|
|
|
|
const permissions = database.objects('permissions');
|
|
|
|
yield put(actions.setAllPermissions(RocketChat.parsePermissions(permissions.slice(0, permissions.length))));
|
|
|
|
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-05-07 20:43:26 +00:00
|
|
|
yield put(connectRequest());
|
2018-04-24 19:34:03 +00:00
|
|
|
} catch (e) {
|
2018-05-18 17:55:08 +00:00
|
|
|
log('selectServer', e);
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2017-09-01 19:42:50 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
const validateServer = function* validateServer({ server }) {
|
|
|
|
try {
|
|
|
|
yield delay(1000);
|
|
|
|
yield call(validate, server);
|
|
|
|
yield put(serverSuccess());
|
|
|
|
} catch (e) {
|
2018-04-24 19:34:03 +00:00
|
|
|
console.warn('validateServer', e);
|
2017-09-01 19:42:50 +00:00
|
|
|
yield put(serverFailure(e));
|
|
|
|
}
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
2017-09-01 19:42:50 +00:00
|
|
|
|
|
|
|
const addServer = function* addServer({ server }) {
|
2018-05-18 17:55:08 +00:00
|
|
|
try {
|
|
|
|
database.databases.serversDB.write(() => {
|
|
|
|
database.databases.serversDB.create('servers', { id: server, current: false }, true);
|
|
|
|
});
|
|
|
|
yield put(setServer(server));
|
|
|
|
yield take(LOGIN.SET_TOKEN);
|
|
|
|
navigate('LoginSignup');
|
|
|
|
} catch (e) {
|
|
|
|
log('addServer', e);
|
|
|
|
}
|
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() {
|
2017-09-01 19:42:50 +00:00
|
|
|
yield takeLatest(SERVER.REQUEST, validateServer);
|
2017-12-20 19:20:06 +00:00
|
|
|
yield takeLatest(SERVER.SELECT, selectServer);
|
|
|
|
yield takeLatest(SERVER.ADD, addServer);
|
2017-08-18 21:30:16 +00:00
|
|
|
};
|
|
|
|
export default root;
|