Chore: Migrate StatusView to Typescript (#3527)

Co-authored-by: AlexAlexandre <alexalexandrejr@gmail.com>
This commit is contained in:
Reinaldo Neto 2021-12-02 10:36:46 -03:00 committed by GitHub
parent ceeca5952d
commit 17c70e73d3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 22 deletions

View File

@ -8,6 +8,7 @@ interface IStatus {
status: string; status: string;
size: number; size: number;
style?: StyleProp<TextStyle>; style?: StyleProp<TextStyle>;
testID?: string;
} }
const Status = React.memo(({ style, status = 'offline', size = 32, ...props }: IStatus) => { const Status = React.memo(({ style, status = 'offline', size = 32, ...props }: IStatus) => {

View File

@ -1,6 +1,7 @@
import React from 'react'; import React from 'react';
import PropTypes from 'prop-types'; import { StackNavigationProp } from '@react-navigation/stack';
import { FlatList, StyleSheet } from 'react-native'; import { FlatList, StyleSheet } from 'react-native';
import { Dispatch } from 'redux';
import { connect } from 'react-redux'; import { connect } from 'react-redux';
import I18n from '../i18n'; import I18n from '../i18n';
@ -53,23 +54,29 @@ const styles = StyleSheet.create({
} }
}); });
class StatusView extends React.Component { interface IUser {
static propTypes = { id: string;
user: PropTypes.shape({ status: string;
id: PropTypes.string, statusText: string;
status: PropTypes.string, }
statusText: PropTypes.string
}),
theme: PropTypes.string,
navigation: PropTypes.object,
isMasterDetail: PropTypes.bool,
setUser: PropTypes.func,
Accounts_AllowInvisibleStatusOption: PropTypes.bool
};
constructor(props) { interface IStatusViewState {
statusText: string;
loading: boolean;
}
interface IStatusViewProps {
navigation: StackNavigationProp<any, 'StatusView'>;
user: IUser;
theme: string;
isMasterDetail: boolean;
setUser: (user: Partial<IUser>) => void;
Accounts_AllowInvisibleStatusOption: boolean;
}
class StatusView extends React.Component<IStatusViewProps, IStatusViewState> {
constructor(props: IStatusViewProps) {
super(props); super(props);
const { statusText } = props.user; const { statusText } = props.user;
this.state = { statusText: statusText || '', loading: false }; this.state = { statusText: statusText || '', loading: false };
this.setHeader(); this.setHeader();
@ -103,7 +110,7 @@ class StatusView extends React.Component {
navigation.goBack(); navigation.goBack();
}; };
setCustomStatus = async statusText => { setCustomStatus = async (statusText: string) => {
const { user, setUser } = this.props; const { user, setUser } = this.props;
this.setState({ loading: true }); this.setState({ loading: true });
@ -147,7 +154,7 @@ class StatusView extends React.Component {
); );
}; };
renderItem = ({ item }) => { renderItem = ({ item }: { item: { id: string; name: string } }) => {
const { statusText } = this.state; const { statusText } = this.state;
const { user, setUser } = this.props; const { user, setUser } = this.props;
const { id, name } = item; const { id, name } = item;
@ -155,6 +162,7 @@ class StatusView extends React.Component {
<List.Item <List.Item
title={name} title={name}
onPress={async () => { onPress={async () => {
// @ts-ignore
logEvent(events[`STATUS_${item.id.toUpperCase()}`]); logEvent(events[`STATUS_${item.id.toUpperCase()}`]);
if (user.status !== item.id) { if (user.status !== item.id) {
try { try {
@ -162,7 +170,7 @@ class StatusView extends React.Component {
if (result.success) { if (result.success) {
setUser({ status: item.id }); setUser({ status: item.id });
} }
} catch (e) { } catch (e: any) {
showErrorAlert(I18n.t(e.data.errorType)); showErrorAlert(I18n.t(e.data.errorType));
logEvent(events.SET_STATUS_FAIL); logEvent(events.SET_STATUS_FAIL);
log(e); log(e);
@ -197,14 +205,14 @@ class StatusView extends React.Component {
} }
} }
const mapStateToProps = state => ({ const mapStateToProps = (state: any) => ({
user: getUserSelector(state), user: getUserSelector(state),
isMasterDetail: state.app.isMasterDetail, isMasterDetail: state.app.isMasterDetail,
Accounts_AllowInvisibleStatusOption: state.settings.Accounts_AllowInvisibleStatusOption ?? true Accounts_AllowInvisibleStatusOption: state.settings.Accounts_AllowInvisibleStatusOption ?? true
}); });
const mapDispatchToProps = dispatch => ({ const mapDispatchToProps = (dispatch: Dispatch) => ({
setUser: user => dispatch(setUserAction(user)) setUser: (user: IUser) => dispatch(setUserAction(user))
}); });
export default connect(mapStateToProps, mapDispatchToProps)(withTheme(StatusView)); export default connect(mapStateToProps, mapDispatchToProps)(withTheme(StatusView));