2019-02-07 16:13:21 +00:00
|
|
|
import { Alert } from 'react-native';
|
|
|
|
import {
|
2019-10-07 20:56:30 +00:00
|
|
|
takeLatest, take, select, delay
|
2019-02-07 16:13:21 +00:00
|
|
|
} from 'redux-saga/effects';
|
|
|
|
|
|
|
|
import Navigation from '../lib/Navigation';
|
|
|
|
import * as types from '../actions/actionsTypes';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import log from '../utils/log';
|
|
|
|
import I18n from '../i18n';
|
|
|
|
|
2019-04-08 12:35:28 +00:00
|
|
|
const watchUserTyping = function* watchUserTyping({ rid, status }) {
|
2019-02-07 16:13:21 +00:00
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(types.LOGIN.SUCCESS);
|
|
|
|
}
|
|
|
|
|
|
|
|
try {
|
2019-04-08 12:35:28 +00:00
|
|
|
yield RocketChat.emitTyping(rid, status);
|
2019-02-07 16:13:21 +00:00
|
|
|
|
|
|
|
if (status) {
|
2019-10-07 20:56:30 +00:00
|
|
|
yield delay(5000);
|
2019-04-08 12:35:28 +00:00
|
|
|
yield RocketChat.emitTyping(rid, false);
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2019-09-03 19:27:57 +00:00
|
|
|
log(e);
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleLeaveRoom = function* handleLeaveRoom({ rid, t }) {
|
|
|
|
try {
|
|
|
|
const result = yield RocketChat.leaveRoom(rid, t);
|
|
|
|
if (result.success) {
|
2019-03-12 16:23:06 +00:00
|
|
|
yield Navigation.navigate('RoomsListView');
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
if (e.data && e.data.errorType === 'error-you-are-last-owner') {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t(e.data.errorType));
|
|
|
|
} else {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t('There_was_an_error_while_action', { action: I18n.t('leaving_room') }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleEraseRoom = function* handleEraseRoom({ rid, t }) {
|
|
|
|
try {
|
|
|
|
const result = yield RocketChat.eraseRoom(rid, t);
|
|
|
|
if (result.success) {
|
2019-03-12 16:23:06 +00:00
|
|
|
yield Navigation.navigate('RoomsListView');
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t('There_was_an_error_while_action', { action: I18n.t('erasing_room') }));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
2019-04-08 12:35:28 +00:00
|
|
|
yield takeLatest(types.ROOM.USER_TYPING, watchUserTyping);
|
2019-02-07 16:13:21 +00:00
|
|
|
yield takeLatest(types.ROOM.LEAVE, handleLeaveRoom);
|
|
|
|
yield takeLatest(types.ROOM.ERASE, handleEraseRoom);
|
|
|
|
};
|
|
|
|
export default root;
|