Render message time and username when there is alias

This commit is contained in:
Rodrigo Nascimento 2017-08-13 21:03:33 -03:00
parent a41a2c1afd
commit c49a45c234
2 changed files with 45 additions and 13 deletions

View File

@ -4,6 +4,8 @@ import { View, Text, StyleSheet } from 'react-native';
import { CachedImage } from 'react-native-img-cache'; import { CachedImage } from 'react-native-img-cache';
import { emojify } from 'react-emojione'; import { emojify } from 'react-emojione';
import Markdown from 'react-native-easy-markdown'; import Markdown from 'react-native-easy-markdown';
import moment from 'moment';
import avatarInitialsAndColor from '../utils/avatarInitialsAndColor'; import avatarInitialsAndColor from '../utils/avatarInitialsAndColor';
const styles = StyleSheet.create({ const styles = StyleSheet.create({
@ -41,35 +43,59 @@ const styles = StyleSheet.create({
flex: 1 flex: 1
}, },
username: { username: {
fontWeight: 'bold', fontWeight: 'bold'
marginBottom: 5 },
usernameView: {
flexDirection: 'row',
alignItems: 'center',
marginBottom: 2
},
alias: {
fontSize: 10,
color: '#888',
paddingLeft: 5
},
time: {
fontSize: 10,
color: '#888',
paddingLeft: 5
} }
}); });
export default class Message extends React.PureComponent { export default class Message extends React.PureComponent {
static propTypes = { static propTypes = {
item: PropTypes.object.isRequired, item: PropTypes.object.isRequired,
baseUrl: PropTypes.string.isRequired baseUrl: PropTypes.string.isRequired,
Message_TimeFormat: PropTypes.string.isRequired
} }
render() { render() {
const { item } = this.props;
const extraStyle = {}; const extraStyle = {};
if (this.props.item.temp) { if (item.temp) {
extraStyle.opacity = 0.3; extraStyle.opacity = 0.3;
} }
const msg = emojify(this.props.item.msg, { output: 'unicode' }); const msg = emojify(item.msg, { output: 'unicode' });
const username = this.props.item.alias || this.props.item.u.username; const username = item.alias || item.u.username;
let { initials, color } = avatarInitialsAndColor(username); let { initials, color } = avatarInitialsAndColor(username);
const avatar = this.props.item.avatar || `${ this.props.baseUrl }/avatar/${ this.props.item.u.username }`; const avatar = item.avatar || `${ this.props.baseUrl }/avatar/${ item.u.username }`;
if (this.props.item.avatar) { if (item.avatar) {
initials = ''; initials = '';
color = 'transparent'; color = 'transparent';
} }
let aliasUsername;
if (item.alias) {
aliasUsername = <Text style={styles.alias}>@{item.u.username}</Text>;
}
const time = moment(item.ts).format(this.props.Message_TimeFormat);
return ( return (
<View style={[styles.message, extraStyle]}> <View style={[styles.message, extraStyle]}>
<View style={[styles.avatarContainer, { backgroundColor: color }]}> <View style={[styles.avatarContainer, { backgroundColor: color }]}>
@ -77,9 +103,12 @@ export default class Message extends React.PureComponent {
<CachedImage style={styles.avatar} source={{ uri: avatar }} /> <CachedImage style={styles.avatar} source={{ uri: avatar }} />
</View> </View>
<View style={styles.texts}> <View style={styles.texts}>
<Text onPress={this._onPress} style={styles.username}> <View style={styles.usernameView}>
{username} <Text onPress={this._onPress} style={styles.username}>
</Text> {username}
</Text>
{aliasUsername}<Text style={styles.time}>{time}</Text>
</View>
<Markdown> <Markdown>
{msg} {msg}
</Markdown> </Markdown>

View File

@ -45,7 +45,8 @@ const styles = StyleSheet.create({
@connect(state => ({ @connect(state => ({
server: state.server, server: state.server,
Site_Url: state.settings.Site_Url Site_Url: state.settings.Site_Url,
Message_TimeFormat: state.settings.Message_TimeFormat
}), dispatch => ({ }), dispatch => ({
actions: bindActionCreators(actions, dispatch) actions: bindActionCreators(actions, dispatch)
})) }))
@ -56,7 +57,8 @@ export default class RoomView extends React.Component {
sid: PropTypes.string, sid: PropTypes.string,
name: PropTypes.string, name: PropTypes.string,
server: PropTypes.string, server: PropTypes.string,
Site_Url: PropTypes.string Site_Url: PropTypes.string,
Message_TimeFormat: PropTypes.string
} }
constructor(props) { constructor(props) {
@ -153,6 +155,7 @@ export default class RoomView extends React.Component {
id={item._id} id={item._id}
item={item} item={item}
baseUrl={this.props.Site_Url} baseUrl={this.props.Site_Url}
Message_TimeFormat={this.props.Message_TimeFormat}
/> />
); );