2020-03-30 20:19:01 +00:00
|
|
|
import React from 'react';
|
|
|
|
import { FlatList, StyleSheet } from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
2022-02-18 13:53:36 +00:00
|
|
|
import { setUser } from '../actions/login';
|
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2022-02-18 13:53:36 +00:00
|
|
|
import Loading from '../containers/Loading';
|
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2020-03-30 20:19:01 +00:00
|
|
|
import Status from '../containers/Status/Status';
|
|
|
|
import TextInput from '../containers/TextInput';
|
2022-02-18 13:53:36 +00:00
|
|
|
import { LISTENER } from '../containers/Toast';
|
2022-03-25 20:05:49 +00:00
|
|
|
import { IApplicationState, IBaseScreen, IUser, TUserStatus } from '../definitions';
|
2022-02-18 13:53:36 +00:00
|
|
|
import I18n from '../i18n';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
|
|
|
import { getUserSelector } from '../selectors/login';
|
|
|
|
import { withTheme } from '../theme';
|
2020-03-30 20:19:01 +00:00
|
|
|
import EventEmitter from '../utils/events';
|
2021-06-04 20:53:39 +00:00
|
|
|
import { showErrorAlert } from '../utils/info';
|
2021-09-13 20:41:05 +00:00
|
|
|
import log, { events, logEvent } from '../utils/log';
|
2020-03-30 20:19:01 +00:00
|
|
|
|
2022-03-25 20:05:49 +00:00
|
|
|
interface IStatus {
|
|
|
|
id: TUserStatus;
|
|
|
|
name: string;
|
|
|
|
}
|
|
|
|
|
|
|
|
const STATUS: IStatus[] = [
|
2021-09-13 20:41:05 +00:00
|
|
|
{
|
|
|
|
id: 'online',
|
|
|
|
name: 'Online'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'busy',
|
|
|
|
name: 'Busy'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'away',
|
|
|
|
name: 'Away'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
id: 'offline',
|
|
|
|
name: 'Invisible'
|
|
|
|
}
|
|
|
|
];
|
2020-03-30 20:19:01 +00:00
|
|
|
|
|
|
|
const styles = StyleSheet.create({
|
|
|
|
inputContainer: {
|
|
|
|
marginTop: 32,
|
|
|
|
marginBottom: 32
|
|
|
|
},
|
|
|
|
inputLeft: {
|
|
|
|
position: 'absolute',
|
2021-03-31 17:47:17 +00:00
|
|
|
top: 12,
|
|
|
|
left: 12
|
2020-03-30 20:19:01 +00:00
|
|
|
},
|
|
|
|
inputStyle: {
|
2021-03-31 17:47:17 +00:00
|
|
|
paddingLeft: 48
|
2020-03-30 20:19:01 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-12-02 13:36:46 +00:00
|
|
|
interface IStatusViewState {
|
|
|
|
statusText: string;
|
|
|
|
loading: boolean;
|
|
|
|
}
|
2020-03-30 20:19:01 +00:00
|
|
|
|
2022-02-18 13:53:36 +00:00
|
|
|
interface IStatusViewProps extends IBaseScreen<any, 'StatusView'> {
|
2022-04-07 16:27:45 +00:00
|
|
|
user: IUser;
|
2021-12-02 13:36:46 +00:00
|
|
|
isMasterDetail: boolean;
|
|
|
|
Accounts_AllowInvisibleStatusOption: boolean;
|
|
|
|
}
|
|
|
|
|
|
|
|
class StatusView extends React.Component<IStatusViewProps, IStatusViewState> {
|
|
|
|
constructor(props: IStatusViewProps) {
|
|
|
|
super(props);
|
2020-03-30 20:19:01 +00:00
|
|
|
const { statusText } = props.user;
|
2021-04-01 14:00:20 +00:00
|
|
|
this.state = { statusText: statusText || '', loading: false };
|
2020-06-15 14:00:46 +00:00
|
|
|
this.setHeader();
|
|
|
|
}
|
2020-03-30 20:19:01 +00:00
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
setHeader = () => {
|
|
|
|
const { navigation, isMasterDetail } = this.props;
|
|
|
|
navigation.setOptions({
|
|
|
|
title: I18n.t('Edit_Status'),
|
2020-10-30 16:15:58 +00:00
|
|
|
headerLeft: isMasterDetail ? undefined : () => <HeaderButton.CancelModal onPress={this.close} />,
|
2020-06-15 14:00:46 +00:00
|
|
|
headerRight: () => (
|
2020-10-30 16:15:58 +00:00
|
|
|
<HeaderButton.Container>
|
2021-09-13 20:41:05 +00:00
|
|
|
<HeaderButton.Item title={I18n.t('Done')} onPress={this.submit} testID='status-view-submit' />
|
2020-10-30 16:15:58 +00:00
|
|
|
</HeaderButton.Container>
|
2020-06-15 14:00:46 +00:00
|
|
|
)
|
|
|
|
});
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-03-30 20:19:01 +00:00
|
|
|
|
2021-09-13 20:41:05 +00:00
|
|
|
submit = async () => {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.STATUS_DONE);
|
2020-03-30 20:19:01 +00:00
|
|
|
const { statusText } = this.state;
|
|
|
|
const { user } = this.props;
|
|
|
|
if (statusText !== user.statusText) {
|
2021-04-19 14:31:43 +00:00
|
|
|
await this.setCustomStatus(statusText);
|
2020-03-30 20:19:01 +00:00
|
|
|
}
|
|
|
|
this.close();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-03-30 20:19:01 +00:00
|
|
|
|
|
|
|
close = () => {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.goBack();
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-03-30 20:19:01 +00:00
|
|
|
|
2021-12-02 13:36:46 +00:00
|
|
|
setCustomStatus = async (statusText: string) => {
|
2022-02-18 13:53:36 +00:00
|
|
|
const { user, dispatch } = this.props;
|
2020-03-30 20:19:01 +00:00
|
|
|
|
|
|
|
this.setState({ loading: true });
|
|
|
|
|
|
|
|
try {
|
2020-04-06 19:32:58 +00:00
|
|
|
const result = await RocketChat.setUserStatus(user.status, statusText);
|
2020-03-30 20:19:01 +00:00
|
|
|
if (result.success) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.STATUS_CUSTOM);
|
2022-02-18 13:53:36 +00:00
|
|
|
dispatch(setUser({ statusText }));
|
2020-03-30 20:19:01 +00:00
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('Status_saved_successfully') });
|
|
|
|
} else {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.STATUS_CUSTOM_F);
|
2020-03-30 20:19:01 +00:00
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('error-could-not-change-status') });
|
|
|
|
}
|
|
|
|
} catch {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.STATUS_CUSTOM_F);
|
2020-03-30 20:19:01 +00:00
|
|
|
EventEmitter.emit(LISTENER, { message: I18n.t('error-could-not-change-status') });
|
|
|
|
}
|
|
|
|
|
|
|
|
this.setState({ loading: false });
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-03-30 20:19:01 +00:00
|
|
|
|
|
|
|
renderHeader = () => {
|
|
|
|
const { statusText } = this.state;
|
|
|
|
const { user, theme } = this.props;
|
|
|
|
|
|
|
|
return (
|
|
|
|
<>
|
|
|
|
<TextInput
|
|
|
|
theme={theme}
|
|
|
|
value={statusText}
|
|
|
|
containerStyle={styles.inputContainer}
|
|
|
|
onChangeText={text => this.setState({ statusText: text })}
|
2022-03-25 20:05:49 +00:00
|
|
|
left={<Status testID={`status-view-current-${user.status}`} style={styles.inputLeft} status={user.status} size={24} />}
|
2020-03-30 20:19:01 +00:00
|
|
|
inputStyle={styles.inputStyle}
|
|
|
|
placeholder={I18n.t('What_are_you_doing_right_now')}
|
|
|
|
testID='status-view-input'
|
|
|
|
/>
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Separator />
|
2020-03-30 20:19:01 +00:00
|
|
|
</>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-03-30 20:19:01 +00:00
|
|
|
|
2022-03-25 20:05:49 +00:00
|
|
|
renderItem = ({ item }: { item: IStatus }) => {
|
2020-04-06 19:32:58 +00:00
|
|
|
const { statusText } = this.state;
|
2022-02-18 13:53:36 +00:00
|
|
|
const { user, dispatch } = this.props;
|
2020-03-30 20:19:01 +00:00
|
|
|
const { id, name } = item;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<List.Item
|
|
|
|
title={name}
|
2021-09-13 20:41:05 +00:00
|
|
|
onPress={async () => {
|
2021-12-02 13:36:46 +00:00
|
|
|
// @ts-ignore
|
2021-09-13 20:41:05 +00:00
|
|
|
logEvent(events[`STATUS_${item.id.toUpperCase()}`]);
|
2020-03-30 20:19:01 +00:00
|
|
|
if (user.status !== item.id) {
|
|
|
|
try {
|
2020-04-06 19:32:58 +00:00
|
|
|
const result = await RocketChat.setUserStatus(item.id, statusText);
|
|
|
|
if (result.success) {
|
2022-03-25 20:05:49 +00:00
|
|
|
dispatch(setUser({ status: item.id }));
|
2020-04-06 19:32:58 +00:00
|
|
|
}
|
2021-12-02 13:36:46 +00:00
|
|
|
} catch (e: any) {
|
2022-03-28 22:52:20 +00:00
|
|
|
const messageError =
|
|
|
|
e.data && e.data.error.includes('[error-too-many-requests]')
|
|
|
|
? I18n.t('error-too-many-requests', { seconds: e.data.error.replace(/\D/g, '') })
|
|
|
|
: e.data.errorType;
|
|
|
|
showErrorAlert(messageError);
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SET_STATUS_FAIL);
|
2020-03-30 20:19:01 +00:00
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
2021-09-13 20:41:05 +00:00
|
|
|
testID={`status-view-${id}`}
|
2021-03-31 17:47:17 +00:00
|
|
|
left={() => <Status size={24} status={item.id} />}
|
2020-03-30 20:19:01 +00:00
|
|
|
/>
|
|
|
|
);
|
2021-09-13 20:41:05 +00:00
|
|
|
};
|
2020-03-30 20:19:01 +00:00
|
|
|
|
|
|
|
render() {
|
|
|
|
const { loading } = this.state;
|
2021-06-04 20:53:39 +00:00
|
|
|
const { Accounts_AllowInvisibleStatusOption } = this.props;
|
|
|
|
|
|
|
|
const status = Accounts_AllowInvisibleStatusOption ? STATUS : STATUS.filter(s => s.id !== 'offline');
|
|
|
|
|
2020-03-30 20:19:01 +00:00
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='status-view'>
|
2020-03-30 20:19:01 +00:00
|
|
|
<FlatList
|
2021-06-04 20:53:39 +00:00
|
|
|
data={status}
|
2020-03-30 20:19:01 +00:00
|
|
|
keyExtractor={item => item.id}
|
|
|
|
renderItem={this.renderItem}
|
|
|
|
ListHeaderComponent={this.renderHeader}
|
2020-10-30 13:59:44 +00:00
|
|
|
ListFooterComponent={List.Separator}
|
|
|
|
ItemSeparatorComponent={List.Separator}
|
2020-03-30 20:19:01 +00:00
|
|
|
/>
|
|
|
|
<Loading visible={loading} />
|
|
|
|
</SafeAreaView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-18 13:53:36 +00:00
|
|
|
const mapStateToProps = (state: IApplicationState) => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
user: getUserSelector(state),
|
2021-06-04 20:53:39 +00:00
|
|
|
isMasterDetail: state.app.isMasterDetail,
|
2022-02-18 13:53:36 +00:00
|
|
|
Accounts_AllowInvisibleStatusOption: (state.settings.Accounts_AllowInvisibleStatusOption as boolean) ?? true
|
2021-04-07 19:11:15 +00:00
|
|
|
});
|
|
|
|
|
2022-02-18 13:53:36 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(StatusView));
|