[FIX] Stop showing message on leave channel (#1896)

* [FIX] Leave room don't show 'was removed' message

* [FIX] Remove duplicated code

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Djorkaeff Alexandre 2020-03-20 13:38:01 -03:00 committed by GitHub
parent c58120a639
commit fe5ee355f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 38 additions and 30 deletions

View File

@ -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]);

View File

@ -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
};
}

View File

@ -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 });
}

View File

@ -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

View File

@ -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 handleLeaveRoom = function* handleLeaveRoom({ rid, t }) {
try {
const result = yield RocketChat.leaveRoom(rid, t);
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);
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;

View File

@ -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));