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
|
|
|
|
2022-08-19 21:14:37 +00:00
|
|
|
import { sendLoadingEvent } from '../../containers/Loading';
|
2022-04-13 20:43:56 +00:00
|
|
|
import KeyboardView from '../../containers/KeyboardView';
|
2022-06-06 14:17:51 +00:00
|
|
|
import scrollPersistTaps from '../../lib/methods/helpers/scrollPersistTaps';
|
2020-03-30 19:50:27 +00:00
|
|
|
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';
|
|
|
|
import { withTheme } from '../../theme';
|
|
|
|
import { getUserSelector } from '../../selectors/login';
|
2022-06-27 18:46:59 +00:00
|
|
|
import { FormTextInput } from '../../containers/TextInput';
|
2022-05-02 22:48:29 +00:00
|
|
|
import { createDiscussionRequest, ICreateDiscussionRequestData } from '../../actions/createDiscussion';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../../containers/SafeAreaView';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { goRoom } from '../../lib/methods/helpers/goRoom';
|
|
|
|
import { events, logEvent } from '../../lib/methods/helpers/log';
|
2021-09-13 20:41:05 +00:00
|
|
|
import styles from './styles';
|
|
|
|
import SelectUsers from './SelectUsers';
|
|
|
|
import SelectChannel from './SelectChannel';
|
2022-05-02 22:48:29 +00:00
|
|
|
import { ICreateChannelViewProps, IResult, IError, ICreateChannelViewState } from './interfaces';
|
|
|
|
import { IApplicationState, ISearchLocal, ISubscription } from '../../definitions';
|
2022-04-07 14:10:03 +00:00
|
|
|
import { E2E_ROOM_TYPES, SWITCH_TRACK_COLOR, themes } from '../../lib/constants';
|
2022-06-27 18:46:59 +00:00
|
|
|
import { getRoomTitle, showErrorAlert } from '../../lib/methods/helpers';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2022-05-02 22:48:29 +00:00
|
|
|
class CreateChannelView extends React.Component<ICreateChannelViewProps, ICreateChannelViewState> {
|
|
|
|
private channel: ISubscription;
|
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;
|
2022-05-02 22:48:29 +00:00
|
|
|
const message = 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
|
|
|
}
|
|
|
|
|
2022-05-02 22:48:29 +00:00
|
|
|
componentDidUpdate(prevProps: ICreateChannelViewProps, prevState: ICreateChannelViewState) {
|
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
|
|
|
}
|
|
|
|
|
2022-08-19 21:14:37 +00:00
|
|
|
if (loading !== prevProps.loading) {
|
|
|
|
sendLoadingEvent({ visible: loading });
|
|
|
|
if (!loading) {
|
2020-03-30 19:50:27 +00:00
|
|
|
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
|
|
|
const item = {
|
2021-09-13 20:41:05 +00:00
|
|
|
rid,
|
2022-04-28 20:37:25 +00:00
|
|
|
name: getRoomTitle(result),
|
2021-09-13 20:41:05 +00:00
|
|
|
t,
|
|
|
|
prid
|
2020-06-15 14:00:46 +00:00
|
|
|
};
|
2022-11-25 13:21:56 +00:00
|
|
|
goRoom({ item, isMasterDetail, popToRoot: true });
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
2022-08-19 21:14:37 +00:00
|
|
|
}
|
2020-03-30 19:50:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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,
|
2022-05-02 22:48:29 +00:00
|
|
|
channel,
|
2021-09-13 20:41:05 +00:00
|
|
|
message: { id: pmid },
|
|
|
|
reply,
|
|
|
|
users,
|
|
|
|
encrypted
|
2020-03-30 19:50:27 +00:00
|
|
|
} = this.state;
|
2022-02-02 18:51:18 +00:00
|
|
|
const { dispatch } = this.props;
|
2020-03-30 19:50:27 +00:00
|
|
|
|
2022-05-02 22:48:29 +00:00
|
|
|
const params: ICreateDiscussionRequestData = {
|
|
|
|
prid: ('prid' in channel && channel.prid) || channel.rid,
|
2021-09-13 20:41:05 +00:00
|
|
|
pmid,
|
|
|
|
t_name,
|
|
|
|
reply,
|
|
|
|
users
|
2021-04-01 12:24:28 +00:00
|
|
|
};
|
|
|
|
if (this.isEncryptionEnabled) {
|
|
|
|
params.encrypted = encrypted ?? false;
|
|
|
|
}
|
|
|
|
|
2022-02-02 18:51:18 +00:00
|
|
|
dispatch(createDiscussionRequest(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
|
|
|
|
2022-05-02 22:48:29 +00:00
|
|
|
return channel && channel.rid && channel.rid.trim().length && name?.trim().length;
|
2020-03-30 19:50:27 +00:00
|
|
|
};
|
|
|
|
|
2022-05-02 22:48:29 +00:00
|
|
|
selectChannel = ({ value }: { value: ISearchLocal }) => {
|
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
|
|
|
|
2022-05-02 22:48:29 +00:00
|
|
|
selectUsers = ({ value }: { value: string[] }) => {
|
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;
|
|
|
|
return encryptionEnabled && E2E_ROOM_TYPES[channel?.t];
|
|
|
|
}
|
|
|
|
|
2022-05-02 22:48:29 +00:00
|
|
|
onEncryptedChange = (value: boolean) => {
|
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;
|
2022-08-19 21:14:37 +00:00
|
|
|
const { server, user, blockUnauthenticatedAccess, theme, serverVersion } = this.props;
|
2020-03-30 19:50:27 +00:00
|
|
|
return (
|
|
|
|
<KeyboardView
|
2023-05-18 21:09:33 +00:00
|
|
|
style={{ backgroundColor: themes[theme!].auxiliaryBackground }}
|
2020-03-30 19:50:27 +00:00
|
|
|
contentContainerStyle={styles.container}
|
2022-08-08 21:02:08 +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}>
|
2023-05-18 21:09:33 +00:00
|
|
|
<Text style={[styles.description, { color: themes[theme!].auxiliaryText }]}>{I18n.t('Discussion_Desc')}</Text>
|
2020-03-30 19:50:27 +00:00
|
|
|
<SelectChannel
|
|
|
|
server={server}
|
|
|
|
userId={user.id}
|
|
|
|
token={user.token}
|
2022-04-28 20:37:25 +00:00
|
|
|
initial={this.channel && { text: 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
|
|
|
/>
|
2022-05-18 19:17:42 +00:00
|
|
|
<FormTextInput
|
2020-03-30 19:50:27 +00:00
|
|
|
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}
|
|
|
|
defaultValue={name}
|
2021-09-15 19:58:14 +00:00
|
|
|
onChangeText={(text: string) => this.setState({ name: text })}
|
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
|
|
|
/>
|
2021-09-13 20:41:05 +00:00
|
|
|
{this.isEncryptionEnabled ? (
|
|
|
|
<>
|
2023-05-18 21:09:33 +00:00
|
|
|
<Text style={[styles.label, { color: themes[theme!].titleText }]}>{I18n.t('Encrypted')}</Text>
|
2021-09-13 20:41:05 +00:00
|
|
|
<Switch value={encrypted} onValueChange={this.onEncryptedChange} trackColor={SWITCH_TRACK_COLOR} />
|
|
|
|
</>
|
|
|
|
) : null}
|
2020-03-30 19:50:27 +00:00
|
|
|
</ScrollView>
|
|
|
|
</SafeAreaView>
|
|
|
|
</KeyboardView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-02 18:51:18 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-03-30 19:50:27 +00:00
|
|
|
user: getUserSelector(state),
|
|
|
|
server: state.server.server,
|
2022-02-02 18:51:18 +00:00
|
|
|
error: state.createDiscussion.error as IError,
|
2020-03-30 19:50:27 +00:00
|
|
|
failure: state.createDiscussion.failure,
|
|
|
|
loading: state.createDiscussion.isFetching,
|
2022-02-02 18:51:18 +00:00
|
|
|
result: state.createDiscussion.result as IResult,
|
|
|
|
blockUnauthenticatedAccess: !!state.settings.Accounts_AvatarBlockUnauthenticatedAccess ?? true,
|
|
|
|
serverVersion: state.server.version as string,
|
2021-01-20 17:34:01 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
|
|
|
encryptionEnabled: state.encryption.enabled
|
2020-03-30 19:50:27 +00:00
|
|
|
});
|
|
|
|
|
2022-02-02 18:51:18 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(CreateChannelView));
|