2020-03-30 20:19:01 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
|
|
|
import { FlatList, StyleSheet } from 'react-native';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import I18n from '../i18n';
|
2020-10-30 13:59:44 +00:00
|
|
|
import * as List from '../containers/List';
|
2020-03-30 20:19:01 +00:00
|
|
|
import Status from '../containers/Status/Status';
|
|
|
|
import TextInput from '../containers/TextInput';
|
|
|
|
import EventEmitter from '../utils/events';
|
|
|
|
import Loading from '../containers/Loading';
|
|
|
|
import RocketChat from '../lib/rocketchat';
|
2020-07-30 19:51:13 +00:00
|
|
|
import log, { logEvent, events } from '../utils/log';
|
2020-03-30 20:19:01 +00:00
|
|
|
|
|
|
|
import { LISTENER } from '../containers/Toast';
|
|
|
|
import { withTheme } from '../theme';
|
|
|
|
import { getUserSelector } from '../selectors/login';
|
2020-10-30 16:15:58 +00:00
|
|
|
import * as HeaderButton from '../containers/HeaderButton';
|
2020-04-06 19:32:58 +00:00
|
|
|
import store from '../lib/createStore';
|
|
|
|
import { setUser } from '../actions/login';
|
2020-06-15 14:00:46 +00:00
|
|
|
import SafeAreaView from '../containers/SafeAreaView';
|
2020-03-30 20:19:01 +00:00
|
|
|
|
|
|
|
const STATUS = [{
|
|
|
|
id: 'online',
|
2020-07-24 19:23:34 +00:00
|
|
|
name: 'Online'
|
2020-03-30 20:19:01 +00:00
|
|
|
}, {
|
|
|
|
id: 'busy',
|
2020-07-24 19:23:34 +00:00
|
|
|
name: 'Busy'
|
2020-03-30 20:19:01 +00:00
|
|
|
}, {
|
|
|
|
id: 'away',
|
2020-07-24 19:23:34 +00:00
|
|
|
name: 'Away'
|
2020-03-30 20:19:01 +00:00
|
|
|
}, {
|
|
|
|
id: 'offline',
|
2020-07-24 19:23:34 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
class StatusView extends React.Component {
|
|
|
|
static propTypes = {
|
|
|
|
user: PropTypes.shape({
|
2020-04-06 19:32:58 +00:00
|
|
|
id: PropTypes.string,
|
2020-03-30 20:19:01 +00:00
|
|
|
status: PropTypes.string,
|
|
|
|
statusText: PropTypes.string
|
|
|
|
}),
|
|
|
|
theme: PropTypes.string,
|
2020-06-15 14:00:46 +00:00
|
|
|
navigation: PropTypes.object,
|
|
|
|
isMasterDetail: PropTypes.bool
|
2020-03-30 20:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
|
|
|
|
const { statusText } = props.user;
|
|
|
|
this.state = { 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>
|
|
|
|
<HeaderButton.Item
|
2020-06-15 14:00:46 +00:00
|
|
|
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
|
|
|
)
|
|
|
|
});
|
2020-03-30 20:19:01 +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) {
|
|
|
|
await this.setCustomStatus();
|
|
|
|
}
|
|
|
|
this.close();
|
|
|
|
}
|
|
|
|
|
|
|
|
close = () => {
|
2020-06-15 14:00:46 +00:00
|
|
|
const { navigation } = this.props;
|
|
|
|
navigation.goBack();
|
2020-03-30 20:19:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
setCustomStatus = async() => {
|
|
|
|
const { statusText } = this.state;
|
2020-04-06 19:32:58 +00:00
|
|
|
const { user } = 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);
|
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 });
|
|
|
|
}
|
|
|
|
|
|
|
|
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 })}
|
|
|
|
left={(
|
|
|
|
<Status
|
|
|
|
testID={`status-view-current-${ user.status }`}
|
|
|
|
style={styles.inputLeft}
|
|
|
|
status={user.status}
|
2021-03-31 17:47:17 +00:00
|
|
|
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
|
|
|
</>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
2020-04-06 19:32:58 +00:00
|
|
|
const { statusText } = this.state;
|
2020-10-30 13:59:44 +00:00
|
|
|
const { user } = 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}
|
2020-03-30 20:19:01 +00:00
|
|
|
onPress={async() => {
|
2020-07-30 19:51:13 +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) {
|
|
|
|
store.dispatch(setUser({ status: item.id }));
|
|
|
|
}
|
2020-03-30 20:19:01 +00:00
|
|
|
} catch (e) {
|
2020-07-30 19:51:13 +00:00
|
|
|
logEvent(events.SET_STATUS_FAIL);
|
2020-03-30 20:19:01 +00:00
|
|
|
log(e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
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
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
const { loading } = this.state;
|
|
|
|
return (
|
2020-10-30 13:59:44 +00:00
|
|
|
<SafeAreaView testID='status-view'>
|
2020-03-30 20:19:01 +00:00
|
|
|
<FlatList
|
|
|
|
data={STATUS}
|
|
|
|
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>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const mapStateToProps = state => ({
|
2020-06-15 14:00:46 +00:00
|
|
|
user: getUserSelector(state),
|
|
|
|
isMasterDetail: state.app.isMasterDetail
|
2020-03-30 20:19:01 +00:00
|
|
|
});
|
|
|
|
|
2020-06-15 14:00:46 +00:00
|
|
|
export default connect(mapStateToProps)(withTheme(StatusView));
|