2018-03-29 17:55:37 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-08-01 19:35:06 +00:00
|
|
|
import { View, Text, ScrollView, SafeAreaView } from 'react-native';
|
2018-03-29 17:55:37 +00:00
|
|
|
import { connect } from 'react-redux';
|
|
|
|
import moment from 'moment';
|
|
|
|
|
2018-04-03 16:24:59 +00:00
|
|
|
import LoggedView from '../View';
|
2018-03-29 17:55:37 +00:00
|
|
|
import Status from '../../containers/status';
|
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import styles from './styles';
|
|
|
|
import sharedStyles from '../Styles';
|
|
|
|
import database from '../../lib/realm';
|
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-05-18 17:55:08 +00:00
|
|
|
|
|
|
|
import log from '../../utils/log';
|
2018-05-18 16:41:47 +00:00
|
|
|
import RoomTypeIcon from '../../containers/RoomTypeIcon';
|
2018-06-01 17:38:13 +00:00
|
|
|
import I18n from '../../i18n';
|
2018-07-10 13:40:32 +00:00
|
|
|
import { iconsMap } from '../../Icons';
|
2018-03-29 17:55:37 +00:00
|
|
|
|
|
|
|
const PERMISSION_EDIT_ROOM = 'edit-room';
|
|
|
|
|
|
|
|
const camelize = str => str.replace(/^(.)/, (match, chr) => chr.toUpperCase());
|
2018-05-23 13:39:18 +00:00
|
|
|
const getRoomTitle = room => (room.t === 'd' ?
|
2018-05-24 20:17:45 +00:00
|
|
|
<Text testID='room-info-view-name' style={styles.roomTitle}>{room.fname}</Text> :
|
|
|
|
[
|
|
|
|
<RoomTypeIcon type={room.t} key='room-info-type' />,
|
|
|
|
<Text testID='room-info-view-name' style={styles.roomTitle} key='room-info-name'>{room.name}</Text>
|
|
|
|
]
|
2018-05-23 13:39:18 +00:00
|
|
|
);
|
2018-07-10 13:40:32 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
@connect(state => ({
|
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
2018-07-10 13:40:32 +00:00
|
|
|
userId: state.login.user && state.login.user.id,
|
2018-03-29 17:55:37 +00:00
|
|
|
activeUsers: state.activeUsers,
|
|
|
|
Message_TimeFormat: state.settings.Message_TimeFormat,
|
|
|
|
roles: state.roles
|
|
|
|
}))
|
2018-07-10 13:40:32 +00:00
|
|
|
/** @extends React.Component */
|
2018-04-03 16:24:59 +00:00
|
|
|
export default class RoomInfoView extends LoggedView {
|
2018-03-29 17:55:37 +00:00
|
|
|
static propTypes = {
|
2018-07-10 13:40:32 +00:00
|
|
|
navigator: PropTypes.object,
|
|
|
|
rid: PropTypes.string,
|
|
|
|
userId: PropTypes.string,
|
2018-03-29 17:55:37 +00:00
|
|
|
activeUsers: PropTypes.object,
|
|
|
|
Message_TimeFormat: PropTypes.string,
|
|
|
|
roles: PropTypes.object
|
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
2018-04-03 16:24:59 +00:00
|
|
|
super('RoomInfoView', props);
|
2018-07-10 13:40:32 +00:00
|
|
|
const { rid } = props;
|
2018-03-29 17:55:37 +00:00
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
this.sub = {
|
|
|
|
unsubscribe: () => {}
|
|
|
|
};
|
|
|
|
this.state = {
|
|
|
|
room: {},
|
|
|
|
roomUser: {},
|
|
|
|
roles: []
|
|
|
|
};
|
2018-07-10 13:40:32 +00:00
|
|
|
props.navigator.setOnNavigatorEvent(this.onNavigatorEvent.bind(this));
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
|
|
|
await this.updateRoom();
|
|
|
|
this.rooms.addListener(this.updateRoom);
|
|
|
|
|
|
|
|
// get user of room
|
2018-07-10 13:40:32 +00:00
|
|
|
if (this.state.room) {
|
|
|
|
if (this.state.room.t === 'd') {
|
|
|
|
try {
|
|
|
|
const roomUser = await RocketChat.getRoomMember(this.state.room.rid, this.props.userId);
|
|
|
|
this.setState({ roomUser });
|
|
|
|
const username = this.state.room.name;
|
|
|
|
|
|
|
|
const activeUser = this.props.activeUsers[roomUser._id];
|
|
|
|
if (!activeUser || !activeUser.utcOffset) {
|
|
|
|
// get full user data looking for utcOffset
|
|
|
|
// will be catched by .on('users) and saved on activeUsers reducer
|
|
|
|
this.getFullUserData(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
// get all users roles
|
|
|
|
// needs to be changed by a better method
|
|
|
|
const allUsersRoles = await RocketChat.getUserRoles();
|
|
|
|
const userRoles = allUsersRoles.find(user => user.username === username);
|
|
|
|
if (userRoles) {
|
|
|
|
this.setState({ roles: userRoles.roles || [] });
|
|
|
|
}
|
|
|
|
} catch (e) {
|
|
|
|
log('RoomInfoView.componentDidMount', e);
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
2018-07-10 13:40:32 +00:00
|
|
|
} else {
|
|
|
|
const permissions = RocketChat.hasPermission([PERMISSION_EDIT_ROOM], this.state.room.rid);
|
|
|
|
if (permissions[PERMISSION_EDIT_ROOM]) {
|
|
|
|
this.props.navigator.setButtons({
|
|
|
|
rightButtons: [{
|
|
|
|
id: 'edit',
|
|
|
|
icon: iconsMap.create,
|
|
|
|
testID: 'room-info-view-edit-button'
|
|
|
|
}]
|
|
|
|
});
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
componentWillUnmount() {
|
|
|
|
this.rooms.removeAllListeners();
|
|
|
|
this.sub.unsubscribe();
|
|
|
|
}
|
|
|
|
|
2018-07-10 13:40:32 +00:00
|
|
|
onNavigatorEvent(event) {
|
|
|
|
if (event.type === 'NavBarButtonPress') {
|
|
|
|
if (event.id === 'edit') {
|
|
|
|
this.props.navigator.push({
|
|
|
|
screen: 'RoomInfoEditView',
|
|
|
|
title: I18n.t('Room_Info_Edit'),
|
|
|
|
passProps: {
|
|
|
|
rid: this.props.rid
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
getFullUserData = async(username) => {
|
2018-06-18 13:30:36 +00:00
|
|
|
try {
|
|
|
|
const result = await RocketChat.subscribe('fullUserData', username);
|
|
|
|
this.sub = result;
|
|
|
|
} catch (e) {
|
|
|
|
log('getFullUserData', e);
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
isDirect = () => this.state.room.t === 'd';
|
|
|
|
|
|
|
|
updateRoom = async() => {
|
|
|
|
const [room] = this.rooms;
|
|
|
|
this.setState({ room });
|
|
|
|
}
|
2018-06-01 17:38:13 +00:00
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
renderItem = (key, room) => (
|
|
|
|
<View style={styles.item}>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text style={styles.itemLabel}>{I18n.t(camelize(key))}</Text>
|
2018-05-23 13:39:18 +00:00
|
|
|
<Text
|
|
|
|
style={[styles.itemContent, !room[key] && styles.itemContent__empty]}
|
|
|
|
testID={`room-info-view-${ key }`}
|
2018-06-01 17:38:13 +00:00
|
|
|
>{ room[key] ? room[key] : I18n.t(`No_${ key }_provided`) }
|
2018-05-23 13:39:18 +00:00
|
|
|
</Text>
|
2018-03-29 17:55:37 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
|
|
|
|
renderRoles = () => (
|
2018-06-13 01:33:00 +00:00
|
|
|
this.state.roles.length > 0 ?
|
|
|
|
<View style={styles.item}>
|
|
|
|
<Text style={styles.itemLabel}>{I18n.t('Roles')}</Text>
|
|
|
|
<View style={styles.rolesContainer}>
|
|
|
|
{this.state.roles.map(role => (
|
|
|
|
<View style={styles.roleBadge} key={role}>
|
|
|
|
<Text>{ this.props.roles[role] }</Text>
|
|
|
|
</View>
|
|
|
|
))}
|
|
|
|
</View>
|
2018-03-29 17:55:37 +00:00
|
|
|
</View>
|
2018-06-13 01:33:00 +00:00
|
|
|
: null
|
2018-03-29 17:55:37 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
renderTimezone = (userId) => {
|
|
|
|
if (this.props.activeUsers[userId]) {
|
|
|
|
const { utcOffset } = this.props.activeUsers[userId];
|
|
|
|
|
|
|
|
if (!utcOffset) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={styles.item}>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text style={styles.itemLabel}>{I18n.t('Timezone')}</Text>
|
2018-03-29 17:55:37 +00:00
|
|
|
<Text style={styles.itemContent}>{moment().utcOffset(utcOffset).format(this.props.Message_TimeFormat)} (UTC { utcOffset })</Text>
|
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2018-05-18 17:55:08 +00:00
|
|
|
renderAvatar = (room, roomUser) => (
|
|
|
|
<Avatar
|
|
|
|
text={room.name}
|
|
|
|
size={100}
|
|
|
|
style={styles.avatar}
|
|
|
|
type={room.t}
|
|
|
|
>
|
|
|
|
{room.t === 'd' ? <Status style={[sharedStyles.status, styles.status]} id={roomUser._id} /> : null}
|
|
|
|
</Avatar>
|
|
|
|
)
|
|
|
|
|
2018-05-24 20:17:45 +00:00
|
|
|
renderBroadcast = () => (
|
|
|
|
<View style={styles.item}>
|
2018-06-01 17:38:13 +00:00
|
|
|
<Text style={styles.itemLabel}>{I18n.t('Broadcast_Channel')}</Text>
|
2018-05-24 20:17:45 +00:00
|
|
|
<Text
|
|
|
|
style={styles.itemContent}
|
|
|
|
testID='room-info-view-broadcast'
|
2018-06-01 17:38:13 +00:00
|
|
|
>{I18n.t('Broadcast_channel_Description')}
|
2018-05-24 20:17:45 +00:00
|
|
|
</Text>
|
|
|
|
</View>
|
|
|
|
)
|
|
|
|
|
2018-03-29 17:55:37 +00:00
|
|
|
render() {
|
|
|
|
const { room, roomUser } = this.state;
|
2018-05-18 17:55:08 +00:00
|
|
|
if (!room) {
|
|
|
|
return <View />;
|
|
|
|
}
|
2018-03-29 17:55:37 +00:00
|
|
|
return (
|
2018-08-01 19:35:06 +00:00
|
|
|
<ScrollView style={styles.scroll}>
|
|
|
|
<SafeAreaView style={styles.container} testID='room-info-view'>
|
|
|
|
<View style={styles.avatarContainer}>
|
|
|
|
{this.renderAvatar(room, roomUser)}
|
|
|
|
<View style={styles.roomTitleContainer}>{ getRoomTitle(room) }</View>
|
|
|
|
</View>
|
|
|
|
{!this.isDirect() ? this.renderItem('description', room) : null}
|
|
|
|
{!this.isDirect() ? this.renderItem('topic', room) : null}
|
|
|
|
{!this.isDirect() ? this.renderItem('announcement', room) : null}
|
|
|
|
{this.isDirect() ? this.renderRoles() : null}
|
|
|
|
{this.isDirect() ? this.renderTimezone(roomUser._id) : null}
|
|
|
|
{room.broadcast ? this.renderBroadcast() : null}
|
|
|
|
</SafeAreaView>
|
2018-03-29 17:55:37 +00:00
|
|
|
</ScrollView>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|