[FIX] NewMessageView Press Item should open DM (#2116)

Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Djorkaeff Alexandre 2020-05-13 16:02:57 -03:00 committed by GitHub
parent ff60cc1fbf
commit 507723ab69
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 50 additions and 52 deletions

39
app/utils/goRoom.js Normal file
View File

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

View File

@ -26,6 +26,7 @@ import { themedHeader } from '../utils/navigation';
import { getUserSelector } from '../selectors/login'; import { getUserSelector } from '../selectors/login';
import Navigation from '../lib/Navigation'; import Navigation from '../lib/Navigation';
import { createChannelRequest } from '../actions/createChannel'; import { createChannelRequest } from '../actions/createChannel';
import { goRoom } from '../utils/goRoom';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
safeAreaView: { safeAreaView: {
@ -123,12 +124,6 @@ class NewMessageView extends React.Component {
this.search(text); this.search(text);
} }
onPressItem = (item) => {
const { navigation } = this.props;
const onPressItem = navigation.getParam('onPressItem', () => {});
onPressItem(item);
}
dismiss = () => { dismiss = () => {
const { navigation } = this.props; const { navigation } = this.props;
return navigation.pop(); return navigation.pop();
@ -231,7 +226,7 @@ class NewMessageView extends React.Component {
<UserItem <UserItem
name={item.search ? item.name : item.fname} name={item.search ? item.name : item.fname}
username={item.search ? item.username : item.name} username={item.search ? item.username : item.name}
onPress={() => this.onPressItem(item)} onPress={() => goRoom(item)}
baseUrl={baseUrl} baseUrl={baseUrl}
testID={`new-message-view-item-${ item.name }`} testID={`new-message-view-item-${ item.name }`}
style={style} style={style}

View File

@ -61,6 +61,7 @@ import {
import { MAX_SIDEBAR_WIDTH } from '../../constants/tablet'; import { MAX_SIDEBAR_WIDTH } from '../../constants/tablet';
import { withSplit } from '../../split'; import { withSplit } from '../../split';
import { getUserSelector } from '../../selectors/login'; import { getUserSelector } from '../../selectors/login';
import { goRoom } from '../../utils/goRoom';
const SCROLL_OFFSET = 56; const SCROLL_OFFSET = 56;
const INITIAL_NUM_TO_RENDER = isTablet ? 20 : 12; const INITIAL_NUM_TO_RENDER = isTablet ? 20 : 12;
@ -102,7 +103,6 @@ class RoomsListView extends React.Component {
static navigationOptions = ({ navigation, screenProps }) => { static navigationOptions = ({ navigation, screenProps }) => {
const searching = navigation.getParam('searching'); const searching = navigation.getParam('searching');
const cancelSearch = navigation.getParam('cancelSearch', () => {}); const cancelSearch = navigation.getParam('cancelSearch', () => {});
const onPressItem = navigation.getParam('onPressItem', () => {});
const initSearching = navigation.getParam( const initSearching = navigation.getParam(
'initSearching', 'initSearching',
() => {} () => {}
@ -137,9 +137,7 @@ class RoomsListView extends React.Component {
<Item <Item
title='new' title='new'
iconName='edit-rounded' iconName='edit-rounded'
onPress={() => navigation.navigate('NewMessageView', { onPress={() => navigation.navigate('NewMessageView')}
onPressItem
})}
testID='rooms-list-view-create-channel' testID='rooms-list-view-create-channel'
/> />
</CustomHeaderButtons> </CustomHeaderButtons>
@ -200,7 +198,6 @@ class RoomsListView extends React.Component {
this.getSubscriptions(); this.getSubscriptions();
const { navigation, closeServerDropdown } = this.props; const { navigation, closeServerDropdown } = this.props;
navigation.setParams({ navigation.setParams({
onPressItem: this._onPressItem,
initSearching: this.initSearching, initSearching: this.initSearching,
cancelSearch: this.cancelSearch cancelSearch: this.cancelSearch
}); });
@ -538,48 +535,15 @@ class RoomsListView extends React.Component {
getUidDirectMessage = room => RocketChat.getUidDirectMessage(room); getUidDirectMessage = room => RocketChat.getUidDirectMessage(room);
goRoom = (item) => { onPressItem = (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 = {}) => {
const { navigation } = this.props; const { navigation } = this.props;
if (!navigation.isFocused()) { if (!navigation.isFocused()) {
return; return;
} }
if (!item.search) {
return this.goRoom(item); this.cancelSearch();
} this.item = item;
if (item.t === 'd') { goRoom(item);
// 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);
}
}; };
toggleSort = () => { toggleSort = () => {
@ -723,7 +687,7 @@ class RoomsListView extends React.Component {
} else if (handleCommandNextRoom(event)) { } else if (handleCommandNextRoom(event)) {
this.goOtherRoom(1); this.goOtherRoom(1);
} else if (handleCommandShowNewMessage(event)) { } else if (handleCommandShowNewMessage(event)) {
navigation.navigate('NewMessageView', { onPressItem: this._onPressItem }); navigation.navigate('NewMessageView');
} else if (handleCommandAddNewServer(event)) { } else if (handleCommandAddNewServer(event)) {
navigation.navigate('NewServerView', { previousServer: server }); navigation.navigate('NewServerView', { previousServer: server });
} }
@ -809,7 +773,7 @@ class RoomsListView extends React.Component {
baseUrl={server} baseUrl={server}
prid={item.prid} prid={item.prid}
showLastMessage={StoreLastMessage} showLastMessage={StoreLastMessage}
onPress={() => this._onPressItem(item)} onPress={() => this.onPressItem(item)}
testID={`rooms-list-view-item-${ item.name }`} testID={`rooms-list-view-item-${ item.name }`}
width={split ? MAX_SIDEBAR_WIDTH : width} width={split ? MAX_SIDEBAR_WIDTH : width}
toggleFav={this.toggleFav} toggleFav={this.toggleFav}