Chore: Migrate SelectServerView to Typescript (#3521)

Co-authored-by: AlexAlexandre <alexalexandrejr@gmail.com>
This commit is contained in:
Reinaldo Neto 2021-12-02 10:27:44 -03:00 committed by GitHub
parent 7da1b70829
commit e93a73d52d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 26 additions and 17 deletions

View File

@ -1,8 +1,8 @@
import React from 'react'; import React from 'react';
import { FlatList } from 'react-native'; import { FlatList } from 'react-native';
import PropTypes from 'prop-types'; import { StackNavigationOptions, StackNavigationProp } from '@react-navigation/stack';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import { Q } from '@nozbe/watermelondb'; import { Q, Model } from '@nozbe/watermelondb';
import I18n from '../i18n'; import I18n from '../i18n';
import StatusBar from '../containers/StatusBar'; import StatusBar from '../containers/StatusBar';
@ -12,29 +12,39 @@ import database from '../lib/database';
import SafeAreaView from '../containers/SafeAreaView'; import SafeAreaView from '../containers/SafeAreaView';
import * as List from '../containers/List'; import * as List from '../containers/List';
const getItemLayout = (data, index) => ({ length: ROW_HEIGHT, offset: ROW_HEIGHT * index, index }); const getItemLayout = (data: any, index: number) => ({ length: ROW_HEIGHT, offset: ROW_HEIGHT * index, index });
const keyExtractor = item => item.id; const keyExtractor = (item: IServer) => item.id;
class SelectServerView extends React.Component { interface IServer extends Model {
static navigationOptions = () => ({ id: string;
iconURL?: string;
name?: string;
}
interface ISelectServerViewState {
servers: IServer[];
}
interface ISelectServerViewProps {
navigation: StackNavigationProp<any, 'SelectServerView'>;
server: string;
}
class SelectServerView extends React.Component<ISelectServerViewProps, ISelectServerViewState> {
static navigationOptions = (): StackNavigationOptions => ({
title: I18n.t('Select_Server') title: I18n.t('Select_Server')
}); });
static propTypes = { state = { servers: [] as IServer[] };
server: PropTypes.string,
navigation: PropTypes.object
};
state = { servers: [] };
async componentDidMount() { async componentDidMount() {
const serversDB = database.servers; const serversDB = database.servers;
const serversCollection = serversDB.get('servers'); const serversCollection = serversDB.get('servers');
const servers = await serversCollection.query(Q.where('rooms_updated_at', Q.notEq(null))).fetch(); const servers: IServer[] = await serversCollection.query(Q.where('rooms_updated_at', Q.notEq(null))).fetch();
this.setState({ servers }); this.setState({ servers });
} }
select = async server => { select = async (server: string) => {
const { server: currentServer, navigation } = this.props; const { server: currentServer, navigation } = this.props;
navigation.navigate('ShareListView'); navigation.navigate('ShareListView');
@ -43,7 +53,7 @@ class SelectServerView extends React.Component {
} }
}; };
renderItem = ({ item }) => { renderItem = ({ item }: { item: IServer }) => {
const { server } = this.props; const { server } = this.props;
return <ServerItem onPress={() => this.select(item.id)} item={item} hasCheck={item.id === server} />; return <ServerItem onPress={() => this.select(item.id)} item={item} hasCheck={item.id === server} />;
}; };
@ -62,7 +72,6 @@ class SelectServerView extends React.Component {
ItemSeparatorComponent={List.Separator} ItemSeparatorComponent={List.Separator}
ListHeaderComponent={List.Separator} ListHeaderComponent={List.Separator}
ListFooterComponent={List.Separator} ListFooterComponent={List.Separator}
enableEmptySections
removeClippedSubviews removeClippedSubviews
keyboardShouldPersistTaps='always' keyboardShouldPersistTaps='always'
/> />
@ -71,7 +80,7 @@ class SelectServerView extends React.Component {
} }
} }
const mapStateToProps = ({ share }) => ({ const mapStateToProps = ({ share }: any) => ({
server: share.server.server server: share.server.server
}); });