Chore: Migrate AddExistingChannelView to Typescript (#3474)

This commit is contained in:
Reinaldo Neto 2021-11-10 17:41:13 -03:00 committed by GitHub
parent 64f7b1f84f
commit 8b4e39d1cf
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 34 additions and 22 deletions

View File

@ -1,5 +1,6 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/native';
import { FlatList, View } from 'react-native'; import { FlatList, View } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Q } from '@nozbe/watermelondb'; import { Q } from '@nozbe/watermelondb';
@ -21,18 +22,27 @@ import { goRoom } from '../utils/goRoom';
import { showErrorAlert } from '../utils/info'; import { showErrorAlert } from '../utils/info';
import debounce from '../utils/debounce'; import debounce from '../utils/debounce';
interface IAddExistingChannelViewState {
// TODO: refactor with Room Model
search: any[];
channels: any[];
selected: string[];
loading: boolean;
}
interface IAddExistingChannelViewProps {
navigation: StackNavigationProp<any, 'AddExistingChannelView'>;
route: RouteProp<{ AddExistingChannelView: { teamId: string } }, 'AddExistingChannelView'>;
theme: string;
isMasterDetail: boolean;
addTeamChannelPermission: string[];
}
const QUERY_SIZE = 50; const QUERY_SIZE = 50;
class AddExistingChannelView extends React.Component { class AddExistingChannelView extends React.Component<IAddExistingChannelViewProps, IAddExistingChannelViewState> {
static propTypes = { private teamId: string;
navigation: PropTypes.object, constructor(props: IAddExistingChannelViewProps) {
route: PropTypes.object,
theme: PropTypes.string,
isMasterDetail: PropTypes.bool,
addTeamChannelPermission: PropTypes.array
};
constructor(props) {
super(props); super(props);
this.query(); this.query();
this.teamId = props.route?.params?.teamId; this.teamId = props.route?.params?.teamId;
@ -49,7 +59,7 @@ class AddExistingChannelView extends React.Component {
const { navigation, isMasterDetail } = this.props; const { navigation, isMasterDetail } = this.props;
const { selected } = this.state; const { selected } = this.state;
const options = { const options: StackNavigationOptions = {
headerTitle: I18n.t('Add_Existing_Channel') headerTitle: I18n.t('Add_Existing_Channel')
}; };
@ -82,9 +92,10 @@ class AddExistingChannelView extends React.Component {
) )
.fetch(); .fetch();
const asyncFilter = async channelsArray => { // TODO: Refactor with Room Model
const asyncFilter = async (channelsArray: any[]) => {
const results = await Promise.all( const results = await Promise.all(
channelsArray.map(async channel => { channelsArray.map(async (channel: any) => {
if (channel.prid) { if (channel.prid) {
return false; return false;
} }
@ -96,7 +107,7 @@ class AddExistingChannelView extends React.Component {
}) })
); );
return channelsArray.filter((_v, index) => results[index]); return channelsArray.filter((_v: any, index: number) => results[index]);
}; };
const channelFiltered = await asyncFilter(channels); const channelFiltered = await asyncFilter(channels);
this.setState({ channels: channelFiltered }); this.setState({ channels: channelFiltered });
@ -105,7 +116,7 @@ class AddExistingChannelView extends React.Component {
} }
}; };
onSearchChangeText = debounce(text => { onSearchChangeText = debounce((text: string) => {
this.query(text); this.query(text);
}, 300); }, 300);
@ -126,7 +137,7 @@ class AddExistingChannelView extends React.Component {
this.setState({ loading: false }); this.setState({ loading: false });
goRoom({ item: result, isMasterDetail }); goRoom({ item: result, isMasterDetail });
} }
} catch (e) { } catch (e: any) {
logEvent(events.CT_ADD_ROOM_TO_TEAM_F); logEvent(events.CT_ADD_ROOM_TO_TEAM_F);
showErrorAlert(I18n.t(e.data.error), I18n.t('Add_Existing_Channel'), () => {}); showErrorAlert(I18n.t(e.data.error), I18n.t('Add_Existing_Channel'), () => {});
this.setState({ loading: false }); this.setState({ loading: false });
@ -137,17 +148,17 @@ class AddExistingChannelView extends React.Component {
const { theme } = this.props; const { theme } = this.props;
return ( return (
<View style={{ backgroundColor: themes[theme].auxiliaryBackground }}> <View style={{ backgroundColor: themes[theme].auxiliaryBackground }}>
<SearchBox onChangeText={text => this.onSearchChangeText(text)} testID='add-existing-channel-view-search' /> <SearchBox onChangeText={(text: string) => this.onSearchChangeText(text)} testID='add-existing-channel-view-search' />
</View> </View>
); );
}; };
isChecked = rid => { isChecked = (rid: string) => {
const { selected } = this.state; const { selected } = this.state;
return selected.includes(rid); return selected.includes(rid);
}; };
toggleChannel = rid => { toggleChannel = (rid: string) => {
const { selected } = this.state; const { selected } = this.state;
animateNextTransition(); animateNextTransition();
@ -161,7 +172,8 @@ class AddExistingChannelView extends React.Component {
} }
}; };
renderItem = ({ item }) => { // TODO: refactor with Room Model
renderItem = ({ item }: { item: any }) => {
const isChecked = this.isChecked(item.rid); const isChecked = this.isChecked(item.rid);
// TODO: reuse logic inside RoomTypeIcon // TODO: reuse logic inside RoomTypeIcon
const icon = item.t === 'p' && !item.teamId ? 'channel-private' : 'channel-public'; const icon = item.t === 'p' && !item.teamId ? 'channel-private' : 'channel-public';
@ -207,7 +219,7 @@ class AddExistingChannelView extends React.Component {
} }
} }
const mapStateToProps = state => ({ const mapStateToProps = (state: any) => ({
isMasterDetail: state.app.isMasterDetail, isMasterDetail: state.app.isMasterDetail,
addTeamChannelPermission: state.permissions['add-team-channel'] addTeamChannelPermission: state.permissions['add-team-channel']
}); });