2017-08-09 01:40:55 +00:00
|
|
|
import React from 'react';
|
2017-08-13 03:19:14 +00:00
|
|
|
import { CachedImage } from 'react-native-img-cache';
|
2017-08-10 20:26:11 +00:00
|
|
|
import MaterialCommunityIcons from 'react-native-vector-icons/MaterialCommunityIcons';
|
2017-08-09 01:40:55 +00:00
|
|
|
import PropTypes from 'prop-types';
|
2017-08-18 21:30:16 +00:00
|
|
|
import { View, Text, StyleSheet, TouchableOpacity } from 'react-native';
|
2017-08-09 01:40:55 +00:00
|
|
|
|
2017-08-13 03:19:14 +00:00
|
|
|
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
|
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
const styles = StyleSheet.create({
|
2017-08-09 18:01:54 +00:00
|
|
|
container: {
|
2017-08-17 19:31:27 +00:00
|
|
|
// flex: 1,
|
2017-08-09 18:01:54 +00:00
|
|
|
flexDirection: 'row',
|
2017-08-13 15:10:50 +00:00
|
|
|
paddingLeft: 16,
|
2017-08-17 19:31:27 +00:00
|
|
|
paddingRight: 16,
|
2017-08-13 15:10:50 +00:00
|
|
|
height: 56,
|
2017-08-09 18:01:54 +00:00
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
number: {
|
2017-08-09 20:08:50 +00:00
|
|
|
minWidth: 20,
|
2017-08-09 18:01:54 +00:00
|
|
|
borderRadius: 5,
|
2017-08-13 15:10:50 +00:00
|
|
|
backgroundColor: '#1d74f5',
|
2017-08-09 18:01:54 +00:00
|
|
|
color: '#fff',
|
|
|
|
textAlign: 'center',
|
|
|
|
overflow: 'hidden',
|
2017-08-13 15:10:50 +00:00
|
|
|
fontSize: 14,
|
2017-08-17 19:31:27 +00:00
|
|
|
paddingLeft: 5,
|
|
|
|
paddingRight: 5
|
2017-08-09 18:01:54 +00:00
|
|
|
},
|
2017-08-13 15:10:50 +00:00
|
|
|
roomName: {
|
2017-08-17 19:31:27 +00:00
|
|
|
flex: 1,
|
2017-08-13 15:10:50 +00:00
|
|
|
fontSize: 16,
|
|
|
|
color: '#444',
|
2017-08-17 19:31:27 +00:00
|
|
|
marginLeft: 16,
|
|
|
|
marginRight: 4
|
2017-08-10 16:25:50 +00:00
|
|
|
},
|
2017-08-13 03:19:14 +00:00
|
|
|
iconContainer: {
|
|
|
|
height: 40,
|
|
|
|
width: 40,
|
|
|
|
borderRadius: 20,
|
|
|
|
overflow: 'hidden',
|
|
|
|
justifyContent: 'center',
|
|
|
|
alignItems: 'center'
|
|
|
|
},
|
|
|
|
icon: {
|
|
|
|
fontSize: 20,
|
2017-08-13 23:19:27 +00:00
|
|
|
color: '#fff'
|
2017-08-13 03:19:14 +00:00
|
|
|
},
|
|
|
|
avatar: {
|
|
|
|
width: 40,
|
|
|
|
height: 40,
|
2017-08-13 15:10:50 +00:00
|
|
|
position: 'absolute',
|
|
|
|
borderRadius: 20
|
2017-08-13 03:19:14 +00:00
|
|
|
},
|
|
|
|
avatarInitials: {
|
2017-08-17 19:31:27 +00:00
|
|
|
fontSize: 20,
|
2017-08-13 03:19:14 +00:00
|
|
|
color: '#ffffff'
|
2017-08-09 01:40:55 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
export default class RoomItem extends React.PureComponent {
|
|
|
|
static propTypes = {
|
2017-08-17 19:31:27 +00:00
|
|
|
type: PropTypes.string.isRequired,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
unread: PropTypes.number,
|
2017-08-13 23:45:47 +00:00
|
|
|
baseUrl: PropTypes.string
|
2017-08-10 16:25:50 +00:00
|
|
|
}
|
2017-08-17 19:31:27 +00:00
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
get icon() {
|
2017-08-17 19:31:27 +00:00
|
|
|
const { type, name, baseUrl } = this.props;
|
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
const icon = {
|
|
|
|
d: 'at',
|
2017-08-10 20:26:11 +00:00
|
|
|
c: 'pound',
|
|
|
|
p: 'lock',
|
|
|
|
l: 'account'
|
2017-08-17 19:31:27 +00:00
|
|
|
}[type];
|
2017-08-13 03:19:14 +00:00
|
|
|
|
2017-08-10 16:25:50 +00:00
|
|
|
if (!icon) {
|
|
|
|
return null;
|
|
|
|
}
|
2017-08-13 03:19:14 +00:00
|
|
|
|
2017-08-17 19:31:27 +00:00
|
|
|
if (type === 'd') {
|
2017-08-13 03:19:14 +00:00
|
|
|
const { initials, color } = avatarInitialsAndColor(name);
|
|
|
|
return (
|
|
|
|
<View style={[styles.iconContainer, { backgroundColor: color }]}>
|
|
|
|
<Text style={styles.avatarInitials}>{initials}</Text>
|
2017-08-17 19:31:27 +00:00
|
|
|
<CachedImage style={styles.avatar} source={{ uri: `${ baseUrl }/avatar/${ name }` }} />
|
2017-08-13 03:19:14 +00:00
|
|
|
</View>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-08-13 15:10:50 +00:00
|
|
|
const { color } = avatarInitialsAndColor(name);
|
|
|
|
|
2017-08-13 03:19:14 +00:00
|
|
|
return (
|
2017-08-13 23:19:27 +00:00
|
|
|
<View style={[styles.iconContainer, { backgroundColor: color }]}>
|
|
|
|
<MaterialCommunityIcons name={icon} style={styles.icon} />
|
2017-08-13 03:19:14 +00:00
|
|
|
</View>
|
|
|
|
);
|
2017-08-09 01:40:55 +00:00
|
|
|
}
|
2017-08-13 03:19:14 +00:00
|
|
|
|
2017-08-17 19:31:27 +00:00
|
|
|
renderNumber = (unread) => {
|
|
|
|
if (!unread || unread <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (unread >= 1000) {
|
|
|
|
unread = '999+';
|
2017-08-09 18:01:54 +00:00
|
|
|
}
|
2017-08-17 19:31:27 +00:00
|
|
|
|
|
|
|
return (
|
|
|
|
<Text style={styles.number}>
|
|
|
|
{ unread }
|
|
|
|
</Text>
|
|
|
|
);
|
2017-08-09 18:01:54 +00:00
|
|
|
}
|
|
|
|
|
2017-08-09 01:40:55 +00:00
|
|
|
render() {
|
2017-08-21 16:16:23 +00:00
|
|
|
const { unread, name, _id } = this.props;
|
2017-08-09 01:40:55 +00:00
|
|
|
return (
|
2017-08-21 16:16:23 +00:00
|
|
|
<TouchableOpacity key={_id} onPress={this.props.onPress} style={styles.container}>
|
2017-08-10 16:25:50 +00:00
|
|
|
{this.icon}
|
2017-08-17 19:31:27 +00:00
|
|
|
<Text style={styles.roomName} ellipsizeMode='tail' numberOfLines={1}>{ name }</Text>
|
|
|
|
{this.renderNumber(unread)}
|
2017-08-18 21:30:16 +00:00
|
|
|
</TouchableOpacity>
|
2017-08-09 01:40:55 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|