2018-09-25 19:28:42 +00:00
|
|
|
import {
|
|
|
|
call, takeLatest, select, put
|
|
|
|
} from 'redux-saga/effects';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { AsyncStorage } from 'react-native';
|
2017-08-17 01:06:31 +00:00
|
|
|
import { METEOR } from '../actions/actionsTypes';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-04-24 19:34:03 +00:00
|
|
|
import { setToken } from '../actions/login';
|
2017-08-17 01:06:31 +00:00
|
|
|
|
2017-09-01 19:42:50 +00:00
|
|
|
const getServer = ({ server }) => server.server;
|
2018-04-24 19:34:03 +00:00
|
|
|
const getToken = function* getToken() {
|
|
|
|
const currentServer = yield select(getServer);
|
|
|
|
const user = yield call([AsyncStorage, 'getItem'], `${ RocketChat.TOKEN_KEY }-${ currentServer }`);
|
|
|
|
if (user) {
|
|
|
|
yield put(setToken(JSON.parse(user)));
|
|
|
|
try {
|
|
|
|
yield call([AsyncStorage, 'setItem'], RocketChat.TOKEN_KEY, JSON.parse(user).token || '');
|
|
|
|
} catch (error) {
|
|
|
|
console.warn('getToken', error);
|
2017-12-20 19:20:06 +00:00
|
|
|
}
|
2018-04-24 19:34:03 +00:00
|
|
|
return JSON.parse(user);
|
2017-08-18 21:30:16 +00:00
|
|
|
}
|
2018-05-07 20:43:26 +00:00
|
|
|
|
|
|
|
yield AsyncStorage.removeItem(RocketChat.TOKEN_KEY);
|
|
|
|
yield put(setToken());
|
|
|
|
return null;
|
2017-08-17 16:55:47 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
|
|
|
|
|
|
|
const connect = (...args) => RocketChat.connect(...args);
|
|
|
|
|
2017-12-20 19:20:06 +00:00
|
|
|
const test = function* test() {
|
2018-04-24 19:34:03 +00:00
|
|
|
try {
|
|
|
|
const server = yield select(getServer);
|
|
|
|
const user = yield call(getToken);
|
|
|
|
// const response =
|
2018-07-10 13:40:32 +00:00
|
|
|
// yield all([call(connect, server, user && user.token ? { resume: user.token, ...user.user } : undefined)]);// , put(loginRequest({ resume: user.token }))]);
|
2018-10-15 20:22:42 +00:00
|
|
|
yield call(connect, server, user && user.token ? { resume: user.token, ...user } : null);
|
2017-12-20 19:20:06 +00:00
|
|
|
// yield put(connectSuccess(response));
|
2018-04-24 19:34:03 +00:00
|
|
|
} catch (err) {
|
|
|
|
console.warn('test', err);
|
2017-12-20 19:20:06 +00:00
|
|
|
// yield put(connectFailure(err.status));
|
2018-04-24 19:34:03 +00:00
|
|
|
}
|
2017-12-20 19:20:06 +00:00
|
|
|
};
|
|
|
|
|
2017-08-17 16:55:47 +00:00
|
|
|
const root = function* root() {
|
2017-09-01 19:42:50 +00:00
|
|
|
yield takeLatest(METEOR.REQUEST, test);
|
2017-12-20 19:20:06 +00:00
|
|
|
// yield take(METEOR.SUCCESS, watchConnect);
|
2018-04-24 19:34:03 +00:00
|
|
|
// yield takeLatest(METEOR.SUCCESS, watchConnect);
|
2017-08-17 16:55:47 +00:00
|
|
|
};
|
|
|
|
export default root;
|