2017-08-21 03:00:41 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2017-11-28 11:01:18 +00:00
|
|
|
import { call, put, takeLatest } from 'redux-saga/effects';
|
2017-08-21 03:00:41 +00:00
|
|
|
import * as actions from '../actions';
|
|
|
|
import { setServer } from '../actions/server';
|
2017-11-13 13:19:24 +00:00
|
|
|
import { restoreToken } from '../actions/login';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { APP } from '../actions/actionsTypes';
|
2017-12-27 15:22:06 +00:00
|
|
|
import database from '../lib/realm';
|
2017-11-19 02:44:55 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2017-08-21 03:00:41 +00:00
|
|
|
|
|
|
|
const restore = function* restore() {
|
|
|
|
try {
|
2017-11-13 13:19:24 +00:00
|
|
|
const token = yield call([AsyncStorage, 'getItem'], 'reactnativemeteor_usertoken');
|
|
|
|
if (token) {
|
|
|
|
yield put(restoreToken(token));
|
|
|
|
}
|
|
|
|
|
2017-08-21 03:00:41 +00:00
|
|
|
const currentServer = yield call([AsyncStorage, 'getItem'], 'currentServer');
|
2017-09-01 19:42:50 +00:00
|
|
|
if (currentServer) {
|
|
|
|
yield put(setServer(currentServer));
|
2017-12-27 15:22:06 +00:00
|
|
|
const settings = database.objects('settings');
|
2017-11-24 20:44:52 +00:00
|
|
|
yield put(actions.setAllSettings(RocketChat.parseSettings(settings.slice(0, settings.length))));
|
2017-12-27 15:22:06 +00:00
|
|
|
const permissions = database.objects('permissions');
|
2017-11-24 20:44:52 +00:00
|
|
|
yield put(actions.setAllPermissions(RocketChat.parsePermissions(permissions.slice(0, permissions.length))));
|
2018-01-16 18:48:05 +00:00
|
|
|
const emojis = database.objects('customEmojis');
|
|
|
|
yield put(actions.setCustomEmojis(RocketChat.parseEmojis(emojis.slice(0, emojis.length))));
|
2017-09-01 19:42:50 +00:00
|
|
|
}
|
2017-11-13 13:19:24 +00:00
|
|
|
yield put(actions.appReady({}));
|
2017-08-21 03:00:41 +00:00
|
|
|
} catch (e) {
|
|
|
|
console.log(e);
|
|
|
|
}
|
|
|
|
};
|
2017-11-28 11:01:18 +00:00
|
|
|
|
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(APP.INIT, restore);
|
|
|
|
};
|
|
|
|
export default root;
|