Rocket.Chat.ReactNative/app/sagas/init.js

43 lines
1.5 KiB
JavaScript
Raw Normal View History

import { AsyncStorage } from 'react-native';
import { call, put, takeLatest } from 'redux-saga/effects';
import * as actions from '../actions';
import { setServer } from '../actions/server';
2017-11-13 13:19:24 +00:00
import { restoreToken } from '../actions/login';
import { APP } from '../actions/actionsTypes';
import { setRoles } from '../actions/roles';
import database from '../lib/realm';
2017-11-19 02:44:55 +00:00
import RocketChat from '../lib/rocketchat';
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));
}
const currentServer = yield call([AsyncStorage, 'getItem'], 'currentServer');
if (currentServer) {
yield put(setServer(currentServer));
const settings = database.objects('settings');
2017-11-24 20:44:52 +00:00
yield put(actions.setAllSettings(RocketChat.parseSettings(settings.slice(0, settings.length))));
const permissions = database.objects('permissions');
2017-11-24 20:44:52 +00:00
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;
}, {})));
}
2017-11-13 13:19:24 +00:00
yield put(actions.appReady({}));
} catch (e) {
console.log(e);
}
};
const root = function* root() {
yield takeLatest(APP.INIT, restore);
};
export default root;