chore: remove mapDispatchToProps to use dispatch prop and clear some types

This commit is contained in:
GleidsonDaniel 2021-12-21 10:17:11 -03:00
parent 34e6ece871
commit 9d7450b300
1 changed files with 37 additions and 45 deletions

View File

@ -1,30 +1,30 @@
import React from 'react'; import { Q } from '@nozbe/watermelondb';
import { StackNavigationProp } from '@react-navigation/stack';
import { RouteProp } from '@react-navigation/native'; import { RouteProp } from '@react-navigation/native';
import orderBy from 'lodash/orderBy';
import React from 'react';
import { FlatList, View } from 'react-native'; import { FlatList, View } from 'react-native';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import orderBy from 'lodash/orderBy';
import { Q } from '@nozbe/watermelondb';
import { Subscription } from 'rxjs'; import { Subscription } from 'rxjs';
import { addUser, removeUser, reset } from '../actions/selectedUsers';
import { themes } from '../constants/colors';
import * as HeaderButton from '../containers/HeaderButton';
import * as List from '../containers/List'; import * as List from '../containers/List';
import Loading from '../containers/Loading';
import SafeAreaView from '../containers/SafeAreaView';
import SearchBox from '../containers/SearchBox';
import StatusBar from '../containers/StatusBar';
import I18n from '../i18n';
import database from '../lib/database'; import database from '../lib/database';
import RocketChat from '../lib/rocketchat'; import RocketChat from '../lib/rocketchat';
import UserItem from '../presentation/UserItem'; import UserItem from '../presentation/UserItem';
import Loading from '../containers/Loading';
import I18n from '../i18n';
import log, { events, logEvent } from '../utils/log';
import SearchBox from '../containers/SearchBox';
import * as HeaderButton from '../containers/HeaderButton';
import StatusBar from '../containers/StatusBar';
import { themes } from '../constants/colors';
import { withTheme } from '../theme';
import { getUserSelector } from '../selectors/login'; import { getUserSelector } from '../selectors/login';
import { addUser as addUserAction, removeUser as removeUserAction, reset as resetAction } from '../actions/selectedUsers';
import { showErrorAlert } from '../utils/info';
import SafeAreaView from '../containers/SafeAreaView';
import sharedStyles from './Styles';
import { ChatsStackParamList } from '../stacks/types'; import { ChatsStackParamList } from '../stacks/types';
import { withTheme } from '../theme';
import { ApplicationState, BaseScreen } from '../types';
import { showErrorAlert } from '../utils/info';
import log, { events, logEvent } from '../utils/log';
import sharedStyles from './Styles';
const ITEM_WIDTH = 250; const ITEM_WIDTH = 250;
const getItemLayout = (_: any, index: number) => ({ length: ITEM_WIDTH, offset: ITEM_WIDTH * index, index }); const getItemLayout = (_: any, index: number) => ({ length: ITEM_WIDTH, offset: ITEM_WIDTH * index, index });
@ -37,19 +37,8 @@ interface IUser {
// username is used when is from searching // username is used when is from searching
username?: string; username?: string;
} }
interface ISelectedUsersViewState {
maxUsers?: number;
search: IUser[];
chats: IUser[];
}
interface ISelectedUsersViewProps { type State = {
navigation: StackNavigationProp<ChatsStackParamList, 'SelectedUsersView'>;
route: RouteProp<ChatsStackParamList, 'SelectedUsersView'>;
baseUrl: string;
addUser(user: IUser): void;
removeUser(user: IUser): void;
reset(): void;
users: IUser[]; users: IUser[];
loading: boolean; loading: boolean;
user: { user: {
@ -58,7 +47,17 @@ interface ISelectedUsersViewProps {
username: string; username: string;
name: string; name: string;
}; };
theme: string; baseUrl: string;
};
interface ISelectedUsersViewState {
maxUsers?: number;
search: IUser[];
chats: IUser[];
}
interface ISelectedUsersViewProps extends BaseScreen, State {
route: RouteProp<ChatsStackParamList, 'SelectedUsersView'>;
} }
class SelectedUsersView extends React.Component<ISelectedUsersViewProps, ISelectedUsersViewState> { class SelectedUsersView extends React.Component<ISelectedUsersViewProps, ISelectedUsersViewState> {
@ -75,9 +74,9 @@ class SelectedUsersView extends React.Component<ISelectedUsersViewProps, ISelect
search: [], search: [],
chats: [] chats: []
}; };
const { user } = this.props; const { user, dispatch } = this.props;
if (this.isGroupChat()) { if (this.isGroupChat()) {
props.addUser({ _id: user.id, name: user.username, fname: user.name }); dispatch(addUser({ _id: user.id, name: user.username, fname: user.name }));
} }
this.setHeader(props.route.params?.showButton); this.setHeader(props.route.params?.showButton);
} }
@ -92,8 +91,8 @@ class SelectedUsersView extends React.Component<ISelectedUsersViewProps, ISelect
} }
componentWillUnmount() { componentWillUnmount() {
const { reset } = this.props; const { dispatch } = this.props;
reset(); dispatch(reset());
if (this.querySubscription && this.querySubscription.unsubscribe) { if (this.querySubscription && this.querySubscription.unsubscribe) {
this.querySubscription.unsubscribe(); this.querySubscription.unsubscribe();
} }
@ -161,8 +160,7 @@ class SelectedUsersView extends React.Component<ISelectedUsersViewProps, ISelect
toggleUser = (user: IUser) => { toggleUser = (user: IUser) => {
const { maxUsers } = this.state; const { maxUsers } = this.state;
const { const {
addUser, dispatch,
removeUser,
users, users,
user: { username } user: { username }
} = this.props; } = this.props;
@ -177,10 +175,10 @@ class SelectedUsersView extends React.Component<ISelectedUsersViewProps, ISelect
return showErrorAlert(I18n.t('Max_number_of_users_allowed_is_number', { maxUsers }), I18n.t('Oops')); return showErrorAlert(I18n.t('Max_number_of_users_allowed_is_number', { maxUsers }), I18n.t('Oops'));
} }
logEvent(events.SELECTED_USERS_ADD_USER); logEvent(events.SELECTED_USERS_ADD_USER);
addUser(user); dispatch(addUser(user));
} else { } else {
logEvent(events.SELECTED_USERS_REMOVE_USER); logEvent(events.SELECTED_USERS_REMOVE_USER);
removeUser(user); dispatch(removeUser(user));
} }
}; };
@ -308,17 +306,11 @@ class SelectedUsersView extends React.Component<ISelectedUsersViewProps, ISelect
}; };
} }
const mapStateToProps = (state: any) => ({ const mapStateToProps = (state: ApplicationState) => ({
baseUrl: state.server.server, baseUrl: state.server.server,
users: state.selectedUsers.users, users: state.selectedUsers.users,
loading: state.selectedUsers.loading, loading: state.selectedUsers.loading,
user: getUserSelector(state) user: getUserSelector(state)
}); });
const mapDispatchToProps = (dispatch: any) => ({ export default connect(mapStateToProps)(withTheme(SelectedUsersView));
addUser: (user: any) => dispatch(addUserAction(user)),
removeUser: (user: any) => dispatch(removeUserAction(user)),
reset: () => dispatch(resetAction())
});
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(SelectedUsersView));