2020-03-30 19:50:27 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { connect } from 'react-redux';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { ScrollView, Switch, Text } from 'react-native';
|
2021-12-03 19:27:57 +00:00
|
|
|
import { StackNavigationOptions } from '@react-navigation/stack';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
import Loading from '../../containers/Loading';
|
|
|
|
import KeyboardView from '../../presentation/KeyboardView';
|
|
|
|
import scrollPersistTaps from '../../utils/scrollPersistTaps';
|
|
|
|
import I18n from '../../i18n';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../../containers/HeaderButton';
|
2020-03-30 19:50:27 +00:00
|
|
|
import StatusBar from '../../containers/StatusBar';
|
2021-01-20 17:34:01 +00:00
|
|
|
import { SWITCH_TRACK_COLOR, themes } from '../../constants/colors';
|
2020-03-30 19:50:27 +00:00
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
|
|
|
import TextInput from '../../containers/TextInput';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
|
|
|
import Navigation from '../../lib/Navigation';
|
|
|
|
import { createDiscussionRequest } from '../../actions/createDiscussion';
|
|
|
|
import { showErrorAlert } from '../../utils/info';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
|
|
|
import { goRoom } from '../../utils/goRoom';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { events, logEvent } from '../../utils/log';
|
2021-01-20 17:34:01 +00:00
|
|
|
import { E2E_ROOM_TYPES } from '../../lib/encryption/constants';
|
2021-09-13 20:41:05 +00:00
|
|
|
import styles from './styles';
|
|
|
|
import SelectUsers from './SelectUsers';
|
|
|
|
import SelectChannel from './SelectChannel';
|
2021-09-15 19:58:14 +00:00
|
|
|
import { ICreateChannelViewProps } from './interfaces';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
class CreateChannelView extends React.Component<ICreateChannelViewProps, any> {
|
|
|
|
private channel: any;
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
constructor(props: ICreateChannelViewProps) {
|
2020-03-30 19:50:27 +00:00
|
|
|
super(props);
|
2020-06-15 14:00:46 +00:00
|
|
|
const { route } = props;
|
|
|
|
this.channel = route.params?.channel;
|
2021-09-15 19:58:14 +00:00
|
|
|
const message: any = route.params?.message ?? {};
|
2020-03-30 19:50:27 +00:00
|
|
|
this.state = {
|
|
|
|
channel: this.channel,
|
|
|
|
message,
|
2020-06-15 14:00:46 +00:00
|
|
|
name: message?.msg || '',
|
2020-03-30 19:50:27 +00:00
|
|
|
users: [],
|
2021-01-20 17:34:01 +00:00
|
|
|
reply: '',
|
|
|
|
encrypted: props.encryptionEnabled
|
2020-03-30 19:50:27 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader();
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
componentDidUpdate(prevProps: any, prevState: any) {
|
2021-02-26 16:01:45 +00:00
|
|
|
const { channel, name } = this.state;
|
2021-09-13 20:41:05 +00:00
|
|
|
const { loading, failure, error, result, isMasterDetail } = this.props;
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-02-26 16:01:45 +00:00
|
|
|
if (channel?.rid !== prevState.channel?.rid || name !== prevState.name) {
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader();
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!loading && loading !== prevProps.loading) {
|
|
|
|
setTimeout(() => {
|
|
|
|
if (failure) {
|
2021-08-27 16:34:38 +00:00
|
|
|
const msg = error.reason || I18n.t('There_was_an_error_while_action', { action: I18n.t('creating_discussion') });
|
2020-03-30 19:50:27 +00:00
|
|
|
showErrorAlert(msg);
|
|
|
|
} else {
|
|
|
|
const { rid, t, prid } = result;
|
2020-06-15 14:00:46 +00:00
|
|
|
if (isMasterDetail) {
|
|
|
|
Navigation.navigate('DrawerNavigator');
|
|
|
|
} else {
|
2020-03-30 19:50:27 +00:00
|
|
|
Navigation.navigate('RoomsListView');
|
|
|
|
}
|
2020-06-15 14:00:46 +00:00
|
|
|
const item = {
|
2021-09-13 20:41:05 +00:00
|
|
|
rid,
|
|
|
|
name: RocketChat.getRoomTitle(result),
|
|
|
|
t,
|
|
|
|
prid
|
2020-06-15 14:00:46 +00:00
|
|
|
};
|
|
|
|
goRoom({ item, isMasterDetail });
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
}, 300);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
setHeader = () => {
|
|
|
|
const { navigation, route } = this.props;
|
|
|
|
const showCloseModal = route.params?.showCloseModal;
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Create_Discussion'),
|
2021-09-13 20:41:05 +00:00
|
|
|
headerRight: this.valid()
|
|
|
|
? () => (
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Container>
|
|
|
|
<HeaderButton.Item title={I18n.t('Create')} onPress={this.submit} testID='create-discussion-submit' />
|
|
|
|
</HeaderButton.Container>
|
2021-09-13 20:41:05 +00:00
|
|
|
)
|
|
|
|
: null,
|
2020-10-30 16:15:58 +00:00
|
|
|
headerLeft: showCloseModal ? () => <HeaderButton.CloseModal navigation={navigation} /> : undefined
|
2021-12-03 19:27:57 +00:00
|
|
|
} as StackNavigationOptions);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-06-15 14:00:46 +00:00
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
submit = () => {
|
|
|
|
const {
|
2021-09-13 20:41:05 +00:00
|
|
|
name: t_name,
|
|
|
|
channel: { prid, rid },
|
|
|
|
message: { id: pmid },
|
|
|
|
reply,
|
|
|
|
users,
|
|
|
|
encrypted
|
2020-03-30 19:50:27 +00:00
|
|
|
} = this.state;
|
|
|
|
const { create } = this.props;
|
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
const params: any = {
|
2021-09-13 20:41:05 +00:00
|
|
|
prid: prid || rid,
|
|
|
|
pmid,
|
|
|
|
t_name,
|
|
|
|
reply,
|
|
|
|
users
|
2021-04-01 12:24:28 +00:00
|
|
|
};
|
|
|
|
if (this.isEncryptionEnabled) {
|
|
|
|
params.encrypted = encrypted ?? false;
|
|
|
|
}
|
|
|
|
|
|
|
|
create(params);
|
2020-03-30 19:50:27 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
valid = () => {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { channel, name } = this.state;
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
return channel && channel.rid && channel.rid.trim().length && name.trim().length;
|
2020-03-30 19:50:27 +00:00
|
|
|
};
|
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
selectChannel = ({ value }: any) => {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_SELECT_CHANNEL);
|
2021-01-20 17:34:01 +00:00
|
|
|
this.setState({ channel: value, encrypted: value?.encrypted });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-07-30 13:26:17 +00:00
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
selectUsers = ({ value }: any) => {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_SELECT_USERS);
|
2020-07-30 13:26:17 +00:00
|
|
|
this.setState({ users: value });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-07-30 13:26:17 +00:00
|
|
|
|
2021-04-01 12:24:28 +00:00
|
|
|
get isEncryptionEnabled() {
|
|
|
|
const { channel } = this.state;
|
|
|
|
const { encryptionEnabled } = this.props;
|
2021-09-15 19:58:14 +00:00
|
|
|
// TODO: remove this ts-ignore when migrate the file: app/lib/encryption/constants.js
|
|
|
|
// @ts-ignore
|
2021-04-01 12:24:28 +00:00
|
|
|
return encryptionEnabled && E2E_ROOM_TYPES[channel?.t];
|
|
|
|
}
|
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
onEncryptedChange = (value: any) => {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_TOGGLE_ENCRY);
|
2021-01-20 17:34:01 +00:00
|
|
|
this.setState({ encrypted: value });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2021-01-20 17:34:01 +00:00
|
|
|
|
2020-03-30 19:50:27 +00:00
|
|
|
render() {
|
2021-09-13 20:41:05 +00:00
|
|
|
const { name, users, encrypted } = this.state;
|
|
|
|
const { server, user, loading, blockUnauthenticatedAccess, theme, serverVersion } = this.props;
|
2020-03-30 19:50:27 +00:00
|
|
|
return (
|
|
|
|
<KeyboardView
|
|
|
|
style={{ backgroundColor: themes[theme].auxiliaryBackground }}
|
|
|
|
contentContainerStyle={styles.container}
|
2021-09-13 20:41:05 +00:00
|
|
|
keyboardVerticalOffset={128}>
|
2020-10-30 13:59:44 +00:00
|
|
|
<StatusBar />
|
|
|
|
<SafeAreaView testID='create-discussion-view' style={styles.container}>
|
2020-03-30 19:50:27 +00:00
|
|
|
<ScrollView {...scrollPersistTaps}>
|
|
|
|
<Text style={[styles.description, { color: themes[theme].auxiliaryText }]}>{I18n.t('Discussion_Desc')}</Text>
|
|
|
|
<SelectChannel
|
|
|
|
server={server}
|
|
|
|
userId={user.id}
|
|
|
|
token={user.token}
|
|
|
|
initial={this.channel && { text: RocketChat.getRoomTitle(this.channel) }}
|
2020-07-30 13:26:17 +00:00
|
|
|
onChannelSelect={this.selectChannel}
|
2020-10-30 15:54:02 +00:00
|
|
|
blockUnauthenticatedAccess={blockUnauthenticatedAccess}
|
2020-11-04 16:53:44 +00:00
|
|
|
serverVersion={serverVersion}
|
2020-03-30 19:50:27 +00:00
|
|
|
theme={theme}
|
|
|
|
/>
|
|
|
|
<TextInput
|
|
|
|
label={I18n.t('Discussion_name')}
|
2021-03-18 14:36:50 +00:00
|
|
|
testID='multi-select-discussion-name'
|
2020-03-30 19:50:27 +00:00
|
|
|
placeholder={I18n.t('A_meaningful_name_for_the_discussion_room')}
|
|
|
|
containerStyle={styles.inputStyle}
|
2021-09-15 19:58:14 +00:00
|
|
|
/* @ts-ignore*/
|
2020-03-30 19:50:27 +00:00
|
|
|
defaultValue={name}
|
2021-09-15 19:58:14 +00:00
|
|
|
onChangeText={(text: string) => this.setState({ name: text })}
|
2020-04-01 12:07:03 +00:00
|
|
|
theme={theme}
|
2020-03-30 19:50:27 +00:00
|
|
|
/>
|
|
|
|
<SelectUsers
|
|
|
|
server={server}
|
|
|
|
userId={user.id}
|
|
|
|
token={user.token}
|
|
|
|
selected={users}
|
2020-07-30 13:26:17 +00:00
|
|
|
onUserSelect={this.selectUsers}
|
2020-10-30 15:54:02 +00:00
|
|
|
blockUnauthenticatedAccess={blockUnauthenticatedAccess}
|
2020-11-04 16:53:44 +00:00
|
|
|
serverVersion={serverVersion}
|
2020-03-30 19:50:27 +00:00
|
|
|
theme={theme}
|
|
|
|
/>
|
2021-09-13 20:41:05 +00:00
|
|
|
{this.isEncryptionEnabled ? (
|
|
|
|
<>
|
|
|
|
<Text style={[styles.label, { color: themes[theme].titleText }]}>{I18n.t('Encrypted')}</Text>
|
|
|
|
<Switch value={encrypted} onValueChange={this.onEncryptedChange} trackColor={SWITCH_TRACK_COLOR} />
|
|
|
|
</>
|
|
|
|
) : null}
|
2020-03-30 19:50:27 +00:00
|
|
|
<Loading visible={loading} />
|
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
const mapStateToProps = (state: any) => ({
|
2020-03-30 19:50:27 +00:00
|
|
|
user: getUserSelector(state),
|
|
|
|
server: state.server.server,
|
|
|
|
error: state.createDiscussion.error,
|
|
|
|
failure: state.createDiscussion.failure,
|
|
|
|
loading: state.createDiscussion.isFetching,
|
2020-06-15 14:00:46 +00:00
|
|
|
result: state.createDiscussion.result,
|
2020-10-30 15:54:02 +00:00
|
|
|
blockUnauthenticatedAccess: state.settings.Accounts_AvatarBlockUnauthenticatedAccess ?? true,
|
2021-02-11 21:44:36 +00:00
|
|
|
serverVersion: state.server.version,
|
2021-01-20 17:34:01 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
|
|
|
encryptionEnabled: state.encryption.enabled
|
2020-03-30 19:50:27 +00:00
|
|
|
});
|
|
|
|
|
2021-09-15 19:58:14 +00:00
|
|
|
const mapDispatchToProps = (dispatch: any) => ({
|
|
|
|
create: (data: any) => dispatch(createDiscussionRequest(data))
|
2020-03-30 19:50:27 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(CreateChannelView));
|