Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] NewMessageView Press Item should open DM #2116

Merged
merged 2 commits into from
May 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app/utils/goRoom.js
Original file line number Diff line number Diff line change
@@ -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);
}
};
9 changes: 2 additions & 7 deletions app/views/NewMessageView.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -231,7 +226,7 @@ class NewMessageView extends React.Component {
<UserItem
name={item.search ? item.name : item.fname}
username={item.search ? item.username : item.name}
onPress={() => this.onPressItem(item)}
onPress={() => goRoom(item)}
baseUrl={baseUrl}
testID={`new-message-view-item-${ item.name }`}
style={style}
Expand Down
54 changes: 9 additions & 45 deletions app/views/RoomsListView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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',
() => {}
Expand Down Expand Up @@ -137,9 +137,7 @@ class RoomsListView extends React.Component {
<Item
title='new'
iconName='edit-rounded'
onPress={() => navigation.navigate('NewMessageView', {
onPressItem
})}
onPress={() => navigation.navigate('NewMessageView')}
testID='rooms-list-view-create-channel'
/>
</CustomHeaderButtons>
Expand Down Expand Up @@ -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
});
Expand Down Expand Up @@ -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 = () => {
Expand Down Expand Up @@ -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 });
}
Expand Down Expand Up @@ -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}
Expand Down