2022-01-11 13:48:01 +00:00
|
|
|
import { Q } from '@nozbe/watermelondb';
|
|
|
|
import orderBy from 'lodash/orderBy';
|
2017-09-25 13:15:28 +00:00
|
|
|
import React from 'react';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { FlatList, View } from 'react-native';
|
2019-01-31 16:08:38 +00:00
|
|
|
import { connect } from 'react-redux';
|
2021-12-13 16:28:59 +00:00
|
|
|
import { Subscription } from 'rxjs';
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
import { addUser, removeUser, reset } from '../actions/selectedUsers';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { themes } from '../lib/constants';
|
2022-01-11 13:48:01 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2021-09-13 20:41:05 +00:00
|
|
|
import * as List from '../containers/List';
|
2018-04-24 19:34:03 +00:00
|
|
|
import Loading from '../containers/Loading';
|
2022-01-11 13:48:01 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2018-08-31 18:13:30 +00:00
|
|
|
import SearchBox from '../containers/SearchBox';
|
2019-03-12 16:23:06 +00:00
|
|
|
import StatusBar from '../containers/StatusBar';
|
2022-04-07 16:27:45 +00:00
|
|
|
import { IApplicationState, IBaseScreen, ISubscription, IUser } from '../definitions';
|
2022-01-11 13:48:01 +00:00
|
|
|
import I18n from '../i18n';
|
|
|
|
import database from '../lib/database';
|
2022-04-09 02:43:31 +00:00
|
|
|
import UserItem from '../containers/UserItem';
|
2022-01-11 13:48:01 +00:00
|
|
|
import { ISelectedUser } from '../reducers/selectedUsers';
|
2020-02-11 14:09:14 +00:00
|
|
|
import { getUserSelector } from '../selectors/login';
|
2022-01-11 13:48:01 +00:00
|
|
|
import { ChatsStackParamList } from '../stacks/types';
|
|
|
|
import { withTheme } from '../theme';
|
2020-04-01 12:28:54 +00:00
|
|
|
import { showErrorAlert } from '../utils/info';
|
2022-01-11 13:48:01 +00:00
|
|
|
import log, { events, logEvent } from '../utils/log';
|
2021-09-13 20:41:05 +00:00
|
|
|
import sharedStyles from './Styles';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { isGroupChat, search } from '../lib/methods';
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2021-05-12 19:01:29 +00:00
|
|
|
const ITEM_WIDTH = 250;
|
2021-12-13 16:28:59 +00:00
|
|
|
const getItemLayout = (_: any, index: number) => ({ length: ITEM_WIDTH, offset: ITEM_WIDTH * index, index });
|
|
|
|
|
|
|
|
interface ISelectedUsersViewState {
|
|
|
|
maxUsers?: number;
|
2022-01-11 13:48:01 +00:00
|
|
|
search: ISelectedUser[];
|
|
|
|
chats: ISelectedUser[];
|
2021-12-13 16:28:59 +00:00
|
|
|
}
|
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
interface ISelectedUsersViewProps extends IBaseScreen<ChatsStackParamList, 'SelectedUsersView'> {
|
|
|
|
// REDUX STATE
|
|
|
|
users: ISelectedUser[];
|
2021-12-13 16:28:59 +00:00
|
|
|
loading: boolean;
|
2022-04-07 16:27:45 +00:00
|
|
|
user: IUser;
|
2022-01-11 13:48:01 +00:00
|
|
|
baseUrl: string;
|
2021-12-13 16:28:59 +00:00
|
|
|
}
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2021-12-13 16:28:59 +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
|
|
|
};
|
2022-01-11 13:48:01 +00:00
|
|
|
const { user, dispatch } = this.props;
|
2020-04-01 12:28:54 +00:00
|
|
|
if (this.isGroupChat()) {
|
2022-04-07 16:27:45 +00:00
|
|
|
dispatch(addUser({ _id: user.id, name: user.username, fname: user.name as string }));
|
2020-04-01 12:28:54 +00:00
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader(props.route.params?.showButton);
|
2018-07-10 13:40:32 +00:00
|
|
|
}
|
|
|
|
|
2021-12-13 16:28:59 +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() {
|
2022-01-11 13:48:01 +00:00
|
|
|
const { dispatch } = this.props;
|
|
|
|
dispatch(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-13 16:28:59 +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-09-13 20:41:05 +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-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
init = async () => {
|
2019-09-16 20:26:32 +00:00
|
|
|
try {
|
|
|
|
const db = database.active;
|
2022-02-01 13:39:09 +00:00
|
|
|
const observable = await db.get('subscriptions').query(Q.where('t', 'd')).observeWithColumns(['room_updated_at']);
|
2019-09-16 20:26:32 +00:00
|
|
|
|
2021-12-13 16:28:59 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2018-04-24 19:34:03 +00:00
|
|
|
|
2021-12-13 16:28:59 +00:00
|
|
|
onSearchChangeText(text: string) {
|
2022-04-28 20:37:25 +00:00
|
|
|
this.handleSearch(text);
|
2018-10-23 21:39:48 +00:00
|
|
|
}
|
|
|
|
|
2022-04-28 20:37:25 +00:00
|
|
|
handleSearch = async (text: string) => {
|
2022-02-01 13:39:09 +00:00
|
|
|
// TODO: When migrate rocketchat.js pass the param IUser to there and the return should be
|
|
|
|
// IUser | TSubscriptionModel, this because we do a local search too
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = (await search({ text, filterRooms: false })) as ISelectedUser[];
|
2018-08-31 18:13:30 +00:00
|
|
|
this.setState({
|
|
|
|
search: result
|
|
|
|
});
|
2021-09-13 20:41:05 +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-13 16:28:59 +00:00
|
|
|
return maxUsers! > 2;
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-04-01 12:28:54 +00:00
|
|
|
|
2021-12-13 16:28:59 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2018-08-31 18:13:30 +00:00
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
toggleUser = (user: ISelectedUser) => {
|
2020-04-01 12:28:54 +00:00
|
|
|
const { maxUsers } = this.state;
|
|
|
|
const {
|
2022-01-11 13:48:01 +00:00
|
|
|
dispatch,
|
2021-09-13 20:41:05 +00:00
|
|
|
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);
|
2022-01-11 13:48:01 +00:00
|
|
|
dispatch(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);
|
2022-01-11 13:48:01 +00:00
|
|
|
dispatch(removeUser(user));
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
_onPressItem = (id: string, item = {} as ISelectedUser) => {
|
2017-09-25 13:15:28 +00:00
|
|
|
if (item.search) {
|
2021-12-13 16:28:59 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
_onPressSelectedItem = (item: ISelectedUser) => 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-13 16:28:59 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2021-12-13 16:28:59 +00:00
|
|
|
setFlatListRef = (ref: FlatList) => (this.flatlist = ref);
|
2021-05-12 19:01:29 +00:00
|
|
|
|
2021-12-13 16:28:59 +00:00
|
|
|
onContentSizeChange = () => this.flatlist?.scrollToEnd({ animated: true });
|
2021-05-12 19:01:29 +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-05-12 19:01:29 +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-05-12 19:01:29 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
renderSelectedItem = ({ item }: { item: ISelectedUser }) => {
|
2021-12-13 16:28:59 +00:00
|
|
|
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-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2018-08-31 16:46:33 +00:00
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
renderItem = ({ item, index }: { item: ISelectedUser; index: number }) => {
|
2019-09-16 20:26:32 +00:00
|
|
|
const { search, chats } = this.state;
|
2021-12-13 16:28:59 +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-13 16:28:59 +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-09-13 20:41:05 +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-09-13 20:41:05 +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
|
2022-04-28 20:37:25 +00:00
|
|
|
.filter(sub => !isGroupChat(sub as ISubscription));
|
2020-04-01 12:28:54 +00:00
|
|
|
|
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-02-26 16:27:04 +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-09-13 20:41:05 +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-09-13 20:41:05 +00:00
|
|
|
};
|
2017-09-25 13:15:28 +00:00
|
|
|
}
|
2019-08-07 13:51:34 +00:00
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
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
|
|
|
});
|
|
|
|
|
2022-01-11 13:48:01 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(SelectedUsersView));
|