2018-02-19 21:19:39 +00:00
|
|
|
import React from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2018-03-23 16:49:51 +00:00
|
|
|
import { View, SectionList, Text, Alert } from 'react-native';
|
2018-02-19 21:19:39 +00:00
|
|
|
import Icon from 'react-native-vector-icons/Ionicons';
|
|
|
|
import MaterialIcon from 'react-native-vector-icons/MaterialIcons';
|
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
|
|
import styles from './styles';
|
|
|
|
import Avatar from '../../containers/Avatar';
|
|
|
|
import Touch from '../../utils/touch';
|
|
|
|
import database from '../../lib/realm';
|
2018-03-02 15:11:34 +00:00
|
|
|
import RocketChat from '../../lib/rocketchat';
|
2018-03-23 16:49:51 +00:00
|
|
|
import { leaveRoom } from '../../actions/room';
|
2018-02-19 21:19:39 +00:00
|
|
|
|
|
|
|
@connect(state => ({
|
2018-03-23 16:49:51 +00:00
|
|
|
baseUrl: state.settings.Site_Url || state.server ? state.server.server : '',
|
|
|
|
user: state.login.user
|
|
|
|
}), dispatch => ({
|
|
|
|
leaveRoom: rid => dispatch(leaveRoom(rid))
|
2018-02-19 21:19:39 +00:00
|
|
|
}))
|
|
|
|
export default class RoomActionsView extends React.PureComponent {
|
|
|
|
static propTypes = {
|
|
|
|
baseUrl: PropTypes.string,
|
2018-03-23 16:49:51 +00:00
|
|
|
user: PropTypes.object,
|
|
|
|
navigation: PropTypes.object,
|
|
|
|
leaveRoom: PropTypes.func
|
2018-02-19 21:19:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
const { rid } = props.navigation.state.params;
|
|
|
|
this.rooms = database.objects('subscriptions').filtered('rid = $0', rid);
|
|
|
|
this.state = {
|
|
|
|
sections: [],
|
2018-03-23 16:49:51 +00:00
|
|
|
room: {},
|
|
|
|
members: []
|
2018-02-19 21:19:39 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
async componentDidMount() {
|
|
|
|
await this.updateRoom();
|
|
|
|
this.updateRoomMembers();
|
2018-02-19 21:19:39 +00:00
|
|
|
this.rooms.addListener(this.updateRoom);
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
componentWillUnmount() {
|
|
|
|
this.rooms.removeAllListeners();
|
|
|
|
}
|
|
|
|
|
|
|
|
onPressTouchable = (item) => {
|
|
|
|
if (item.route) {
|
|
|
|
return this.props.navigation.navigate(item.route, item.params);
|
|
|
|
}
|
|
|
|
if (item.event) {
|
|
|
|
return item.event();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
getRoomTitle = room => (room.t === 'd' ? room.fname : room.name);
|
|
|
|
|
|
|
|
updateRoomMembers = async() => {
|
|
|
|
let members;
|
|
|
|
try {
|
|
|
|
const membersResult = await RocketChat.getRoomMembers(this.state.room.rid, false);
|
|
|
|
members = membersResult.records;
|
|
|
|
} catch (error) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
this.setState({ members });
|
|
|
|
this.updateSections();
|
|
|
|
}
|
|
|
|
|
|
|
|
updateRoom = async() => {
|
2018-02-19 21:19:39 +00:00
|
|
|
const [room] = this.rooms;
|
2018-03-23 16:49:51 +00:00
|
|
|
await this.setState({ room });
|
2018-02-19 21:19:39 +00:00
|
|
|
this.updateSections();
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
updateSections = async() => {
|
2018-03-23 16:49:51 +00:00
|
|
|
const { rid, t, blocked } = this.state.room;
|
|
|
|
const { members } = this.state;
|
2018-02-19 21:19:39 +00:00
|
|
|
const sections = [{
|
|
|
|
data: [{ icon: 'ios-star', name: 'USER' }],
|
|
|
|
renderItem: this.renderRoomInfo
|
|
|
|
}, {
|
|
|
|
data: [
|
|
|
|
{ icon: 'ios-call-outline', name: 'Voice call' },
|
|
|
|
{ icon: 'ios-videocam-outline', name: 'Video call' }
|
|
|
|
],
|
|
|
|
renderItem: this.renderItem
|
|
|
|
}, {
|
|
|
|
data: [
|
2018-03-23 16:49:51 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-attach',
|
|
|
|
name: 'Files',
|
|
|
|
route: 'RoomFiles',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-03-02 15:11:34 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-at-outline',
|
|
|
|
name: 'Mentions',
|
|
|
|
route: 'MentionedMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-02-19 21:19:39 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-star-outline',
|
|
|
|
name: 'Starred',
|
|
|
|
route: 'StarredMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
|
|
|
{ icon: 'ios-search', name: 'Search' },
|
|
|
|
{ icon: 'ios-share-outline', name: 'Share' },
|
|
|
|
{
|
|
|
|
icon: 'ios-pin',
|
|
|
|
name: 'Pinned',
|
|
|
|
route: 'PinnedMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-03-23 16:49:51 +00:00
|
|
|
{
|
|
|
|
icon: 'ios-code',
|
|
|
|
name: 'Snippets',
|
|
|
|
route: 'SnippetedMessages',
|
|
|
|
params: { rid }
|
|
|
|
},
|
2018-02-19 21:19:39 +00:00
|
|
|
{ icon: 'ios-notifications-outline', name: 'Notifications preferences' }
|
|
|
|
],
|
|
|
|
renderItem: this.renderItem
|
|
|
|
}];
|
|
|
|
if (t === 'd') {
|
|
|
|
sections.push({
|
|
|
|
data: [
|
|
|
|
{ icon: 'ios-volume-off', name: 'Mute user' },
|
2018-03-23 16:49:51 +00:00
|
|
|
{
|
|
|
|
icon: 'block',
|
|
|
|
name: `${ blocked ? 'Unblock' : 'Block' } user`,
|
|
|
|
type: 'danger',
|
|
|
|
event: () => this.toggleBlockUser()
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
],
|
|
|
|
renderItem: this.renderItem
|
|
|
|
});
|
|
|
|
} else if (t === 'c' || t === 'p') {
|
2018-03-23 16:49:51 +00:00
|
|
|
if (members.length > 0) {
|
|
|
|
sections[2].data.unshift({
|
|
|
|
icon: 'ios-people',
|
|
|
|
name: 'Members',
|
|
|
|
description: (members.length === 1 ? `${ members.length } member` : `${ members.length } members`),
|
|
|
|
route: 'RoomMembers',
|
|
|
|
params: { rid, members }
|
|
|
|
});
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
sections.push({
|
|
|
|
data: [
|
|
|
|
{ icon: 'ios-volume-off', name: 'Mute channel' },
|
2018-03-23 16:49:51 +00:00
|
|
|
{
|
|
|
|
icon: 'block',
|
|
|
|
name: 'Leave channel',
|
|
|
|
type: 'danger',
|
|
|
|
event: () => this.leaveChannel()
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
],
|
|
|
|
renderItem: this.renderItem
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.setState({ sections });
|
|
|
|
}
|
|
|
|
|
2018-03-23 16:49:51 +00:00
|
|
|
toggleBlockUser = () => {
|
|
|
|
const { rid, blocked } = this.state.room;
|
|
|
|
const { members } = this.state;
|
|
|
|
const member = members.find(m => m.id !== this.props.user.id);
|
|
|
|
RocketChat.toggleBlockUser(rid, member._id, !blocked);
|
|
|
|
}
|
|
|
|
|
|
|
|
leaveChannel = () => {
|
|
|
|
const { room } = this.state;
|
|
|
|
Alert.alert(
|
|
|
|
'Are you sure?',
|
|
|
|
`Are you sure you want to leave the room ${ this.getRoomTitle(room) }?`,
|
|
|
|
[
|
|
|
|
{
|
|
|
|
text: 'Cancel',
|
|
|
|
style: 'cancel'
|
|
|
|
},
|
|
|
|
{
|
|
|
|
text: 'Yes, leave it!',
|
|
|
|
style: 'destructive',
|
|
|
|
onPress: async() => {
|
|
|
|
this.props.leaveRoom(room.rid);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
]
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2018-03-02 15:11:34 +00:00
|
|
|
renderRoomInfo = ({ item }) => {
|
2018-03-23 16:49:51 +00:00
|
|
|
const { room } = this.state;
|
|
|
|
const { name, t, topic } = room;
|
2018-03-02 15:11:34 +00:00
|
|
|
return (
|
|
|
|
this.renderTouchableItem([
|
|
|
|
<Avatar
|
|
|
|
key='avatar'
|
|
|
|
text={name}
|
|
|
|
size={50}
|
|
|
|
style={styles.avatar}
|
|
|
|
baseUrl={this.props.baseUrl}
|
|
|
|
type={t}
|
|
|
|
/>,
|
|
|
|
<View key='name' style={styles.roomTitleContainer}>
|
2018-03-23 16:49:51 +00:00
|
|
|
<Text style={styles.roomTitle}>{ this.getRoomTitle(room) }</Text>
|
2018-03-02 15:11:34 +00:00
|
|
|
<Text style={styles.roomDescription} ellipsizeMode='tail' numberOfLines={1}>{t === 'd' ? `@${ name }` : topic}</Text>
|
|
|
|
</View>,
|
|
|
|
<Icon key='icon' name='ios-arrow-forward' size={20} style={styles.sectionItemIcon} color='#cbced1' />
|
|
|
|
], item)
|
|
|
|
);
|
|
|
|
}
|
2018-02-19 21:19:39 +00:00
|
|
|
|
|
|
|
renderTouchableItem = (subview, item) => (
|
|
|
|
<Touch
|
2018-03-23 16:49:51 +00:00
|
|
|
onPress={() => this.onPressTouchable(item)}
|
2018-02-19 21:19:39 +00:00
|
|
|
underlayColor='#FFFFFF'
|
|
|
|
activeOpacity={0.5}
|
|
|
|
accessibilityLabel={item.name}
|
|
|
|
accessibilityTraits='button'
|
|
|
|
>
|
|
|
|
<View style={styles.sectionItem}>
|
|
|
|
{subview}
|
|
|
|
</View>
|
|
|
|
</Touch>
|
|
|
|
)
|
|
|
|
|
|
|
|
renderItem = ({ item }) => {
|
|
|
|
if (item.type === 'danger') {
|
|
|
|
const subview = [
|
|
|
|
<MaterialIcon key='icon' name={item.icon} size={20} style={[styles.sectionItemIcon, styles.textColorDanger]} />,
|
|
|
|
<Text key='name' style={[styles.sectionItemName, styles.textColorDanger]}>{ item.name }</Text>
|
|
|
|
];
|
|
|
|
return this.renderTouchableItem(subview, item);
|
|
|
|
}
|
|
|
|
const subview = [
|
|
|
|
<Icon key='left-icon' name={item.icon} size={24} style={styles.sectionItemIcon} />,
|
|
|
|
<Text key='name' style={styles.sectionItemName}>{ item.name }</Text>,
|
|
|
|
item.description && <Text key='description' style={styles.sectionItemDescription}>{ item.description }</Text>,
|
|
|
|
<Icon key='right-icon' name='ios-arrow-forward' size={20} style={styles.sectionItemIcon} color='#cbced1' />
|
|
|
|
];
|
|
|
|
return this.renderTouchableItem(subview, item);
|
|
|
|
}
|
|
|
|
|
|
|
|
renderSectionSeparator = (data) => {
|
|
|
|
if (!data.trailingItem) {
|
|
|
|
if (!data.trailingSection) {
|
|
|
|
return <View style={styles.sectionSeparatorBorder} />;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
return (
|
|
|
|
<View style={[styles.sectionSeparator, data.leadingSection && styles.sectionSeparatorBorder]} />
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
render() {
|
|
|
|
return (
|
|
|
|
<SectionList
|
|
|
|
style={styles.container}
|
|
|
|
stickySectionHeadersEnabled={false}
|
|
|
|
sections={this.state.sections}
|
|
|
|
SectionSeparatorComponent={this.renderSectionSeparator}
|
|
|
|
keyExtractor={(item, index) => index}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|