Chore: Evaluate SelectListView - TypeScript (#4119)

* Chore: Evaluate SelectListView - TypeScript

* minor tweak
This commit is contained in:
Reinaldo Neto 2022-05-02 09:40:19 -03:00 committed by GitHub
parent 8896370a6c
commit e5f140e231
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 23 additions and 26 deletions

View File

@ -0,0 +1,4 @@
import { TSubscriptionModel } from './ISubscription';
export type TDataSelect = Pick<TSubscriptionModel, 'rid'> &
Partial<Pick<TSubscriptionModel, 't' | 'name' | 'teamMain' | 'alert'>>;

View File

@ -2,13 +2,13 @@ import { NavigatorScreenParams } from '@react-navigation/core';
import { TextInputProps } from 'react-native'; import { TextInputProps } from 'react-native';
import Model from '@nozbe/watermelondb/Model'; import Model from '@nozbe/watermelondb/Model';
import { IRoom } from '../definitions/IRoom';
import { IOptionsField } from '../views/NotificationPreferencesView/options'; import { IOptionsField } from '../views/NotificationPreferencesView/options';
import { IServer } from '../definitions/IServer'; import { IServer } from '../definitions/IServer';
import { IAttachment } from '../definitions/IAttachment'; import { IAttachment } from '../definitions/IAttachment';
import { IMessage, TMessageModel } from '../definitions/IMessage'; import { IMessage, TMessageModel } from '../definitions/IMessage';
import { ISubscription, SubscriptionType, TSubscriptionModel } from '../definitions/ISubscription'; import { ISubscription, SubscriptionType, TSubscriptionModel } from '../definitions/ISubscription';
import { ICannedResponse } from '../definitions/ICannedResponse'; import { ICannedResponse } from '../definitions/ICannedResponse';
import { TDataSelect } from '../definitions/IDataSelect';
import { ModalStackParamList } from './MasterDetailStack/types'; import { ModalStackParamList } from './MasterDetailStack/types';
export type ChatsStackParamList = { export type ChatsStackParamList = {
@ -43,13 +43,13 @@ export type ChatsStackParamList = {
joined: boolean; joined: boolean;
}; };
SelectListView: { SelectListView: {
data?: IRoom[]; data?: TDataSelect[];
title: string; title: string;
infoText?: string; infoText?: string;
nextAction: (selected: string[]) => void; nextAction: (selected: string[]) => void;
showAlert?: () => void; showAlert?: () => void;
isSearch?: boolean; isSearch?: boolean;
onSearch?: (text: string) => Promise<Partial<IRoom[]> | any>; onSearch?: (text: string) => Promise<TDataSelect[] | any>;
isRadio?: boolean; isRadio?: boolean;
}; };
RoomInfoView: { RoomInfoView: {

View File

@ -16,15 +16,7 @@ import RoomTypeIcon from '../../containers/RoomTypeIcon';
import SafeAreaView from '../../containers/SafeAreaView'; import SafeAreaView from '../../containers/SafeAreaView';
import Status from '../../containers/Status'; import Status from '../../containers/Status';
import StatusBar from '../../containers/StatusBar'; import StatusBar from '../../containers/StatusBar';
import { import { IApplicationState, IBaseScreen, ISubscription, IUser, SubscriptionType, TSubscriptionModel } from '../../definitions';
IApplicationState,
IBaseScreen,
IRoom,
ISubscription,
IUser,
SubscriptionType,
TSubscriptionModel
} from '../../definitions';
import { withDimensions } from '../../dimensions'; import { withDimensions } from '../../dimensions';
import I18n from '../../i18n'; import I18n from '../../i18n';
import database from '../../lib/database'; import database from '../../lib/database';
@ -587,14 +579,14 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
if (result.success) { if (result.success) {
if (result.rooms?.length) { if (result.rooms?.length) {
const teamChannels = result.rooms.map((r: any) => ({ const teamChannels = result.rooms.map(r => ({
rid: r._id, rid: r._id,
name: r.name, name: r.name,
teamId: r.teamId teamId: r.teamId
})); }));
navigation.navigate('SelectListView', { navigation.navigate('SelectListView', {
title: 'Converting_Team_To_Channel', title: 'Converting_Team_To_Channel',
data: teamChannels as any, data: teamChannels,
infoText: 'Select_Team_Channels_To_Delete', infoText: 'Select_Team_Channels_To_Delete',
nextAction: (data: string[]) => this.convertTeamToChannelConfirmation(data) nextAction: (data: string[]) => this.convertTeamToChannelConfirmation(data)
}); });
@ -728,10 +720,10 @@ class RoomActionsView extends React.Component<IRoomActionsViewProps, IRoomAction
if (teamRooms.length) { if (teamRooms.length) {
const data = teamRooms.map(team => ({ const data = teamRooms.map(team => ({
rid: team.teamId, rid: team.teamId as string,
t: team.t, t: team.t,
name: team.name name: team.name
})) as IRoom[]; // TODO: review this usage later }));
navigation.navigate('SelectListView', { navigation.navigate('SelectListView', {
title: 'Move_to_Team', title: 'Move_to_Team',
infoText: 'Move_Channel_Paragraph', infoText: 'Move_Channel_Paragraph',

View File

@ -18,7 +18,8 @@ import { animateNextTransition } from '../utils/layoutAnimation';
import { ICON_SIZE } from '../containers/List/constants'; import { ICON_SIZE } from '../containers/List/constants';
import SearchBox from '../containers/SearchBox'; import SearchBox from '../containers/SearchBox';
import sharedStyles from './Styles'; import sharedStyles from './Styles';
import { IRoom } from '../definitions/IRoom'; import { IApplicationState } from '../definitions';
import { TDataSelect } from '../definitions/IDataSelect';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
buttonText: { buttonText: {
@ -29,8 +30,8 @@ const styles = StyleSheet.create({
}); });
interface ISelectListViewState { interface ISelectListViewState {
data?: IRoom[]; data?: TDataSelect[];
dataFiltered?: IRoom[]; dataFiltered?: TDataSelect[];
isSearching: boolean; isSearching: boolean;
selected: string[]; selected: string[];
} }
@ -53,7 +54,7 @@ class SelectListView extends React.Component<ISelectListViewProps, ISelectListVi
private isSearch: boolean; private isSearch: boolean;
private onSearch?: (text: string) => Promise<Partial<IRoom[]> | any>; private onSearch?: (text: string) => Promise<TDataSelect[] | any>;
private isRadio?: boolean; private isRadio?: boolean;
@ -122,7 +123,7 @@ class SelectListView extends React.Component<ISelectListViewProps, ISelectListVi
search = async (text: string) => { search = async (text: string) => {
try { try {
this.setState({ isSearching: true }); this.setState({ isSearching: true });
const result = (await this.onSearch?.(text)) as IRoom[]; const result = await this.onSearch?.(text);
this.setState({ dataFiltered: result }); this.setState({ dataFiltered: result });
} catch (e) { } catch (e) {
log(e); log(e);
@ -150,19 +151,19 @@ class SelectListView extends React.Component<ISelectListViewProps, ISelectListVi
} }
}; };
renderItem = ({ item }: { item: Partial<IRoom> }) => { renderItem = ({ item }: { item: TDataSelect }) => {
const { theme } = this.props; const { theme } = this.props;
const { selected } = this.state; const { selected } = this.state;
const channelIcon = item.t === 'p' ? 'channel-private' : 'channel-public'; const channelIcon = item.t === 'p' ? 'channel-private' : 'channel-public';
const teamIcon = item.t === 'p' ? 'teams-private' : 'teams'; const teamIcon = item.t === 'p' ? 'teams-private' : 'teams';
const icon = item.teamMain ? teamIcon : channelIcon; const icon = item.teamMain ? teamIcon : channelIcon;
const checked = this.isChecked(item.rid!) ? 'check' : ''; const checked = this.isChecked(item.rid) ? 'check' : '';
const showRadio = () => ( const showRadio = () => (
<RadioButton <RadioButton
testID={selected ? `radio-button-selected-${item.name}` : `radio-button-unselected-${item.name}`} testID={selected ? `radio-button-selected-${item.name}` : `radio-button-unselected-${item.name}`}
selected={selected.includes(item.rid!)} selected={selected.includes(item.rid)}
color={themes[theme].actionTintColor} color={themes[theme].actionTintColor}
size={ICON_SIZE} size={ICON_SIZE}
/> />
@ -182,7 +183,7 @@ class SelectListView extends React.Component<ISelectListViewProps, ISelectListVi
title={item.name} title={item.name}
translateTitle={false} translateTitle={false}
testID={`select-list-view-item-${item.name}`} testID={`select-list-view-item-${item.name}`}
onPress={() => (item.alert ? this.showAlert() : this.toggleItem(item.rid!))} onPress={() => (item.alert ? this.showAlert() : this.toggleItem(item.rid))}
alert={item.alert} alert={item.alert}
left={() => <List.Icon name={icon} color={themes[theme].controlText} />} left={() => <List.Icon name={icon} color={themes[theme].controlText} />}
right={() => (this.isRadio ? showRadio() : showCheck())} right={() => (this.isRadio ? showRadio() : showCheck())}
@ -211,7 +212,7 @@ class SelectListView extends React.Component<ISelectListViewProps, ISelectListVi
} }
} }
const mapStateToProps = (state: any) => ({ const mapStateToProps = (state: IApplicationState) => ({
isMasterDetail: state.app.isMasterDetail isMasterDetail: state.app.isMasterDetail
}); });