2017-09-25 13:15:28 +00:00
|
|
|
import React from 'react';
|
2021-12-24 13:12:49 +00:00
|
|
|
import { StackNavigationProp } from '@react-navigation/stack';
|
|
|
|
import { RouteProp } from '@react-navigation/native';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { FlatList, View } from 'react-native';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-02-10 13:35:44 +00:00
|
|
|
import orderBy from 'lodash/orderBy';
|
2019-09-16 20:26:32 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
2021-12-24 13:12:49 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2021-10-01 18:12:19 +00:00
|
|
|
import * as List from '../containers/List';
|
2019-09-16 20:26:32 +00:00
|
|
|
import database from '../lib/database';
|
2017-09-25 13:15:28 +00:00
|
|
|
import RocketChat from '../lib/rocketchat';
|
2018-08-31 16:46:33 +00:00
|
|
|
import UserItem from '../presentation/UserItem';
|
2018-04-24 19:34:03 +00:00
|
|
|
import Loading from '../containers/Loading';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../i18n';
|
2021-10-01 18:12:19 +00:00
|
|
|
import log, { events, logEvent } from '../utils/log';
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2019-12-04 16:39:53 +00:00
|
|
|
import { themes } from '../constants/colors';
|
|
|
|
import { withTheme } from '../theme';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../selectors/login';
|
2021-10-01 18:12:19 +00:00
|
|
|
import { addUser as addUserAction, removeUser as removeUserAction, reset as resetAction } from '../actions/selectedUsers';
|
2020-04-01 12:28:54 +00:00
|
|
|
import { showErrorAlert } from '../utils/info';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2021-10-01 18:12:19 +00:00
|
|
|
import sharedStyles from './Styles';
|
2021-12-24 13:12:49 +00:00
|
|
|
import { ChatsStackParamList } from '../stacks/types';
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2021-06-15 20:13:42 +00:00
|
|
|
const ITEM_WIDTH = 250;
|
2021-12-24 13:12:49 +00:00
|
|
|
const getItemLayout = (_: any, index: number) => ({ length: ITEM_WIDTH, offset: ITEM_WIDTH * index, index });
|
|
|
|
|
|
|
|
interface IUser {
|
|
|
|
_id: string;
|
|
|
|
name: string;
|
|
|
|
fname: string;
|
|
|
|
search?: boolean;
|
|
|
|
// username is used when is from searching
|
|
|
|
username?: string;
|
|
|
|
}
|
|
|
|
interface ISelectedUsersViewState {
|
|
|
|
maxUsers?: number;
|
|
|
|
search: IUser[];
|
|
|
|
chats: IUser[];
|
|
|
|
}
|
|
|
|
|
|
|
|
interface ISelectedUsersViewProps {
|
|
|
|
navigation: StackNavigationProp<ChatsStackParamList, 'SelectedUsersView'>;
|
|
|
|
route: RouteProp<ChatsStackParamList, 'SelectedUsersView'>;
|
|
|
|
baseUrl: string;
|
|
|
|
addUser(user: IUser): void;
|
|
|
|
removeUser(user: IUser): void;
|
|
|
|
reset(): void;
|
|
|
|
users: IUser[];
|
|
|
|
loading: boolean;
|
|
|
|
user: {
|
|
|
|
id: string;
|
|
|
|
token: string;
|
|
|
|
username: string;
|
|
|
|
name: string;
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2021-12-24 13:12:49 +00:00
|
|
|
theme: string;
|
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
class SelectedUsersView extends React.Component<ISelectedUsersViewProps, ISelectedUsersViewState> {
|
|
|
|
private flatlist?: FlatList;
|
|
|
|
|
|
|
|
private querySubscription?: Subscription;
|
|
|
|
|
|
|
|
constructor(props: ISelectedUsersViewProps) {
|
2019-05-28 13:03:08 +00:00
|
|
|
super(props);
|
2019-09-16 20:26:32 +00:00
|
|
|
this.init();
|
2020-06-15 14:00:46 +00:00
|
|
|
const maxUsers = props.route.params?.maxUsers;
|
2017-09-25 13:15:28 +00:00
|
|
|
this.state = {
|
2020-04-01 12:28:54 +00:00
|
|
|
maxUsers,
|
2019-09-16 20:26:32 +00:00
|
|
|
search: [],
|
|
|
|
chats: []
|
2017-09-25 13:15:28 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
const { user } = this.props;
|
|
|
|
if (this.isGroupChat()) {
|
|
|
|
props.addUser({ _id: user.id, name: user.username, fname: user.name });
|
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader(props.route.params?.showButton);
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
componentDidUpdate(prevProps: ISelectedUsersViewProps) {
|
2020-04-01 12:28:54 +00:00
|
|
|
if (this.isGroupChat()) {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { users } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
if (prevProps.users.length !== users.length) {
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader(users.length > 0);
|
2020-04-01 12:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
componentWillUnmount() {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { reset } = this.props;
|
|
|
|
reset();
|
2019-09-16 20:26:32 +00:00
|
|
|
if (this.querySubscription && this.querySubscription.unsubscribe) {
|
|
|
|
this.querySubscription.unsubscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
// showButton can be sent as route params or updated by the component
|
2021-12-24 13:12:49 +00:00
|
|
|
setHeader = (showButton?: boolean) => {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { navigation, route } = this.props;
|
|
|
|
const title = route.params?.title ?? I18n.t('Select_Users');
|
|
|
|
const buttonText = route.params?.buttonText ?? I18n.t('Next');
|
|
|
|
const maxUsers = route.params?.maxUsers;
|
|
|
|
const nextAction = route.params?.nextAction ?? (() => {});
|
|
|
|
const options = {
|
|
|
|
title,
|
2021-10-01 18:12:19 +00:00
|
|
|
headerRight: () =>
|
2020-06-15 14:00:46 +00:00
|
|
|
(!maxUsers || showButton) && (
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item title={buttonText} onPress={nextAction} testID='selected-users-view-submit' />
|
|
|
|
</HeaderButton.Container>
|
2020-06-15 14:00:46 +00:00
|
|
|
)
|
|
|
|
};
|
|
|
|
navigation.setOptions(options);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2019-09-16 20:26:32 +00:00
|
|
|
// eslint-disable-next-line react/sort-comp
|
2021-10-01 18:12:19 +00:00
|
|
|
init = async () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
|
|
|
const observable = await db.collections
|
|
|
|
.get('subscriptions')
|
|
|
|
.query(Q.where('t', 'd'))
|
|
|
|
.observeWithColumns(['room_updated_at']);
|
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
// TODO: Refactor when migrate room
|
|
|
|
this.querySubscription = observable.subscribe((data: any) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const chats = orderBy(data, ['roomUpdatedAt'], ['desc']);
|
|
|
|
this.setState({ chats });
|
|
|
|
});
|
|
|
|
} catch (e) {
|
|
|
|
log(e);
|
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
onSearchChangeText(text: string) {
|
2018-10-23 21:39:48 +00:00
|
|
|
this.search(text);
|
|
|
|
}
|
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
search = async (text: string) => {
|
2018-08-31 18:13:30 +00:00
|
|
|
const result = await RocketChat.search({ text, filterRooms: false });
|
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2020-04-01 12:28:54 +00:00
|
|
|
isGroupChat = () => {
|
|
|
|
const { maxUsers } = this.state;
|
2021-12-24 13:12:49 +00:00
|
|
|
return maxUsers! > 2;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
isChecked = (username: string) => {
|
2018-09-25 19:28:42 +00:00
|
|
|
const { users } = this.props;
|
|
|
|
return users.findIndex(el => el.name === username) !== -1;
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
toggleUser = (user: IUser) => {
|
2020-04-01 12:28:54 +00:00
|
|
|
const { maxUsers } = this.state;
|
|
|
|
const {
|
2021-10-01 18:12:19 +00:00
|
|
|
addUser,
|
|
|
|
removeUser,
|
|
|
|
users,
|
|
|
|
user: { username }
|
2020-04-01 12:28:54 +00:00
|
|
|
} = this.props;
|
|
|
|
|
|
|
|
// Disallow removing self user from the direct message group
|
|
|
|
if (this.isGroupChat() && username === user.name) {
|
|
|
|
return;
|
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
if (!this.isChecked(user.name)) {
|
2020-04-01 12:28:54 +00:00
|
|
|
if (this.isGroupChat() && users.length === maxUsers) {
|
|
|
|
return showErrorAlert(I18n.t('Max_number_of_users_allowed_is_number', { maxUsers }), I18n.t('Oops'));
|
|
|
|
}
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.SELECTED_USERS_ADD_USER);
|
2018-09-25 19:28:42 +00:00
|
|
|
addUser(user);
|
2017-09-25 13:15:28 +00:00
|
|
|
} else {
|
2020-07-30 13:26:17 +00:00
|
|
|
logEvent(events.SELECTED_USERS_REMOVE_USER);
|
2018-09-25 19:28:42 +00:00
|
|
|
removeUser(user);
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
_onPressItem = (id: string, item = {} as IUser) => {
|
2017-09-25 13:15:28 +00:00
|
|
|
if (item.search) {
|
2021-12-24 13:12:49 +00:00
|
|
|
this.toggleUser({ _id: item._id, name: item.username!, fname: item.name });
|
2017-09-25 13:15:28 +00:00
|
|
|
} else {
|
2018-08-31 18:13:30 +00:00
|
|
|
this.toggleUser({ _id: item._id, name: item.name, fname: item.fname });
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
_onPressSelectedItem = (item: IUser) => this.toggleUser(item);
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
renderHeader = () => {
|
|
|
|
const { theme } = this.props;
|
|
|
|
return (
|
|
|
|
<View style={{ backgroundColor: themes[theme].backgroundColor }}>
|
2021-12-24 13:12:49 +00:00
|
|
|
<SearchBox onChangeText={(text: string) => this.onSearchChangeText(text)} testID='select-users-view-search' />
|
2019-12-04 16:39:53 +00:00
|
|
|
{this.renderSelected()}
|
|
|
|
</View>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
setFlatListRef = (ref: FlatList) => (this.flatlist = ref);
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
onContentSizeChange = () => this.flatlist?.scrollToEnd({ animated: true });
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
renderSelected = () => {
|
2019-12-04 16:39:53 +00:00
|
|
|
const { users, theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
|
|
|
if (users.length === 0) {
|
2017-09-25 13:15:28 +00:00
|
|
|
return null;
|
|
|
|
}
|
2021-06-15 20:13:42 +00:00
|
|
|
|
2017-09-25 13:15:28 +00:00
|
|
|
return (
|
2018-04-24 19:34:03 +00:00
|
|
|
<FlatList
|
2018-09-25 19:28:42 +00:00
|
|
|
data={users}
|
2021-06-15 20:13:42 +00:00
|
|
|
ref={this.setFlatListRef}
|
|
|
|
onContentSizeChange={this.onContentSizeChange}
|
|
|
|
getItemLayout={getItemLayout}
|
2018-04-24 19:34:03 +00:00
|
|
|
keyExtractor={item => item._id}
|
2019-12-04 16:39:53 +00:00
|
|
|
style={[sharedStyles.separatorTop, { borderColor: themes[theme].separatorColor }]}
|
2018-08-31 18:13:30 +00:00
|
|
|
contentContainerStyle={{ marginVertical: 5 }}
|
2018-04-24 19:34:03 +00:00
|
|
|
renderItem={this.renderSelectedItem}
|
2017-09-25 13:15:28 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
horizontal
|
|
|
|
/>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
renderSelectedItem = ({ item }: { item: IUser }) => {
|
|
|
|
const { theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={item.fname}
|
|
|
|
username={item.name}
|
|
|
|
onPress={() => this._onPressSelectedItem(item)}
|
2021-10-01 18:12:19 +00:00
|
|
|
testID={`selected-user-${item.name}`}
|
2018-09-25 19:28:42 +00:00
|
|
|
style={{ paddingRight: 15 }}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-09-25 19:28:42 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
renderItem = ({ item, index }: { item: IUser; index: number }) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2021-12-24 13:12:49 +00:00
|
|
|
const { theme } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
|
2018-08-31 18:13:30 +00:00
|
|
|
const name = item.search ? item.name : item.fname;
|
2021-12-24 13:12:49 +00:00
|
|
|
const username = item.search ? item.username! : item.name;
|
2019-12-04 16:39:53 +00:00
|
|
|
let style = { borderColor: themes[theme].separatorColor };
|
2018-08-31 18:13:30 +00:00
|
|
|
if (index === 0) {
|
2019-12-04 16:39:53 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorTop };
|
2018-08-31 18:13:30 +00:00
|
|
|
}
|
2018-09-25 19:28:42 +00:00
|
|
|
if (search.length > 0 && index === search.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
2019-09-16 20:26:32 +00:00
|
|
|
if (search.length === 0 && index === chats.length - 1) {
|
2018-08-31 18:13:30 +00:00
|
|
|
style = { ...style, ...sharedStyles.separatorBottom };
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<UserItem
|
|
|
|
name={name}
|
|
|
|
username={username}
|
|
|
|
onPress={() => this._onPressItem(item._id, item)}
|
2021-10-01 18:12:19 +00:00
|
|
|
testID={`select-users-view-item-${item.name}`}
|
2018-08-31 18:13:30 +00:00
|
|
|
icon={this.isChecked(username) ? 'check' : null}
|
|
|
|
style={style}
|
2019-12-04 16:39:53 +00:00
|
|
|
theme={theme}
|
2018-08-31 18:13:30 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
renderList = () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2019-12-04 16:39:53 +00:00
|
|
|
const { theme } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
|
|
|
|
const data = (search.length > 0 ? search : chats)
|
|
|
|
// filter DM between multiple users
|
|
|
|
.filter(sub => !RocketChat.isGroupChat(sub));
|
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
|
|
|
<FlatList
|
2020-04-01 12:28:54 +00:00
|
|
|
data={data}
|
2018-09-25 19:28:42 +00:00
|
|
|
extraData={this.props}
|
|
|
|
keyExtractor={item => item._id}
|
|
|
|
renderItem={this.renderItem}
|
2021-03-15 20:16:34 +00:00
|
|
|
ItemSeparatorComponent={List.Separator}
|
2018-09-25 19:28:42 +00:00
|
|
|
ListHeaderComponent={this.renderHeader}
|
2019-12-04 16:39:53 +00:00
|
|
|
contentContainerStyle={{ backgroundColor: themes[theme].backgroundColor }}
|
2018-09-25 19:28:42 +00:00
|
|
|
keyboardShouldPersistTaps='always'
|
|
|
|
/>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2018-09-25 19:28:42 +00:00
|
|
|
render = () => {
|
2020-10-30 13:59:44 +00:00
|
|
|
const { loading } = this.props;
|
2018-09-25 19:28:42 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='select-users-view'>
|
|
|
|
<StatusBar />
|
2018-09-25 19:28:42 +00:00
|
|
|
{this.renderList()}
|
|
|
|
<Loading visible={loading} />
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
2021-10-01 18:12:19 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2020-02-11 14:09:14 +00:00
|
|
|
baseUrl: state.server.server,
|
2019-08-07 13:51:34 +00:00
|
|
|
users: state.selectedUsers.users,
|
|
|
|
loading: state.selectedUsers.loading,
|
2020-02-11 14:09:14 +00:00
|
|
|
user: getUserSelector(state)
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2021-12-24 13:12:49 +00:00
|
|
|
const mapDispatchToProps = (dispatch: any) => ({
|
|
|
|
addUser: (user: any) => dispatch(addUserAction(user)),
|
|
|
|
removeUser: (user: any) => dispatch(removeUserAction(user)),
|
2020-04-01 12:28:54 +00:00
|
|
|
reset: () => dispatch(resetAction())
|
2019-08-07 13:51:34 +00:00
|
|
|
});
|
|
|
|
|
2019-12-04 16:39:53 +00:00
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(SelectedUsersView));
|