diff --git a/app/actions/actionsTypes.js b/app/actions/actionsTypes.js index bf07a329..786c411f 100644 --- a/app/actions/actionsTypes.js +++ b/app/actions/actionsTypes.js @@ -31,7 +31,7 @@ export const ROOMS = createRequestTypes('ROOMS', [ 'OPEN_SEARCH_HEADER', 'CLOSE_SEARCH_HEADER' ]); -export const ROOM = createRequestTypes('ROOM', ['LEAVE', 'DELETE_INIT', 'DELETE_FINISH', 'USER_TYPING']); +export const ROOM = createRequestTypes('ROOM', ['LEAVE', 'DELETE', 'REMOVED', 'USER_TYPING']); export const APP = createRequestTypes('APP', ['START', 'READY', 'INIT', 'INIT_LOCAL_SETTINGS']); export const MESSAGES = createRequestTypes('MESSAGES', ['REPLY_BROADCAST']); export const CREATE_CHANNEL = createRequestTypes('CREATE_CHANNEL', [...defaultTypes]); diff --git a/app/actions/room.js b/app/actions/room.js index 8d4e17e9..76a37b65 100644 --- a/app/actions/room.js +++ b/app/actions/room.js @@ -8,17 +8,17 @@ export function leaveRoom(rid, t) { }; } -export function deleteRoomInit(rid, t) { +export function deleteRoom(rid, t) { return { - type: types.ROOM.DELETE_INIT, + type: types.ROOM.DELETE, rid, t }; } -export function deleteRoomFinish() { +export function removedRoom() { return { - type: types.ROOM.DELETE_FINISH + type: types.ROOM.REMOVED }; } diff --git a/app/lib/methods/subscriptions/rooms.js b/app/lib/methods/subscriptions/rooms.js index 8ecb8370..7ba99097 100644 --- a/app/lib/methods/subscriptions/rooms.js +++ b/app/lib/methods/subscriptions/rooms.js @@ -14,7 +14,7 @@ import { handlePayloadUserInteraction } from '../actions'; import buildMessage from '../helpers/buildMessage'; import RocketChat from '../../rocketchat'; import EventEmmiter from '../../../utils/events'; -import { deleteRoomFinish } from '../../../actions/room'; +import { removedRoom } from '../../../actions/room'; const removeListener = listener => listener.stop(); @@ -245,7 +245,7 @@ export default function subscribeRooms() { // Delete and remove events come from this stream // Here we identify which one was triggered if (data.rid === roomState.rid && roomState.isDeleting) { - store.dispatch(deleteRoomFinish()); + store.dispatch(removedRoom()); } else { EventEmmiter.emit('ROOM_REMOVED', { rid: data.rid }); } diff --git a/app/reducers/room.js b/app/reducers/room.js index a689a448..6a34aa8d 100644 --- a/app/reducers/room.js +++ b/app/reducers/room.js @@ -7,13 +7,19 @@ const initialState = { export default function(state = initialState, action) { switch (action.type) { - case ROOM.DELETE_INIT: + case ROOM.LEAVE: return { ...state, rid: action.rid, isDeleting: true }; - case ROOM.DELETE_FINISH: + case ROOM.DELETE: + return { + ...state, + rid: action.rid, + isDeleting: true + }; + case ROOM.REMOVED: return { ...state, isDeleting: false diff --git a/app/sagas/room.js b/app/sagas/room.js index 13eb8c75..a5cd48b4 100644 --- a/app/sagas/room.js +++ b/app/sagas/room.js @@ -5,7 +5,7 @@ import { import Navigation from '../lib/Navigation'; import * as types from '../actions/actionsTypes'; -import { deleteRoomFinish } from '../actions/room'; +import { removedRoom } from '../actions/room'; import RocketChat from '../lib/rocketchat'; import log from '../utils/log'; import I18n from '../i18n'; @@ -28,12 +28,24 @@ const watchUserTyping = function* watchUserTyping({ rid, status }) { } }; +const handleRemovedRoom = function* handleLeaveRoom({ result }) { + if (result.success) { + yield Navigation.navigate('RoomsListView'); + } + // 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()); + } +}; + const handleLeaveRoom = function* handleLeaveRoom({ rid, t }) { try { const result = yield RocketChat.leaveRoom(rid, t); - if (result.success) { - yield Navigation.navigate('RoomsListView'); - } + yield handleRemovedRoom({ result }); } catch (e) { if (e.data && e.data.errorType === 'error-you-are-last-owner') { Alert.alert(I18n.t('Oops'), I18n.t(e.data.errorType)); @@ -46,17 +58,7 @@ const handleLeaveRoom = function* handleLeaveRoom({ rid, t }) { const handleDeleteRoom = function* handleDeleteRoom({ rid, t }) { try { const result = yield RocketChat.deleteRoom(rid, t); - if (result.success) { - yield Navigation.navigate('RoomsListView'); - } - // types.ROOM.DELETE_FINISH is triggered by `subscriptions-changed` with `removed` arg - const { timeout } = yield race({ - deleteFinished: take(types.ROOM.DELETE_FINISH), - timeout: delay(3000) - }); - if (timeout) { - put(deleteRoomFinish()); - } + yield handleRemovedRoom({ result }); } catch (e) { Alert.alert(I18n.t('Oops'), I18n.t('There_was_an_error_while_action', { action: I18n.t('deleting_room') })); } @@ -65,6 +67,6 @@ const handleDeleteRoom = function* handleDeleteRoom({ rid, t }) { const root = function* root() { yield takeLatest(types.ROOM.USER_TYPING, watchUserTyping); yield takeLatest(types.ROOM.LEAVE, handleLeaveRoom); - yield takeLatest(types.ROOM.DELETE_INIT, handleDeleteRoom); + yield takeLatest(types.ROOM.DELETE, handleDeleteRoom); }; export default root; diff --git a/app/views/RoomInfoEditView/index.js b/app/views/RoomInfoEditView/index.js index fe217ec1..63de0c02 100644 --- a/app/views/RoomInfoEditView/index.js +++ b/app/views/RoomInfoEditView/index.js @@ -11,7 +11,7 @@ import isEqual from 'lodash/isEqual'; import semver from 'semver'; import database from '../../lib/database'; -import { deleteRoomInit as deleteRoomInitAction } from '../../actions/room'; +import { deleteRoom as deleteRoomAction } from '../../actions/room'; import KeyboardView from '../../presentation/KeyboardView'; import sharedStyles from '../Styles'; import styles from './styles'; @@ -56,7 +56,7 @@ class RoomInfoEditView extends React.Component { static propTypes = { navigation: PropTypes.object, - deleteRoomInit: PropTypes.func, + deleteRoom: PropTypes.func, serverVersion: PropTypes.string, theme: PropTypes.string }; @@ -253,7 +253,7 @@ class RoomInfoEditView extends React.Component { delete = () => { const { room } = this.state; - const { deleteRoomInit } = this.props; + const { deleteRoom } = this.props; Alert.alert( I18n.t('Are_you_sure_question_mark'), @@ -266,7 +266,7 @@ class RoomInfoEditView extends React.Component { { text: I18n.t('Yes_action_it', { action: I18n.t('delete') }), style: 'destructive', - onPress: () => deleteRoomInit(room.rid, room.t) + onPress: () => deleteRoom(room.rid, room.t) } ], { cancelable: false } @@ -554,7 +554,7 @@ const mapStateToProps = state => ({ }); const mapDispatchToProps = dispatch => ({ - deleteRoomInit: (rid, t) => dispatch(deleteRoomInitAction(rid, t)) + deleteRoom: (rid, t) => dispatch(deleteRoomAction(rid, t)) }); export default connect(mapStateToProps, mapDispatchToProps)(withTheme(RoomInfoEditView));