From a305676fd6587076fb8be9d4cb3c907ea51245ab Mon Sep 17 00:00:00 2001 From: Gerzon Z Date: Fri, 7 May 2021 08:01:27 -0400 Subject: [PATCH] Added deleteTeam function --- app/i18n/locales/en.json | 4 +- app/lib/rocketchat.js | 4 ++ app/views/RoomInfoEditView/index.js | 104 +++++++++++++++++++++------- app/views/SelectListView.js | 3 +- 4 files changed, 89 insertions(+), 26 deletions(-) diff --git a/app/i18n/locales/en.json b/app/i18n/locales/en.json index ea20c91e7..e6138f8ec 100644 --- a/app/i18n/locales/en.json +++ b/app/i18n/locales/en.json @@ -737,5 +737,7 @@ "Removing_user_from_this_Team": "You are removing {{user}} from this Team", "Remove_User_Teams": "Select channels you want the user to be removed from.", "Remove_Member": "Remove Member", - "Error": "Error" + "Error": "Error", + "Delete_Team": "Delete Team", + "Delete_Team_Warning": "You are deleting this team." } diff --git a/app/lib/rocketchat.js b/app/lib/rocketchat.js index 5478c6b22..fad815a2b 100644 --- a/app/lib/rocketchat.js +++ b/app/lib/rocketchat.js @@ -772,6 +772,10 @@ const RocketChat = { // RC 3.13.0 return this.post('teams.removeMember', { teamName, userId }); }, + deleteTeam({ teamName }) { + // RC 3.13.0 + return this.post('teams.delete', { teamName }); + }, joinRoom(roomId, joinCode, type) { // TODO: join code // RC 0.48.0 diff --git a/app/views/RoomInfoEditView/index.js b/app/views/RoomInfoEditView/index.js index 7552b093a..1986e2725 100644 --- a/app/views/RoomInfoEditView/index.js +++ b/app/views/RoomInfoEditView/index.js @@ -8,6 +8,7 @@ import { BLOCK_CONTEXT } from '@rocket.chat/ui-kit'; import ImagePicker from 'react-native-image-crop-picker'; import { dequal } from 'dequal'; import isEmpty from 'lodash/isEmpty'; +import { Q } from '@nozbe/watermelondb'; import { compareServerVersion, methods } from '../../lib/utils'; import database from '../../lib/database'; @@ -41,6 +42,7 @@ const PERMISSION_ARCHIVE = 'archive-room'; const PERMISSION_UNARCHIVE = 'unarchive-room'; const PERMISSION_DELETE_C = 'delete-c'; const PERMISSION_DELETE_P = 'delete-p'; +const PERMISSION_EDIT_TEAM_CHANNEL = 'edit-team-channel'; class RoomInfoEditView extends React.Component { static navigationOptions = () => ({ @@ -48,6 +50,7 @@ class RoomInfoEditView extends React.Component { }) static propTypes = { + navigation: PropTypes.object, route: PropTypes.object, deleteRoom: PropTypes.func, serverVersion: PropTypes.string, @@ -58,7 +61,8 @@ class RoomInfoEditView extends React.Component { archiveRoomPermission: PropTypes.array, unarchiveRoomPermission: PropTypes.array, deleteCPermission: PropTypes.array, - deletePPermission: PropTypes.array + deletePPermission: PropTypes.array, + editTeamChannelPermission: PropTypes.array }; constructor(props) { @@ -100,7 +104,8 @@ class RoomInfoEditView extends React.Component { archiveRoomPermission, unarchiveRoomPermission, deleteCPermission, - deletePPermission + deletePPermission, + editTeamChannelPermission } = this.props; const rid = route.params?.rid; if (!rid) { @@ -116,25 +121,51 @@ class RoomInfoEditView extends React.Component { this.init(this.room); }); - const result = await RocketChat.hasPermission([ - setReadOnlyPermission, - setReactWhenReadOnlyPermission, - archiveRoomPermission, - unarchiveRoomPermission, - deleteCPermission, - deletePPermission - ], rid); + let result; - this.setState({ - permissions: { - [PERMISSION_SET_READONLY]: result[0], - [PERMISSION_SET_REACT_WHEN_READONLY]: result[1], - [PERMISSION_ARCHIVE]: result[2], - [PERMISSION_UNARCHIVE]: result[3], - [PERMISSION_DELETE_C]: result[4], - [PERMISSION_DELETE_P]: result[5] - } - }); + if (this.room.teamId) { + result = await RocketChat.hasPermission([ + setReadOnlyPermission, + setReactWhenReadOnlyPermission, + archiveRoomPermission, + unarchiveRoomPermission, + deleteCPermission, + deletePPermission, + editTeamChannelPermission + ], rid); + + this.setState({ + permissions: { + [PERMISSION_SET_READONLY]: result[0], + [PERMISSION_SET_REACT_WHEN_READONLY]: result[1], + [PERMISSION_ARCHIVE]: result[2], + [PERMISSION_UNARCHIVE]: result[3], + [PERMISSION_DELETE_C]: result[4], + [PERMISSION_DELETE_P]: result[5], + [PERMISSION_EDIT_TEAM_CHANNEL]: result[6] + } + }); + } else { + result = await RocketChat.hasPermission([ + setReadOnlyPermission, + setReactWhenReadOnlyPermission, + archiveRoomPermission, + unarchiveRoomPermission, + deleteCPermission, + deletePPermission + ], rid); + + this.setState({ + permissions: { + [PERMISSION_SET_READONLY]: result[0], + [PERMISSION_SET_REACT_WHEN_READONLY]: result[1], + [PERMISSION_ARCHIVE]: result[2], + [PERMISSION_UNARCHIVE]: result[3], + [PERMISSION_DELETE_C]: result[4], + [PERMISSION_DELETE_P]: result[5] + } + }); + } } catch (e) { log(e); } @@ -284,13 +315,37 @@ class RoomInfoEditView extends React.Component { }, 100); } + deleteTeam = async(teamName) => { + const { navigation } = this.props; + const { room } = this.state; + try { + const result = await RocketChat.deleteTeam({ teamName }); + if (result.success) { + const db = database.active; + const subCollection = db.get('subscriptions'); + const teamChannels = await subCollection.query( + Q.and(Q.where('team_id', Q.eq(this.room.teamId), Q.where('name', Q.notEq(this.room.name)))) + ); + if (teamChannels.length) { + navigation.navigate('SelectListView', { + title: 'Delete_Team', teamChannels: this.teamChannels, teamName: room.name, subtitle: 'Select_Teams', delete: this.delete + }); + } else { + navigation.navigate('RoomsListView'); + } + } + } catch (e) { + log(e); + } + } + delete = () => { const { room } = this.state; const { deleteRoom } = this.props; Alert.alert( - I18n.t('Are_you_sure_question_mark'), - I18n.t('Delete_Room_Warning'), + I18n.t('Confirmation'), + I18n.t('Delete_Team_Warning'), [ { text: I18n.t('Cancel'), @@ -299,7 +354,7 @@ class RoomInfoEditView extends React.Component { { text: I18n.t('Yes_action_it', { action: I18n.t('delete') }), style: 'destructive', - onPress: () => deleteRoom(room.rid, room.t) + onPress: () => (this.room.teamId ? this.deleteTeam(room.name) : deleteRoom(room.rid, room.t)) } ], { cancelable: false } @@ -678,7 +733,8 @@ const mapStateToProps = state => ({ archiveRoomPermission: state.permissions[PERMISSION_ARCHIVE], unarchiveRoomPermission: state.permissions[PERMISSION_UNARCHIVE], deleteCPermission: state.permissions[PERMISSION_DELETE_C], - deletePPermission: state.permissions[PERMISSION_DELETE_P] + deletePPermission: state.permissions[PERMISSION_DELETE_P], + editTeamChannelPermission: state.permissions[PERMISSION_EDIT_TEAM_CHANNEL] }); const mapDispatchToProps = dispatch => ({ diff --git a/app/views/SelectListView.js b/app/views/SelectListView.js index 5f21f2aec..4db265084 100644 --- a/app/views/SelectListView.js +++ b/app/views/SelectListView.js @@ -71,6 +71,7 @@ class SelectListView extends React.Component { this.subtitle = props.route?.params?.subtitle; this.teamName = props.route?.params?.teamName; this.room = props.route?.params?.room; + this.delete = props.route?.params?.delete; this.state = { data: teamChannels, selected: [], @@ -96,7 +97,7 @@ class SelectListView extends React.Component { options.headerRight = () => ( - + );