2019-02-07 16:13:21 +00:00
|
|
|
import { Alert } from 'react-native';
|
2020-05-08 17:36:10 +00:00
|
|
|
import prompt from 'react-native-prompt-android';
|
2019-02-07 16:13:21 +00:00
|
|
|
import {
|
2020-03-06 13:13:24 +00:00
|
|
|
takeLatest, take, select, delay, race, put
|
2019-02-07 16:13:21 +00:00
|
|
|
} from 'redux-saga/effects';
|
|
|
|
|
|
|
|
import Navigation from '../lib/Navigation';
|
|
|
|
import * as types from '../actions/actionsTypes';
|
2020-03-20 16:38:01 +00:00
|
|
|
import { removedRoom } from '../actions/room';
|
2019-02-07 16:13:21 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import log from '../utils/log';
|
|
|
|
import I18n from '../i18n';
|
2020-05-08 17:36:10 +00:00
|
|
|
import { showErrorAlert } from '../utils/info';
|
2019-02-07 16:13:21 +00:00
|
|
|
|
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-09-16 20:26:32 +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-08-23 13:18:47 +00:00
|
|
|
log(e);
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
const handleRemovedRoom = function* handleRemovedRoom() {
|
2020-06-15 14:00:46 +00:00
|
|
|
const isMasterDetail = yield select(state => state.app.isMasterDetail);
|
|
|
|
if (isMasterDetail) {
|
|
|
|
yield Navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
yield Navigation.navigate('RoomsListView');
|
|
|
|
}
|
2020-03-20 16:38:01 +00:00
|
|
|
// types.ROOM.REMOVE is triggered by `subscriptions-changed` with `removed` arg
|
|
|
|
const { timeout } = yield race({
|
|
|
|
deleteFinished: take(types.ROOM.REMOVED),
|
|
|
|
timeout: delay(3000)
|
|
|
|
});
|
|
|
|
if (timeout) {
|
|
|
|
put(removedRoom());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-07 16:13:21 +00:00
|
|
|
const handleLeaveRoom = function* handleLeaveRoom({ rid, t }) {
|
|
|
|
try {
|
|
|
|
const result = yield RocketChat.leaveRoom(rid, t);
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success) {
|
|
|
|
yield handleRemovedRoom();
|
|
|
|
}
|
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') }));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-06 13:13:24 +00:00
|
|
|
const handleDeleteRoom = function* handleDeleteRoom({ rid, t }) {
|
2019-02-07 16:13:21 +00:00
|
|
|
try {
|
2020-03-06 13:13:24 +00:00
|
|
|
const result = yield RocketChat.deleteRoom(rid, t);
|
2020-05-08 17:36:10 +00:00
|
|
|
if (result.success) {
|
|
|
|
yield handleRemovedRoom();
|
|
|
|
}
|
2019-02-07 16:13:21 +00:00
|
|
|
} catch (e) {
|
2020-03-06 13:13:24 +00:00
|
|
|
Alert.alert(I18n.t('Oops'), I18n.t('There_was_an_error_while_action', { action: I18n.t('deleting_room') }));
|
2019-02-07 16:13:21 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-05-08 17:36:10 +00:00
|
|
|
const handleCloseRoom = function* handleCloseRoom({ rid }) {
|
|
|
|
const requestComment = yield select(state => state.settings.Livechat_request_comment_when_closing_conversation);
|
|
|
|
|
|
|
|
const closeRoom = async(comment = '') => {
|
|
|
|
try {
|
|
|
|
await RocketChat.closeLivechat(rid, comment);
|
2020-06-15 14:00:46 +00:00
|
|
|
const isMasterDetail = await select(state => state.app.isMasterDetail);
|
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
Navigation.navigate('RoomsListView');
|
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!requestComment) {
|
|
|
|
const comment = I18n.t('Chat_closed_by_agent');
|
|
|
|
return closeRoom(comment);
|
|
|
|
}
|
|
|
|
|
|
|
|
prompt(
|
|
|
|
I18n.t('Closing_chat'),
|
|
|
|
I18n.t('Please_add_a_comment'),
|
|
|
|
[
|
|
|
|
{ text: I18n.t('Cancel'), onPress: () => { }, style: 'cancel' },
|
|
|
|
{
|
|
|
|
text: I18n.t('Submit'),
|
|
|
|
onPress: comment => closeRoom(comment)
|
|
|
|
}
|
|
|
|
],
|
|
|
|
{
|
|
|
|
cancelable: true
|
|
|
|
}
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleForwardRoom = function* handleForwardRoom({ transferData }) {
|
|
|
|
try {
|
|
|
|
const result = yield RocketChat.forwardLivechat(transferData);
|
|
|
|
if (result === true) {
|
2020-06-15 14:00:46 +00:00
|
|
|
const isMasterDetail = yield select(state => state.app.isMasterDetail);
|
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
|
|
|
Navigation.navigate('RoomsListView');
|
|
|
|
}
|
2020-05-08 17:36:10 +00:00
|
|
|
} else {
|
|
|
|
showErrorAlert(I18n.t('No_available_agents_to_transfer'), I18n.t('Oops'));
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
showErrorAlert(e.reason, I18n.t('Oops'));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-02-07 16:13:21 +00:00
|
|
|
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);
|
2020-03-20 16:38:01 +00:00
|
|
|
yield takeLatest(types.ROOM.DELETE, handleDeleteRoom);
|
2020-05-08 17:36:10 +00:00
|
|
|
yield takeLatest(types.ROOM.CLOSE, handleCloseRoom);
|
|
|
|
yield takeLatest(types.ROOM.FORWARD, handleForwardRoom);
|
2019-02-07 16:13:21 +00:00
|
|
|
};
|
|
|
|
export default root;
|