2017-08-21 03:00:41 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2018-12-05 20:52:08 +00:00
|
|
|
import { put, takeLatest, all } from 'redux-saga/effects';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
2017-08-21 03:00:41 +00:00
|
|
|
import * as actions from '../actions';
|
2018-08-01 19:35:06 +00:00
|
|
|
import { selectServerRequest } from '../actions/server';
|
2018-09-04 14:29:20 +00:00
|
|
|
import { setAllPreferences } from '../actions/sortPreferences';
|
2017-09-01 19:42:50 +00:00
|
|
|
import { APP } from '../actions/actionsTypes';
|
2017-11-19 02:44:55 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-05-18 17:55:08 +00:00
|
|
|
import log from '../utils/log';
|
2017-08-21 03:00:41 +00:00
|
|
|
|
|
|
|
const restore = function* restore() {
|
|
|
|
try {
|
2018-12-05 20:52:08 +00:00
|
|
|
const { token, server } = yield all({
|
|
|
|
token: AsyncStorage.getItem(RocketChat.TOKEN_KEY),
|
|
|
|
server: AsyncStorage.getItem('currentServer')
|
|
|
|
});
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2018-09-04 14:29:20 +00:00
|
|
|
const sortPreferences = yield RocketChat.getSortPreferences();
|
|
|
|
yield put(setAllPreferences(sortPreferences));
|
|
|
|
|
2018-12-05 20:52:08 +00:00
|
|
|
if (!token || !server) {
|
|
|
|
yield all([
|
|
|
|
AsyncStorage.removeItem(RocketChat.TOKEN_KEY),
|
|
|
|
AsyncStorage.removeItem('currentServer')
|
|
|
|
]);
|
|
|
|
yield put(actions.appStart('outside'));
|
|
|
|
} else if (server) {
|
|
|
|
yield put(selectServerRequest(server));
|
|
|
|
}
|
|
|
|
|
2017-11-13 13:19:24 +00:00
|
|
|
yield put(actions.appReady({}));
|
2017-08-21 03:00:41 +00:00
|
|
|
} catch (e) {
|
2018-05-18 17:55:08 +00:00
|
|
|
log('restore', e);
|
2017-08-21 03:00:41 +00:00
|
|
|
}
|
|
|
|
};
|
2017-11-28 11:01:18 +00:00
|
|
|
|
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(APP.INIT, restore);
|
|
|
|
};
|
|
|
|
export default root;
|