verdnatura-chat/app/views/RoomInfoView/index.js

369 lines
10 KiB
JavaScript
Raw Normal View History

import React from 'react';
import PropTypes from 'prop-types';
import { View, Text, ScrollView } from 'react-native';
2020-02-19 19:43:47 +00:00
import { BorderlessButton } from 'react-native-gesture-handler';
import { connect } from 'react-redux';
import moment from 'moment';
2019-03-12 16:23:06 +00:00
import { SafeAreaView } from 'react-navigation';
2020-02-19 19:43:47 +00:00
import { CustomIcon } from '../../lib/Icons';
import Status from '../../containers/Status';
import Avatar from '../../containers/Avatar';
import styles from './styles';
import sharedStyles from '../Styles';
import database from '../../lib/database';
import RocketChat from '../../lib/rocketchat';
import RoomTypeIcon from '../../containers/RoomTypeIcon';
2018-06-01 17:38:13 +00:00
import I18n from '../../i18n';
2019-03-12 16:23:06 +00:00
import { CustomHeaderButtons, Item } from '../../containers/HeaderButton';
import StatusBar from '../../containers/StatusBar';
2019-04-29 16:03:52 +00:00
import log from '../../utils/log';
2019-12-11 23:01:12 +00:00
import { themes } from '../../constants/colors';
import { withTheme } from '../../theme';
import { themedHeader } from '../../utils/navigation';
2020-02-19 19:43:47 +00:00
import { getUserSelector } from '../../selectors/login';
const PERMISSION_EDIT_ROOM = 'edit-room';
const camelize = str => str.replace(/^(.)/, (match, chr) => chr.toUpperCase());
2020-02-19 19:43:47 +00:00
const getRoomTitle = (room, type, name, username, theme) => (type === 'd'
? (
<>
<Text testID='room-info-view-name' style={[styles.roomTitle, { color: themes[theme].titleText }]}>{ name }</Text>
{username && <Text testID='room-info-view-username' style={[styles.roomUsername, { color: themes[theme].auxiliaryText }]}>{`@${ username }`}</Text>}
</>
)
: (
<View style={styles.roomTitleRow}>
2019-12-11 23:01:12 +00:00
<RoomTypeIcon type={room.prid ? 'discussion' : room.t} key='room-info-type' theme={theme} />
<Text testID='room-info-view-name' style={[styles.roomTitle, { color: themes[theme].titleText }]} key='room-info-name'>{room.prid ? room.fname : room.name}</Text>
</View>
)
2018-05-23 13:39:18 +00:00
);
class RoomInfoView extends React.Component {
2019-12-11 23:01:12 +00:00
static navigationOptions = ({ navigation, screenProps }) => {
2019-03-12 16:23:06 +00:00
const showEdit = navigation.getParam('showEdit');
const rid = navigation.getParam('rid');
2020-01-29 20:20:36 +00:00
const t = navigation.getParam('t');
return {
2020-01-29 20:20:36 +00:00
title: t === 'd' ? I18n.t('User_Info') : I18n.t('Room_Info'),
2019-12-11 23:01:12 +00:00
...themedHeader(screenProps.theme),
2019-03-12 16:23:06 +00:00
headerRight: showEdit
? (
<CustomHeaderButtons>
<Item iconName='edit' onPress={() => navigation.navigate('RoomInfoEditView', { rid })} testID='room-info-view-edit-button' />
</CustomHeaderButtons>
)
: null
};
}
static propTypes = {
2019-03-12 16:23:06 +00:00
navigation: PropTypes.object,
user: PropTypes.shape({
id: PropTypes.string,
token: PropTypes.string
}),
baseUrl: PropTypes.string,
2019-12-11 23:01:12 +00:00
Message_TimeFormat: PropTypes.string,
theme: PropTypes.string
}
constructor(props) {
super(props);
const room = props.navigation.getParam('room');
2019-04-29 16:03:52 +00:00
this.rid = props.navigation.getParam('rid');
this.t = props.navigation.getParam('t');
this.state = {
room: room || {},
roomUser: {},
parsedRoles: []
};
}
async componentDidMount() {
2019-04-29 16:03:52 +00:00
if (this.t === 'd') {
const { user } = this.props;
const roomUserId = RocketChat.getRoomMemberId(this.rid, user.id);
try {
const result = await RocketChat.getUserInfo(roomUserId);
if (result.success) {
const { roles } = result.user;
let parsedRoles = [];
if (roles && roles.length) {
parsedRoles = await Promise.all(roles.map(async(role) => {
const description = await this.getRoleDescription(role);
return description;
}));
}
this.setState({ roomUser: result.user, parsedRoles });
}
} catch (e) {
log(e);
}
return;
}
const { navigation } = this.props;
let room = navigation.getParam('room');
if (room && room.observe) {
this.roomObservable = room.observe();
this.subscription = this.roomObservable
.subscribe((changes) => {
this.setState({ room: changes });
});
} else {
try {
const result = await RocketChat.getRoomInfo(this.rid);
if (result.success) {
// eslint-disable-next-line prefer-destructuring
room = result.room;
this.setState({ room });
}
} catch (e) {
log(e);
}
}
const permissions = await RocketChat.hasPermission([PERMISSION_EDIT_ROOM], room.rid);
2020-02-19 19:43:47 +00:00
if (permissions[PERMISSION_EDIT_ROOM] && !room.prid && this.t !== 'l') {
navigation.setParams({ showEdit: true });
}
}
componentWillUnmount() {
if (this.subscription && this.subscription.unsubscribe) {
this.subscription.unsubscribe();
}
}
getRoleDescription = async(id) => {
const db = database.active;
try {
const rolesCollection = db.collections.get('roles');
const role = await rolesCollection.find(id);
if (role) {
return role.description;
}
return null;
} catch (e) {
return null;
}
}
2018-06-01 17:38:13 +00:00
2020-02-19 19:43:47 +00:00
goRoom = async() => {
const { roomUser } = this.state;
const { username: name } = roomUser;
const { navigation } = this.props;
try {
const result = await RocketChat.createDirectMessage(name);
if (result.success) {
await navigation.navigate('RoomsListView');
const rid = result.room._id;
navigation.navigate('RoomView', { rid, name, t: 'd' });
}
} catch (e) {
// do nothing
}
}
videoCall = () => RocketChat.callJitsi(this.rid)
isDirect = () => this.t === 'd'
2019-12-11 23:01:12 +00:00
renderItem = (key, room) => {
const { theme } = this.props;
return (
<View style={styles.item}>
<Text style={[styles.itemLabel, { color: themes[theme].titleText }]}>{I18n.t(camelize(key))}</Text>
<Text
style={[styles.itemContent, !room[key] && styles.itemContent__empty, { color: themes[theme].auxiliaryText }]}
testID={`room-info-view-${ key }`}
>{ room[key] ? room[key] : I18n.t(`No_${ key }_provided`) }
</Text>
</View>
);
}
renderRole = (description) => {
2019-12-11 23:01:12 +00:00
const { theme } = this.props;
2019-04-29 16:03:52 +00:00
if (description) {
return (
2020-02-19 19:43:47 +00:00
<View style={[styles.roleBadge, { backgroundColor: themes[theme].auxiliaryBackground }]} key={description}>
<Text style={styles.role}>{ description }</Text>
2019-04-29 16:03:52 +00:00
</View>
);
}
return null;
}
renderRoles = () => {
const { parsedRoles } = this.state;
2020-02-19 19:43:47 +00:00
const { theme } = this.props;
if (parsedRoles && parsedRoles.length) {
2019-04-29 16:03:52 +00:00
return (
<View style={styles.item}>
2020-02-19 19:43:47 +00:00
<Text style={[styles.itemLabel, { color: themes[theme].titleText }]}>{I18n.t('Roles')}</Text>
2019-04-29 16:03:52 +00:00
<View style={styles.rolesContainer}>
{parsedRoles.map(role => this.renderRole(role))}
</View>
2019-04-29 16:03:52 +00:00
</View>
);
}
return null;
}
2019-04-29 16:03:52 +00:00
renderTimezone = () => {
const { roomUser } = this.state;
2019-12-11 23:01:12 +00:00
const { Message_TimeFormat, theme } = this.props;
2019-04-29 16:03:52 +00:00
if (roomUser) {
const { utcOffset } = roomUser;
if (!utcOffset) {
return null;
}
return (
<View style={styles.item}>
2019-12-11 23:01:12 +00:00
<Text style={[styles.itemLabel, { color: themes[theme].titleText }]}>{I18n.t('Timezone')}</Text>
<Text style={[styles.itemContent, { color: themes[theme].auxiliaryText }]}>{moment().utcOffset(utcOffset).format(Message_TimeFormat)} (UTC { utcOffset })</Text>
</View>
);
}
return null;
}
renderAvatar = (room, roomUser) => {
2020-01-29 20:20:36 +00:00
const { baseUrl, user, theme } = this.props;
return (
<Avatar
text={room.name || roomUser.username}
size={100}
style={styles.avatar}
type={this.t}
baseUrl={baseUrl}
2019-04-18 20:57:35 +00:00
userId={user.id}
token={user.token}
>
2020-01-29 20:20:36 +00:00
{this.t === 'd' && roomUser._id ? <Status style={[sharedStyles.status, styles.status]} theme={theme} size={24} id={roomUser._id} /> : null}
</Avatar>
);
}
renderBroadcast = () => (
<View style={styles.item}>
2018-06-01 17:38:13 +00:00
<Text style={styles.itemLabel}>{I18n.t('Broadcast_Channel')}</Text>
<Text
style={styles.itemContent}
testID='room-info-view-broadcast'
2018-06-01 17:38:13 +00:00
>{I18n.t('Broadcast_channel_Description')}
</Text>
</View>
)
2019-04-29 16:03:52 +00:00
renderCustomFields = () => {
const { roomUser } = this.state;
if (roomUser) {
const { customFields } = roomUser;
2019-04-29 16:03:52 +00:00
if (!roomUser.customFields) {
return null;
}
return (
Object.keys(customFields).map((title) => {
if (!customFields[title]) {
return;
}
return (
<View style={styles.item} key={title}>
<Text style={styles.itemLabel}>{title}</Text>
<Text style={styles.itemContent}>{customFields[title]}</Text>
</View>
);
})
);
}
return null;
}
2020-02-19 19:43:47 +00:00
renderButton = (onPress, iconName, text) => {
const { theme } = this.props;
return (
<BorderlessButton
onPress={onPress}
style={styles.roomButton}
>
<CustomIcon
name={iconName}
size={30}
color={themes[theme].actionTintColor}
/>
<Text style={[styles.roomButtonText, { color: themes[theme].actionTintColor }]}>{text}</Text>
</BorderlessButton>
);
}
renderButtons = () => (
<View style={styles.roomButtonsContainer}>
{this.renderButton(this.goRoom, 'message', I18n.t('Message'))}
{this.renderButton(this.videoCall, 'video', I18n.t('Video_call'))}
</View>
)
renderChannel = () => {
const { room } = this.state;
return (
<>
{this.renderItem('description', room)}
{this.renderItem('topic', room)}
{this.renderItem('announcement', room)}
{room.broadcast ? this.renderBroadcast() : null}
</>
);
}
renderDirect = () => {
const { roomUser } = this.state;
return (
<>
{this.renderRoles()}
{this.renderTimezone()}
{this.renderCustomFields(roomUser._id)}
</>
);
}
render() {
const { room, roomUser } = this.state;
2019-12-11 23:01:12 +00:00
const { theme } = this.props;
2020-02-19 19:43:47 +00:00
const isDirect = this.isDirect();
if (!room) {
return <View />;
}
return (
2019-12-11 23:01:12 +00:00
<ScrollView style={[styles.scroll, { backgroundColor: themes[theme].backgroundColor }]}>
<StatusBar theme={theme} />
<SafeAreaView
style={[styles.container, { backgroundColor: themes[theme].backgroundColor }]}
forceInset={{ vertical: 'never' }}
testID='room-info-view'
>
2020-02-19 19:43:47 +00:00
<View style={[styles.avatarContainer, isDirect && styles.avatarContainerDirectRoom, { backgroundColor: themes[theme].auxiliaryBackground }]}>
{this.renderAvatar(room, roomUser)}
2020-02-19 19:43:47 +00:00
<View style={styles.roomTitleContainer}>{ getRoomTitle(room, this.t, roomUser && roomUser.name, roomUser && roomUser.username, theme) }</View>
{isDirect ? this.renderButtons() : null}
</View>
2020-02-19 19:43:47 +00:00
{isDirect ? this.renderDirect() : this.renderChannel()}
</SafeAreaView>
</ScrollView>
);
}
}
const mapStateToProps = state => ({
2020-02-19 19:43:47 +00:00
baseUrl: state.server.server,
user: getUserSelector(state),
Message_TimeFormat: state.settings.Message_TimeFormat
});
2019-12-11 23:01:12 +00:00
export default connect(mapStateToProps)(withTheme(RoomInfoView));