[FIX] App not showing proper alert on team leave (#3161)

* [IMPROVEMENT] refactoring how to leave team

* Fix the data passed to leaveTeam

* Fixed the lint error in i18n, the path of i18n, merged two ifs in one

* Fixed the Saga's flow when try to leave a room

* Fixed params passed to leaveRoom

* Fix the function name of leaveTeam

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Reinaldo Neto 2021-06-07 13:18:14 -03:00 committed by GitHub
parent 29ccb47456
commit 0b7461e800
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 34 additions and 43 deletions

View File

@ -14,11 +14,12 @@ export function unsubscribeRoom(rid) {
};
}
export function leaveRoom(rid, t) {
export function leaveRoom(roomType, room, selected) {
return {
type: types.ROOM.LEAVE,
rid,
t
room,
roomType,
selected
};
}

View File

@ -724,6 +724,7 @@
"creating_team": "creating team",
"team-name-already-exists": "A team with that name already exists",
"Add_Channel_to_Team": "Add Channel to Team",
"Left_The_Team_Successfully": "Left the team successfully",
"Create_New": "Create New",
"Add_Existing": "Add Existing",
"Add_Existing_Channel": "Add Existing Channel",

View File

@ -22,7 +22,7 @@ export default function(state = initialState, action) {
case ROOM.LEAVE:
return {
...state,
rid: action.rid,
rid: action.room.rid,
isDeleting: true
};
case ROOM.DELETE:

View File

@ -4,6 +4,7 @@ import {
takeLatest, take, select, delay, race, put
} from 'redux-saga/effects';
import EventEmitter from '../utils/events';
import Navigation from '../lib/Navigation';
import * as types from '../actions/actionsTypes';
import { removedRoom } from '../actions/room';
@ -11,6 +12,7 @@ import RocketChat from '../lib/rocketchat';
import log, { logEvent, events } from '../utils/log';
import I18n from '../i18n';
import { showErrorAlert } from '../utils/info';
import { LISTENER } from '../containers/Toast';
const watchUserTyping = function* watchUserTyping({ rid, status }) {
const auth = yield select(state => state.login.isAuthenticated);
@ -30,13 +32,18 @@ const watchUserTyping = function* watchUserTyping({ rid, status }) {
}
};
const handleRemovedRoom = function* handleRemovedRoom() {
const handleRemovedRoom = function* handleRemovedRoom(roomType) {
const isMasterDetail = yield select(state => state.app.isMasterDetail);
if (isMasterDetail) {
yield Navigation.navigate('DrawerNavigator');
} else {
yield Navigation.navigate('RoomsListView');
}
if (roomType === 'team') {
EventEmitter.emit(LISTENER, { message: I18n.t('Left_The_Team_Successfully') });
}
// types.ROOM.REMOVE is triggered by `subscriptions-changed` with `removed` arg
const { timeout } = yield race({
deleteFinished: take(types.ROOM.REMOVED),
@ -47,12 +54,19 @@ const handleRemovedRoom = function* handleRemovedRoom() {
}
};
const handleLeaveRoom = function* handleLeaveRoom({ rid, t }) {
const handleLeaveRoom = function* handleLeaveRoom({ room, roomType, selected }) {
logEvent(events.RA_LEAVE);
try {
const result = yield RocketChat.leaveRoom(rid, t);
if (result.success) {
yield handleRemovedRoom();
let result = {};
if (roomType === 'channel') {
result = yield RocketChat.leaveRoom(room.rid, room.t);
} else if (roomType === 'team') {
result = yield RocketChat.leaveTeam({ teamName: room.name, ...(selected && { rooms: selected }) });
}
if (result?.success) {
yield handleRemovedRoom(roomType);
}
} catch (e) {
logEvent(events.RA_LEAVE_F);

View File

@ -10,7 +10,9 @@ import { Q } from '@nozbe/watermelondb';
import { compareServerVersion, methods } from '../../lib/utils';
import Touch from '../../utils/touch';
import { setLoading as setLoadingAction } from '../../actions/selectedUsers';
import { leaveRoom as leaveRoomAction, closeRoom as closeRoomAction } from '../../actions/room';
import {
leaveRoom as leaveRoomAction, closeRoom as closeRoomAction
} from '../../actions/room';
import styles from './styles';
import sharedStyles from '../Styles';
import Avatar from '../../containers/Avatar';
@ -54,7 +56,6 @@ class RoomActionsView extends React.Component {
theme: PropTypes.string,
fontScale: PropTypes.number,
serverVersion: PropTypes.string,
isMasterDetail: PropTypes.bool,
addUserToJoinedRoomPermission: PropTypes.array,
addUserToAnyCRoomPermission: PropTypes.array,
addUserToAnyPRoomPermission: PropTypes.array,
@ -426,39 +427,13 @@ class RoomActionsView extends React.Component {
showConfirmationAlert({
message: I18n.t('Are_you_sure_you_want_to_leave_the_room', { room: RocketChat.getRoomTitle(room) }),
confirmationText: I18n.t('Yes_action_it', { action: I18n.t('leave') }),
onPress: () => leaveRoom(room.rid, room.t)
onPress: () => leaveRoom('channel', room)
});
}
handleLeaveTeam = async(selected) => {
logEvent(events.RA_LEAVE_TEAM);
try {
const { room } = this.state;
const { navigation, isMasterDetail } = this.props;
const result = await RocketChat.leaveTeam({ teamName: room.name, ...(selected && { rooms: selected }) });
if (result.success) {
if (isMasterDetail) {
navigation.navigate('DrawerNavigator');
} else {
navigation.navigate('RoomsListView');
}
}
} catch (e) {
logEvent(events.RA_LEAVE_TEAM_F);
log(e);
showErrorAlert(
e.data.error
? I18n.t(e.data.error)
: I18n.t('There_was_an_error_while_action', { action: I18n.t('leaving_team') }),
I18n.t('Cannot_leave')
);
}
}
leaveTeam = async() => {
const { room } = this.state;
const { navigation } = this.props;
const { navigation, leaveRoom } = this.props;
try {
const db = database.active;
@ -479,21 +454,21 @@ class RoomActionsView extends React.Component {
title: 'Leave_Team',
data: teamChannels,
infoText: 'Select_Team_Channels',
nextAction: data => this.handleLeaveTeam(data),
nextAction: data => leaveRoom('team', room, data),
showAlert: () => showErrorAlert(I18n.t('Last_owner_team_room'), I18n.t('Cannot_leave'))
});
} else {
showConfirmationAlert({
message: I18n.t('You_are_leaving_the_team', { team: RocketChat.getRoomTitle(room) }),
confirmationText: I18n.t('Yes_action_it', { action: I18n.t('leave') }),
onPress: () => this.handleLeaveTeam()
onPress: () => leaveRoom('team', room)
});
}
} catch (e) {
showConfirmationAlert({
message: I18n.t('You_are_leaving_the_team', { team: RocketChat.getRoomTitle(room) }),
confirmationText: I18n.t('Yes_action_it', { action: I18n.t('leave') }),
onPress: () => this.handleLeaveTeam()
onPress: () => leaveRoom('team', room)
});
}
}
@ -1122,7 +1097,7 @@ const mapStateToProps = state => ({
});
const mapDispatchToProps = dispatch => ({
leaveRoom: (rid, t) => dispatch(leaveRoomAction(rid, t)),
leaveRoom: (roomType, room, selected) => dispatch(leaveRoomAction(roomType, room, selected)),
closeRoom: rid => dispatch(closeRoomAction(rid)),
setLoadingInvite: loading => dispatch(setLoadingAction(loading))
});