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

53 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-04-24 12:17:38 +00:00
import { takeLatest, select } from 'redux-saga/effects';
import RocketChat from '../lib/rocketchat';
import { setBadgeCount } from '../notifications/push';
2019-05-28 16:18:46 +00:00
import log from '../utils/log';
2020-04-14 14:07:51 +00:00
import { localAuthenticate, saveLastLocalAuthenticationSession } from '../utils/localAuthentication';
2020-04-15 17:39:54 +00:00
import { APP_STATE } from '../actions/actionsTypes';
const appHasComeBackToForeground = function* appHasComeBackToForeground() {
const appRoot = yield select(state => state.app.root);
if (appRoot === 'outside') {
return;
}
const auth = yield select(state => state.login.isAuthenticated);
if (!auth) {
return;
}
try {
2020-04-14 13:07:13 +00:00
const server = yield select(state => state.server.server);
2020-04-17 19:11:06 +00:00
yield localAuthenticate(server);
setBadgeCount();
return yield RocketChat.setUserPresenceOnline();
} catch (e) {
log(e);
}
};
const appHasComeBackToBackground = function* appHasComeBackToBackground() {
const appRoot = yield select(state => state.app.root);
if (appRoot === 'outside') {
return;
}
const auth = yield select(state => state.login.isAuthenticated);
if (!auth) {
return;
}
try {
2020-04-14 14:07:51 +00:00
yield RocketChat.setUserPresenceAway();
const server = yield select(state => state.server.server);
yield saveLastLocalAuthenticationSession(server);
} catch (e) {
log(e);
}
};
const root = function* root() {
2020-04-15 17:39:54 +00:00
yield takeLatest(APP_STATE.FOREGROUND, appHasComeBackToForeground);
yield takeLatest(APP_STATE.BACKGROUND, appHasComeBackToBackground);
};
export default root;