Chore: Migrate CreateChannelView to Typescript (#3486)

Co-authored-by: AlexAlexandre <alexalexandrejr@gmail.com>
Co-authored-by: Diego Mello <diegolmello@gmail.com>
This commit is contained in:
Reinaldo Neto 2021-11-10 18:18:35 -03:00 committed by GitHub
parent 138f0ab444
commit 3249c54d03
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 72 additions and 49 deletions

View File

@ -27,13 +27,11 @@ export const FormContainerInner = ({ children }: { children: React.ReactNode }):
); );
const FormContainer = ({ children, theme, testID, ...props }: IFormContainer): JSX.Element => ( const FormContainer = ({ children, theme, testID, ...props }: IFormContainer): JSX.Element => (
// @ts-ignore
<KeyboardView <KeyboardView
style={{ backgroundColor: themes[theme].backgroundColor }} style={{ backgroundColor: themes[theme].backgroundColor }}
contentContainerStyle={sharedStyles.container} contentContainerStyle={sharedStyles.container}
keyboardVerticalOffset={128}> keyboardVerticalOffset={128}>
<StatusBar /> <StatusBar />
{/* @ts-ignore*/}
<ScrollView <ScrollView
style={sharedStyles.container} style={sharedStyles.container}
contentContainerStyle={[sharedStyles.containerScrollView, styles.scrollView]} contentContainerStyle={[sharedStyles.containerScrollView, styles.scrollView]}

View File

@ -1,6 +1,6 @@
import React from 'react'; import React from 'react';
// @ts-ignore // @ts-ignore
import { Pressable, StyleSheet, Text, View } from 'react-native'; import { Pressable, StyleProp, StyleSheet, Text, View, ViewStyle } from 'react-native';
import Avatar from '../containers/Avatar'; import Avatar from '../containers/Avatar';
import { CustomIcon } from '../lib/Icons'; import { CustomIcon } from '../lib/Icons';
@ -44,8 +44,8 @@ interface IUserItem {
username: string; username: string;
onPress(): void; onPress(): void;
testID: string; testID: string;
onLongPress(): void; onLongPress?: () => void;
style: any; style?: StyleProp<ViewStyle>;
icon: string; icon: string;
theme: string; theme: string;
} }

View File

@ -1,7 +1,9 @@
import React from 'react'; import React from 'react';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import PropTypes from 'prop-types'; import { Dispatch } from 'redux';
import { FlatList, ScrollView, StyleSheet, Switch, Text, View } from 'react-native'; import { StackNavigationProp } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/native';
import { FlatList, ScrollView, StyleSheet, Switch, Text, View, SwitchProps } from 'react-native';
import { dequal } from 'dequal'; import { dequal } from 'dequal';
import * as List from '../containers/List'; import * as List from '../containers/List';
@ -66,30 +68,59 @@ const styles = StyleSheet.create({
} }
}); });
class CreateChannelView extends React.Component { interface IOtherUser {
static propTypes = { _id: string;
navigation: PropTypes.object, name: string;
route: PropTypes.object, fname: string;
baseUrl: PropTypes.string, }
create: PropTypes.func.isRequired,
removeUser: PropTypes.func.isRequired,
error: PropTypes.object,
failure: PropTypes.bool,
isFetching: PropTypes.bool,
encryptionEnabled: PropTypes.bool,
users: PropTypes.array.isRequired,
user: PropTypes.shape({
id: PropTypes.string,
token: PropTypes.string,
roles: PropTypes.array
}),
theme: PropTypes.string,
teamId: PropTypes.string,
createPublicChannelPermission: PropTypes.array,
createPrivateChannelPermission: PropTypes.array
};
constructor(props) { interface ICreateFunction extends Omit<ICreateChannelViewState, 'channelName' | 'permissions'> {
name: string;
users: string[];
teamId: string;
}
interface ICreateChannelViewState {
channelName: string;
type: boolean;
readOnly: boolean;
encrypted: boolean;
broadcast: boolean;
isTeam: boolean;
permissions: boolean[];
}
interface ICreateChannelViewProps {
navigation: StackNavigationProp<any, 'CreateChannelView'>;
route: RouteProp<{ CreateChannelView: { isTeam: boolean; teamId: string } }, 'CreateChannelView'>;
baseUrl: string;
create: (data: ICreateFunction) => void;
removeUser: (user: IOtherUser) => void;
error: object;
failure: boolean;
isFetching: boolean;
encryptionEnabled: boolean;
users: IOtherUser[];
user: {
id: string;
token: string;
roles: string[];
};
theme: string;
teamId: string;
createPublicChannelPermission: string[];
createPrivateChannelPermission: string[];
}
interface ISwitch extends SwitchProps {
id: string;
label: string;
}
class CreateChannelView extends React.Component<ICreateChannelViewProps, ICreateChannelViewState> {
private teamId: string;
constructor(props: ICreateChannelViewProps) {
super(props); super(props);
const { route } = this.props; const { route } = this.props;
const isTeam = route?.params?.isTeam || false; const isTeam = route?.params?.isTeam || false;
@ -110,7 +141,7 @@ class CreateChannelView extends React.Component {
this.handleHasPermission(); this.handleHasPermission();
} }
shouldComponentUpdate(nextProps, nextState) { shouldComponentUpdate(nextProps: ICreateChannelViewProps, nextState: ICreateChannelViewState) {
const { channelName, type, readOnly, broadcast, encrypted, permissions } = this.state; const { channelName, type, readOnly, broadcast, encrypted, permissions } = this.state;
const { users, isFetching, encryptionEnabled, theme, createPublicChannelPermission, createPrivateChannelPermission } = const { users, isFetching, encryptionEnabled, theme, createPublicChannelPermission, createPrivateChannelPermission } =
this.props; this.props;
@ -153,7 +184,7 @@ class CreateChannelView extends React.Component {
return false; return false;
} }
componentDidUpdate(prevProps) { componentDidUpdate(prevProps: ICreateChannelViewProps) {
const { createPublicChannelPermission, createPrivateChannelPermission } = this.props; const { createPublicChannelPermission, createPrivateChannelPermission } = this.props;
if ( if (
!dequal(createPublicChannelPermission, prevProps.createPublicChannelPermission) || !dequal(createPublicChannelPermission, prevProps.createPublicChannelPermission) ||
@ -172,7 +203,7 @@ class CreateChannelView extends React.Component {
}); });
}; };
toggleRightButton = channelName => { toggleRightButton = (channelName: string) => {
const { navigation } = this.props; const { navigation } = this.props;
navigation.setOptions({ navigation.setOptions({
headerRight: () => headerRight: () =>
@ -184,7 +215,7 @@ class CreateChannelView extends React.Component {
}); });
}; };
onChangeText = channelName => { onChangeText = (channelName: string) => {
this.toggleRightButton(channelName); this.toggleRightButton(channelName);
this.setState({ channelName }); this.setState({ channelName });
}; };
@ -215,13 +246,13 @@ class CreateChannelView extends React.Component {
Review.pushPositiveEvent(); Review.pushPositiveEvent();
}; };
removeUser = user => { removeUser = (user: IOtherUser) => {
logEvent(events.CR_REMOVE_USER); logEvent(events.CR_REMOVE_USER);
const { removeUser } = this.props; const { removeUser } = this.props;
removeUser(user); removeUser(user);
}; };
renderSwitch = ({ id, value, label, onValueChange, disabled = false }) => { renderSwitch = ({ id, value, label, onValueChange, disabled = false }: ISwitch) => {
const { theme } = this.props; const { theme } = this.props;
return ( return (
<View style={[styles.switchContainer, { backgroundColor: themes[theme].backgroundColor }]}> <View style={[styles.switchContainer, { backgroundColor: themes[theme].backgroundColor }]}>
@ -253,7 +284,7 @@ class CreateChannelView extends React.Component {
value: permissions[1] ? type : false, value: permissions[1] ? type : false,
disabled: isDisabled, disabled: isDisabled,
label: isTeam ? 'Private_Team' : 'Private_Channel', label: isTeam ? 'Private_Team' : 'Private_Channel',
onValueChange: value => { onValueChange: (value: boolean) => {
logEvent(events.CR_TOGGLE_TYPE); logEvent(events.CR_TOGGLE_TYPE);
// If we set the channel as public, encrypted status should be false // If we set the channel as public, encrypted status should be false
this.setState(({ encrypted }) => ({ type: value, encrypted: value && encrypted })); this.setState(({ encrypted }) => ({ type: value, encrypted: value && encrypted }));
@ -313,8 +344,8 @@ class CreateChannelView extends React.Component {
}); });
} }
renderItem = ({ item }) => { renderItem = ({ item }: { item: IOtherUser }) => {
const { baseUrl, user, theme } = this.props; const { theme } = this.props;
return ( return (
<UserItem <UserItem
@ -323,8 +354,6 @@ class CreateChannelView extends React.Component {
onPress={() => this.removeUser(item)} onPress={() => this.removeUser(item)}
testID={`create-channel-view-item-${item.name}`} testID={`create-channel-view-item-${item.name}`}
icon='check' icon='check'
baseUrl={baseUrl}
user={user}
theme={theme} theme={theme}
/> />
); );
@ -348,7 +377,6 @@ class CreateChannelView extends React.Component {
]} ]}
renderItem={this.renderItem} renderItem={this.renderItem}
ItemSeparatorComponent={List.Separator} ItemSeparatorComponent={List.Separator}
enableEmptySections
keyboardShouldPersistTaps='always' keyboardShouldPersistTaps='always'
/> />
); );
@ -371,7 +399,6 @@ class CreateChannelView extends React.Component {
<TextInput <TextInput
autoFocus autoFocus
style={[styles.input, { backgroundColor: themes[theme].backgroundColor }]} style={[styles.input, { backgroundColor: themes[theme].backgroundColor }]}
label={isTeam ? I18n.t('Team_Name') : I18n.t('Channel_Name')}
value={channelName} value={channelName}
onChangeText={this.onChangeText} onChangeText={this.onChangeText}
placeholder={isTeam ? I18n.t('Team_Name') : I18n.t('Channel_Name')} placeholder={isTeam ? I18n.t('Team_Name') : I18n.t('Channel_Name')}
@ -406,7 +433,7 @@ class CreateChannelView extends React.Component {
} }
} }
const mapStateToProps = state => ({ const mapStateToProps = (state: any) => ({
baseUrl: state.server.server, baseUrl: state.server.server,
isFetching: state.createChannel.isFetching, isFetching: state.createChannel.isFetching,
encryptionEnabled: state.encryption.enabled, encryptionEnabled: state.encryption.enabled,
@ -416,9 +443,9 @@ const mapStateToProps = state => ({
createPrivateChannelPermission: state.permissions['create-p'] createPrivateChannelPermission: state.permissions['create-p']
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = (dispatch: Dispatch) => ({
create: data => dispatch(createChannelRequestAction(data)), create: (data: ICreateFunction) => dispatch(createChannelRequestAction(data)),
removeUser: user => dispatch(removeUserAction(user)) removeUser: (user: IOtherUser) => dispatch(removeUserAction(user))
}); });
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(CreateChannelView)); export default connect(mapStateToProps, mapDispatchToProps)(withTheme(CreateChannelView));

View File

@ -150,14 +150,12 @@ class CreateChannelView extends React.Component<ICreateChannelViewProps, any> {
const { name, users, encrypted } = this.state; const { name, users, encrypted } = this.state;
const { server, user, loading, blockUnauthenticatedAccess, theme, serverVersion } = this.props; const { server, user, loading, blockUnauthenticatedAccess, theme, serverVersion } = this.props;
return ( return (
// @ts-ignore
<KeyboardView <KeyboardView
style={{ backgroundColor: themes[theme].auxiliaryBackground }} style={{ backgroundColor: themes[theme].auxiliaryBackground }}
contentContainerStyle={styles.container} contentContainerStyle={styles.container}
keyboardVerticalOffset={128}> keyboardVerticalOffset={128}>
<StatusBar /> <StatusBar />
<SafeAreaView testID='create-discussion-view' style={styles.container}> <SafeAreaView testID='create-discussion-view' style={styles.container}>
{/* @ts-ignore*/}
<ScrollView {...scrollPersistTaps}> <ScrollView {...scrollPersistTaps}>
<Text style={[styles.description, { color: themes[theme].auxiliaryText }]}>{I18n.t('Discussion_Desc')}</Text> <Text style={[styles.description, { color: themes[theme].auxiliaryText }]}>{I18n.t('Discussion_Desc')}</Text>
<SelectChannel <SelectChannel