diff --git a/app/utils/goRoom.js b/app/utils/goRoom.js new file mode 100644 index 000000000..70a4fe013 --- /dev/null +++ b/app/utils/goRoom.js @@ -0,0 +1,39 @@ +import Navigation from '../lib/Navigation'; +import RocketChat from '../lib/rocketchat'; + +const navigate = (item) => { + Navigation.navigate('RoomView', { + rid: item.rid, + name: RocketChat.getRoomTitle(item), + t: item.t, + prid: item.prid, + room: item, + search: item.search, + visitor: item.visitor, + roomUserId: RocketChat.getUidDirectMessage(item) + }); +}; + +export const goRoom = async(item = {}) => { + if (!item.search) { + return navigate(item); + } + if (item.t === 'd') { + // if user is using the search we need first to join/create room + try { + const { username } = item; + const result = await RocketChat.createDirectMessage(username); + if (result.success) { + return navigate({ + rid: result.room._id, + name: username, + t: 'd' + }); + } + } catch { + // Do nothing + } + } else { + return navigate(item); + } +}; diff --git a/app/views/NewMessageView.js b/app/views/NewMessageView.js index cecd09886..42f9c1d5e 100644 --- a/app/views/NewMessageView.js +++ b/app/views/NewMessageView.js @@ -26,6 +26,7 @@ import { themedHeader } from '../utils/navigation'; import { getUserSelector } from '../selectors/login'; import Navigation from '../lib/Navigation'; import { createChannelRequest } from '../actions/createChannel'; +import { goRoom } from '../utils/goRoom'; const styles = StyleSheet.create({ safeAreaView: { @@ -123,12 +124,6 @@ class NewMessageView extends React.Component { this.search(text); } - onPressItem = (item) => { - const { navigation } = this.props; - const onPressItem = navigation.getParam('onPressItem', () => {}); - onPressItem(item); - } - dismiss = () => { const { navigation } = this.props; return navigation.pop(); @@ -231,7 +226,7 @@ class NewMessageView extends React.Component { this.onPressItem(item)} + onPress={() => goRoom(item)} baseUrl={baseUrl} testID={`new-message-view-item-${ item.name }`} style={style} diff --git a/app/views/RoomsListView/index.js b/app/views/RoomsListView/index.js index 699bd3b9b..15d00d1aa 100644 --- a/app/views/RoomsListView/index.js +++ b/app/views/RoomsListView/index.js @@ -61,6 +61,7 @@ import { import { MAX_SIDEBAR_WIDTH } from '../../constants/tablet'; import { withSplit } from '../../split'; import { getUserSelector } from '../../selectors/login'; +import { goRoom } from '../../utils/goRoom'; const SCROLL_OFFSET = 56; const INITIAL_NUM_TO_RENDER = isTablet ? 20 : 12; @@ -102,7 +103,6 @@ class RoomsListView extends React.Component { static navigationOptions = ({ navigation, screenProps }) => { const searching = navigation.getParam('searching'); const cancelSearch = navigation.getParam('cancelSearch', () => {}); - const onPressItem = navigation.getParam('onPressItem', () => {}); const initSearching = navigation.getParam( 'initSearching', () => {} @@ -137,9 +137,7 @@ class RoomsListView extends React.Component { navigation.navigate('NewMessageView', { - onPressItem - })} + onPress={() => navigation.navigate('NewMessageView')} testID='rooms-list-view-create-channel' /> @@ -200,7 +198,6 @@ class RoomsListView extends React.Component { this.getSubscriptions(); const { navigation, closeServerDropdown } = this.props; navigation.setParams({ - onPressItem: this._onPressItem, initSearching: this.initSearching, cancelSearch: this.cancelSearch }); @@ -538,48 +535,15 @@ class RoomsListView extends React.Component { getUidDirectMessage = room => RocketChat.getUidDirectMessage(room); - goRoom = (item) => { - const { navigation } = this.props; - this.cancelSearch(); - this.item = item; - navigation.navigate('RoomView', { - rid: item.rid, - name: this.getRoomTitle(item), - t: item.t, - prid: item.prid, - room: item, - search: item.search, - visitor: item.visitor, - roomUserId: this.getUidDirectMessage(item) - }); - } - - _onPressItem = async(item = {}) => { + onPressItem = (item = {}) => { const { navigation } = this.props; if (!navigation.isFocused()) { return; } - if (!item.search) { - return this.goRoom(item); - } - if (item.t === 'd') { - // if user is using the search we need first to join/create room - try { - const { username } = item; - const result = await RocketChat.createDirectMessage(username); - if (result.success) { - return this.goRoom({ - rid: result.room._id, - name: username, - t: 'd' - }); - } - } catch (e) { - log(e); - } - } else { - return this.goRoom(item); - } + + this.cancelSearch(); + this.item = item; + goRoom(item); }; toggleSort = () => { @@ -723,7 +687,7 @@ class RoomsListView extends React.Component { } else if (handleCommandNextRoom(event)) { this.goOtherRoom(1); } else if (handleCommandShowNewMessage(event)) { - navigation.navigate('NewMessageView', { onPressItem: this._onPressItem }); + navigation.navigate('NewMessageView'); } else if (handleCommandAddNewServer(event)) { navigation.navigate('NewServerView', { previousServer: server }); } @@ -809,7 +773,7 @@ class RoomsListView extends React.Component { baseUrl={server} prid={item.prid} showLastMessage={StoreLastMessage} - onPress={() => this._onPressItem(item)} + onPress={() => this.onPressItem(item)} testID={`rooms-list-view-item-${ item.name }`} width={split ? MAX_SIDEBAR_WIDTH : width} toggleFav={this.toggleFav}